Advertisement
Hirsw0w

HFile - Hirsw0w's Files

Jul 21st, 2014
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. /*
  2. * Hirsw0w File Save.
  3. * Author : Hirsw0w
  4. * Version : 1.0
  5. *
  6. * Explain:
  7. * I Have created a new file save, fast respone.
  8. * all the stocks helping you with the save
  9. *
  10. * Defines:
  11. *
  12. * MAX_VALUE_STRING is the max chars in the value. (default: 50 )
  13. * MAX_DATA_STRING is the max chars in the data. (default: 50 )
  14. * MAX_FILE_NAME is the max chars in the file name. (default: 100 )
  15. * MAX_DATA is the max data the include can contain. (default: 75 )
  16. *
  17. * Macors:
  18. *
  19. * HFile_Create(filename) that a macro for the functions fclose and fopen. ( thats create a new file )
  20. * HFile_Exists(filename) that a macro for the function file_exists. ( thats check if file exists )
  21. * HFile_Delete(filename) that a macro for the function delete_file. ( thats delete a file )
  22. *
  23. * Stocks:
  24. *
  25. * HFile_Copy(file1,file2) - Copy file for other location
  26. * HFile_Rename(file1,file2) - change file name for another
  27. * HFile_Open(filename) - Open a file ( need to write and read )
  28. * HFile_Close(filename) - Close a file after usage ( needed for save )
  29. * HFile_WriteString(Data[],Value[]) - Write string save on the file.
  30. * HFile_WriteInt(Data[],Value) - Write a Int( Number ) save on the file.
  31. * HFile_WriteFloat(Data[],Float:Value) - Write a Float save on the file.
  32. * HFile_WriteBool(Data[],bool:Value) - Write a boolean save on the file.
  33. * HFile_ReadString(Data[]) - Read a string from the file by the data.
  34. * HFile_ReadInt(Data[]) - Read a Int( Number ) from the file by the data.
  35. * HFile_ReadFloat(Data[]) - Read a Float from the file by the data.
  36. * HFile_ReadBool(Data[]) - Read a Boolean from the file by the data.
  37. * HFile_UnSet(Data[]) - Remove a data.
  38. * HFile_IsSet(Data[]) - Check if Data is exists
  39. * HFile_GetDataName(Value[]) - Get a data name by the value
  40. * HFIle_GetFileSize(filename[]) - Get the file size.
  41. *
  42. * Bugs: don't found yet.
  43. *
  44. * Enjoy!
  45. */
  46.  
  47. #if defined _HFile_included
  48. #endinput
  49. #endif
  50. #define _HFile_included
  51.  
  52. #define MAX_VALUE_STRING 50
  53. #define MAX_DATA_STRING 50
  54. #define MAX_FILE_NAME 100
  55.  
  56. #define MAX_DATA 75
  57.  
  58. #define HFile_Create(%0) fclose(fopen(%0, "wt"))
  59.  
  60. #define HFile_Exists(%0) file_exists(%0)
  61.  
  62. #define HFile_Delete(%0) delete_file(%0)
  63.  
  64. new Data[MAX_DATA][MAX_DATA_STRING],Value[MAX_DATA][MAX_VALUE_STRING],File[MAX_FILE_NAME];
  65. new bool:FileOpen;
  66.  
  67. stock HFile_Copy(file1[],file2[]) {
  68. if(!HFile_Exists(file1)) return 0;
  69. if(HFile_Exists(file2)) HFile_Delete(file2);
  70. new HFile1 = fopen(file1,"rt");
  71.  
  72. new tmpres[128];
  73. while(!feof(HFile1))
  74. {
  75. fgets(HFile1, tmpres, sizeof tmpres - 1);
  76. replace_all(tmpres,128,"^n","");
  77. replace_all(tmpres,128,"^r","");
  78. write_file(file2,tmpres);
  79. }
  80. fclose(HFile1);
  81. return 1;
  82. }
  83.  
  84. stock HFile_Rename(file1[],file2[]) {
  85. HFile_Copy(file1,file2);
  86. HFile_Delete(file1);
  87. return 1;
  88. }
  89.  
  90. stock HFile_Open(filename[]) {
  91. if(!file_exists(filename)) {
  92. HFile_Create(filename);
  93. return 1;
  94. }
  95. new tmpres[128];
  96. new HFile = fopen(filename, "rt");
  97. new i;
  98. while(!feof(HFile))
  99. {
  100. fgets(HFile, tmpres, sizeof tmpres - 1);
  101. if(tmpres[0] == '/' && tmpres[1] == '/' || strlen(tmpres) < 3)
  102. continue;
  103.  
  104. i++;
  105.  
  106. strtok( tmpres,Data[i],MAX_DATA_STRING,Value[i],MAX_VALUE_STRING, '=');
  107. }
  108.  
  109. FileOpen = true;
  110. formatex(File,MAX_FILE_NAME,filename);
  111. fclose(HFile);
  112. return 1;
  113. }
  114.  
  115. stock HFile_Close() {
  116. if(!FileOpen) return 1;
  117. HFile_Delete(File);
  118. new string[150];
  119. for(new i; i <= sizeof Data-1;i++) {
  120. if(!strlen(Data[i])) continue;
  121.  
  122. formatex(string,150,"%s=%s",Data[i],Value[i])
  123. write_file(File,string);
  124. Data[i] = "",Value[i] = "";
  125. }
  126. File = "";
  127. FileOpen = false;
  128. return 1;
  129. }
  130.  
  131. stock HFile_WriteString(DataN[],ValueN[]) {
  132. if(!FileOpen) return 0;
  133. new lines;
  134.  
  135. for(new i;i < sizeof Data;i++) {
  136. if(!strlen(Data[i])) continue;
  137.  
  138. lines++;
  139. if(equali(DataN,Data[i])) {
  140. formatex(Value[i],MAX_VALUE_STRING,ValueN);
  141. return 1;
  142. }
  143. }
  144. formatex(Data[lines],MAX_DATA_STRING,DataN);
  145. formatex(Value[lines],MAX_VALUE_STRING,ValueN);
  146. return 1;
  147. }
  148.  
  149. stock HFile_WriteInt(DataN[],ValueN) {
  150. new num[20];
  151. formatex(num,20,"%d",ValueN);
  152. return HFile_WriteString(DataN,num);
  153. }
  154.  
  155. stock HFile_WriteFloat(DataN[],Float:ValueN) {
  156. new num[30];
  157. formatex(num,30,"%f",ValueN);
  158. return HFile_WriteString(DataN,num);
  159. }
  160.  
  161. stock HFile_WriteBool(DataN[],bool:ValueN) return HFile_WriteString(DataN,ValueN ? "true":"false");
  162.  
  163. stock HFile_ReadString(DataN[]) {
  164. if(FileOpen) {
  165.  
  166. for(new i;i < sizeof Data;i++) {
  167. if(!strlen(Data[i])) continue;
  168.  
  169. if(equali(Data[i],DataN)) return Value[i];
  170. }
  171. }
  172. return Value[MAX_DATA-1];
  173. }
  174.  
  175. stock HFile_ReadInt(DataN[]) return str_to_num(HFile_ReadString(DataN));
  176.  
  177. stock Float:HFile_ReadFloat(DataN[]) return str_to_float(HFile_ReadString(DataN));
  178.  
  179. stock HFile_ReadBool(DataN[]) return (equali(HFile_ReadString(DataN),"true")) ? true:false;
  180.  
  181. stock HFile_UnSet(DataN[]) {
  182. if(!FileOpen) return 0;
  183.  
  184. for(new i;i < sizeof Data;i++) {
  185. if(!strlen(Data[i])) continue;
  186.  
  187. if(equali(Data[i],DataN)) Data[i] = "",Value[i] = "";
  188. }
  189. return 1;
  190. }
  191.  
  192. stock HFile_IsSet(DataN[]) {
  193. if(!FileOpen) return false;
  194.  
  195. for(new i;i < sizeof Data;i++) {
  196. if(!strlen(Data[i])) continue;
  197.  
  198. if(equali(Data[i],DataN)) return true;
  199. }
  200. return false;
  201. }
  202.  
  203. stock HFile_GetDataName(ValueN[]) {
  204. if(FileOpen) {
  205.  
  206. for(new i;i < sizeof Data;i++) {
  207. if(!strlen(Data[i])) continue;
  208.  
  209. if(equali(Value[i],ValueN)) return Data[i];
  210. }
  211. }
  212. return Data[MAX_DATA-1];
  213. }
  214.  
  215.  
  216. stock HFile_GetFileSize(filename[]) {
  217. new tmpres[128];
  218. new HFile = fopen(filename, "rt");
  219. new i;
  220. while(!feof(HFile))
  221. {
  222. fgets(HFile, tmpres, sizeof tmpres - 1);
  223. if(tmpres[0] == '/' && tmpres[1] == '/' || strlen(tmpres) < 3)
  224. continue;
  225.  
  226. i += strlen(tmpres);
  227. }
  228. fclose(HFile);
  229. return i;
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement