nfvault.inc
Original include source with line numbers.
| 1 | |
| 2 | #if defined _nfvault_included |
| 3 | #endinput |
| 4 | #endif |
| 5 | #define _nfvault_included |
| 6 | |
| 7 | #if !defined NFV_WILDCARD |
| 8 | #define NFV_WILDCARD "*" |
| 9 | #endif |
| 10 | |
| 11 | #if !defined NFV_TAKEALL |
| 12 | #define NFV_TAKEALL -1 |
| 13 | #endif |
| 14 | |
| 15 | #if !defined NFV_SAVETYPE |
| 16 | #define NFV_SAVETYPE 0 // 0 - steamid ; 1 - name |
| 17 | #endif |
| 18 | |
| 19 | |
| 20 | |
| 21 | /* This is an example script for using the basic nfvault stocks |
| 22 | |
| 23 | new filename[128]; |
| 24 | copy( filename, charsmax(filename), nfv_file("file.txt") ); |
| 25 | nfv_set_data( filename, "key", "name", "data" ); |
| 26 | nfv_set_data( filename, "key", "name2", "data2" ); |
| 27 | nfv_set_data( filename, "key", "name2", "data_2" ); |
| 28 | nfv_set_data( filename, "key", "name3", "data3" ); |
| 29 | nfv_set_data( filename, "key", "name4", "data4" ); |
| 30 | new take_string[10]; |
| 31 | nfv_take_data( filename, "key", "name4", take_string, 9 ); |
| 32 | // take_string == "data4" |
| 33 | |
| 34 | nfv_set_data( filename, "key2", "other_name", "even_more_data" ); |
| 35 | nfv_set_data( filename, "key2", "other_name", "even_more_data2", false ); |
| 36 | |
| 37 | nfv_set_data( filename, "key3", "name", "more_data" ); |
| 38 | new get_string[10]; |
| 39 | nfv_get_data( filename, "key3", NFV_WILDCARD, get_string, 9 ); |
| 40 | // get_string == "more_data" |
| 41 | |
| 42 | // This is an example of the file for the preceding script |
| 43 | |
| 44 | "TimeStamp" "1234567890" |
| 45 | ;key |
| 46 | "name" "data" |
| 47 | "name2" "data_2" |
| 48 | "name3" "data3" |
| 49 | ;key2 |
| 50 | "other_name" "even_more_data" |
| 51 | "other_name" "even_more_data2" |
| 52 | ;key3 |
| 53 | "name" "more_data" |
| 54 | |
| 55 | */ |
| 56 | |
| 57 | enum NFV_TYPE |
| 58 | { |
| 59 | NFVT_INT, |
| 60 | NFVT_BOOL, |
| 61 | NFVT_FLOAT, |
| 62 | NFVT_ARRAY, |
| 63 | NFVT_VECTOR, |
| 64 | NFVT_STRING, |
| 65 | NFVT_CELLARRAY, |
| 66 | NFVT_CELLARRAY2, |
| 67 | } |
| 68 | |
| 69 | enum NFV_PROP ( <<= 1 ) |
| 70 | { |
| 71 | NFV_NONE = 0, |
| 72 | NFV_OVERWRITE = 1, |
| 73 | NFV_NO_QUOTES, |
| 74 | NFV_NO_REPLACE_QUOTE, |
| 75 | NFV_IF_NOT_EXIST, |
| 76 | } |
| 77 | |
| 78 | stock const quote_char = '"'; //this can mess up NFV |
| 79 | stock const quote_replace_char = 30; //an unused character, in notepad it is a square |
| 80 | stock const temp_file_name[] = "temp_data_file.txt"; |
| 81 | stock DataDir[128]; |
| 82 | |
| 83 | /** |
| 84 | * Sets multiple datas in file under key and name. |
| 85 | * |
| 86 | * @param filename The file location for the data. |
| 87 | * @param key The key within the file to look for. Set to "" for no key. |
| 88 | * @param nfv_prop Properties to apply to the saved data in a bitsum. |
| 89 | * @param identifier The character used to identify different keys. |
| 90 | * |
| 91 | * @note The optional parameters are to add names and datas. They should be provided in the form: |
| 92 | * NFVT_*, name[], data |
| 93 | * The type NFVT_ARRAY needs a size provided after the data: |
| 94 | * NFVT_ARRAY, name[], data[], size |
| 95 | * |
| 96 | * @param ... NFVT_* constant for what type of data it is. |
| 97 | * @param ... The name under key to look for. |
| 98 | * @param ... The data to write that matches with the NFV_* constant. |
| 99 | * |
| 100 | * @example nfv_add_key( filename[], key[], NFV_NO_QUOTES|NFV_IF_NOT_EXIST, ';', |
| 101 | * NFVT_INT, int_name[], 1, |
| 102 | * NFVT_BOOL, bool_name[], true, |
| 103 | * NFVT_FLOAT, float_name[], 1.0, |
| 104 | * NFVT_ARRAY, array_name[], any:array[], sizeof array, |
| 105 | * NFVT_VECTOR, vector_name[], Float:vec[3], |
| 106 | * NFVT_STRING, string_name[], string[], |
| 107 | * NFVT_CELLARRAY, cellarray_name[], Array:cellarray, |
| 108 | * NFVT_CELLARRAY2, cellarray2_name[], Array:cellarray2 |
| 109 | * ) |
| 110 | * |
| 111 | * @return 1 if successful. |
| 112 | */ |
| 113 | stock nfv_add_key(const filename[], const key[], const NFV_PROP:nfv_prop=NFV_OVERWRITE, const identifier=';', any:...) |
| 114 | { |
| 115 | new argnum = numargs(); |
| 116 | if( argnum == 4 ) |
| 117 | { |
| 118 | nfv_set_data(filename, key, "", "", nfv_prop, identifier); |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | new tempname[64], tempdata[128], Float:vec[3], tempsize; |
| 123 | for( new i=4, j=0; i<argnum; i+=3 ) |
| 124 | { |
| 125 | j=0; |
| 126 | do tempname[j] = getarg(i+1, j); |
| 127 | while( tempname[j++] != '^0' && j < 64 ); |
| 128 | switch( getarg(i) ) |
| 129 | { |
| 130 | case NFVT_INT: |
| 131 | { |
| 132 | nfv_set_num(filename, key, tempname, getarg(i+2), nfv_prop, identifier); |
| 133 | } |
| 134 | case NFVT_BOOL: |
| 135 | { |
| 136 | nfv_set_bool(filename, key, tempname, bool:getarg(i+2), nfv_prop, identifier); |
| 137 | } |
| 138 | case NFVT_FLOAT: |
| 139 | { |
| 140 | nfv_set_float(filename, key, tempname, Float:getarg(i+2), nfv_prop, identifier); |
| 141 | } |
| 142 | case NFVT_ARRAY: |
| 143 | { |
| 144 | tempsize = min( 128, getarg(i+3) ); |
| 145 | for( j=0; j < tempsize; j++ ) |
| 146 | tempdata[j] = getarg(i+2, j); |
| 147 | nfv_set_array(filename, key, tempname, tempdata, tempsize, nfv_prop, identifier); |
| 148 | i++; //increase by an extra one for the size |
| 149 | } |
| 150 | case NFVT_VECTOR: |
| 151 | { |
| 152 | for( j=0; j<3; j++ ) |
| 153 | vec[j] = Float:getarg(i+2, j); |
| 154 | nfv_set_vec(filename, key, tempname, vec, nfv_prop, identifier); |
| 155 | } |
| 156 | case NFVT_STRING: |
| 157 | { |
| 158 | j=0; |
| 159 | do tempdata[j] = getarg(i+2, j); |
| 160 | while( tempdata[j++] != '^0' && j < 128 ); |
| 161 | nfv_set_data(filename, key, tempname, tempdata, nfv_prop, identifier); |
| 162 | } |
| 163 | case NFVT_CELLARRAY: |
| 164 | { |
| 165 | nfv_set_cellarray(filename, key, tempname, Array:getarg(i+2), nfv_prop, identifier); |
| 166 | } |
| 167 | case NFVT_CELLARRAY2: |
| 168 | { |
| 169 | nfv_set_cellarray2(filename, key, tempname, Array:getarg(i+2), nfv_prop, identifier); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Sets data in file under key and name. |
| 178 | * |
| 179 | * @param filename The file location for the data. |
| 180 | * @param key The key within the file to look for. Set to "" for no key. |
| 181 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 182 | * @param data The data to write. |
| 183 | * @param nfv_prop Properties to apply to the saved data in a bitsum. |
| 184 | * @param identifier The character used to identify different keys. |
| 185 | * @return 1 if successful. |
| 186 | */ |
| 187 | stock nfv_set_data(const filename[], const key[], name[], data[], const NFV_PROP:nfv_prop=NFV_OVERWRITE, const identifier=';') |
| 188 | { |
| 189 | new g = fopen(temp_file_name, "wt"); |
| 190 | new f = fopen(filename, "rt"); |
| 191 | |
| 192 | // lets put a time stamp |
| 193 | fprintf(g, "^"TimeStamp^" ^"%d^"^n", get_systime() ); |
| 194 | |
| 195 | new _data[512], _name[64], _other[3]; |
| 196 | |
| 197 | new bool:wrote_data = false, bool:found_key = false, bool:found_time = false; |
| 198 | |
| 199 | if( !(nfv_prop&NFV_NO_REPLACE_QUOTE) ) |
| 200 | { |
| 201 | nfv_replace_quote( name ); |
| 202 | nfv_replace_quote( data ); |
| 203 | } |
| 204 | |
| 205 | //If using a blank key, detect it already |
| 206 | if( !key[0] ) |
| 207 | found_key = true; |
| 208 | |
| 209 | while( !feof(f) ) |
| 210 | { |
| 211 | fgets(f, _data, charsmax(_data)); |
| 212 | |
| 213 | if( !found_time ) |
| 214 | { |
| 215 | if( equal(_data, "^"TimeStamp^"", 11) ) |
| 216 | { |
| 217 | found_time = true; |
| 218 | continue; |
| 219 | } |
| 220 | } |
| 221 | if( _data[0] == identifier ) |
| 222 | { |
| 223 | //if found key and wrote data continue and copy to g |
| 224 | // if didnt copy data, write data before copying to g |
| 225 | if( found_key ) |
| 226 | { |
| 227 | if( !wrote_data ) |
| 228 | { |
| 229 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %s^n" : "^"%s^" ^"%s^"^n", name, data); |
| 230 | wrote_data = true; |
| 231 | } |
| 232 | } |
| 233 | //else check if correct key, copy to g |
| 234 | else |
| 235 | { |
| 236 | //Make sure we get something parsed |
| 237 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 238 | { |
| 239 | if( equal( _name, key ) ) |
| 240 | found_key = true; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | fputs(g, _data); |
| 245 | } |
| 246 | else |
| 247 | { |
| 248 | if( !wrote_data ) |
| 249 | { |
| 250 | if( nfv_prop&NFV_OVERWRITE ) |
| 251 | { |
| 252 | //Make sure we get something parsed |
| 253 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 254 | { |
| 255 | //if name matches, write data to g, skip copy |
| 256 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 257 | { |
| 258 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %s^n" : "^"%s^" ^"%s^"^n", name, data); |
| 259 | wrote_data = true; |
| 260 | continue; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | else if( nfv_prop&NFV_IF_NOT_EXIST ) |
| 265 | { |
| 266 | //Make sure we get something parsed |
| 267 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 268 | { |
| 269 | //if name matches, stop, close files, delete temp file, return 0 |
| 270 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 271 | { |
| 272 | fclose(g); |
| 273 | fclose(f); |
| 274 | |
| 275 | delete_file(temp_file_name); |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | //else copy to g |
| 284 | fputs(g, _data); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if( !found_key ) |
| 289 | { |
| 290 | fprintf(g, "%c%s^n", identifier, key); |
| 291 | } |
| 292 | if( !wrote_data ) |
| 293 | { |
| 294 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %s^n" : "^"%s^" ^"%s^"^n", name, data); |
| 295 | } |
| 296 | |
| 297 | fclose(g); |
| 298 | fclose(f); |
| 299 | |
| 300 | delete_file(filename); |
| 301 | |
| 302 | while( !rename_file(temp_file_name, filename, 1) ) { } |
| 303 | |
| 304 | return 1; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Sets data as number in file under key and name. |
| 309 | * |
| 310 | * @param filename The file location for the data. |
| 311 | * @param key The key within the file to look for. Set to "" for no key. |
| 312 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 313 | * @param data The number to write. |
| 314 | * @param nfv_prop Properties to apply to the saved data in a bitsum. |
| 315 | * @param identifier The character used to identify different keys. |
| 316 | * @return 1 if successful. |
| 317 | */ |
| 318 | stock nfv_set_num(const filename[], const key[], name[], const data, const NFV_PROP:nfv_prop=NFV_OVERWRITE, const identifier=';') |
| 319 | { |
| 320 | new g = fopen(temp_file_name, "wt"); |
| 321 | new f = fopen(filename, "rt"); |
| 322 | |
| 323 | // lets put a time stamp |
| 324 | fprintf(g, "^"TimeStamp^" ^"%d^"^n", get_systime() ); |
| 325 | |
| 326 | new _data[512], _name[64], _other[3]; |
| 327 | |
| 328 | new bool:wrote_data = false, bool:found_key = false, bool:found_time = false; |
| 329 | |
| 330 | if( !(nfv_prop&NFV_NO_REPLACE_QUOTE) ) |
| 331 | { |
| 332 | nfv_replace_quote( name ); |
| 333 | } |
| 334 | |
| 335 | //If using a blank key, detect it already |
| 336 | if( !key[0] ) |
| 337 | found_key = true; |
| 338 | |
| 339 | while( !feof(f) ) |
| 340 | { |
| 341 | fgets(f, _data, charsmax(_data)); |
| 342 | |
| 343 | if( !found_time ) |
| 344 | { |
| 345 | if( equal(_data, "^"TimeStamp^"", 11) ) |
| 346 | { |
| 347 | found_time = true; |
| 348 | continue; |
| 349 | } |
| 350 | } |
| 351 | if( _data[0] == identifier ) |
| 352 | { |
| 353 | //if found key and wrote data continue and copy to g |
| 354 | // if didnt copy data, write data before copying to g |
| 355 | if( found_key ) |
| 356 | { |
| 357 | if( !wrote_data ) |
| 358 | { |
| 359 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %d^n" : "^"%s^" ^"%d^"^n", name, data); |
| 360 | wrote_data = true; |
| 361 | } |
| 362 | } |
| 363 | //else check if correct key, copy to g |
| 364 | else |
| 365 | { |
| 366 | //Make sure we get something parsed |
| 367 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 368 | { |
| 369 | if( equal( _name, key ) ) |
| 370 | found_key = true; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | fputs(g, _data); |
| 375 | } |
| 376 | else |
| 377 | { |
| 378 | if( !wrote_data ) |
| 379 | { |
| 380 | if( nfv_prop&NFV_OVERWRITE ) |
| 381 | { |
| 382 | //Make sure we get something parsed |
| 383 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 384 | { |
| 385 | //if name matches, write data to g, skip copy |
| 386 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 387 | { |
| 388 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %d^n" : "^"%s^" ^"%d^"^n", name, data); |
| 389 | wrote_data = true; |
| 390 | continue; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | else if( nfv_prop&NFV_IF_NOT_EXIST ) |
| 395 | { |
| 396 | //Make sure we get something parsed |
| 397 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 398 | { |
| 399 | //if name matches, stop, close files, delete temp file, return 0 |
| 400 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 401 | { |
| 402 | fclose(g); |
| 403 | fclose(f); |
| 404 | |
| 405 | delete_file(temp_file_name); |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | //else copy to g |
| 414 | fputs(g, _data); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | if( !found_key ) |
| 419 | { |
| 420 | fprintf(g, "%c%s^n", identifier, key); |
| 421 | } |
| 422 | if( !wrote_data ) |
| 423 | { |
| 424 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %d^n" : "^"%s^" ^"%d^"^n", name, data); |
| 425 | } |
| 426 | |
| 427 | fclose(g); |
| 428 | fclose(f); |
| 429 | |
| 430 | delete_file(filename); |
| 431 | |
| 432 | while( !rename_file(temp_file_name, filename, 1) ) { } |
| 433 | |
| 434 | return 1; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Adds to a pre-existing data number in file under key and name. |
| 439 | * |
| 440 | * @param filename The file location for the data. |
| 441 | * @param key The key within the file to look for. Set to "" for no key. |
| 442 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 443 | * @param data The number to add. |
| 444 | * @param nfv_prop Properties to apply to the saved data in a bitsum. |
| 445 | * @param identifier The character used to identify different keys. |
| 446 | * |
| 447 | * @note Will assume NFV_OVERWRITE is in nfv_prop and ignore NFV_IF_NOT_EXIST |
| 448 | * |
| 449 | * @return Returns new data. |
| 450 | */ |
| 451 | stock nfv_add_num(const filename[], const key[], name[], const data, const NFV_PROP:nfv_prop=NFV_OVERWRITE, const identifier=';') |
| 452 | { |
| 453 | new g = fopen(temp_file_name, "wt"); |
| 454 | new f = fopen(filename, "rt"); |
| 455 | |
| 456 | // lets put a time stamp |
| 457 | fprintf(g, "^"TimeStamp^" ^"%d^"^n", get_systime() ); |
| 458 | |
| 459 | new _data[512], _name[64], _other[64], _value = data; |
| 460 | |
| 461 | new bool:wrote_data = false, bool:found_key = false, bool:found_time = false; |
| 462 | |
| 463 | if( !(nfv_prop&NFV_NO_REPLACE_QUOTE) ) |
| 464 | { |
| 465 | nfv_replace_quote( name ); |
| 466 | } |
| 467 | |
| 468 | //If using a blank key, detect it already |
| 469 | if( !key[0] ) |
| 470 | found_key = true; |
| 471 | |
| 472 | while( !feof(f) ) |
| 473 | { |
| 474 | fgets(f, _data, charsmax(_data)); |
| 475 | |
| 476 | if( !found_time ) |
| 477 | { |
| 478 | if( equal(_data, "^"TimeStamp^"", 11) ) |
| 479 | { |
| 480 | found_time = true; |
| 481 | continue; |
| 482 | } |
| 483 | } |
| 484 | if( _data[0] == identifier ) |
| 485 | { |
| 486 | //if found key and wrote data continue and copy to g |
| 487 | // if didnt copy data, write data before copying to g |
| 488 | if( found_key ) |
| 489 | { |
| 490 | if( !wrote_data ) |
| 491 | { |
| 492 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %d^n" : "^"%s^" ^"%d^"^n", name, data); |
| 493 | wrote_data = true; |
| 494 | } |
| 495 | } |
| 496 | //else check if correct key, copy to g |
| 497 | else |
| 498 | { |
| 499 | //Make sure we get something parsed |
| 500 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 501 | { |
| 502 | if( equal( _name, key ) ) |
| 503 | found_key = true; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | fputs(g, _data); |
| 508 | } |
| 509 | else |
| 510 | { |
| 511 | if( found_key && !wrote_data ) |
| 512 | { |
| 513 | //Make sure we get something parsed |
| 514 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 515 | { |
| 516 | //if name matches, write data to g, skip copy |
| 517 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 518 | { |
| 519 | _value = data + str_to_num(_other); |
| 520 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %d^n" : "^"%s^" ^"%d^"^n", name, _value ); |
| 521 | wrote_data = true; |
| 522 | continue; |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | //else copy to g |
| 527 | fputs(g, _data); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | if( !found_key ) |
| 532 | { |
| 533 | fprintf(g, "%c%s^n", identifier, key); |
| 534 | } |
| 535 | if( !wrote_data ) |
| 536 | { |
| 537 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %d^n" : "^"%s^" ^"%d^"^n", name, data); |
| 538 | } |
| 539 | |
| 540 | fclose(g); |
| 541 | fclose(f); |
| 542 | |
| 543 | delete_file(filename); |
| 544 | |
| 545 | while( !rename_file(temp_file_name, filename, 1) ) { } |
| 546 | |
| 547 | return _value; |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * Sets data as a floating point in file under key and name. |
| 552 | * |
| 553 | * @param filename The file location for the data. |
| 554 | * @param key The key within the file to look for. Set to "" for no key. |
| 555 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 556 | * @param data The floating point to write. |
| 557 | * @param nfv_prop Properties to apply to the saved data in a bitsum. |
| 558 | * @param identifier The character used to identify different keys. |
| 559 | * @return 1 if successful. |
| 560 | */ |
| 561 | stock nfv_set_float(const filename[], const key[], name[], const Float:data, const NFV_PROP:nfv_prop=NFV_OVERWRITE, const identifier=';') |
| 562 | { |
| 563 | new g = fopen(temp_file_name, "wt"); |
| 564 | new f = fopen(filename, "rt"); |
| 565 | |
| 566 | // lets put a time stamp |
| 567 | fprintf(g, "^"TimeStamp^" ^"%d^"^n", get_systime() ); |
| 568 | |
| 569 | new _data[512], _name[64], _other[3]; |
| 570 | |
| 571 | new bool:wrote_data = false, bool:found_key = false, bool:found_time = false; |
| 572 | |
| 573 | if( !(nfv_prop&NFV_NO_REPLACE_QUOTE) ) |
| 574 | { |
| 575 | nfv_replace_quote( name ); |
| 576 | } |
| 577 | |
| 578 | //If using a blank key, detect it already |
| 579 | if( !key[0] ) |
| 580 | found_key = true; |
| 581 | |
| 582 | while( !feof(f) ) |
| 583 | { |
| 584 | fgets(f, _data, charsmax(_data)); |
| 585 | |
| 586 | if( !found_time ) |
| 587 | { |
| 588 | if( equal(_data, "^"TimeStamp^"", 11) ) |
| 589 | { |
| 590 | found_time = true; |
| 591 | continue; |
| 592 | } |
| 593 | } |
| 594 | if( _data[0] == identifier ) |
| 595 | { |
| 596 | //if found key and wrote data continue and copy to g |
| 597 | // if didnt copy data, write data before copying to g |
| 598 | if( found_key ) |
| 599 | { |
| 600 | if( !wrote_data ) |
| 601 | { |
| 602 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %f^n" : "^"%s^" ^"%f^"^n", name, data); |
| 603 | wrote_data = true; |
| 604 | } |
| 605 | } |
| 606 | //else check if correct key, copy to g |
| 607 | else |
| 608 | { |
| 609 | //Make sure we get something parsed |
| 610 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 611 | { |
| 612 | if( equal( _name, key ) ) |
| 613 | found_key = true; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | fputs(g, _data); |
| 618 | } |
| 619 | else |
| 620 | { |
| 621 | if( !wrote_data ) |
| 622 | { |
| 623 | if( nfv_prop&NFV_OVERWRITE ) |
| 624 | { |
| 625 | //Make sure we get something parsed |
| 626 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 627 | { |
| 628 | //if name matches, write data to g, skip copy |
| 629 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 630 | { |
| 631 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %f^n" : "^"%s^" ^"%f^"^n", name, data); |
| 632 | wrote_data = true; |
| 633 | continue; |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | else if( nfv_prop&NFV_IF_NOT_EXIST ) |
| 638 | { |
| 639 | //Make sure we get something parsed |
| 640 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 641 | { |
| 642 | //if name matches, stop, close files, delete temp file, return 0 |
| 643 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 644 | { |
| 645 | fclose(g); |
| 646 | fclose(f); |
| 647 | |
| 648 | delete_file(temp_file_name); |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | //else copy to g |
| 657 | fputs(g, _data); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | if( !found_key ) |
| 662 | { |
| 663 | fprintf(g, "%c%s^n", identifier, key); |
| 664 | } |
| 665 | if( !wrote_data ) |
| 666 | { |
| 667 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %f^n" : "^"%s^" ^"%f^"^n", name, data); |
| 668 | } |
| 669 | |
| 670 | fclose(g); |
| 671 | fclose(f); |
| 672 | |
| 673 | delete_file(filename); |
| 674 | |
| 675 | while( !rename_file(temp_file_name, filename, 1) ) { } |
| 676 | |
| 677 | return 1; |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Adds to pre-existing floating point data in file under key and name. |
| 682 | * |
| 683 | * @param filename The file location for the data. |
| 684 | * @param key The key within the file to look for. Set to "" for no key. |
| 685 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 686 | * @param data The floating point to add. |
| 687 | * @param nfv_prop Properties to apply to the saved data in a bitsum. |
| 688 | * @param identifier The character used to identify different keys. |
| 689 | * |
| 690 | * @note Will assume NFV_OVERWRITE is in nfv_prop and ignore NFV_IF_NOT_EXIST |
| 691 | * |
| 692 | * @return Returns new data. |
| 693 | */ |
| 694 | stock Float:nfv_add_float(const filename[], const key[], name[], const Float:data, const NFV_PROP:nfv_prop=NFV_OVERWRITE, const identifier=';') |
| 695 | { |
| 696 | new g = fopen(temp_file_name, "wt"); |
| 697 | new f = fopen(filename, "rt"); |
| 698 | |
| 699 | // lets put a time stamp |
| 700 | fprintf(g, "^"TimeStamp^" ^"%d^"^n", get_systime() ); |
| 701 | |
| 702 | new _data[512], _name[64], _other[64], Float:_value=data; |
| 703 | |
| 704 | new bool:wrote_data = false, bool:found_key = false, bool:found_time = false; |
| 705 | |
| 706 | if( !(nfv_prop&NFV_NO_REPLACE_QUOTE) ) |
| 707 | { |
| 708 | nfv_replace_quote( name ); |
| 709 | } |
| 710 | |
| 711 | //If using a blank key, detect it already |
| 712 | if( !key[0] ) |
| 713 | found_key = true; |
| 714 | |
| 715 | while( !feof(f) ) |
| 716 | { |
| 717 | fgets(f, _data, charsmax(_data)); |
| 718 | |
| 719 | if( !found_time ) |
| 720 | { |
| 721 | if( equal(_data, "^"TimeStamp^"", 11) ) |
| 722 | { |
| 723 | found_time = true; |
| 724 | continue; |
| 725 | } |
| 726 | } |
| 727 | if( _data[0] == identifier ) |
| 728 | { |
| 729 | //if found key and wrote data continue and copy to g |
| 730 | // if didnt copy data, write data before copying to g |
| 731 | if( found_key ) |
| 732 | { |
| 733 | if( !wrote_data ) |
| 734 | { |
| 735 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %f^n" : "^"%s^" ^"%f^"^n", name, data); |
| 736 | wrote_data = true; |
| 737 | } |
| 738 | } |
| 739 | //else check if correct key, copy to g |
| 740 | else |
| 741 | { |
| 742 | //Make sure we get something parsed |
| 743 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 744 | { |
| 745 | if( equal( _name, key ) ) |
| 746 | found_key = true; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | fputs(g, _data); |
| 751 | } |
| 752 | else |
| 753 | { |
| 754 | if( found_key && !wrote_data ) |
| 755 | { |
| 756 | //Make sure we get something parsed |
| 757 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 758 | { |
| 759 | //if name matches, write data to g, skip copy |
| 760 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 761 | { |
| 762 | _value = data + str_to_float(_other); |
| 763 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %f^n" : "^"%s^" ^"%f^"^n", name, _value ); |
| 764 | wrote_data = true; |
| 765 | continue; |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | //else copy to g |
| 770 | fputs(g, _data); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | if( !found_key ) |
| 775 | { |
| 776 | fprintf(g, "%c%s^n", identifier, key); |
| 777 | } |
| 778 | if( !wrote_data ) |
| 779 | { |
| 780 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %f^n" : "^"%s^" ^"%f^"^n", name, data); |
| 781 | } |
| 782 | |
| 783 | fclose(g); |
| 784 | fclose(f); |
| 785 | |
| 786 | delete_file(filename); |
| 787 | |
| 788 | while( !rename_file(temp_file_name, filename, 1) ) { } |
| 789 | |
| 790 | return _value; |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * Sets data as bool in file under key and name. |
| 795 | * |
| 796 | * @param filename The file location for the data. |
| 797 | * @param key The key within the file to look for. Set to "" for no key. |
| 798 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 799 | * @param data The boolean value to write. |
| 800 | * @param nfv_prop Properties to apply to the saved data in a bitsum. |
| 801 | * @param identifier The character used to identify different keys. |
| 802 | * @return 1 if successful. |
| 803 | */ |
| 804 | stock nfv_set_bool(const filename[], const key[], name[], const bool:data, const NFV_PROP:nfv_prop=NFV_OVERWRITE, const identifier=';') |
| 805 | { |
| 806 | new g = fopen(temp_file_name, "wt"); |
| 807 | new f = fopen(filename, "rt"); |
| 808 | |
| 809 | // lets put a time stamp |
| 810 | fprintf(g, "^"TimeStamp^" ^"%d^"^n", get_systime() ); |
| 811 | |
| 812 | new _data[512], _name[64], _other[3]; |
| 813 | |
| 814 | new bool:wrote_data = false, bool:found_key = false, bool:found_time = false; |
| 815 | |
| 816 | if( !(nfv_prop&NFV_NO_REPLACE_QUOTE) ) |
| 817 | { |
| 818 | nfv_replace_quote( name ); |
| 819 | } |
| 820 | |
| 821 | //If using a blank key, detect it already |
| 822 | if( !key[0] ) |
| 823 | found_key = true; |
| 824 | |
| 825 | while( !feof(f) ) |
| 826 | { |
| 827 | fgets(f, _data, charsmax(_data)); |
| 828 | |
| 829 | if( !found_time ) |
| 830 | { |
| 831 | if( equal(_data, "^"TimeStamp^"", 11) ) |
| 832 | { |
| 833 | found_time = true; |
| 834 | continue; |
| 835 | } |
| 836 | } |
| 837 | if( _data[0] == identifier ) |
| 838 | { |
| 839 | //if found key and wrote data continue and copy to g |
| 840 | // if didnt copy data, write data before copying to g |
| 841 | if( found_key ) |
| 842 | { |
| 843 | if( !wrote_data ) |
| 844 | { |
| 845 | if( nfv_prop&NFV_NO_QUOTES ) |
| 846 | fprintf(g, data ? "%s true^n" : "%s false^n", name); |
| 847 | else |
| 848 | fprintf(g, data ? "^"%s^" ^"true^"^n" : "^"%s^" ^"false^"^n", name); |
| 849 | wrote_data = true; |
| 850 | } |
| 851 | } |
| 852 | //else check if correct key, copy to g |
| 853 | else |
| 854 | { |
| 855 | //Make sure we get something parsed |
| 856 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 857 | { |
| 858 | if( equal( _name, key ) ) |
| 859 | found_key = true; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | fputs(g, _data); |
| 864 | } |
| 865 | else |
| 866 | { |
| 867 | if( !wrote_data ) |
| 868 | { |
| 869 | if( nfv_prop&NFV_OVERWRITE ) |
| 870 | { |
| 871 | //Make sure we get something parsed |
| 872 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 873 | { |
| 874 | //if name matches, write data to g, skip copy |
| 875 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 876 | { |
| 877 | if( nfv_prop&NFV_NO_QUOTES ) |
| 878 | fprintf(g, data ? "%s true^n" : "%s false^n", name); |
| 879 | else |
| 880 | fprintf(g, data ? "^"%s^" ^"true^"^n" : "^"%s^" ^"false^"^n", name); |
| 881 | wrote_data = true; |
| 882 | continue; |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | else if( nfv_prop&NFV_IF_NOT_EXIST ) |
| 887 | { |
| 888 | //Make sure we get something parsed |
| 889 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 890 | { |
| 891 | //if name matches, stop, close files, delete temp file, return 0 |
| 892 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 893 | { |
| 894 | fclose(g); |
| 895 | fclose(f); |
| 896 | |
| 897 | delete_file(temp_file_name); |
| 898 | |
| 899 | return 0; |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | //else copy to g |
| 906 | fputs(g, _data); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | if( !found_key ) |
| 911 | { |
| 912 | fprintf(g, "%c%s^n", identifier, key); |
| 913 | } |
| 914 | if( !wrote_data ) |
| 915 | { |
| 916 | if( nfv_prop&NFV_NO_QUOTES ) |
| 917 | fprintf(g, data ? "%s true^n" : "%s false^n", name); |
| 918 | else |
| 919 | fprintf(g, data ? "^"%s^" ^"true^"^n" : "^"%s^" ^"false^"^n", name); |
| 920 | } |
| 921 | |
| 922 | fclose(g); |
| 923 | fclose(f); |
| 924 | |
| 925 | delete_file(filename); |
| 926 | |
| 927 | while( !rename_file(temp_file_name, filename, 1) ) { } |
| 928 | |
| 929 | return 1; |
| 930 | } |
| 931 | |
| 932 | /** |
| 933 | * Sets data to the opposite of what it was in file under key and name. |
| 934 | * |
| 935 | * @param filename The file location for the data. |
| 936 | * @param key The key within the file to look for. Set to "" for no key. |
| 937 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 938 | * @param nfv_prop Properties to apply to the saved data in a bitsum. |
| 939 | * @param identifier The character used to identify different keys. |
| 940 | * |
| 941 | * @note Will assume NFV_OVERWRITE is in nfv_prop and ignore NFV_IF_NOT_EXIST |
| 942 | * |
| 943 | * @return Returns new data. |
| 944 | */ |
| 945 | stock bool:nfv_xor_bool(const filename[], const key[], name[], const NFV_PROP:nfv_prop=NFV_OVERWRITE, const identifier=';') |
| 946 | { |
| 947 | new g = fopen(temp_file_name, "wt"); |
| 948 | new f = fopen(filename, "rt"); |
| 949 | |
| 950 | // lets put a time stamp |
| 951 | fprintf(g, "^"TimeStamp^" ^"%d^"^n", get_systime() ); |
| 952 | |
| 953 | new _data[512], _name[64], _other[64], bool:_value = true; |
| 954 | |
| 955 | new bool:wrote_data = false, bool:found_key = false, bool:found_time = false; |
| 956 | |
| 957 | if( !(nfv_prop&NFV_NO_REPLACE_QUOTE) ) |
| 958 | { |
| 959 | nfv_replace_quote( name ); |
| 960 | } |
| 961 | |
| 962 | //If using a blank key, detect it already |
| 963 | if( !key[0] ) |
| 964 | found_key = true; |
| 965 | |
| 966 | while( !feof(f) ) |
| 967 | { |
| 968 | fgets(f, _data, charsmax(_data)); |
| 969 | |
| 970 | if( !found_time ) |
| 971 | { |
| 972 | if( equal(_data, "^"TimeStamp^"", 11) ) |
| 973 | { |
| 974 | found_time = true; |
| 975 | continue; |
| 976 | } |
| 977 | } |
| 978 | if( _data[0] == identifier ) |
| 979 | { |
| 980 | //if found key and wrote data continue and copy to g |
| 981 | // if didnt copy data, write data before copying to g |
| 982 | if( found_key ) |
| 983 | { |
| 984 | if( !wrote_data ) |
| 985 | { |
| 986 | if( nfv_prop&NFV_NO_QUOTES ) |
| 987 | fprintf(g, "%s true^n", name); |
| 988 | else |
| 989 | fprintf(g, "^"%s^" ^"true^"^n", name); |
| 990 | wrote_data = true; |
| 991 | } |
| 992 | } |
| 993 | //else check if correct key, copy to g |
| 994 | else |
| 995 | { |
| 996 | //Make sure we get something parsed |
| 997 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 998 | { |
| 999 | if( equal( _name, key ) ) |
| 1000 | found_key = true; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | fputs(g, _data); |
| 1005 | } |
| 1006 | else |
| 1007 | { |
| 1008 | if( found_key && !wrote_data ) |
| 1009 | { |
| 1010 | //Make sure we get something parsed |
| 1011 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1012 | { |
| 1013 | //if name matches, write data to g, skip copy |
| 1014 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 1015 | { |
| 1016 | _value = !str_to_bool( _other ); |
| 1017 | if( nfv_prop&NFV_NO_QUOTES ) |
| 1018 | fprintf(g, _value ? "%s true^n" : "%s false^n", name); |
| 1019 | else |
| 1020 | fprintf(g, _value ? "^"%s^" ^"true^"^n" : "^"%s^" ^"false^"^n", name); |
| 1021 | wrote_data = true; |
| 1022 | continue; |
| 1023 | } |
| 1024 | } |
| 1025 | } |
| 1026 | //else copy to g |
| 1027 | fputs(g, _data); |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | if( !found_key ) |
| 1032 | { |
| 1033 | fprintf(g, "%c%s^n", identifier, key); |
| 1034 | } |
| 1035 | if( !wrote_data ) |
| 1036 | { |
| 1037 | if( nfv_prop&NFV_NO_QUOTES ) |
| 1038 | fprintf(g, "%s true^n", name); |
| 1039 | else |
| 1040 | fprintf(g, "^"%s^" ^"true^"^n", name); |
| 1041 | } |
| 1042 | |
| 1043 | fclose(g); |
| 1044 | fclose(f); |
| 1045 | |
| 1046 | delete_file(filename); |
| 1047 | |
| 1048 | while( !rename_file(temp_file_name, filename, 1) ) { } |
| 1049 | |
| 1050 | return _value; |
| 1051 | } |
| 1052 | |
| 1053 | /** |
| 1054 | * Sets data as a vector in file under key and name. |
| 1055 | * |
| 1056 | * @param filename The file location for the data. |
| 1057 | * @param key The key within the file to look for. Set to "" for no key. |
| 1058 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 1059 | * @param data The vector to write. |
| 1060 | * @param nfv_prop Properties to apply to the saved data in a bitsum. |
| 1061 | * @param identifier The character used to identify different keys. |
| 1062 | * @return 1 if successful. |
| 1063 | */ |
| 1064 | stock nfv_set_vec(const filename[], const key[], name[], const Float:data[3], const NFV_PROP:nfv_prop=NFV_OVERWRITE, const identifier=';') |
| 1065 | { |
| 1066 | new g = fopen(temp_file_name, "wt"); |
| 1067 | new f = fopen(filename, "rt"); |
| 1068 | |
| 1069 | // lets put a time stamp |
| 1070 | fprintf(g, "^"TimeStamp^" ^"%d^"^n", get_systime() ); |
| 1071 | |
| 1072 | new _data[512], _name[64], _other[3]; |
| 1073 | |
| 1074 | new bool:wrote_data = false, bool:found_key = false, bool:found_time = false; |
| 1075 | |
| 1076 | if( !(nfv_prop&NFV_NO_REPLACE_QUOTE) ) |
| 1077 | { |
| 1078 | nfv_replace_quote( name ); |
| 1079 | } |
| 1080 | |
| 1081 | //If using a blank key, detect it already |
| 1082 | if( !key[0] ) |
| 1083 | found_key = true; |
| 1084 | |
| 1085 | while( !feof(f) ) |
| 1086 | { |
| 1087 | fgets(f, _data, charsmax(_data)); |
| 1088 | |
| 1089 | if( !found_time ) |
| 1090 | { |
| 1091 | if( equal(_data, "^"TimeStamp^"", 11) ) |
| 1092 | { |
| 1093 | found_time = true; |
| 1094 | continue; |
| 1095 | } |
| 1096 | } |
| 1097 | if( _data[0] == identifier ) |
| 1098 | { |
| 1099 | //if found key and wrote data continue and copy to g |
| 1100 | // if didnt copy data, write data before copying to g |
| 1101 | if( found_key ) |
| 1102 | { |
| 1103 | if( !wrote_data ) |
| 1104 | { |
| 1105 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %f,%f,%f^n" : "^"%s^" ^"%f,%f,%f^"^n", name, data[0], data[1], data[2]); |
| 1106 | wrote_data = true; |
| 1107 | } |
| 1108 | } |
| 1109 | //else check if correct key, copy to g |
| 1110 | else |
| 1111 | { |
| 1112 | //Make sure we get something parsed |
| 1113 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1114 | { |
| 1115 | if( equal( _name, key ) ) |
| 1116 | found_key = true; |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | fputs(g, _data); |
| 1121 | } |
| 1122 | else |
| 1123 | { |
| 1124 | if( !wrote_data ) |
| 1125 | { |
| 1126 | if( nfv_prop&NFV_OVERWRITE ) |
| 1127 | { |
| 1128 | //Make sure we get something parsed |
| 1129 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1130 | { |
| 1131 | //if name matches, write data to g, skip copy |
| 1132 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 1133 | { |
| 1134 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %f,%f,%f^n" : "^"%s^" ^"%f,%f,%f^"^n", name, data[0], data[1], data[2]); |
| 1135 | wrote_data = true; |
| 1136 | continue; |
| 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | else if( nfv_prop&NFV_IF_NOT_EXIST ) |
| 1141 | { |
| 1142 | //Make sure we get something parsed |
| 1143 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1144 | { |
| 1145 | //if name matches, stop, close files, delete temp file, return 0 |
| 1146 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 1147 | { |
| 1148 | fclose(g); |
| 1149 | fclose(f); |
| 1150 | |
| 1151 | delete_file(temp_file_name); |
| 1152 | |
| 1153 | return 0; |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | //else copy to g |
| 1160 | fputs(g, _data); |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | if( !found_key ) |
| 1165 | { |
| 1166 | fprintf(g, "%c%s^n", identifier, key); |
| 1167 | } |
| 1168 | if( !wrote_data ) |
| 1169 | { |
| 1170 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %f,%f,%f^n" : "^"%s^" ^"%f,%f,%f^"^n", name, data[0], data[1], data[2]); |
| 1171 | } |
| 1172 | |
| 1173 | fclose(g); |
| 1174 | fclose(f); |
| 1175 | |
| 1176 | delete_file(filename); |
| 1177 | |
| 1178 | while( !rename_file(temp_file_name, filename, 1) ) { } |
| 1179 | |
| 1180 | return 1; |
| 1181 | } |
| 1182 | |
| 1183 | /** |
| 1184 | * Sets data as an array in file under key and name. |
| 1185 | * |
| 1186 | * @param filename The file location for the data. |
| 1187 | * @param key The key within the file to look for. Set to "" for no key. |
| 1188 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 1189 | * @param data The array to write. |
| 1190 | * @param array_size The size of the array. |
| 1191 | * @param nfv_prop Properties to apply to the saved data in a bitsum. |
| 1192 | * @param identifier The character used to identify different keys. |
| 1193 | * |
| 1194 | * @note This will save each element as the integer form |
| 1195 | * |
| 1196 | * @return 1 if successful. |
| 1197 | */ |
| 1198 | stock nfv_set_array(const filename[], const key[], name[], const any:data[], const array_size, const NFV_PROP:nfv_prop=NFV_OVERWRITE, const identifier=';') |
| 1199 | { |
| 1200 | new g = fopen(temp_file_name, "wt"); |
| 1201 | new f = fopen(filename, "rt"); |
| 1202 | |
| 1203 | // lets put a time stamp |
| 1204 | fprintf(g, "^"TimeStamp^" ^"%d^"^n", get_systime() ); |
| 1205 | |
| 1206 | new _data[512], _name[64], _other[3], i; |
| 1207 | |
| 1208 | new bool:wrote_data = false, bool:found_key = false, bool:found_time = false; |
| 1209 | |
| 1210 | if( !(nfv_prop&NFV_NO_REPLACE_QUOTE) ) |
| 1211 | { |
| 1212 | nfv_replace_quote( name ); |
| 1213 | } |
| 1214 | |
| 1215 | //If using a blank key, detect it already |
| 1216 | if( !key[0] ) |
| 1217 | found_key = true; |
| 1218 | |
| 1219 | while( !feof(f) ) |
| 1220 | { |
| 1221 | fgets(f, _data, charsmax(_data)); |
| 1222 | |
| 1223 | if( !found_time ) |
| 1224 | { |
| 1225 | if( equal(_data, "^"TimeStamp^"", 11) ) |
| 1226 | { |
| 1227 | found_time = true; |
| 1228 | continue; |
| 1229 | } |
| 1230 | } |
| 1231 | if( _data[0] == identifier ) |
| 1232 | { |
| 1233 | //if found key and wrote data continue and copy to g |
| 1234 | // if didnt copy data, write data before copying to g |
| 1235 | if( found_key ) |
| 1236 | { |
| 1237 | if( !wrote_data ) |
| 1238 | { |
| 1239 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %d" : "^"%s^" ^"%d", name, data[0]); |
| 1240 | for( i=1; i<array_size; i++ ) |
| 1241 | fprintf(g, ",%d", data[i]); |
| 1242 | fputs(g, nfv_prop&NFV_NO_QUOTES ? "^n" : "^"^n"); |
| 1243 | wrote_data = true; |
| 1244 | } |
| 1245 | } |
| 1246 | //else check if correct key, copy to g |
| 1247 | else |
| 1248 | { |
| 1249 | //Make sure we get something parsed |
| 1250 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1251 | { |
| 1252 | if( equal( _name, key ) ) |
| 1253 | found_key = true; |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | fputs(g, _data); |
| 1258 | } |
| 1259 | else |
| 1260 | { |
| 1261 | if( !wrote_data ) |
| 1262 | { |
| 1263 | if( nfv_prop&NFV_OVERWRITE ) |
| 1264 | { |
| 1265 | //Make sure we get something parsed |
| 1266 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1267 | { |
| 1268 | //if name matches, write data to g, skip copy |
| 1269 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 1270 | { |
| 1271 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %d" : "^"%s^" ^"%d", name, data[0]); |
| 1272 | for( i=1; i<array_size; i++ ) |
| 1273 | fprintf(g, ",%d", data[i]); |
| 1274 | fputs(g, nfv_prop&NFV_NO_QUOTES ? "^n" : "^"^n"); |
| 1275 | wrote_data = true; |
| 1276 | continue; |
| 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | else if( nfv_prop&NFV_IF_NOT_EXIST ) |
| 1281 | { |
| 1282 | //Make sure we get something parsed |
| 1283 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1284 | { |
| 1285 | //if name matches, stop, close files, delete temp file, return 0 |
| 1286 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 1287 | { |
| 1288 | fclose(g); |
| 1289 | fclose(f); |
| 1290 | |
| 1291 | delete_file(temp_file_name); |
| 1292 | |
| 1293 | return 0; |
| 1294 | } |
| 1295 | } |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | //else copy to g |
| 1300 | fputs(g, _data); |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | if( !found_key ) |
| 1305 | { |
| 1306 | fprintf(g, "%c%s^n", identifier, key); |
| 1307 | } |
| 1308 | if( !wrote_data ) |
| 1309 | { |
| 1310 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %d" : "^"%s^" ^"%d", name, data[0]); |
| 1311 | for( i=1; i<array_size; i++ ) |
| 1312 | fprintf(g, ",%d", data[i]); |
| 1313 | fputs(g, nfv_prop&NFV_NO_QUOTES ? "^n" : "^"^n"); |
| 1314 | } |
| 1315 | |
| 1316 | fclose(g); |
| 1317 | fclose(f); |
| 1318 | |
| 1319 | delete_file(filename); |
| 1320 | |
| 1321 | while( !rename_file(temp_file_name, filename, 1) ) { } |
| 1322 | |
| 1323 | return 1; |
| 1324 | } |
| 1325 | |
| 1326 | /** |
| 1327 | * Sets data as an array in file under key and name. |
| 1328 | * |
| 1329 | * @param filename The file location for the data. |
| 1330 | * @param key The key within the file to look for. Set to "" for no key. |
| 1331 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 1332 | * @param data The cell array to write. |
| 1333 | * @param array_size The size of the array. |
| 1334 | * @param nfv_prop Properties to apply to the saved data in a bitsum. |
| 1335 | * @param identifier The character used to identify different keys. |
| 1336 | * |
| 1337 | * @note This will save each element as the integer form |
| 1338 | * |
| 1339 | * @return 1 if successful. |
| 1340 | */ |
| 1341 | stock nfv_set_cellarray(const filename[], const key[], name[], const Array:data, const NFV_PROP:nfv_prop=NFV_OVERWRITE, const identifier=';') |
| 1342 | { |
| 1343 | new g = fopen(temp_file_name, "wt"); |
| 1344 | new f = fopen(filename, "rt"); |
| 1345 | |
| 1346 | // lets put a time stamp |
| 1347 | fprintf(g, "^"TimeStamp^" ^"%d^"^n", get_systime() ); |
| 1348 | |
| 1349 | new _data[512], _name[64], _other[3], i; |
| 1350 | |
| 1351 | new bool:wrote_data = false, bool:found_key = false, bool:found_time = false; |
| 1352 | new array_size = ArraySize(data); |
| 1353 | |
| 1354 | if( !(nfv_prop&NFV_NO_REPLACE_QUOTE) ) |
| 1355 | { |
| 1356 | nfv_replace_quote( name ); |
| 1357 | } |
| 1358 | |
| 1359 | //If using a blank key, detect it already |
| 1360 | if( !key[0] ) |
| 1361 | found_key = true; |
| 1362 | |
| 1363 | while( !feof(f) ) |
| 1364 | { |
| 1365 | fgets(f, _data, charsmax(_data)); |
| 1366 | |
| 1367 | if( !found_time ) |
| 1368 | { |
| 1369 | if( equal(_data, "^"TimeStamp^"", 11) ) |
| 1370 | { |
| 1371 | found_time = true; |
| 1372 | continue; |
| 1373 | } |
| 1374 | } |
| 1375 | if( _data[0] == identifier ) |
| 1376 | { |
| 1377 | //if found key and wrote data continue and copy to g |
| 1378 | // if didnt copy data, write data before copying to g |
| 1379 | if( found_key ) |
| 1380 | { |
| 1381 | if( !wrote_data ) |
| 1382 | { |
| 1383 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %d" : "^"%s^" ^"%d", name, ArrayGetCell(data,0)); |
| 1384 | for( i=1; i<array_size; i++ ) |
| 1385 | fprintf(g, ",%d", ArrayGetCell(data,i)); |
| 1386 | fputs(g, nfv_prop&NFV_NO_QUOTES ? "^n" : "^"^n"); |
| 1387 | wrote_data = true; |
| 1388 | } |
| 1389 | } |
| 1390 | //else check if correct key, copy to g |
| 1391 | else |
| 1392 | { |
| 1393 | //Make sure we get something parsed |
| 1394 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1395 | { |
| 1396 | if( equal( _name, key ) ) |
| 1397 | found_key = true; |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | fputs(g, _data); |
| 1402 | } |
| 1403 | else |
| 1404 | { |
| 1405 | if( !wrote_data ) |
| 1406 | { |
| 1407 | if( nfv_prop&NFV_OVERWRITE ) |
| 1408 | { |
| 1409 | //Make sure we get something parsed |
| 1410 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1411 | { |
| 1412 | //if name matches, write data to g, skip copy |
| 1413 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 1414 | { |
| 1415 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %d" : "^"%s^" ^"%d", name, ArrayGetCell(data,0)); |
| 1416 | for( i=1; i<array_size; i++ ) |
| 1417 | fprintf(g, ",%d", ArrayGetCell(data,i)); |
| 1418 | fputs(g, nfv_prop&NFV_NO_QUOTES ? "^n" : "^"^n"); |
| 1419 | wrote_data = true; |
| 1420 | continue; |
| 1421 | } |
| 1422 | } |
| 1423 | } |
| 1424 | else if( nfv_prop&NFV_IF_NOT_EXIST ) |
| 1425 | { |
| 1426 | //Make sure we get something parsed |
| 1427 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1428 | { |
| 1429 | //if name matches, stop, close files, delete temp file, return 0 |
| 1430 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 1431 | { |
| 1432 | fclose(g); |
| 1433 | fclose(f); |
| 1434 | |
| 1435 | delete_file(temp_file_name); |
| 1436 | |
| 1437 | return 0; |
| 1438 | } |
| 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | //else copy to g |
| 1444 | fputs(g, _data); |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | if( !found_key ) |
| 1449 | { |
| 1450 | fprintf(g, "%c%s^n", identifier, key); |
| 1451 | } |
| 1452 | if( !wrote_data ) |
| 1453 | { |
| 1454 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %d" : "^"%s^" ^"%d", name, ArrayGetCell(data,0)); |
| 1455 | for( i=1; i<array_size; i++ ) |
| 1456 | fprintf(g, ",%d", ArrayGetCell(data,i)); |
| 1457 | fputs(g, nfv_prop&NFV_NO_QUOTES ? "^n" : "^"^n"); |
| 1458 | } |
| 1459 | |
| 1460 | fclose(g); |
| 1461 | fclose(f); |
| 1462 | |
| 1463 | delete_file(filename); |
| 1464 | |
| 1465 | while( !rename_file(temp_file_name, filename, 1) ) { } |
| 1466 | |
| 1467 | return 1; |
| 1468 | } |
| 1469 | |
| 1470 | /** |
| 1471 | * Sets data as array of strings in file under key and name. |
| 1472 | * |
| 1473 | * @param filename The file location for the data. |
| 1474 | * @param key The key within the file to look for. Set to "" for no key. |
| 1475 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 1476 | * @param data The cell array to write. |
| 1477 | * @param array_size The size of the array. |
| 1478 | * @param nfv_prop Properties to apply to the saved data in a bitsum. |
| 1479 | * @param identifier The character used to identify different keys. |
| 1480 | * |
| 1481 | * @note This will save in the form: "first","second","third","etc." |
| 1482 | * |
| 1483 | * @return 1 if successful. |
| 1484 | */ |
| 1485 | stock nfv_set_cellarray2(const filename[], const key[], name[], const Array:data, const NFV_PROP:nfv_prop=NFV_OVERWRITE, const identifier=';') |
| 1486 | { |
| 1487 | new g = fopen(temp_file_name, "wt"); |
| 1488 | new f = fopen(filename, "rt"); |
| 1489 | |
| 1490 | // lets put a time stamp |
| 1491 | fprintf(g, "^"TimeStamp^" ^"%d^"^n", get_systime() ); |
| 1492 | |
| 1493 | new _data[512], _name[64], _other[3], i; |
| 1494 | new temp_string[64]; |
| 1495 | |
| 1496 | new bool:wrote_data = false, bool:found_key = false, bool:found_time = false; |
| 1497 | new array_size = ArraySize(data); |
| 1498 | |
| 1499 | if( !(nfv_prop&NFV_NO_REPLACE_QUOTE) ) |
| 1500 | { |
| 1501 | nfv_replace_quote( name ); |
| 1502 | } |
| 1503 | |
| 1504 | //If using a blank key, detect it already |
| 1505 | if( !key[0] ) |
| 1506 | found_key = true; |
| 1507 | |
| 1508 | while( !feof(f) ) |
| 1509 | { |
| 1510 | fgets(f, _data, charsmax(_data)); |
| 1511 | |
| 1512 | if( !found_time ) |
| 1513 | { |
| 1514 | if( equal(_data, "^"TimeStamp^"", 11) ) |
| 1515 | { |
| 1516 | found_time = true; |
| 1517 | continue; |
| 1518 | } |
| 1519 | } |
| 1520 | if( _data[0] == identifier ) |
| 1521 | { |
| 1522 | //if found key and wrote data continue and copy to g |
| 1523 | // if didnt copy data, write data before copying to g |
| 1524 | if( found_key ) |
| 1525 | { |
| 1526 | if( !wrote_data ) |
| 1527 | { |
| 1528 | ArrayGetString(data, 0, temp_string, charsmax(temp_string)); |
| 1529 | nfv_replace_quote( temp_string ); |
| 1530 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %s" : "^"%s^" ^"%s", name, temp_string); |
| 1531 | for( i=1; i<array_size; i++ ) |
| 1532 | { |
| 1533 | ArrayGetString(data, i, temp_string, charsmax(temp_string)); |
| 1534 | nfv_replace_quote( temp_string ); |
| 1535 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? ",%s" : "^",^"%s^"", temp_string); |
| 1536 | } |
| 1537 | fputs(g, nfv_prop&NFV_NO_QUOTES ? "^n" : "^"^n"); |
| 1538 | wrote_data = true; |
| 1539 | } |
| 1540 | } |
| 1541 | //else check if correct key, copy to g |
| 1542 | else |
| 1543 | { |
| 1544 | //Make sure we get something parsed |
| 1545 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1546 | { |
| 1547 | if( equal( _name, key ) ) |
| 1548 | found_key = true; |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | fputs(g, _data); |
| 1553 | } |
| 1554 | else |
| 1555 | { |
| 1556 | if( !wrote_data ) |
| 1557 | { |
| 1558 | if( nfv_prop&NFV_OVERWRITE ) |
| 1559 | { |
| 1560 | //Make sure we get something parsed |
| 1561 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1562 | { |
| 1563 | //if name matches, write data to g, skip copy |
| 1564 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 1565 | { |
| 1566 | ArrayGetString(data, 0, temp_string, charsmax(temp_string)); |
| 1567 | nfv_replace_quote( temp_string ); |
| 1568 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %s" : "^"%s^" ^"%s", name, temp_string); |
| 1569 | for( i=1; i<array_size; i++ ) |
| 1570 | { |
| 1571 | ArrayGetString(data, i, temp_string, charsmax(temp_string)); |
| 1572 | nfv_replace_quote( temp_string ); |
| 1573 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? ",%s" : "^",^"%s^"", temp_string); |
| 1574 | } |
| 1575 | fputs(g, nfv_prop&NFV_NO_QUOTES ? "^n" : "^"^n"); |
| 1576 | wrote_data = true; |
| 1577 | continue; |
| 1578 | } |
| 1579 | } |
| 1580 | } |
| 1581 | else if( nfv_prop&NFV_IF_NOT_EXIST ) |
| 1582 | { |
| 1583 | //Make sure we get something parsed |
| 1584 | if( parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1585 | { |
| 1586 | //if name matches, stop, close files, delete temp file, return 0 |
| 1587 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 1588 | { |
| 1589 | fclose(g); |
| 1590 | fclose(f); |
| 1591 | |
| 1592 | delete_file(temp_file_name); |
| 1593 | |
| 1594 | return 0; |
| 1595 | } |
| 1596 | } |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | //else copy to g |
| 1601 | fputs(g, _data); |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | if( !found_key ) |
| 1606 | { |
| 1607 | fprintf(g, "%c%s^n", identifier, key); |
| 1608 | } |
| 1609 | if( !wrote_data ) |
| 1610 | { |
| 1611 | ArrayGetString(data, 0, temp_string, charsmax(temp_string)); |
| 1612 | nfv_replace_quote( temp_string ); |
| 1613 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? "%s %s" : "^"%s^" ^"%s", name, temp_string); |
| 1614 | for( i=1; i<array_size; i++ ) |
| 1615 | { |
| 1616 | ArrayGetString(data, i, temp_string, charsmax(temp_string)); |
| 1617 | nfv_replace_quote( temp_string ); |
| 1618 | fprintf(g, nfv_prop&NFV_NO_QUOTES ? ",%s" : "^",^"%s^"", temp_string); |
| 1619 | } |
| 1620 | fputs(g, nfv_prop&NFV_NO_QUOTES ? "^n" : "^"^n"); |
| 1621 | } |
| 1622 | |
| 1623 | fclose(g); |
| 1624 | fclose(f); |
| 1625 | |
| 1626 | delete_file(filename); |
| 1627 | |
| 1628 | while( !rename_file(temp_file_name, filename, 1) ) { } |
| 1629 | |
| 1630 | return 1; |
| 1631 | } |
| 1632 | |
| 1633 | /** |
| 1634 | * Retrieves data in file under key and name. |
| 1635 | * |
| 1636 | * @param filename The file location to search for the data. |
| 1637 | * @param key The key within the file to look for. Set to "" for no key. Set to NFV_WILDCARD for any key. |
| 1638 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 1639 | * @param data The string that will contain the data retrieved. |
| 1640 | * @param len The size of the data string. |
| 1641 | * @param identifier The character used to identify different keys. |
| 1642 | * @param key_tell_start Where to start searching for key. |
| 1643 | * @param key_tell_find Where the key was found. |
| 1644 | * @param name_tell_start Where to start searching for name. |
| 1645 | * @param name_tell_find Where the name was found. |
| 1646 | * @param key_copy The string that will contain the key of the data retrieved. Only used with NFV_WILDCARD for key. |
| 1647 | * @param key_len If specified, the size of the key found to copy. Only used with NFV_WILDCARD for key. |
| 1648 | * @param name_copy The string that will contain the name of the data retrieved. Only used with NFV_WILDCARD for name. |
| 1649 | * @param name_len If specified, the size of the name found to copy. Only used with NFV_WILDCARD for name. |
| 1650 | * @return 1 if data found. |
| 1651 | */ |
| 1652 | stock nfv_get_data(const filename[], const key[], name[], data[]="", len=0, const identifier=';', const key_tell_start=0, &key_tell_find=0, const name_tell_start=0, &name_tell_find=0, key_copy[]="", const key_len=0, name_copy[]="", const name_len=0) |
| 1653 | { |
| 1654 | //Haven't found anything yet |
| 1655 | key_tell_find = name_tell_find = -1; |
| 1656 | |
| 1657 | if( key_tell_start <= -1 || name_tell_start <= -1 || !file_exists(filename) ) |
| 1658 | return 0; |
| 1659 | |
| 1660 | new f = fopen(filename, "rt"); |
| 1661 | |
| 1662 | new bool:found_key = false; |
| 1663 | new _data[512], _name[64], _other[2], _temp_tell; |
| 1664 | |
| 1665 | nfv_replace_quote( name ); |
| 1666 | |
| 1667 | //Seek to key_tell_start |
| 1668 | if( key_tell_start ) |
| 1669 | fseek(f, key_tell_start, SEEK_SET); |
| 1670 | |
| 1671 | //If using a blank key, detect it already |
| 1672 | if( !key[0] ) |
| 1673 | found_key = true; |
| 1674 | |
| 1675 | while( !feof(f) ) |
| 1676 | { |
| 1677 | _temp_tell = ftell(f); |
| 1678 | fgets(f, _data, charsmax(_data)); |
| 1679 | |
| 1680 | if( _data[0] == identifier ) |
| 1681 | { |
| 1682 | if( found_key ) |
| 1683 | { |
| 1684 | break; |
| 1685 | } |
| 1686 | else |
| 1687 | { |
| 1688 | //Make sure we get something parsed |
| 1689 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1690 | { |
| 1691 | if( equal( key, _name ) || equal( key, NFV_WILDCARD ) ) |
| 1692 | { |
| 1693 | if( key_len > 0 ) |
| 1694 | copy( key_copy, key_len, _name ); |
| 1695 | |
| 1696 | found_key = true; |
| 1697 | |
| 1698 | //Seek to name_tell_start |
| 1699 | if( name_tell_start ) |
| 1700 | fseek(f, name_tell_start, SEEK_SET); |
| 1701 | |
| 1702 | key_tell_find = _temp_tell; |
| 1703 | } |
| 1704 | } |
| 1705 | } |
| 1706 | } |
| 1707 | else |
| 1708 | { |
| 1709 | if( found_key && parse(_data, _name, charsmax(_name), data, len) ) |
| 1710 | { |
| 1711 | if( equal(_name, name) || equal( name, NFV_WILDCARD ) ) |
| 1712 | { |
| 1713 | if( name_len > 0 ) |
| 1714 | { |
| 1715 | copy( name_copy, name_len, _name ); |
| 1716 | nfv_return_quote( name_copy ); |
| 1717 | } |
| 1718 | |
| 1719 | name_tell_find = _temp_tell; |
| 1720 | nfv_return_quote( data ); |
| 1721 | |
| 1722 | fclose(f); |
| 1723 | return 1; |
| 1724 | } |
| 1725 | } |
| 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | fclose(f); |
| 1730 | data[0] = '^0'; |
| 1731 | return 0; |
| 1732 | } |
| 1733 | |
| 1734 | /** |
| 1735 | * Retrieves data as number form in file under key and name. |
| 1736 | * |
| 1737 | * @param filename The file location to search for the data. |
| 1738 | * @param key The key within the file to look for. Set to "" for no key. Set to NFV_WILDCARD for any key. |
| 1739 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 1740 | * @param identifier The character used to identify different keys. |
| 1741 | * @param key_tell_start Where to start searching for key. |
| 1742 | * @param key_tell_find Where the key was found. |
| 1743 | * @param name_tell_start Where to start searching for name. |
| 1744 | * @param name_tell_find Where the name was found. |
| 1745 | * @param key_copy The string that will contain the key of the data retrieved. Only used with NFV_WILDCARD for key. |
| 1746 | * @param key_len If specified, the size of the key found to copy. Only used with NFV_WILDCARD for key. |
| 1747 | * @param name_copy The string that will contain the name of the data retrieved. Only used with NFV_WILDCARD for name. |
| 1748 | * @param name_len If specified, the size of the name found to copy. Only used with NFV_WILDCARD for name. |
| 1749 | * @return Numeric value of data found. 0 if no data. |
| 1750 | */ |
| 1751 | stock nfv_get_num(const filename[], const key[], name[], const identifier=';', const key_tell_start=0, &key_tell_find=0, const name_tell_start=0, &name_tell_find=0, key_copy[]="", const key_len=0, name_copy[]="", const name_len=0) |
| 1752 | { |
| 1753 | //Haven't found anything yet |
| 1754 | key_tell_find = name_tell_find = -1; |
| 1755 | |
| 1756 | if( key_tell_start <= -1 || name_tell_start <= -1 || !file_exists(filename) ) |
| 1757 | return 0; |
| 1758 | |
| 1759 | new f = fopen(filename, "rt"); |
| 1760 | |
| 1761 | new bool:found_key = false; |
| 1762 | new _data[512], _name[64], _other[64], _temp_tell; |
| 1763 | |
| 1764 | nfv_replace_quote( name ); |
| 1765 | |
| 1766 | //Seek to key_tell_start |
| 1767 | if( key_tell_start ) |
| 1768 | fseek(f, key_tell_start, SEEK_SET); |
| 1769 | |
| 1770 | //If using a blank key, detect it already |
| 1771 | if( !key[0] ) |
| 1772 | found_key = true; |
| 1773 | |
| 1774 | while( !feof(f) ) |
| 1775 | { |
| 1776 | _temp_tell = ftell(f); |
| 1777 | fgets(f, _data, charsmax(_data)); |
| 1778 | |
| 1779 | if( _data[0] == identifier ) |
| 1780 | { |
| 1781 | if( found_key ) |
| 1782 | { |
| 1783 | break; |
| 1784 | } |
| 1785 | else |
| 1786 | { |
| 1787 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1788 | { |
| 1789 | if( equal( key, _name ) || equal( key, NFV_WILDCARD ) ) |
| 1790 | { |
| 1791 | if( key_len > 0 ) |
| 1792 | copy( key_copy, key_len, _name ); |
| 1793 | |
| 1794 | found_key = true; |
| 1795 | |
| 1796 | //Seek to name_tell_start |
| 1797 | if( name_tell_start ) |
| 1798 | fseek(f, name_tell_start, SEEK_SET); |
| 1799 | |
| 1800 | key_tell_find = _temp_tell; |
| 1801 | } |
| 1802 | } |
| 1803 | } |
| 1804 | } |
| 1805 | else |
| 1806 | { |
| 1807 | if( found_key && parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1808 | { |
| 1809 | if( equal(_name, name) || equal( name, NFV_WILDCARD ) ) |
| 1810 | { |
| 1811 | if( name_len > 0 ) |
| 1812 | { |
| 1813 | copy( name_copy, name_len, _name ); |
| 1814 | nfv_return_quote( name_copy ); |
| 1815 | } |
| 1816 | |
| 1817 | name_tell_find = _temp_tell; |
| 1818 | |
| 1819 | fclose(f); |
| 1820 | return str_to_num(_other); |
| 1821 | } |
| 1822 | } |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | fclose(f); |
| 1827 | return 0; |
| 1828 | } |
| 1829 | |
| 1830 | /** |
| 1831 | * Retrieves data as floating point form in file under key and name. |
| 1832 | * |
| 1833 | * @param filename The file location to search for the data. |
| 1834 | * @param key The key within the file to look for. Set to "" for no key. Set to NFV_WILDCARD for any key. |
| 1835 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 1836 | * @param identifier The character used to identify different keys. |
| 1837 | * @param key_tell_start Where to start searching for key. |
| 1838 | * @param key_tell_find Where the key was found. |
| 1839 | * @param name_tell_start Where to start searching for name. |
| 1840 | * @param name_tell_find Where the name was found. |
| 1841 | * @param key_copy The string that will contain the key of the data retrieved. Only used with NFV_WILDCARD for key. |
| 1842 | * @param key_len If specified, the size of the key found to copy. Only used with NFV_WILDCARD for key. |
| 1843 | * @param name_copy The string that will contain the name of the data retrieved. Only used with NFV_WILDCARD for name. |
| 1844 | * @param name_len If specified, the size of the name found to copy. Only used with NFV_WILDCARD for name. |
| 1845 | * @return Floating point value of data found. 0.0 if no data. |
| 1846 | */ |
| 1847 | stock Float:nfv_get_float(const filename[], const key[], name[], const identifier=';', const key_tell_start=0, &key_tell_find=0, const name_tell_start=0, &name_tell_find=0, key_copy[]="", const key_len=0, name_copy[]="", const name_len=0) |
| 1848 | { |
| 1849 | //Haven't found anything yet |
| 1850 | key_tell_find = name_tell_find = -1; |
| 1851 | |
| 1852 | if( key_tell_start <= -1 || name_tell_start <= -1 || !file_exists(filename) ) |
| 1853 | return 0.0; |
| 1854 | |
| 1855 | new f = fopen(filename, "rt"); |
| 1856 | |
| 1857 | new bool:found_key = false; |
| 1858 | new _data[512], _name[64], _other[64], _temp_tell; |
| 1859 | |
| 1860 | nfv_replace_quote( name ); |
| 1861 | |
| 1862 | //Seek to key_tell_start |
| 1863 | if( key_tell_start ) |
| 1864 | fseek(f, key_tell_start, SEEK_SET); |
| 1865 | |
| 1866 | //If using a blank key, detect it already |
| 1867 | if( !key[0] ) |
| 1868 | found_key = true; |
| 1869 | |
| 1870 | while( !feof(f) ) |
| 1871 | { |
| 1872 | _temp_tell = ftell(f); |
| 1873 | fgets(f, _data, charsmax(_data)); |
| 1874 | |
| 1875 | if( _data[0] == identifier ) |
| 1876 | { |
| 1877 | if( found_key ) |
| 1878 | { |
| 1879 | break; |
| 1880 | } |
| 1881 | else |
| 1882 | { |
| 1883 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1884 | { |
| 1885 | if( equal( key, _name ) || equal( key, NFV_WILDCARD ) ) |
| 1886 | { |
| 1887 | if( key_len > 0 ) |
| 1888 | copy( key_copy, key_len, _name ); |
| 1889 | |
| 1890 | found_key = true; |
| 1891 | |
| 1892 | //Seek to name_tell_start |
| 1893 | if( name_tell_start ) |
| 1894 | fseek(f, name_tell_start, SEEK_SET); |
| 1895 | |
| 1896 | key_tell_find = _temp_tell; |
| 1897 | } |
| 1898 | } |
| 1899 | } |
| 1900 | } |
| 1901 | else |
| 1902 | { |
| 1903 | if( found_key && parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1904 | { |
| 1905 | if( equal(_name, name) || equal( name, NFV_WILDCARD ) ) |
| 1906 | { |
| 1907 | if( name_len > 0 ) |
| 1908 | { |
| 1909 | copy( name_copy, name_len, _name ); |
| 1910 | nfv_return_quote( name_copy ); |
| 1911 | } |
| 1912 | |
| 1913 | name_tell_find = _temp_tell; |
| 1914 | |
| 1915 | fclose(f); |
| 1916 | return str_to_float(_other); |
| 1917 | } |
| 1918 | } |
| 1919 | } |
| 1920 | } |
| 1921 | |
| 1922 | fclose(f); |
| 1923 | return 0.0; |
| 1924 | } |
| 1925 | |
| 1926 | /** |
| 1927 | * Retrieves data as boolean form in file under key and name. |
| 1928 | * |
| 1929 | * @param filename The file location to search for the data. |
| 1930 | * @param key The key within the file to look for. Set to "" for no key. Set to NFV_WILDCARD for any key. |
| 1931 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 1932 | * @param identifier The character used to identify different keys. |
| 1933 | * @param key_tell_start Where to start searching for key. |
| 1934 | * @param key_tell_find Where the key was found. |
| 1935 | * @param name_tell_start Where to start searching for name. |
| 1936 | * @param name_tell_find Where the name was found. |
| 1937 | * @param key_copy The string that will contain the key of the data retrieved. Only used with NFV_WILDCARD for key. |
| 1938 | * @param key_len If specified, the size of the key found to copy. Only used with NFV_WILDCARD for key. |
| 1939 | * @param name_copy The string that will contain the name of the data retrieved. Only used with NFV_WILDCARD for name. |
| 1940 | * @param name_len If specified, the size of the name found to copy. Only used with NFV_WILDCARD for name. |
| 1941 | * @return Boolean value of data found. False if no data. |
| 1942 | */ |
| 1943 | stock bool:nfv_get_bool(const filename[], const key[], name[], const identifier=';', const key_tell_start=0, &key_tell_find=0, const name_tell_start=0, &name_tell_find=0, key_copy[]="", const key_len=0, name_copy[]="", const name_len=0) |
| 1944 | { |
| 1945 | //Haven't found anything yet |
| 1946 | key_tell_find = name_tell_find = -1; |
| 1947 | |
| 1948 | if( key_tell_start <= -1 || name_tell_start <= -1 || !file_exists(filename) ) |
| 1949 | return false; |
| 1950 | |
| 1951 | new f = fopen(filename, "rt"); |
| 1952 | |
| 1953 | new bool:found_key = false; |
| 1954 | new _data[512], _name[64], _other[64], _temp_tell; |
| 1955 | |
| 1956 | nfv_replace_quote( name ); |
| 1957 | |
| 1958 | //Seek to key_tell_start |
| 1959 | if( key_tell_start ) |
| 1960 | fseek(f, key_tell_start, SEEK_SET); |
| 1961 | |
| 1962 | //If using a blank key, detect it already |
| 1963 | if( !key[0] ) |
| 1964 | found_key = true; |
| 1965 | |
| 1966 | while( !feof(f) ) |
| 1967 | { |
| 1968 | _temp_tell = ftell(f); |
| 1969 | fgets(f, _data, charsmax(_data)); |
| 1970 | |
| 1971 | if( _data[0] == identifier ) |
| 1972 | { |
| 1973 | if( found_key ) |
| 1974 | { |
| 1975 | break; |
| 1976 | } |
| 1977 | else |
| 1978 | { |
| 1979 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 1980 | { |
| 1981 | if( equal( key, _name ) || equal( key, NFV_WILDCARD ) ) |
| 1982 | { |
| 1983 | if( key_len > 0 ) |
| 1984 | copy( key_copy, key_len, _name ); |
| 1985 | |
| 1986 | found_key = true; |
| 1987 | |
| 1988 | //Seek to name_tell_start |
| 1989 | if( name_tell_start ) |
| 1990 | fseek(f, name_tell_start, SEEK_SET); |
| 1991 | |
| 1992 | key_tell_find = _temp_tell; |
| 1993 | } |
| 1994 | } |
| 1995 | } |
| 1996 | } |
| 1997 | else |
| 1998 | { |
| 1999 | if( found_key && parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 2000 | { |
| 2001 | if( equal(_name, name) || equal( name, NFV_WILDCARD ) ) |
| 2002 | { |
| 2003 | if( name_len > 0 ) |
| 2004 | { |
| 2005 | copy( name_copy, name_len, _name ); |
| 2006 | nfv_return_quote( name_copy ); |
| 2007 | } |
| 2008 | |
| 2009 | if( len ) |
| 2010 | { |
| 2011 | copy( data, len, _other ); |
| 2012 | nfv_return_quote( data ); |
| 2013 | } |
| 2014 | |
| 2015 | name_tell_find = _temp_tell; |
| 2016 | |
| 2017 | fclose(f); |
| 2018 | |
| 2019 | return str_to_bool(_other); |
| 2020 | } |
| 2021 | } |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | fclose(f); |
| 2026 | data[0] = '^0'; |
| 2027 | return false; |
| 2028 | } |
| 2029 | |
| 2030 | /** |
| 2031 | * Retrieves data as array form in file under key and name. |
| 2032 | * |
| 2033 | * @param filename The file location to search for the data. |
| 2034 | * @param key The key within the file to look for. Set to "" for no key. Set to NFV_WILDCARD for any key. |
| 2035 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 2036 | * @param data The array that will contain the data retrieved. |
| 2037 | * @param size The size of the data array. |
| 2038 | * @param identifier The character used to identify different keys. |
| 2039 | * @param key_tell_start Where to start searching for key. |
| 2040 | * @param key_tell_find Where the key was found. |
| 2041 | * @param name_tell_start Where to start searching for name. |
| 2042 | * @param name_tell_find Where the name was found. |
| 2043 | * @param key_copy The string that will contain the key of the data retrieved. Only used with NFV_WILDCARD for key. |
| 2044 | * @param key_len If specified, the size of the key found to copy. Only used with NFV_WILDCARD for key. |
| 2045 | * @param name_copy The string that will contain the name of the data retrieved. Only used with NFV_WILDCARD for name. |
| 2046 | * @param name_len If specified, the size of the name found to copy. Only used with NFV_WILDCARD for name. |
| 2047 | * @return Whether or not data was found. |
| 2048 | */ |
| 2049 | stock nfv_get_array(const filename[], const key[], name[], any:data[], const size, const identifier=';', const key_tell_start=0, &key_tell_find=0, const name_tell_start=0, &name_tell_find=0, key_copy[]="", const key_len=0, name_copy[]="", const name_len=0) |
| 2050 | { |
| 2051 | //Haven't found anything yet |
| 2052 | key_tell_find = name_tell_find = -1; |
| 2053 | |
| 2054 | if( key_tell_start <= -1 || name_tell_start <= -1 || !file_exists(filename) ) |
| 2055 | return false; |
| 2056 | |
| 2057 | new f = fopen(filename, "rt"); |
| 2058 | |
| 2059 | new bool:found_key = false; |
| 2060 | new _data[512], _name[64], _other[64], _temp_tell; |
| 2061 | |
| 2062 | nfv_replace_quote( name ); |
| 2063 | |
| 2064 | //Seek to key_tell_start |
| 2065 | if( key_tell_start ) |
| 2066 | fseek(f, key_tell_start, SEEK_SET); |
| 2067 | |
| 2068 | //If using a blank key, detect it already |
| 2069 | if( !key[0] ) |
| 2070 | found_key = true; |
| 2071 | |
| 2072 | while( !feof(f) ) |
| 2073 | { |
| 2074 | _temp_tell = ftell(f); |
| 2075 | fgets(f, _data, charsmax(_data)); |
| 2076 | |
| 2077 | if( _data[0] == identifier ) |
| 2078 | { |
| 2079 | if( found_key ) |
| 2080 | { |
| 2081 | break; |
| 2082 | } |
| 2083 | else |
| 2084 | { |
| 2085 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 2086 | { |
| 2087 | if( equal( key, _name ) || equal( key, NFV_WILDCARD ) ) |
| 2088 | { |
| 2089 | if( key_len > 0 ) |
| 2090 | copy( key_copy, key_len, _name ); |
| 2091 | |
| 2092 | found_key = true; |
| 2093 | |
| 2094 | //Seek to name_tell_start |
| 2095 | if( name_tell_start ) |
| 2096 | fseek(f, name_tell_start, SEEK_SET); |
| 2097 | |
| 2098 | key_tell_find = _temp_tell; |
| 2099 | } |
| 2100 | } |
| 2101 | } |
| 2102 | } |
| 2103 | else |
| 2104 | { |
| 2105 | if( found_key && parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 2106 | { |
| 2107 | if( equal(_name, name) || equal( name, NFV_WILDCARD ) ) |
| 2108 | { |
| 2109 | if( name_len > 0 ) |
| 2110 | { |
| 2111 | copy( name_copy, name_len, _name ); |
| 2112 | nfv_return_quote( name_copy ); |
| 2113 | } |
| 2114 | |
| 2115 | name_tell_find = _temp_tell; |
| 2116 | |
| 2117 | fclose(f); |
| 2118 | |
| 2119 | str_to_array(_other, data, size); |
| 2120 | |
| 2121 | return true; |
| 2122 | } |
| 2123 | } |
| 2124 | } |
| 2125 | } |
| 2126 | |
| 2127 | fclose(f); |
| 2128 | for( new i; i<size; i++ ) |
| 2129 | data[i] = 0; |
| 2130 | return false; |
| 2131 | } |
| 2132 | |
| 2133 | /** |
| 2134 | * Retrieves data as vector form in file under key and name. |
| 2135 | * |
| 2136 | * @param filename The file location to search for the data. |
| 2137 | * @param key The key within the file to look for. Set to "" for no key. Set to NFV_WILDCARD for any key. |
| 2138 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 2139 | * @param data The vector that will contain the data retrieved. |
| 2140 | * @param identifier The character used to identify different keys. |
| 2141 | * @param key_tell_start Where to start searching for key. |
| 2142 | * @param key_tell_find Where the key was found. |
| 2143 | * @param name_tell_start Where to start searching for name. |
| 2144 | * @param name_tell_find Where the name was found. |
| 2145 | * @param key_copy The string that will contain the key of the data retrieved. Only used with NFV_WILDCARD for key. |
| 2146 | * @param key_len If specified, the size of the key found to copy. Only used with NFV_WILDCARD for key. |
| 2147 | * @param name_copy The string that will contain the name of the data retrieved. Only used with NFV_WILDCARD for name. |
| 2148 | * @param name_len If specified, the size of the name found to copy. Only used with NFV_WILDCARD for name. |
| 2149 | * @return Whether or not data was found. |
| 2150 | */ |
| 2151 | stock nfv_get_vec(const filename[], const key[], name[], Float:data[3], const identifier=';', const key_tell_start=0, &key_tell_find=0, const name_tell_start=0, &name_tell_find=0, key_copy[]="", const key_len=0, name_copy[]="", const name_len=0) |
| 2152 | { |
| 2153 | //Haven't found anything yet |
| 2154 | key_tell_find = name_tell_find = -1; |
| 2155 | |
| 2156 | if( key_tell_start <= -1 || name_tell_start <= -1 || !file_exists(filename) ) |
| 2157 | return false; |
| 2158 | |
| 2159 | new f = fopen(filename, "rt"); |
| 2160 | |
| 2161 | new bool:found_key = false; |
| 2162 | new _data[512], _name[64], _other[64], _temp_tell; |
| 2163 | |
| 2164 | nfv_replace_quote( name ); |
| 2165 | |
| 2166 | //Seek to key_tell_start |
| 2167 | if( key_tell_start ) |
| 2168 | fseek(f, key_tell_start, SEEK_SET); |
| 2169 | |
| 2170 | //If using a blank key, detect it already |
| 2171 | if( !key[0] ) |
| 2172 | found_key = true; |
| 2173 | |
| 2174 | while( !feof(f) ) |
| 2175 | { |
| 2176 | _temp_tell = ftell(f); |
| 2177 | fgets(f, _data, charsmax(_data)); |
| 2178 | |
| 2179 | if( _data[0] == identifier ) |
| 2180 | { |
| 2181 | if( found_key ) |
| 2182 | { |
| 2183 | break; |
| 2184 | } |
| 2185 | else |
| 2186 | { |
| 2187 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 2188 | { |
| 2189 | if( equal( key, _name ) || equal( key, NFV_WILDCARD ) ) |
| 2190 | { |
| 2191 | if( key_len > 0 ) |
| 2192 | copy( key_copy, key_len, _name ); |
| 2193 | |
| 2194 | found_key = true; |
| 2195 | |
| 2196 | //Seek to name_tell_start |
| 2197 | if( name_tell_start ) |
| 2198 | fseek(f, name_tell_start, SEEK_SET); |
| 2199 | |
| 2200 | key_tell_find = _temp_tell; |
| 2201 | } |
| 2202 | } |
| 2203 | } |
| 2204 | } |
| 2205 | else |
| 2206 | { |
| 2207 | if( found_key && parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 2208 | { |
| 2209 | if( equal(_name, name) || equal( name, NFV_WILDCARD ) ) |
| 2210 | { |
| 2211 | if( name_len > 0 ) |
| 2212 | { |
| 2213 | copy( name_copy, name_len, _name ); |
| 2214 | nfv_return_quote( name_copy ); |
| 2215 | } |
| 2216 | |
| 2217 | name_tell_find = _temp_tell; |
| 2218 | |
| 2219 | fclose(f); |
| 2220 | |
| 2221 | str_to_vector(_other, data); |
| 2222 | |
| 2223 | return true; |
| 2224 | } |
| 2225 | } |
| 2226 | } |
| 2227 | } |
| 2228 | |
| 2229 | fclose(f); |
| 2230 | for( new i; i<3; i++ ) |
| 2231 | data[i] = 0.0; |
| 2232 | return false; |
| 2233 | } |
| 2234 | |
| 2235 | /** |
| 2236 | * Retrieves data as cellarray form in file under key and name. |
| 2237 | * |
| 2238 | * @param filename The file location to search for the data. |
| 2239 | * @param key The key within the file to look for. Set to "" for no key. Set to NFV_WILDCARD for any key. |
| 2240 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 2241 | * @param data The cell array that will contain the data retrieved. |
| 2242 | * @param identifier The character used to identify different keys. |
| 2243 | * @param key_tell_start Where to start searching for key. |
| 2244 | * @param key_tell_find Where the key was found. |
| 2245 | * @param name_tell_start Where to start searching for name. |
| 2246 | * @param name_tell_find Where the name was found. |
| 2247 | * @param key_copy The string that will contain the key of the data retrieved. Only used with NFV_WILDCARD for key. |
| 2248 | * @param key_len If specified, the size of the key found to copy. Only used with NFV_WILDCARD for key. |
| 2249 | * @param name_copy The string that will contain the name of the data retrieved. Only used with NFV_WILDCARD for name. |
| 2250 | * @param name_len If specified, the size of the name found to copy. Only used with NFV_WILDCARD for name. |
| 2251 | * |
| 2252 | * @note If an existing cell array is provided, it will push new data to the end of it. |
| 2253 | * |
| 2254 | * @return The cell array handle. Invalid_Array if no data. |
| 2255 | */ |
| 2256 | stock Array:nfv_get_cellarray(const filename[], const key[], name[], &Array:data=Invalid_Array, const identifier=';', const key_tell_start=0, &key_tell_find=0, const name_tell_start=0, &name_tell_find=0, key_copy[]="", const key_len=0, name_copy[]="", const name_len=0) |
| 2257 | { |
| 2258 | //Haven't found anything yet |
| 2259 | key_tell_find = name_tell_find = -1; |
| 2260 | |
| 2261 | if( key_tell_start <= -1 || name_tell_start <= -1 || !file_exists(filename) ) |
| 2262 | return Invalid_Array; |
| 2263 | |
| 2264 | new f = fopen(filename, "rt"); |
| 2265 | |
| 2266 | new bool:found_key = false; |
| 2267 | new _data[512], _name[64], _other[64], _temp_tell; |
| 2268 | |
| 2269 | nfv_replace_quote( name ); |
| 2270 | |
| 2271 | //Seek to key_tell_start |
| 2272 | if( key_tell_start ) |
| 2273 | fseek(f, key_tell_start, SEEK_SET); |
| 2274 | |
| 2275 | //If using a blank key, detect it already |
| 2276 | if( !key[0] ) |
| 2277 | found_key = true; |
| 2278 | |
| 2279 | while( !feof(f) ) |
| 2280 | { |
| 2281 | _temp_tell = ftell(f); |
| 2282 | fgets(f, _data, charsmax(_data)); |
| 2283 | |
| 2284 | if( _data[0] == identifier ) |
| 2285 | { |
| 2286 | if( found_key ) |
| 2287 | { |
| 2288 | break; |
| 2289 | } |
| 2290 | else |
| 2291 | { |
| 2292 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 2293 | { |
| 2294 | if( equal( key, _name ) || equal( key, NFV_WILDCARD ) ) |
| 2295 | { |
| 2296 | if( key_len > 0 ) |
| 2297 | copy( key_copy, key_len, _name ); |
| 2298 | |
| 2299 | found_key = true; |
| 2300 | |
| 2301 | //Seek to name_tell_start |
| 2302 | if( name_tell_start ) |
| 2303 | fseek(f, name_tell_start, SEEK_SET); |
| 2304 | |
| 2305 | key_tell_find = _temp_tell; |
| 2306 | } |
| 2307 | } |
| 2308 | } |
| 2309 | } |
| 2310 | else |
| 2311 | { |
| 2312 | if( found_key && parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 2313 | { |
| 2314 | if( equal(_name, name) || equal( name, NFV_WILDCARD ) ) |
| 2315 | { |
| 2316 | if( name_len > 0 ) |
| 2317 | { |
| 2318 | copy( name_copy, name_len, _name ); |
| 2319 | nfv_return_quote( name_copy ); |
| 2320 | } |
| 2321 | |
| 2322 | name_tell_find = _temp_tell; |
| 2323 | |
| 2324 | fclose(f); |
| 2325 | |
| 2326 | return str_to_cellarray(_other, data); |
| 2327 | } |
| 2328 | } |
| 2329 | } |
| 2330 | } |
| 2331 | |
| 2332 | fclose(f); |
| 2333 | return Invalid_Array; |
| 2334 | } |
| 2335 | |
| 2336 | /** |
| 2337 | * Retrieves data as cellarray string form in file under key and name. |
| 2338 | * |
| 2339 | * @param filename The file location to search for the data. |
| 2340 | * @param key The key within the file to look for. Set to "" for no key. Set to NFV_WILDCARD for any key. |
| 2341 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 2342 | * @param data The cell array that will contain the data retrieved. |
| 2343 | * @param identifier The character used to identify different keys. |
| 2344 | * @param key_tell_start Where to start searching for key. |
| 2345 | * @param key_tell_find Where the key was found. |
| 2346 | * @param name_tell_start Where to start searching for name. |
| 2347 | * @param name_tell_find Where the name was found. |
| 2348 | * @param key_copy The string that will contain the key of the data retrieved. Only used with NFV_WILDCARD for key. |
| 2349 | * @param key_len If specified, the size of the key found to copy. Only used with NFV_WILDCARD for key. |
| 2350 | * @param name_copy The string that will contain the name of the data retrieved. Only used with NFV_WILDCARD for name. |
| 2351 | * @param name_len If specified, the size of the name found to copy. Only used with NFV_WILDCARD for name. |
| 2352 | * |
| 2353 | * @note If an existing cell array is provided, it will push new strings to the end of it. |
| 2354 | * |
| 2355 | * @return The cell array handle. Invalid_Array if no data. |
| 2356 | */ |
| 2357 | stock Array:nfv_get_cellarray2(const filename[], const key[], name[], &Array:data=Invalid_Array, const identifier=';', const key_tell_start=0, &key_tell_find=0, const name_tell_start=0, &name_tell_find=0, key_copy[]="", const key_len=0, name_copy[]="", const name_len=0) |
| 2358 | { |
| 2359 | //Haven't found anything yet |
| 2360 | key_tell_find = name_tell_find = -1; |
| 2361 | |
| 2362 | if( key_tell_start <= -1 || name_tell_start <= -1 || !file_exists(filename) ) |
| 2363 | return Invalid_Array; |
| 2364 | |
| 2365 | new f = fopen(filename, "rt"); |
| 2366 | |
| 2367 | new bool:found_key = false; |
| 2368 | new _data[512], _name[64], _other[64], _temp_tell; |
| 2369 | |
| 2370 | nfv_replace_quote( name ); |
| 2371 | |
| 2372 | //Seek to key_tell_start |
| 2373 | if( key_tell_start ) |
| 2374 | fseek(f, key_tell_start, SEEK_SET); |
| 2375 | |
| 2376 | //If using a blank key, detect it already |
| 2377 | if( !key[0] ) |
| 2378 | found_key = true; |
| 2379 | |
| 2380 | while( !feof(f) ) |
| 2381 | { |
| 2382 | _temp_tell = ftell(f); |
| 2383 | fgets(f, _data, charsmax(_data)); |
| 2384 | |
| 2385 | if( _data[0] == identifier ) |
| 2386 | { |
| 2387 | if( found_key ) |
| 2388 | { |
| 2389 | break; |
| 2390 | } |
| 2391 | else |
| 2392 | { |
| 2393 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 2394 | { |
| 2395 | if( equal( key, _name ) || equal( key, NFV_WILDCARD ) ) |
| 2396 | { |
| 2397 | if( key_len > 0 ) |
| 2398 | copy( key_copy, key_len, _name ); |
| 2399 | |
| 2400 | found_key = true; |
| 2401 | |
| 2402 | //Seek to name_tell_start |
| 2403 | if( name_tell_start ) |
| 2404 | fseek(f, name_tell_start, SEEK_SET); |
| 2405 | |
| 2406 | key_tell_find = _temp_tell; |
| 2407 | } |
| 2408 | } |
| 2409 | } |
| 2410 | } |
| 2411 | else |
| 2412 | { |
| 2413 | if( found_key && parse(_data, _name, charsmax(_name), _other, charsmax(_other)) ) |
| 2414 | { |
| 2415 | if( equal(_name, name) || equal( name, NFV_WILDCARD ) ) |
| 2416 | { |
| 2417 | if( name_len > 0 ) |
| 2418 | { |
| 2419 | copy( name_copy, name_len, _name ); |
| 2420 | nfv_return_quote( name_copy ); |
| 2421 | } |
| 2422 | |
| 2423 | name_tell_find = _temp_tell; |
| 2424 | |
| 2425 | fclose(f); |
| 2426 | |
| 2427 | return str_to_cellarray2(_other, data); |
| 2428 | } |
| 2429 | } |
| 2430 | } |
| 2431 | } |
| 2432 | |
| 2433 | fclose(f); |
| 2434 | return Invalid_Array; |
| 2435 | } |
| 2436 | |
| 2437 | /** |
| 2438 | * Removes data in file under key and name. |
| 2439 | * |
| 2440 | * @param filename The file location to search for the data. |
| 2441 | * @param key The key within the file to look for. Set to "" for no key. |
| 2442 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 2443 | * @param data The string that will contain the data removed. |
| 2444 | * @param len The size of the data string. |
| 2445 | * @param take_times How many times to take out of the file. Set to NFV_TAKEALL for all. |
| 2446 | * @param identifier The character used to identify different keys. |
| 2447 | * @param name_copy The string that will contain the name of the data retrieved. Only used with NFV_WILDCARD for name. |
| 2448 | * @param name_len If specified, the size of the name found to copy. Only used with NFV_WILDCARD for name. |
| 2449 | * @return How many datas were removed. |
| 2450 | */ |
| 2451 | stock nfv_take_data(const filename[], const key[], name[], data[]="", len=0, const take_times=1, const identifier=';', name_copy[]="", const name_len=0) |
| 2452 | { |
| 2453 | if( !file_exists(filename) ) |
| 2454 | return 0; |
| 2455 | |
| 2456 | new g = fopen(temp_file_name, "wt"); |
| 2457 | new f = fopen(filename, "rt"); |
| 2458 | |
| 2459 | // lets put a time stamp |
| 2460 | fprintf(g, "^"TimeStamp^" ^"%d^"^n", get_systime() ); |
| 2461 | |
| 2462 | new _data[512], _name[64], _other[3]; |
| 2463 | |
| 2464 | nfv_replace_quote( name ); |
| 2465 | |
| 2466 | new took_data, bool:found_key = false, bool:found_time = false; |
| 2467 | |
| 2468 | //If using a blank key, detect it already |
| 2469 | if( !key[0] ) |
| 2470 | found_key = true; |
| 2471 | |
| 2472 | while( !feof(f) ) |
| 2473 | { |
| 2474 | fgets(f, _data, charsmax(_data)); |
| 2475 | |
| 2476 | if( !found_time ) |
| 2477 | { |
| 2478 | if( equal(_data, "^"TimeStamp^"", 11) ) |
| 2479 | { |
| 2480 | found_time = true; |
| 2481 | continue; |
| 2482 | } |
| 2483 | } |
| 2484 | if( _data[0] == identifier ) |
| 2485 | { |
| 2486 | //if found key and wrote data continue and copy to g |
| 2487 | // if didnt copy data, write data before copying to g |
| 2488 | if( found_key ) |
| 2489 | { |
| 2490 | //if we took the data, we still need to copy all the data |
| 2491 | if( !took_data ) |
| 2492 | { |
| 2493 | break; |
| 2494 | } |
| 2495 | } |
| 2496 | //else check if correct key, copy to g |
| 2497 | else |
| 2498 | { |
| 2499 | //Make sure we get something parsed |
| 2500 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 2501 | { |
| 2502 | if( equal( _name, key ) ) |
| 2503 | { |
| 2504 | found_key = true; |
| 2505 | //If taking all, don't put the key in |
| 2506 | if( take_times == NFV_TAKEALL ) |
| 2507 | continue; |
| 2508 | } |
| 2509 | } |
| 2510 | } |
| 2511 | |
| 2512 | fputs(g, _data); |
| 2513 | } |
| 2514 | else |
| 2515 | { |
| 2516 | //only need to check the data to take if we havent taken anything |
| 2517 | if( found_key && ( took_data < take_times || take_times == NFV_TAKEALL ) ) |
| 2518 | { |
| 2519 | |
| 2520 | //Make sure we get something parsed |
| 2521 | if( parse(_data, _name, charsmax(_name), data, len) ) |
| 2522 | { |
| 2523 | //if name matches, save data, skip copy |
| 2524 | if( equal( _name, name ) || equal( name, NFV_WILDCARD ) ) |
| 2525 | { |
| 2526 | if( name_len > 0 ) |
| 2527 | { |
| 2528 | copy( name_copy, name_len, _name ); |
| 2529 | nfv_return_quote( name_copy ); |
| 2530 | } |
| 2531 | |
| 2532 | nfv_return_quote( data ); |
| 2533 | took_data++; |
| 2534 | |
| 2535 | continue; |
| 2536 | } |
| 2537 | } |
| 2538 | } |
| 2539 | |
| 2540 | //else copy to g |
| 2541 | fputs(g, _data); |
| 2542 | } |
| 2543 | } |
| 2544 | |
| 2545 | fclose(g); |
| 2546 | fclose(f); |
| 2547 | |
| 2548 | if( took_data ) |
| 2549 | { |
| 2550 | delete_file(filename); |
| 2551 | while( !rename_file(temp_file_name, filename, 1) ) { } |
| 2552 | } |
| 2553 | else |
| 2554 | { |
| 2555 | delete_file(temp_file_name); |
| 2556 | data[0] = '^0'; |
| 2557 | } |
| 2558 | |
| 2559 | return took_data; |
| 2560 | } |
| 2561 | |
| 2562 | /** |
| 2563 | * Retrieves data in file under key and name with reference to line numbers. |
| 2564 | * |
| 2565 | * @param filename The file location to search for the data. |
| 2566 | * @param key The key within the file to look for. Set to "" for no key. |
| 2567 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 2568 | * @param data The string that will contain the data retrieved. |
| 2569 | * @param len The size of the data string. |
| 2570 | * @param identifier The character used to identify different keys. |
| 2571 | * @param key_line_start Where to start searching for key. |
| 2572 | * @param key_line_find Where the key was found. |
| 2573 | * @param name_line_start Where to start searching for name. |
| 2574 | * @param name_line_find Where the name was found. |
| 2575 | * @param name_copy The string that will contain the name of the data retrieved. Only used with NFV_WILDCARD for name. |
| 2576 | * @param name_len If specified, the size of the name found to copy. Only used with NFV_WILDCARD for name. |
| 2577 | * @return 1 if successful, 0 on failure. |
| 2578 | */ |
| 2579 | stock nfv_get_linedata(const filename[], const key[], name[], data[]="", len=0, const identifier=';', const key_line_start=0, &key_line_find=0, const name_line_start=0, &name_line_find=0, name_copy[], const name_len=0) |
| 2580 | { |
| 2581 | if( key_line_start <= -1 || name_line_start <= -1 || !file_exists(filename) ) |
| 2582 | return 0; |
| 2583 | |
| 2584 | new f = fopen(filename, "rt"); |
| 2585 | |
| 2586 | new bool:found_key = false; |
| 2587 | new _data[512], _name[64], _other[2], _line = -1; |
| 2588 | |
| 2589 | //If using a blank key, detect it already |
| 2590 | if( !key[0] ) |
| 2591 | found_key = true; |
| 2592 | |
| 2593 | //Haven't found anything yet |
| 2594 | key_line_find = name_line_find = -1; |
| 2595 | |
| 2596 | while( !feof(f) ) |
| 2597 | { |
| 2598 | fgets(f, _data, charsmax(_data)); |
| 2599 | _line++; |
| 2600 | |
| 2601 | if( _data[0] == identifier && _line > key_line_start ) |
| 2602 | { |
| 2603 | if( found_key ) |
| 2604 | { |
| 2605 | break; |
| 2606 | } |
| 2607 | else |
| 2608 | { |
| 2609 | //Make sure we get something parsed |
| 2610 | if( parse(_data[1], _name, charsmax(_name), _other, charsmax(_other)) ) |
| 2611 | { |
| 2612 | if( equal( key, _name ) ) |
| 2613 | { |
| 2614 | found_key = true; |
| 2615 | |
| 2616 | key_line_find = _line; |
| 2617 | } |
| 2618 | } |
| 2619 | } |
| 2620 | } |
| 2621 | else if( _line > name_line_start ) |
| 2622 | { |
| 2623 | //Make sure we get something parsed |
| 2624 | if( parse(_data, _name, charsmax(_name), data, len) ) |
| 2625 | { |
| 2626 | if( found_key ) |
| 2627 | { |
| 2628 | if( equal(_name, name) || equal( name, NFV_WILDCARD ) ) |
| 2629 | { |
| 2630 | if( name_len > 0 ) |
| 2631 | { |
| 2632 | copy( name_copy, name_len, _name ); |
| 2633 | nfv_return_quote( name_copy ); |
| 2634 | } |
| 2635 | |
| 2636 | name_line_find = _line; |
| 2637 | nfv_return_quote( data ); |
| 2638 | |
| 2639 | fclose(f); |
| 2640 | return 1; |
| 2641 | } |
| 2642 | } |
| 2643 | } |
| 2644 | } |
| 2645 | } |
| 2646 | |
| 2647 | fclose(f); |
| 2648 | data[0] = '^0'; |
| 2649 | return 0; |
| 2650 | } |
| 2651 | |
| 2652 | /** |
| 2653 | * Removes data from all files in the NFVault folder. |
| 2654 | * |
| 2655 | * @param key The key within the file to remove data. |
| 2656 | * @param name The name within the key to remove data. Set to NFV_WILDCARD to prune all names. |
| 2657 | * @param start Checks if last TimeStamp is after this UnixTime. Set to -1 to prune all. |
| 2658 | * @param end Checks if last TimeStamp is before this UnixTime. Set to -1 to prune all. |
| 2659 | * @param identifier The character used to identify different keys. |
| 2660 | * @noreturn |
| 2661 | */ |
| 2662 | stock nfv_prune(const key[], name[], const start=-1, const end=-1, const identifier=';') |
| 2663 | { |
| 2664 | new directory[128], filename[128], fullfile[128]; |
| 2665 | |
| 2666 | get_localinfo( "amxx_datadir", directory, charsmax(directory) ); |
| 2667 | add( directory, charsmax(directory), "/nfvault/" ); |
| 2668 | |
| 2669 | new dir = open_dir( directory, filename, charsmax(filename) ); |
| 2670 | |
| 2671 | if( !dir ) |
| 2672 | return; |
| 2673 | |
| 2674 | new bool:prune_all; |
| 2675 | if( start == -1 && end == -1 ) |
| 2676 | prune_all = true; |
| 2677 | |
| 2678 | do |
| 2679 | { |
| 2680 | formatex( fullfile, charsmax(fullfile), "%s%s", directory, filename ); |
| 2681 | if( !file_exists(fullfile) ) |
| 2682 | break; |
| 2683 | |
| 2684 | if( prune_all ) |
| 2685 | { |
| 2686 | while( nfv_take_data( filename, key, name, _, _, NFV_TAKEALL, identifier ) ) {} |
| 2687 | } |
| 2688 | else if( start < nfv_get_num( filename, "", "TimeStamp" ) < end ) |
| 2689 | { |
| 2690 | while( nfv_take_data( filename, key, name, _, _, NFV_TAKEALL, identifier ) ) {} |
| 2691 | } |
| 2692 | } |
| 2693 | while( next_file(dir, filename, charsmax(filename)) ) |
| 2694 | |
| 2695 | close_dir(dir); |
| 2696 | } |
| 2697 | |
| 2698 | /** |
| 2699 | * Updates a file's TimeStamp |
| 2700 | * |
| 2701 | * @param filename The file to be touched. |
| 2702 | * @param new_time Timestamp to set for the file in Unix time. Set to -1 for current time. |
| 2703 | * @return 1 if successful. |
| 2704 | */ |
| 2705 | stock nfv_touch(const filename[], const new_time=-1) |
| 2706 | { |
| 2707 | new g = fopen(temp_file_name, "wt"); |
| 2708 | new f = fopen(filename, "rt"); |
| 2709 | |
| 2710 | // lets update the time stamp |
| 2711 | if( new_time == -1 ) |
| 2712 | fprintf(g, "^"TimeStamp^" ^"%d^"^n", get_systime() ); |
| 2713 | else |
| 2714 | fprintf(g, "^"TimeStamp^" ^"%d^"^n", new_time ); |
| 2715 | |
| 2716 | new _data[512]; |
| 2717 | |
| 2718 | new bool:found_time = false; |
| 2719 | |
| 2720 | while( !feof(f) ) |
| 2721 | { |
| 2722 | fgets(f, _data, charsmax(_data)); |
| 2723 | |
| 2724 | if( !found_time ) |
| 2725 | { |
| 2726 | if( equal(_data, "^"TimeStamp^"", 11) ) |
| 2727 | { |
| 2728 | found_time = true; |
| 2729 | continue; |
| 2730 | } |
| 2731 | } |
| 2732 | fputs(g, _data); |
| 2733 | } |
| 2734 | |
| 2735 | fclose(g); |
| 2736 | fclose(f); |
| 2737 | |
| 2738 | delete_file(filename); |
| 2739 | |
| 2740 | while( !rename_file(temp_file_name, filename, 1) ) { } |
| 2741 | |
| 2742 | return 1; |
| 2743 | } |
| 2744 | |
| 2745 | /** |
| 2746 | * Finds a player's unique save key |
| 2747 | * |
| 2748 | * @param id The index of the client. |
| 2749 | * @return Returns string containing the key. |
| 2750 | */ |
| 2751 | stock PlayerSaveKey(id) |
| 2752 | { |
| 2753 | new player_key[35]; |
| 2754 | |
| 2755 | #if NFV_SAVETYPE == 0 |
| 2756 | if( is_user_bot(id) ) |
| 2757 | { |
| 2758 | new botname[32]; |
| 2759 | get_user_name(id,botname,31); |
| 2760 | |
| 2761 | //Get Rid of BOT Tag |
| 2762 | |
| 2763 | //PODBot |
| 2764 | replace(botname,31,"[POD]",""); |
| 2765 | replace(botname,31,"[P*D]",""); |
| 2766 | replace(botname,31,"[P0D]",""); |
| 2767 | |
| 2768 | //CZ Bots |
| 2769 | replace(botname,31,"[BOT] ",""); |
| 2770 | |
| 2771 | //Attempt to get rid of the skill tag so we save with bots true name |
| 2772 | new lastchar = strlen(botname) - 1; |
| 2773 | if( equal(botname[lastchar],")",1) ) |
| 2774 | { |
| 2775 | for( new x = lastchar - 1; x > 0; x-- ) |
| 2776 | { |
| 2777 | if ( equal(botname[x],"(",1) ) |
| 2778 | { |
| 2779 | botname[x - 1] = 0; |
| 2780 | break; |
| 2781 | } |
| 2782 | if ( !isdigit(botname[x]) ) |
| 2783 | break; |
| 2784 | } |
| 2785 | } |
| 2786 | if( strlen(botname) > 0 ) |
| 2787 | formatex( player_key, 34, "[BOT]%s", botname ); |
| 2788 | } |
| 2789 | //Hack for STEAM's retardedness with listen servers |
| 2790 | else if( !is_dedicated_server() && id == 1 ) |
| 2791 | copy( player_key, 34, "loopback" ); |
| 2792 | else |
| 2793 | { |
| 2794 | static sv_lan; |
| 2795 | if( !sv_lan ) |
| 2796 | sv_lan = get_cvar_pointer("sv_lan"); |
| 2797 | if( get_pcvar_num(sv_lan) == 1 ) |
| 2798 | { |
| 2799 | get_user_ip( id, player_key, 34, 1 ); // by ip without port |
| 2800 | } |
| 2801 | else |
| 2802 | { |
| 2803 | get_user_authid( id, player_key, 34 ); // by steamid |
| 2804 | if( equali(player_key,"STEAM_ID_LAN") || equali(player_key,"4294967295") ) |
| 2805 | get_user_ip( id, player_key, 34, 1 ); // by ip without port |
| 2806 | } |
| 2807 | } |
| 2808 | #else |
| 2809 | get_user_name( id, player_key, 34 ); |
| 2810 | #endif |
| 2811 | |
| 2812 | if( strlen(player_key) > 0 ) |
| 2813 | { |
| 2814 | nfv_string_clean(player_key, player_key, 34); |
| 2815 | } |
| 2816 | |
| 2817 | //Check to make sure we got something useable |
| 2818 | if( equali(player_key, "STEAM_ID_PENDING") ) |
| 2819 | { |
| 2820 | player_key[0] = '^0'; |
| 2821 | } |
| 2822 | |
| 2823 | return player_key; |
| 2824 | } |
| 2825 | |
| 2826 | /** |
| 2827 | * Cleans a filename of invalid characters |
| 2828 | * |
| 2829 | * @param string The string to clean. |
| 2830 | * @param output The cleaned string. |
| 2831 | * @param len The length of output to fill. |
| 2832 | * @return Returns 1 on success. |
| 2833 | */ |
| 2834 | stock nfv_string_clean( const string[], output[], len ) |
| 2835 | { |
| 2836 | new i, j; |
| 2837 | while( i<len && j<len ) |
| 2838 | { |
| 2839 | switch( string[i] ) |
| 2840 | { |
| 2841 | case '/', '\', '*', ':', '?', '"', '<', '>', '|': |
| 2842 | { |
| 2843 | i++; |
| 2844 | } |
| 2845 | case ' ': |
| 2846 | { |
| 2847 | output[j++] = '_'; |
| 2848 | i++; |
| 2849 | } |
| 2850 | case '^0': |
| 2851 | { |
| 2852 | output[j++] = string[i++]; |
| 2853 | break; |
| 2854 | } |
| 2855 | default: |
| 2856 | { |
| 2857 | output[j++] = string[i++]; |
| 2858 | } |
| 2859 | } |
| 2860 | } |
| 2861 | return 1; |
| 2862 | } |
| 2863 | |
| 2864 | /** |
| 2865 | * Replaces quotation marks with the replacement character. |
| 2866 | * |
| 2867 | * @param string The string to clean. |
| 2868 | * @noreturn |
| 2869 | */ |
| 2870 | stock nfv_replace_quote( string[] ) |
| 2871 | { |
| 2872 | for( new i; string[i] != '^0'; i++ ) |
| 2873 | { |
| 2874 | if( string[i] == quote_char ) |
| 2875 | string[i] = quote_replace_char; |
| 2876 | } |
| 2877 | } |
| 2878 | |
| 2879 | /** |
| 2880 | * Replaces replacement characters with a quotation mark. |
| 2881 | * |
| 2882 | * @param string The string to clean. |
| 2883 | * @noreturn |
| 2884 | */ |
| 2885 | stock nfv_return_quote( string[] ) |
| 2886 | { |
| 2887 | for( new i; string[i] != '^0'; i++ ) |
| 2888 | { |
| 2889 | if( string[i] == quote_replace_char ) |
| 2890 | string[i] = quote_char; |
| 2891 | } |
| 2892 | } |
| 2893 | |
| 2894 | /** |
| 2895 | * Returns a file in the nfvault folder |
| 2896 | * |
| 2897 | * @param filename The filename and extension of the file. |
| 2898 | * @param filepath The entire path, filename and extension of the file is returned if size is > 0. |
| 2899 | * @param size The size to fill filepath with. Set to 0 to ignore. |
| 2900 | * @return Returns string containing the nfvault file. |
| 2901 | */ |
| 2902 | stock nfv_file(const filename[], filepath[]="", const size=0) |
| 2903 | { |
| 2904 | new file[128], _filename[128]; |
| 2905 | if( !DataDir[0] ) |
| 2906 | { |
| 2907 | get_localinfo( "amxx_datadir", DataDir, charsmax(DataDir) ); |
| 2908 | add( DataDir, charsmax(DataDir), "/nfvault" ); |
| 2909 | if( !dir_exists(DataDir) ) |
| 2910 | mkdir(DataDir); |
| 2911 | } |
| 2912 | nfv_string_clean( filename, _filename, 127 ); |
| 2913 | formatex( file, charsmax(file), "%s/%s", DataDir, _filename ); |
| 2914 | |
| 2915 | if( size ) |
| 2916 | copy( filepath, size, file ); |
| 2917 | |
| 2918 | return file; |
| 2919 | } |
| 2920 | /** |
| 2921 | * Creates a file with a TimeStamp. |
| 2922 | * |
| 2923 | * @param filename The file location for the data. |
| 2924 | * @param new_time Timestamp to set for the file in Unix time. Set to -1 for current time. |
| 2925 | * @return 1 if created, -1 on existing, 0 on failure. |
| 2926 | */ |
| 2927 | stock nfv_create_file(const filename[], const new_time=-1) |
| 2928 | { |
| 2929 | if( file_exists( filename ) ) |
| 2930 | return -1; |
| 2931 | |
| 2932 | new f = fopen(filename, "wt"); |
| 2933 | |
| 2934 | // lets put a time stamp |
| 2935 | if( new_time == -1 ) |
| 2936 | fprintf(f, "^"TimeStamp^" ^"%d^"^n", get_systime() ); |
| 2937 | else |
| 2938 | fprintf(f, "^"TimeStamp^" ^"%d^"^n", new_time ); |
| 2939 | |
| 2940 | fclose(f); |
| 2941 | |
| 2942 | return 1; |
| 2943 | } |
| 2944 | |
| 2945 | /** |
| 2946 | * Finds a player's unique save file |
| 2947 | * |
| 2948 | * @param id The index of the client. |
| 2949 | * @return Returns string containing the file. |
| 2950 | */ |
| 2951 | stock nfv_player_file(const id) |
| 2952 | { |
| 2953 | new file[128]; |
| 2954 | if( !DataDir[0] ) |
| 2955 | { |
| 2956 | get_localinfo( "amxx_datadir", DataDir, charsmax(DataDir) ); |
| 2957 | add( DataDir, charsmax(DataDir), "/nfvault" ); |
| 2958 | if( !dir_exists(DataDir) ) |
| 2959 | mkdir(DataDir); |
| 2960 | } |
| 2961 | formatex( file, charsmax(file), "%s/%s.txt", DataDir, PlayerSaveKey(id) ); |
| 2962 | return file; |
| 2963 | } |
| 2964 | |
| 2965 | /** |
| 2966 | * Sets data for a player under key and name. |
| 2967 | * |
| 2968 | * @param id The index of the client. |
| 2969 | * @param key The key within the file to look for. Set to "" for no key. |
| 2970 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 2971 | * @param data The data to write. |
| 2972 | * @param nfv_prop Properties to apply to the saved data in a bitsum. |
| 2973 | * @param identifier The character used to identify different keys. |
| 2974 | * @return 1 if successful. |
| 2975 | */ |
| 2976 | stock player_set_data(const id, const key[], name[], data[], const NFV_PROP:nfv_prop=NFV_OVERWRITE, const identifier=';') |
| 2977 | return nfv_set_data(nfv_player_file(id), key, name, data, nfv_prop, identifier); |
| 2978 | |
| 2979 | /** |
| 2980 | * Sets number for a player under key and name. |
| 2981 | * |
| 2982 | * @param id The index of the client. |
| 2983 | * @param key The key within the file to look for. Set to "" for no key. |
| 2984 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 2985 | * @param data The number to write. |
| 2986 | * @param nfv_prop Properties to apply to the saved data in a bitsum. |
| 2987 | * @param identifier The character used to identify different keys. |
| 2988 | * @return 1 if successful. |
| 2989 | */ |
| 2990 | stock player_set_num(const id, const key[], name[], const data, const NFV_PROP:nfv_prop=NFV_OVERWRITE, const identifier=';') |
| 2991 | return nfv_set_num(nfv_player_file(id), key, name, data, nfv_prop, identifier); |
| 2992 | |
| 2993 | /** |
| 2994 | * Adds number for a player under key and name. |
| 2995 | * |
| 2996 | * @param id The index of the client. |
| 2997 | * @param key The key within the file to look for. Set to "" for no key. |
| 2998 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 2999 | * @param data The number to write. |
| 3000 | * @param nfv_prop Properties to apply to the saved data in a bitsum. |
| 3001 | * @param identifier The character used to identify different keys. |
| 3002 | * @return 1 if successful. |
| 3003 | */ |
| 3004 | stock player_add_num(const id, const key[], name[], const data, const NFV_PROP:nfv_prop=NFV_OVERWRITE, const identifier=';') |
| 3005 | return nfv_add_num(nfv_player_file(id), key, name, data, nfv_prop, identifier); |
| 3006 | |
| 3007 | /** |
| 3008 | * Sets float for a player under key and name. |
| 3009 | * |
| 3010 | * @param id The index of the client. |
| 3011 | * @param key The key within the file to look for. Set to "" for no key. |
| 3012 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 3013 | * @param data The floating point to write. |
| 3014 | * @param nfv_prop Properties to apply to the saved data in a bitsum. |
| 3015 | * @param identifier The character used to identify different keys. |
| 3016 | * @return 1 if successful. |
| 3017 | */ |
| 3018 | stock player_set_float(const id, const key[], name[], const Float:data, const NFV_PROP:nfv_prop=NFV_OVERWRITE, const identifier=';') |
| 3019 | return nfv_set_float(nfv_player_file(id), key, name, data, nfv_prop, identifier); |
| 3020 | |
| 3021 | /** |
| 3022 | * Adds a float for a player under key and name. |
| 3023 | * |
| 3024 | * @param id The index of the client. |
| 3025 | * @param key The key within the file to look for. Set to "" for no key. |
| 3026 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 3027 | * @param data The floating point to write. |
| 3028 | * @param nfv_prop Properties to apply to the saved data in a bitsum. |
| 3029 | * @param identifier The character used to identify different keys. |
| 3030 | * @return 1 if successful. |
| 3031 | */ |
| 3032 | stock player_add_float(const id, const key[], name[], const Float:data, const NFV_PROP:nfv_prop=NFV_OVERWRITE, const identifier=';') |
| 3033 | return nfv_add_float(nfv_player_file(id), key, name, data, nfv_prop, identifier); |
| 3034 | |
| 3035 | /** |
| 3036 | * Retrieves data for a player under key and name. |
| 3037 | * |
| 3038 | * @param id The index of the client. |
| 3039 | * @param key The key within the file to look for. Set to "" for no key. |
| 3040 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 3041 | * @param data The string that will contain the data retrieved. |
| 3042 | * @param len The size of the data string. |
| 3043 | * @param identifier The character used to identify different keys. |
| 3044 | * @return 1 if successful. |
| 3045 | */ |
| 3046 | stock player_get_data(const id, const key[], name[], data[]="", len=0, const identifier=';') |
| 3047 | return nfv_get_data(nfv_player_file(id), key, name, data, len, identifier); |
| 3048 | |
| 3049 | /** |
| 3050 | * Retrieves data as a number for a player under key and name. |
| 3051 | * |
| 3052 | * @param id The index of the client. |
| 3053 | * @param key The key within the file to look for. Set to "" for no key. |
| 3054 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 3055 | * @param identifier The character used to identify different keys. |
| 3056 | * @return Numeric value of data found. 0 if no data. |
| 3057 | */ |
| 3058 | stock player_get_num(const id, const key[], name[], const identifier=';') |
| 3059 | return nfv_get_num(nfv_player_file(id), key, name, identifier); |
| 3060 | |
| 3061 | /** |
| 3062 | * Retrieves data as a float for a player under key and name. |
| 3063 | * |
| 3064 | * @param id The index of the client. |
| 3065 | * @param key The key within the file to look for. Set to "" for no key. |
| 3066 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 3067 | * @param identifier The character used to identify different keys. |
| 3068 | * @return Floating point value of data found. 0.0 if no data. |
| 3069 | */ |
| 3070 | stock Float:player_get_float(const id, const key[], name[], const identifier=';') |
| 3071 | return nfv_get_float(nfv_player_file(id), key, name, identifier); |
| 3072 | |
| 3073 | /** |
| 3074 | * Removes data for a player under key and name. |
| 3075 | * |
| 3076 | * @param id The index of the client. |
| 3077 | * @param key The key within the file to look for. Set to "" for no key. |
| 3078 | * @param name The name under key to look for. Set to NFV_WILDCARD for any name. |
| 3079 | * @param data The string that will contain the data removed. |
| 3080 | * @param len The size of the data string. |
| 3081 | * @param take_times How many times to take out of the file. Set to NFV_TAKEALL for all. |
| 3082 | * @param identifier The character used to identify different keys. |
| 3083 | * @return How many datas were removed. |
| 3084 | */ |
| 3085 | stock player_take_data(const id, const key[], name[], data[]="", len=0, const take_times=1, const identifier=';') |
| 3086 | return nfv_take_data(nfv_player_file(id), key, name, data, len, take_times, identifier); |
| 3087 | |
| 3088 | /** |
| 3089 | * Switches two key's in a file. |
| 3090 | * |
| 3091 | * @param filename The file location for the data. |
| 3092 | * @param key1 The first key to switch. |
| 3093 | * @param key2 The second key to switch. |
| 3094 | * @param identifier The character used to identify different keys. |
| 3095 | * |
| 3096 | * @note Data under key1 will switch to key2. Data under key2 will switch to key1. |
| 3097 | * |
| 3098 | * @return 1 if successful. |
| 3099 | */ |
| 3100 | stock nfv_switch_keys(const filename[], const key1[], const key2[], const identifier=';') |
| 3101 | { |
| 3102 | new g = fopen(temp_file_name, "wt"); |
| 3103 | new f = fopen(filename, "rt"); |
| 3104 | |
| 3105 | // lets put a time stamp |
| 3106 | fprintf(g, "^"TimeStamp^" ^"%d^"^n", get_systime() ); |
| 3107 | |
| 3108 | new _data[512], _key[64], _other[3]; |
| 3109 | |
| 3110 | new bool:found_time = false; |
| 3111 | |
| 3112 | while( !feof(f) ) |
| 3113 | { |
| 3114 | fgets(f, _data, charsmax(_data)); |
| 3115 | |
| 3116 | if( !found_time ) |
| 3117 | { |
| 3118 | if( equal(_data, "^"TimeStamp^"", 11) ) |
| 3119 | { |
| 3120 | found_time = true; |
| 3121 | continue; |
| 3122 | } |
| 3123 | } |
| 3124 | if( _data[0] == identifier ) |
| 3125 | { |
| 3126 | //Make sure we get something parsed |
| 3127 | if( parse(_data[1], _key, charsmax(_key), _other, charsmax(_other)) ) |
| 3128 | { |
| 3129 | if( equal( _key, key1 ) ) |
| 3130 | { |
| 3131 | fprintf(g, "%c%s^n", identifier, key2); |
| 3132 | continue; |
| 3133 | } |
| 3134 | if( equal( _key, key2 ) ) |
| 3135 | { |
| 3136 | fprintf(g, "%c%s^n", identifier, key1); |
| 3137 | continue; |
| 3138 | } |
| 3139 | } |
| 3140 | } |
| 3141 | fputs(g, _data); |
| 3142 | } |
| 3143 | |
| 3144 | fclose(g); |
| 3145 | fclose(f); |
| 3146 | |
| 3147 | delete_file(filename); |
| 3148 | |
| 3149 | while( !rename_file(temp_file_name, filename, 1) ) { } |
| 3150 | |
| 3151 | return 1; |
| 3152 | } |
| 3153 | |
| 3154 | /** |
| 3155 | * Changes data from one key to another in a file. |
| 3156 | * |
| 3157 | * @param filename The file location for the data. |
| 3158 | * @param key1 The key to change from. |
| 3159 | * @param key2 The key to change to. |
| 3160 | * @param nfv_prop Properties to apply to the saved data in a bitsum. |
| 3161 | * @param identifier The character used to identify different keys. |
| 3162 | * |
| 3163 | * @note Data under key1 will change to key2. |
| 3164 | * |
| 3165 | * @return Returns how many keys were switched. |
| 3166 | */ |
| 3167 | stock nfv_change_key(const filename[], const key1[], const key2[], const NFV_PROP:nfv_prop=NFV_OVERWRITE, const identifier=';') |
| 3168 | { |
| 3169 | new name[32], data[128], i; |
| 3170 | |
| 3171 | while( nfv_take_data( filename, key1, NFV_WILDCARD, data, 127, 1, identifier, name, 31) ) |
| 3172 | { |
| 3173 | nfv_set_data( filename, key2, name, data, nfv_prop, identifier); |
| 3174 | i++; |
| 3175 | } |
| 3176 | |
| 3177 | return i; |
| 3178 | } |
| 3179 | |
| 3180 | /** |
| 3181 | * Removes all data under a key in a file. |
| 3182 | * |
| 3183 | * @param filename The file location for the data. |
| 3184 | * @param key The key to remove from. |
| 3185 | * @param identifier The character used to identify different keys. |
| 3186 | * @return Returns how many datas were removed with the key. |
| 3187 | */ |
| 3188 | stock nfv_remove_key(const filename[], const key[], const identifier=';') |
| 3189 | { |
| 3190 | return nfv_take_data( filename, key, NFV_WILDCARD, _, _, NFV_TAKEALL, identifier ); |
| 3191 | } |
| 3192 | |
| 3193 | /** |
| 3194 | * Returns whether a string should be true or false |
| 3195 | * |
| 3196 | * @param string The string to look at. |
| 3197 | * @return Returns true or false. |
| 3198 | */ |
| 3199 | stock bool:str_to_bool( const string[] ) |
| 3200 | { |
| 3201 | //Numeric values are true |
| 3202 | if( str_to_float( string ) != 0.0 ) |
| 3203 | return true; |
| 3204 | |
| 3205 | //False, No, 0, "" are false |
| 3206 | //0.0 will be detected as 0 |
| 3207 | switch( string[0] ) |
| 3208 | { |
| 3209 | case 'F','f','N','n','0','^0': |
| 3210 | return false; |
| 3211 | } |
| 3212 | |
| 3213 | //Everything else is true |
| 3214 | return true; |
| 3215 | } |
| 3216 | |
| 3217 | /** |
| 3218 | * Fills an array with numbers from a string |
| 3219 | * |
| 3220 | * @param string The string to look at. |
| 3221 | * @param array The array to fill. |
| 3222 | * @param size The size of the array. |
| 3223 | * @noreturn |
| 3224 | */ |
| 3225 | stock str_to_array( const string[], array[], const size ) |
| 3226 | { |
| 3227 | new left[32], right[128], i; |
| 3228 | copy(right, charsmax(right), string); |
| 3229 | |
| 3230 | while( i<size ) |
| 3231 | { |
| 3232 | strtok(right, left, charsmax(left), right, charsmax(right), ',', 1); |
| 3233 | array[i++] = str_to_num( left ); |
| 3234 | } |
| 3235 | } |
| 3236 | |
| 3237 | /** |
| 3238 | * Fills a vector with numbers from a string |
| 3239 | * |
| 3240 | * @param string The string to look at. |
| 3241 | * @param vec The vector to fill. |
| 3242 | * @noreturn |
| 3243 | */ |
| 3244 | stock str_to_vector( const string[], Float:vec[3] ) |
| 3245 | { |
| 3246 | new left[32], right[128], i; |
| 3247 | copy(right, charsmax(right), string); |
| 3248 | |
| 3249 | while( i<3 ) |
| 3250 | { |
| 3251 | strtok(right, left, charsmax(left), right, charsmax(right), ',', 1); |
| 3252 | array[i++] = str_to_float( left ); |
| 3253 | } |
| 3254 | } |
| 3255 | |
| 3256 | /** |
| 3257 | * Pushes cells into a cell array with numbers from a string |
| 3258 | * |
| 3259 | * @param string The string to look at. |
| 3260 | * @param array The cell array to push into. If none provided, it creates one. |
| 3261 | * @return Returns the cell array handle. |
| 3262 | */ |
| 3263 | stock Array:str_to_cellarray( const string[], &Array:array=Invalid_Array ) |
| 3264 | { |
| 3265 | if( array == Invalid_Array ) |
| 3266 | array = ArrayCreate(1,1); |
| 3267 | |
| 3268 | new left[32], right[128]; |
| 3269 | copy(right, charsmax(right), string); |
| 3270 | |
| 3271 | while( strtok(right, left, charsmax(left), right, charsmax(right), ',', 1) ) |
| 3272 | { |
| 3273 | ArrayPushCell(array, str_to_num( left ) ); |
| 3274 | } |
| 3275 | |
| 3276 | return array; |
| 3277 | } |
| 3278 | |
| 3279 | /** |
| 3280 | * Pushes strings into a cell array with substrings from a string |
| 3281 | * |
| 3282 | * @param string The string to look at. |
| 3283 | * @param array The cell array to push into. If none provided, it creates one. |
| 3284 | * @return Returns the cell array handle. |
| 3285 | */ |
| 3286 | stock Array:str_to_cellarray2( const string[], &Array:array=Invalid_Array ) |
| 3287 | { |
| 3288 | if( array == Invalid_Array ) |
| 3289 | array = ArrayCreate(32,1); |
| 3290 | |
| 3291 | new left[128], right[256]; |
| 3292 | copy(right, charsmax(right), string); |
| 3293 | replace_all( right, charsmax(right), ",", " " ); |
| 3294 | |
| 3295 | while( strbreak(right, left, charsmax(left), right, charsmax(right)) ) |
| 3296 | { |
| 3297 | nfv_return_quote( left ); |
| 3298 | ArrayPushString(array, left ); |
| 3299 | } |
| 3300 | |
| 3301 | return array; |
| 3302 | } |
| 3303 | |
| 3304 | |