viewing paste topic/4953- readline.diff | Diff

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
 src/map/script.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)
 
diff --git a/src/map/script.c b/src/map/script.c
index d3d98ad..7fab157 100644
--- a/src/map/script.c
+++ b/src/map/script.c
@@ -18685,10 +18685,86 @@ bool script_hp_add(char *name, char *args, bool (*func)(struct script_state *st)
        return script->add_builtin(&buildin, true);
 }
 
+/*     readline scriptcommand
+       reads the line of a text file on the server
+       *ReadLine( "<path>", <line> ); */
+BUILDIN( readline ) {
+       FILE *fp;
+       const char * path;
+       char *output;
+       char line[1024];
+       unsigned int position;
+       unsigned i = 0;
+
+       path = script_getstr( st, 2 );
+       position = script_getnum( st, 3 );
+
+       fp = fopen( path, "r" );
+       if( fp == NULL ) {
+               ShowError( "buildin:read:invalid path %s", path );
+               st->state = STOP;
+               return 0;
+       }
+
+       while( i < position ) {
+               if( !fgets( line, sizeof( line ), fp ) ) {
+                       ShowError( "buildin:read:reached end of file (line %d). there is no line %d", i, position );
+                       fclose( fp );
+                       st->state = STOP;
+                       return 0;
+               }
+               i++;
+       }
+
+       //Now we're at the right position
+       if( !fgets( line, sizeof( line ), fp ) ) {
+               ShowError( "buildin:read:line %d is empty or the end of file", position );
+               fclose( fp );
+               st->state = STOP;
+               return 0;
+       }
+       fclose( fp );
+
+       output = (char *)aMalloc( sizeof( line ) * sizeof( char ) );
+       memcpy( output, line, sizeof( line ) );
+       script_pushstr( st, output );
+       return 0;
+}
+
+/*     getline scriptcommand
+       returns the amount of lines in a text file
+       *GetLine( "<path>" ); */
+BUILDIN( getline ) {
+       FILE *fp;
+       unsigned int lines = 0;
+       const char * path;
+       char line[1024];
+
+       path = script_getstr( st, 2 );
+
+       fp = fopen( path, "r" );
+       if( fp == NULL ) {
+               ShowError( "buildin_getline:file %s not found", path );
+               script_pushint( st, -1 );
+               return 0;
+       }
+
+       while( !feof( fp ) ) {
+               fgets( line, sizeof( line ), fp );
+               lines++;
+       }
+       fclose( fp );
+
+       script_pushint( st, lines );
+       return 0;
+}
+
 #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
 #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
 void script_parse_builtin(void) {
        struct script_function BUILDIN[] = {
+               BUILDIN_DEF( readline, "si" ),
+               BUILDIN_DEF( getline, "s" ),
                // NPC interaction
                BUILDIN_DEF(mes,"s*"),
                BUILDIN_DEF(next,""),
 
Viewed 725 times, submitted by AnnieRuru.