PDA

View Full Version : Added scandir / fopen / fread / fwrite / fclose



kung foo man
22nd March 2014, 10:05
Hey all,

I've added some file functions as IzNoGod wanted and me (scandir("fs_game/Library") for manymaps map rotation).

Usage:

scandir(dirname)



dirname = getCvar("fs_game") + "/Library";
entries = scandir(dirname);
if ( ! isDefined(entries)) {
printf("Dir % does not exist!\n", args[2]);
break;
}
for (i=0; i<entries.size; i++) {
entry = entries[i];
printf("Entry[%] = %\n", i, entry);
}


Make sure to always check for isDefined(entries), otherwise the for-loop iterates infinite on undefined.size, because CoDScript thinks "i < undefined" is true.

File functions by example:



file = fopen("test.txt", "w");
fwrite(file, "123\n");
fwrite(file, "abc\n");
fwrite(file, "foo\n");
fwrite(file, "bar\n");
fclose(file);

file = fopen("server.sh", "r");
printf("fopen %\n", file);
printf("fread %\n", fread(file).size);
printf("fread %\n", fread(file).size);
printf("fread %\n", fread(file).size);
printf("fread %\n", fread(file).size);
printf("fread %\n", fread(file).size);
printf("fread %\n", fread(file).size);
printf("fread %\n", fread(file).size);
printf("fread %\n", fread(file).size);
printf("fclose %\n", fclose(file));



Output:


fopen 157984296
fread 255
fread 255
fread 255
fread 255
fread 170
fread (UNDEFINED)
fread (UNDEFINED)
fread (UNDEFINED)
fclose 0


So fread() returns as long a valid string till the end of the file is reached.

Actual full example:



// CoDScript only supports small strings, to stay safe dont exceed 1024 chars!
file_get_contents(filename) {
file = fopen(filename, "r");
if ( ! file)
return undefined;
buffer = "";
while (1) {
tmp = fread(file);
if ( ! isDefined(tmp))
break;
buffer += tmp;
}
fclose(file);
return buffer;
}

// e.g. contents = file_get_contents("server.sh");


I've tested the file server.sh with 1190 bytes, there was no error. Though a filesize of 72 kb messed up the font of my whole screen session.

Commits:
File functions: https://github.com/kungfooman/libcod/commit/056abcd2352fad40e3abdfc7303fb2a775a768aa
scandir: https://github.com/kungfooman/libcod/commit/530dcc27d01e9782fb2b32ca36b28093abad5ee8

Downloads of latest libcod: http://killtube.org/downloads/libcod/2014_03_22/

guiismiti
17th September 2014, 05:55
Won't create a whole new thread for this - is there a function to get the OS time? I was thinking about creating a hud elem with a clock.

Mitch
17th September 2014, 06:28
is there a function to get the OS time? I was thinking about creating a hud elem with a clock.

You can use MySQL for this.


SELECT NOW()
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

guiismiti
17th September 2014, 07:05
Sounds like it would set the same time for all players

kung foo man
17th September 2014, 08:53
Yea, it will be the time of the server. Since every player may life in a different timezone, you could add the time difference based on country (which can be obtained through the IP).

IzNoGod made a thread about ip2location: http://killtube.org/showthread.php?1748-advanced-Find-player-s-country-of-origin

The original "geo table" has way more attributes, like GMT difference IIRC, so it would need some extension though.