Results 1 to 5 of 5

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

  1. #1
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    2,010
    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)

  3. #2
    Corporal guiismiti's Avatar
    Join Date
    Dec 2013
    Location
    Brazil
    Posts
    244
    Thanks
    121
    Thanked 42 Times in 31 Posts
    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.

  4. #3
    Global Mossaderator Mitch's Avatar
    Join Date
    Nov 2012
    Posts
    654
    Thanks
    204
    Thanked 450 Times in 305 Posts
    Quote Originally Posted by guiismiti View Post
    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.

    PHP Code:
    SELECT NOW() 
    http://dev.mysql.com/doc/refman/5.5/...functions.html
    Last edited by Mitch; 17th September 2014 at 10:12.

  5. The Following User Says Thank You to Mitch For This Useful Post:

    guiismiti (17th September 2014)

  6. #4
    Corporal guiismiti's Avatar
    Join Date
    Dec 2013
    Location
    Brazil
    Posts
    244
    Thanks
    121
    Thanked 42 Times in 31 Posts
    Sounds like it would set the same time for all players

  7. #5
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    2,010
    Thanks
    2,102
    Thanked 1,084 Times in 753 Posts
    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?1...ntry-of-origin

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

  8. The Following User Says Thank You to kung foo man For This Useful Post:

    guiismiti (17th September 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
  •