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/
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/