Reading from External Files - By Ad0
Well, I had a goal today! I wanted to read a
file, and then, Print the content of the file to the console. I
made it, with a bit help from AnthonyJ. He made me look closer
and correct my stupidious little bug that I should have corrected
myself, so a BIG thanks to him!
I am still sticking me to the server-side, also Game. And I will still work in g_cmds.c. So open that file, and find an open space for coding.
The Function we are going to create is a function
that reads the file from an argument of our function, and puts
the content on the console.
Create a function, named "Cmd_ReadFile_f". The function
would look like this. Do not just paste this and pretend like you
understand all this - Read, Understand, Learn, and then write it
off, and even better: in your own, personal way:
void Cmd_ReadFile_f( gentity_t *ent) {
}
When we write our function, we want to have some declarations first, and the first declaration would be the dec. of the filename:
char filename[MAX_STRING_TOKENS]; //max chars for strings
Now we have fixed the filename declaration. You
could change "filename" to if you want, but just
remember to follow it down the function.
When I looked a the read code, I saw that you had to include the
handle of the file. It's a definition. And now, let's define it
to, let's say: "t". The "alias would look like
this:
fileHandle_t f;
And now, we have to add a declaration for our text buffer (the content of the text stored in temp memory):
char buffer[MAX_ARENAS_TEXT]; //max text
And now, our last definition: The length if the text (in letters) the buffer is. This is for checkingif the file contains anything, and so on:
int length; //The int for length of buffer
The first thing to do now, is to catch the argument from the console. This is also shown in my Command Argument tutorial. Look here, and learn:
trap_Argv( 1, filename, sizeof( filename ) ); //Catches the command argument
Look at this. Now it catches the file name.
Now, let's add a check if you have typed any argument plus an
error message in red colour. I also have a tutorial containing
use of colours:
if ( !Q_stricmp( filename,
"" ) ) //Triggers the function under if no arguent
entered
trap_SendServerCommand( ent-g_entities, va("print \""S_COLOR_RED"No
file name entered!\n\""));
Done! Now, let's check the length of the file:
length = trap_FS_FOpenFile( filename, &f, FS_READ ); //Checks the file length
Logic, isn't it?
And for not mixing file lengths, I added a statement that cleared
the buffer count:
buffer[length] = 0;
And now we could add two ways of checking if it's the right file, or if the file is empty in two ways. I just show you the code. The rest is up to you. I am trying to learn as best as I can:
if ( !length) { //If NOT
any length
trap_SendServerCommand( ent-g_entities, va("print \"I
could not open your file: %s\n\"",filename));
return;
}
if (length <0) { //If Length less than zero
trap_SendServerCommand( ent-g_entities, va("print \"I
could not open your file: %s\n\"",filename));
return;
}
Now we got the check done. As a limiter for
larger files, we can add a code for controlling the buffer.
Thanks, AnthonyJ!:
if (length >= MAX_ARENAS_TEXT) //Checks if length of file s bigger than limit length = MAX_ARENAS_TEXT-1; //Cuts all text after 1 letter minus
I explored how the file reading function worked by looking in the Quake 3: Arena source, and I found out that I had to add this code for making it Read and close the current file:
trap_FS_Read(buffer,length,f);
//Reads the file into the buffer trap_SendServerCommand( ent-g_entities,
va("print \"Here Goes:\n %s \n\"", buffer));
//Prints the buffer to client
trap_FS_FCloseFile( f ); //Closes the buffer
%s Prints the buffer. %s get's the prameter at the end: buffer. See?
The only thing that remains, is to add this as a command in the command list. Scroll down till you find that list of commands they all look like my code I am foing to show.I show this for reference:
else if (Q_stricmp (cmd, "read") == 0)
Cmd_ReadFile_f( ent );
And this adds the command (for those who didn't know that).
I will add a tutorial of controlling File Input, like read parts of the text in chunks and print them out (so the buffer donesn't need to be big).
And remember: Quake doesn't allow to read files that exists in other places than in, and under your mod directory you are putting the compiled result in. q3key won't get read, beacause Mr. Carmack didn't want it to. So don't try anything smart.
I hope you understood this, I really, really do! Just send questions to my mail address above.
For my music, go here!
Cheers!