Guest User

e_file

a guest
Jun 26th, 2013
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.46 KB | None | 0 0
  1. /*
  2. _______
  3. |:::::::| __ __ ___ ____ __ TM
  4. |::| \::\ /::/ /::::| |::::| |::|
  5. |:::::| \::\/::/ /::/ |::| |::|
  6. |::| /::/\::\ \::\ |::::| |::|
  7. |:::::::| /::/ \::\ \::::| |::| |::::::|
  8. |::::|
  9.  
  10. [Include] e_admin | New advance admin include | v1
  11. By Excel
  12.  
  13. * Drake1994 - For script help with his dFile include
  14.  
  15. Copyright (c) 2013
  16. All Rights Reserved
  17. */
  18.  
  19. //======================================
  20. #include <a_samp>
  21. //======================================
  22. /*
  23. ------------> E_Functions Information: <------------
  24.  
  25. * efile_create - Create a file.
  26. * efile_delete - Delete a file.
  27.  
  28. * efile_open - Open a file.
  29. * efile_close - Close a file.
  30.  
  31. * efile_WriteString - Write string to the file.
  32. * efile_writeint - Write integer to the file.
  33. * efile_writebool - Write a bool integer to the file.
  34. * efile_writefloat - Write a float integer to the file.
  35.  
  36. * efile_multiset - Write multi parameter and value to the file.
  37. * efile_multiget - Get multi parameter and value from the file.
  38.  
  39. * efile_readstring - Read a string from the file.
  40. * efile_ReadInt - Read an integer from the file.
  41. * efile_ReadFloat - Read a float integer from the file.
  42.  
  43. * efile_movefile - Move a file to the destination.
  44. * efile_renamefile - Rename a file.
  45. * efile_savefile - Write the cache to the file.
  46.  
  47. * efile_FileExists - Check if the file exists.
  48.  
  49. * efile_isset - Check if the parameter has value.
  50. * efile_unset - Delete the parameter's value.
  51.  
  52. * efile_parsestring - Check if the parameter and the string is equal.
  53. * efile_ParseInt - Check if the parameter and the integer is equal.
  54. * efile_ParseFloat - Check if the parameter and the float is equal.
  55. * efile_ParseBool - Check if the parameter and the bool is equal.
  56. */
  57.  
  58. //======================================
  59. #if defined _e_file_included
  60. #endinput
  61. #endif
  62. #define _e_file_included
  63. //======================================
  64.  
  65. /*
  66. ------------> E_Natives <------------
  67.  
  68. native efile_create(location[]);
  69. native efile_delete(location[]);
  70.  
  71. native efile_open(location[]);
  72. native efile_close();
  73.  
  74. native efile_writestring(parameter[], value[]);
  75. native efile_writeint(parameter[], value[]);
  76. native efile_writebool(parameter[], bool:value);
  77. native efile_writefloat(parameter[], Float.value);
  78.  
  79. native efile_multiset(type[],{Float,_}:...);
  80. native efile_multiget(type[],{Float,_}:...);
  81.  
  82. native efile_readstring(parameter[]);
  83. native efile_ReadInt(parameter[]);
  84. native efile_ReadFloat(parameter[]);
  85. native efile_ReadBool(parameter[]);
  86.  
  87. native efile_movefile(location[], newlocation[]);
  88. native efile_renamefile(location[], newname[]);
  89. native efile_savefile();
  90. native efile_exists(location[]);
  91.  
  92. native efile_isset(parameter[]);
  93. native efile_unset(parameter[]);
  94.  
  95. native efile_parsestring(parameter[], value[]);
  96. native efile_parseint(parameter[], value);
  97. native efile_parsefloat(parameter[], Float:value);
  98. native efile_parsebool(parameter[], bool:value);
  99. */
  100.  
  101. //======================================
  102. #define \
  103. efile_writestring(%0,%1) \
  104. efile_buffer(%0,%1)
  105.  
  106. #if !defined \
  107. strcpy
  108. #define \
  109. strcpy(%0,%1,%2) \
  110. strcat((%0[0] = EOS, %0), %1, %2 + 1)
  111. #endif
  112. //======================================
  113.  
  114. //======================================
  115. #define \
  116. MAX_PARAMETER_SIZE \
  117. (0x80)
  118. #define \
  119. MAX_ERTEK_SIZE \
  120. (0x100)
  121. #define \
  122. FILE_NAME \
  123. (0x40)
  124. #define \
  125. MAX_LINES \
  126. (0x3E8)
  127. //======================================
  128.  
  129. //======================================
  130. static stock
  131. fajlnev[FILE_NAME], paramtar[MAX_PARAMETER_SIZE],
  132.  
  133. formazas[512+3], olvasas[512],
  134.  
  135. File:fajlmegnyitas, File:celfajl,
  136.  
  137. eredmeny[256], sorokszama = 0;
  138.  
  139. static stock
  140. parameternek[MAX_LINES],
  141.  
  142. param[MAX_LINES][MAX_PARAMETER_SIZE],
  143.  
  144. ert[MAX_LINES][MAX_ERTEK_SIZE];
  145. //======================================
  146.  
  147. //======================================
  148. stock bool:boolstr2(string[]){
  149. if (!string[0] || string[0] == '0' || !strcmp(string, "false", true)) return false;
  150. return true;
  151. }
  152. //======================================
  153.  
  154. //======================================
  155. /*
  156. Function: efile_create(location[]);
  157. Usage: Creates a new directory in scriptfiles.
  158. */
  159. stock efile_create(location[])
  160. {
  161. if(fexist(location)) return false;
  162. fclose(fopen(location, io_write));
  163. return true;
  164. }
  165. //======================================
  166.  
  167. //======================================
  168. /*
  169. Function: efile_exists(location[]);
  170. Usage: Checks if the location given exists.
  171. */
  172. stock efile_exists(location[])
  173. { return fexist(location); }
  174. //======================================
  175.  
  176. //======================================
  177. /*
  178. Function: efile_delete(location[]);
  179. Usage: Deletes the existing file defined.
  180. */
  181. stock efile_delete(location[])
  182. {
  183. if(!fexist(location)) return false;
  184. fremove(location);
  185. return true;
  186. }
  187. //======================================
  188.  
  189. //======================================
  190. /*
  191. Function: efile_readstring(parameter[]);
  192. Usage: Reads the string from the file.
  193. */
  194. stock efile_readstring(parameter[])
  195. {
  196. new
  197. i = 0x0;
  198. for(; i < sorokszama; ++i)
  199. {
  200. if(strcmp(param[i], parameter, false)) continue;
  201. return ert[i];
  202. }
  203. eredmeny[0] = EOS;
  204. return eredmeny;
  205. }
  206. //======================================
  207.  
  208. //======================================
  209. /*
  210. Function: efile_open(location[]);
  211. Usage: Opens the existing file from scriptfiles.
  212. */
  213. stock efile_open(location[])
  214. {
  215. new
  216. sorokszama2 = 0x-1,
  217. talalat;
  218. olvasas[0] = EOS;
  219. fajlmegnyitas = fopen(location,io_read);
  220. while(fread(fajlmegnyitas, olvasas))
  221. {
  222. talalat = strfind(olvasas, "=", false);
  223. strmid(param[++sorokszama2], olvasas, 0, talalat);
  224. strmid(ert[sorokszama2], olvasas, talalat+1, strfind(olvasas, "\r\n", false));
  225. parameternek[sorokszama2] = efile_hashstring(param[sorokszama2]);
  226. }
  227. sorokszama = (sorokszama2 + 1);
  228. fclose(fajlmegnyitas);
  229. format(fajlnev,FILE_NAME,"%s",location);
  230. return true;
  231. }
  232. //======================================
  233.  
  234. //======================================
  235. /*
  236. Function: efile_hashstring(parameter[]);
  237. Usage: Hashes the string given in parameters.
  238. */
  239. stock efile_hashstring(parameter[])
  240. {
  241. new
  242. totalContado = 0, i = strlen(parameter);
  243. while(i-- != 0) totalContado += parameter[i];
  244. return totalContado;
  245. }
  246. //======================================
  247.  
  248. //======================================
  249. /*
  250. Function: efile_writeint(parameter[], value);
  251. Usage: Write value to a new parameter.
  252. */
  253. stock efile_writeint(parameter[], value)
  254. {
  255. new
  256. intvalue[MAX_ERTEK_SIZE];
  257. format(intvalue, sizeof(intvalue), "%d", value);
  258. return efile_buffer(parameter, intvalue);
  259. }
  260. //======================================
  261.  
  262. //======================================
  263. /*
  264. Function: efile_writebool(parameter[], bool:value);
  265. Usage: Write booling value to a new parameter.
  266. */
  267. stock efile_writebool(parameter[], bool:value)
  268. {
  269. if(value) efile_buffer(parameter, "true");
  270. else efile_buffer(parameter, "false");
  271. return true;
  272. }
  273. //======================================
  274.  
  275. //======================================
  276. /*
  277. Function: efile_writefloat(parameter[], Float:value);
  278. Usage: Write booling value to a new parameter.
  279. */
  280. stock efile_writefloat(parameter[], Float:value)
  281. {
  282. new
  283. floatvalue[MAX_ERTEK_SIZE];
  284. format(floatvalue, sizeof(floatvalue), "%f", value);
  285. return efile_buffer(parameter, floatvalue);
  286. }
  287. //======================================
  288.  
  289. //======================================
  290. /*
  291. Function: efile_multiset(type[],{Float,_}:...);
  292. Usage: This is an advance way to set file data wheather its a bool, int, string or float.
  293. */
  294. stock efile_multiset(type[],{Float,_}:...)
  295. {
  296. new
  297. i = 0x-1;
  298. while(++i<strlen(type))
  299. {
  300. new
  301. l = 0x-1,
  302. iLoop = -1,
  303. string2[512];
  304. while(++l<MAX_PARAMETER_SIZE) paramtar[l] = getarg(1 + (i * 2), l);
  305. switch(type[i])
  306. {
  307. case 'b': efile_writebool(paramtar, bool:getarg(2 + (i * 2)));
  308. case 'd', 'i': efile_writeint(paramtar, getarg(2 + (i * 2)));
  309. case 'f': efile_writefloat(paramtar, Float:(getarg(2 + (i * 2))));
  310. case 's':
  311. {
  312. while((++iLoop != MAX_ERTEK_SIZE)) string2[iLoop] = getarg(2 + (i * 2), iLoop);
  313. efile_writestring(paramtar, string2);
  314. }
  315. }
  316. }
  317. return false;
  318. }
  319. //======================================
  320.  
  321. //======================================
  322. /*
  323. Function: efile_multiget(tipusok[],{Float,_}:...);
  324. Usage: This is an advance way to get file data wheather its a bool, int, string or float.
  325. */
  326. stock efile_multiget(type[],{Float,_}:...)
  327. {
  328. new
  329. i = 0x-1;
  330. while(++i<strlen(type))
  331. {
  332. new
  333. l = 0x-1,
  334. iLoop = 0x-1;
  335. while(++l<MAX_PARAMETER_SIZE) paramtar[l] = getarg(1 + (i * 2), l);
  336. switch(type[i])
  337. {
  338. case 'b': setarg(2+(i*2), 0, boolstr2(efile_readstring(paramtar)));
  339. case 'd', 'i': setarg(2+(i*2), 0, strval(efile_readstring(paramtar)));
  340. case 'f': setarg(2+(i*2), 0, _:floatstr(efile_readstring(paramtar)));
  341. case 's':
  342. {
  343. format(formazas,sizeof(formazas),"%s",efile_readstring(paramtar));
  344. while((strlen(efile_readstring(paramtar)) != iLoop++)) setarg(2+(i*2),iLoop,formazas[iLoop]);
  345. }
  346. default: return print("e_file (ERROR): Unknown type defination."), 0;
  347. }
  348. }
  349. return true;
  350. }
  351. //======================================
  352.  
  353. //======================================
  354. /*
  355. Function: efile_close();
  356. Usage: This closes a file which is opened before this native.
  357. */
  358. stock efile_close()
  359. {
  360. new
  361. i = -1;
  362. for(; ++i != sorokszama ;)
  363. {
  364. param[i][0] = EOS;
  365. ert[i][0] = EOS;
  366. parameternek[i] = 0;
  367. }
  368. return sorokszama = 0;
  369. }
  370. //======================================
  371.  
  372. //======================================
  373. /*
  374. Function: efile_savefile();
  375. Usage: Saves the data writen on file.
  376. */
  377. stock efile_savefile()
  378. {
  379. new
  380. iLoop = -1;
  381. fajlmegnyitas = fopen(fajlnev, io_write);
  382. while(++iLoop != sorokszama)
  383. {
  384. if(strlen(param[iLoop]) > 0)
  385. {
  386. format(formazas, sizeof(formazas), "%s=%s\r\n", param[iLoop], ert[iLoop]);
  387. fwrite(fajlmegnyitas, formazas);
  388. }else break;
  389. }
  390. return fclose(fajlmegnyitas);
  391. }
  392. //======================================
  393.  
  394. //======================================
  395. /*
  396. Function: efile_renamefile(location[], newname[]);
  397. Usage: Rename the file.
  398. */
  399. stock efile_renamefile(location[], newname[])
  400. { return efile_movefile(location, newname); }
  401. //======================================
  402.  
  403. //======================================
  404. /*
  405. Function: efile_movefile(location[], newlocation[]);
  406. Usage: Moves a existing file to a new location.
  407. */
  408. stock efile_movefile(location[], newlocation[])
  409. {
  410. if(!fexist(location)) return false;
  411. efile_create(newlocation);
  412. fajlmegnyitas = fopen(location, io_read);
  413. celfajl = fopen(newlocation, io_write);
  414. while(fread(fajlmegnyitas, olvasas)) fwrite(celfajl, olvasas);
  415. fclose(fajlmegnyitas);
  416. fclose(celfajl);
  417. fremove(location);
  418. return true;
  419. }
  420. //======================================
  421.  
  422. //======================================
  423. stock efile_parsestring(parameter[], value[])
  424. {
  425. format(formazas, sizeof(formazas), "%s", efile_readstring(parameter));
  426. if(strval(efile_readstring(parameter)) == strval(value)) return true;
  427. else return false;
  428. }
  429. //======================================
  430.  
  431. //======================================
  432. stock efile_parseint(parameter[], value)
  433. {
  434. new intvalue[MAX_PARAMETER_SIZE];
  435. format(intvalue, sizeof(intvalue), "%d", value);
  436. return efile_parsestring(parameter, intvalue);
  437. }
  438. //======================================
  439.  
  440. //======================================
  441. stock efile_ParseFloat(parameter[], Float:value)
  442. {
  443. new floatvalue[MAX_PARAMETER_SIZE];
  444. format(floatvalue, sizeof(floatvalue), "%f", value);
  445. return efile_parsestring(parameter, floatvalue);
  446. }
  447. //======================================
  448.  
  449. //======================================
  450. stock efile_ParseBool(parameter[], bool:value)
  451. {
  452. new boolean;
  453. if(value) boolean = efile_parsestring(parameter, "true");
  454. else boolean = efile_parsestring(parameter, "false");
  455. return boolean;
  456. }
  457. //======================================
  458.  
  459. //======================================
  460. /*
  461. Function: efile_isset(parameter[]);
  462. Usage: Checks wheather the paramater has a value or not.
  463. */
  464. stock efile_isset(parameter[])
  465. {
  466. if(!strlen(efile_readstring(parameter))) return false;
  467. else return true;
  468. }
  469. //======================================
  470.  
  471. //======================================
  472. /*
  473. Function: efile_unset(parameter[]);
  474. Usage: Reset the parameter value Or you can say it deletes it.
  475. */
  476. stock efile_unset(parameter[])
  477. {
  478. new
  479. i = 0x-1;
  480. for( ; ++i != sorokszama ; )
  481. {
  482. if(!strcmp(param[i], parameter, false))
  483. {
  484. return ert[i][0] = EOS;
  485. }
  486. }
  487. return true;
  488. }
  489. //======================================
  490.  
  491. //======================================
  492. /*
  493. - Not Used -
  494. Function: efile_buffer(parameter[], str[]);
  495. Usage: Not in use for you.
  496. */
  497. stock efile_buffer(parameter[], str[])
  498. {
  499. new i, valami = efile_hashstring(parameter);
  500. for( ; i != sorokszama ; ++i) if(valami == parameternek[i])
  501. {
  502. if(strcmp(param[i], parameter, false)) continue;
  503. return strcpy(ert[i], str, MAX_ERTEK_SIZE);
  504. }
  505. parameternek[i]=valami;
  506. return strcpy(param[i], parameter, MAX_PARAMETER_SIZE), strcpy(ert[i], str, MAX_ERTEK_SIZE), sorokszama++, true;
  507. }
  508. //======================================
Advertisement
Add Comment
Please, Sign In to add comment