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( "", ); */ +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( "" ); */ +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,""),