memory.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /****************************************************************/
  2. /* */
  3. /* Memory */
  4. /* */
  5. /* Description: Fonctions de manipulation de la memoire */
  6. /* Auteur: LePhenixNoir */
  7. /* Version: 3.0 */
  8. /* Date: 11.06.2014 */
  9. /* Fichier: memory.c - Code des fonctions */
  10. /* */
  11. /****************************************************************/
  12. #ifndef __FXLIB_H__
  13. #include "fxlib.h"
  14. #endif
  15. #ifndef _STDIO
  16. #include <stdio.h>
  17. #endif
  18. #ifndef _STDLIB
  19. #include <stdlib.h>
  20. #endif
  21. #ifndef _STRING
  22. #include <string.h>
  23. #endif
  24. #include "memory.h"
  25. int memory_errors = 0;
  26. void memory_seterrors(int e)
  27. {
  28. memory_errors = (e!=0);
  29. }
  30. void memory_error(char *from, char *func, int val)
  31. {
  32. unsigned int key;
  33. char info[20];
  34. if(!memory_errors) return;
  35. sprintf(info,"%d",val);
  36. PopUpWin(6);
  37. locate(4,2); Print((unsigned char *)"Memory ERROR !!");
  38. locate(3,4); Print((unsigned char *)"FROM:");
  39. locate(8,4); Print((unsigned char *)from);
  40. locate(3,5); Print((unsigned char *)"FUNC:");
  41. locate(8,5); Print((unsigned char *)func);
  42. locate(3,6); Print((unsigned char *)"INFO:");
  43. locate(8,6); Print((unsigned char *)info);
  44. locate(3,7); Print((unsigned char *)"META:");
  45. locate(8,7);
  46. switch(val)
  47. {
  48. case 1: Print((unsigned char *)"NotEnoughRAM"); break;
  49. case -1: Print((unsigned char *)"Nonexisting"); break;
  50. case -5: Print((unsigned char *)"WrongDevice"); break;
  51. case -8: Print((unsigned char *)"AccessDenied"); break;
  52. case -14: Print((unsigned char *)"ReadOnly"); break;
  53. case -31: Print((unsigned char *)"DeviceError"); break;
  54. case -35: Print((unsigned char *)"NotEmpty"); break;
  55. default: Print((unsigned char *)"Other"); break;
  56. }
  57. GetKey(&key);
  58. }
  59. FONTCHARACTER *memory_char2font(char *adresse)
  60. {
  61. FONTCHARACTER *adr;
  62. int i;
  63. adr = calloc((strlen(adresse)+1),sizeof(FONTCHARACTER));
  64. for(i=0;i<strlen(adresse);i++) *(adr+i) = *(adresse+i);
  65. return adr;
  66. }
  67. int memory_createfile(char *adresse, int size)
  68. {
  69. FONTCHARACTER *adr = memory_char2font(adresse);
  70. int i = Bfile_CreateFile(adr,size);
  71. if(i<0) memory_error("createfile()","CreateFile()",i);
  72. free(adr);
  73. return i;
  74. }
  75. int memory_createdir(char *adresse)
  76. {
  77. FONTCHARACTER *adr = memory_char2font(adresse);
  78. int i = Bfile_CreateDirectory(adr);
  79. if(i<0) memory_error("createdir()","CreateDir.()",i);
  80. free(adr);
  81. return 1;
  82. }
  83. int memory_openfile(char *adresse, int mode)
  84. {
  85. FONTCHARACTER *adr = memory_char2font(adresse);
  86. int i = Bfile_OpenFile(adr,mode);
  87. if(i<0) memory_error("openfile()","OpenFile()",i);
  88. free(adr);
  89. return i;
  90. }
  91. int memory_deletefile(char *adresse)
  92. {
  93. FONTCHARACTER *adr = memory_char2font(adresse);
  94. int i = Bfile_DeleteFile(adr);
  95. if(i<0) memory_error("deletefil.()","DeleteFil.()",i);
  96. free(adr);
  97. return i;
  98. }
  99. char **memory_alloc(int l)
  100. {
  101. char **p = calloc(l,sizeof(char *));
  102. int i; for(i=0;i<l;i++) *(p+i) = calloc(20,1);
  103. return p;
  104. }
  105. void memory_free(char **p, int l)
  106. {
  107. int i; for(i=0;i<l;i++) free(*(p+i));
  108. free(p);
  109. }
  110. int memory_find(char *adresse, char **files, int max)
  111. {
  112. FONTCHARACTER *adr = memory_char2font(adresse);
  113. FONTCHARACTER found[30];
  114. FILE_INFO fileInfo;
  115. int searchHandle,i=1,j,x;
  116. if(x = Bfile_FindFirst(adr,&searchHandle,found,&fileInfo)) return 0;
  117. for(j=0;j<14 && *(found+j);j++) *(*files+j) = *(found+j);
  118. while(Bfile_FindNext(searchHandle,found,&fileInfo)==0 && i<max) {
  119. for(j=0;j<14 && *(found+j);j++) *(*(files+i)+j) = *(found+j);
  120. i++; }
  121. Bfile_FindClose(searchHandle);
  122. free(adr);
  123. return i;
  124. }
  125. int memory_exists(char *adresse)
  126. {
  127. char *file[1];
  128. int x;
  129. *file = malloc(14); **file=0;
  130. x = memory_find(adresse,file,1);
  131. free(*file);
  132. return x!=0;
  133. }
  134. void *memory_load(char *adresse)
  135. {
  136. FONTCHARACTER *adr = memory_char2font(adresse);
  137. int handle, x, size;
  138. void *p;
  139. if((handle=Bfile_OpenFile(adr,_OPENMODE_READ))<0) { memory_error("load()","OpenFile()",handle); return NULL; }
  140. size = Bfile_GetFileSize(handle)+1;
  141. p = calloc(size,1);
  142. if(!p) {
  143. memory_error("load()","malloc()",1);
  144. Bfile_CloseFile(handle); free(adr); return NULL; }
  145. if((x=Bfile_ReadFile(handle,p,size,0))<0) {
  146. memory_error("load()","ReadFile()",x);
  147. Bfile_CloseFile(handle); free(adr); return NULL; }
  148. Bfile_CloseFile(handle);
  149. free(adr);
  150. return p;
  151. }
  152. int memory_save(char *adresse, void *data, int l)
  153. {
  154. FONTCHARACTER *adr = memory_char2font(adresse);
  155. int x=0, handle;
  156. if(memory_exists(adresse)) x = Bfile_DeleteFile(adr);
  157. if(x<0) { memory_error("save()","DeleteFile()",x); free(adr); return x; }
  158. x = Bfile_CreateFile(adr,l+1);
  159. if(x<0) { memory_error("save()","CreateFile()",x); free(adr); return x; }
  160. handle = Bfile_OpenFile(adr,0x02);
  161. if(handle<0) { memory_error("save()","OpenFile()",handle); free(adr); return handle; }
  162. x = memory_writefile(handle,data,l);
  163. if(x<0) { memory_error("save()","WriteFile()",x); free(adr); return x; }
  164. memory_closefile(handle);
  165. free(adr);
  166. return 0;
  167. }
  168. int memory_user_select(char **files, int n, int extension, int exit)
  169. {
  170. const unsigned char icons[7][32] = {
  171. { 0x0,0x3c,0xf,0xc4,0xf0,0x4,0x80,0x4,0x80,0x2,0x80,0x2,0x40,0x2,0x40,0x2,0x40,0x2,0x40,0x2,0x40,0x1,0x40,0x1,0x20,0x1,0x20,0xf,0x23,0xf0,0x3c,0x0 },
  172. { 0x0,0x3c,0xf,0xc4,0xf0,0x4,0x80,0x74,0x87,0x82,0x98,0x2,0x40,0x2,0x40,0x3a,0x43,0xc2,0x5c,0x2,0x40,0x39,0x43,0xc1,0x2c,0x1,0x20,0xf,0x23,0xf0,0x3c,0x0 },
  173. { 0x0,0x3c,0xf,0xc4,0xf0,0x74,0x87,0x94,0xb8,0x12,0xa0,0xa,0x63,0x8a,0x52,0x8a,0x54,0x4a,0x54,0x66,0x54,0x25,0x48,0x1d,0x29,0xe1,0x2e,0xf,0x23,0xf0,0x3c,0x0 },
  174. { 0x0,0x3c,0xf,0xc4,0xf0,0x4,0x87,0xc4,0x88,0x22,0x8c,0x62,0x4b,0xa2,0x44,0x42,0x42,0x82,0x42,0x82,0x42,0x81,0x44,0x41,0x2f,0xe1,0x20,0xf,0x23,0xf0,0x3c,0x0 },
  175. { 0x0,0x3c,0xf,0xc4,0xf0,0x4,0x87,0xe4,0x88,0x12,0x88,0x12,0x48,0x12,0x47,0xe2,0x44,0x22,0x44,0x22,0x44,0x21,0x44,0x21,0x23,0xc1,0x20,0xf,0x23,0xf0,0x3c,0x0 },
  176. { 0x0,0x3c,0xf,0xc4,0xf0,0x4,0x80,0x64,0x87,0xb2,0x98,0x52,0x51,0xb2,0x57,0x52,0x51,0xd2,0x4b,0xa,0x48,0x19,0x49,0xe1,0x2e,0x1,0x20,0xf,0x23,0xf0,0x3c,0x0 },
  177. { 0x0,0x3c,0xf,0xc4,0xf0,0x4,0x80,0xe4,0x9c,0xa2,0x90,0xa2,0x58,0xe2,0x50,0x2,0x40,0x12,0x4a,0x2a,0x4a,0x39,0x4e,0x29,0x22,0x1,0x20,0xf,0x23,0xf0,0x3c,0x0 } };
  178. char *exts[19] = { ".txt", ".c", ".h", ".cpp", ".hpp", ".bmp",".jpg",".png",".gif", ".sav", ".g1m",".g2m",".g1r",".g2r", ".g1e",".g2e",".g1a", ".hex",".bin" };
  179. unsigned char indexs[19] = { 1,1,1,1,1, 2,2,2,2, 3, 4,4,4,4, 5,5,5, 6,6 };
  180. unsigned char *icoind = malloc(n);
  181. unsigned int key;
  182. int i,j,k,t;
  183. int p=0, offset=0;
  184. if(!icoind) { memory_error("user_sele.()","malloc()",1); return -2; }
  185. for(i=0;i<n;i++)
  186. {
  187. for(t=-1,j=0;*(*(files+i)+j);j++) if(*(*(files+i)+j) == '.') t = j;
  188. icoind[i] = (t==-1?1:0);
  189. for(k=0;k<19;k++)
  190. if(!strcmp(*(files+i)+t,exts[k])) { icoind[i]=indexs[k]; break; }
  191. if(!extension && t+1) *(*(files+i)+t) = 0;
  192. }
  193. while(1)
  194. {
  195. Bdisp_AllClr_VRAM();
  196. for(t=0;t<(n>3?3:n);t++)
  197. {
  198. if(icoind[offset+i]!=255) for(i=0;i<32;i++) {
  199. k = icons[icoind[offset+t]][i];
  200. for(j=0;j<8;j++) {
  201. if(k&1) Bdisp_SetPoint_VRAM(11-j+8*(i&1),20*t+4+(i>>1),1);
  202. k >>= 1; } }
  203. PrintXY(24,20*t+9,(const unsigned char *)*(files+offset+t),0);
  204. }
  205. Bdisp_DrawLineVRAM(2,20*p+3,2,20*p+20);
  206. Bdisp_DrawLineVRAM(3,20*p+2,99,20*p+2);
  207. Bdisp_DrawLineVRAM(3,20*p+21,99,20*p+21);
  208. Bdisp_DrawLineVRAM(100,20*p+3,100,20*p+20);
  209. if(offset>0) PrintXY(114,6,(const unsigned char *)"\346\234",0);
  210. if(offset+3<n) PrintXY(114,51,(const unsigned char *)"\346\235",0);
  211. while(1)
  212. {
  213. GetKey(&key);
  214. if(key==30002 && exit) { free(icoind); return -1; }
  215. if(key==30004) break;
  216. if(key==30018 && (offset||p)) { if(p==2) p--; else if(offset) offset--; else p--; break; }
  217. if(key==30023 && (offset+p+1<n)) { if(p==0) p++; else if(offset+3<n) offset++; else p++; break; }
  218. }
  219. if(key==30004) break;
  220. }
  221. free(icoind);
  222. return offset+p;
  223. }
  224. void *memory_user_autoload(char *prefix, char *selector, int l, int extension, int exit)
  225. {
  226. char **files = memory_alloc(l);
  227. char *adr = malloc(strlen(prefix)+strlen(selector)+1);
  228. void *data;
  229. int x;
  230. sprintf(adr,"%s%s",prefix,selector);
  231. x = memory_find(adr,files,l);
  232. free(adr);
  233. x = memory_user_select(files,x,extension,exit);
  234. adr = malloc(strlen(prefix)+strlen(files[x])+1);
  235. sprintf(adr,"%s%s",prefix,files[x]);
  236. data = memory_load(adr);
  237. free(adr);
  238. memory_free(files,l);
  239. return data;
  240. }