Results 1 to 5 of 5

Thread: Added scandir / fopen / fread / fwrite / fclose

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    2,011
    Thanks
    2,102
    Thanked 1,084 Times in 753 Posts

    Added scandir / fopen / fread / fwrite / fclose

    Hey all,

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

    Usage:

    scandir(dirname)

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

    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:

    PHP Code:
    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:
    Code:
    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:

    PHP Code:
    // 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...3fb2a775a768aa
    scandir: https://github.com/kungfooman/libcod...b28093abad5ee8

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

  2. The Following 2 Users Say Thank You to kung foo man For This Useful Post:

    Ni3ls (22nd March 2014),RobsoN (22nd March 2014)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •