Little fix after the last commit (mostly a git fail)
[eigenmath-fx.git] / memory.c
blob9e9fa1a00fdb1ef67d4879c5e92cc8f1a19c186f
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 /****************************************************************/
13 #ifndef __FXLIB_H__
14 #include "fxlib.h"
15 #endif
17 #ifndef _STDIO
18 #include <stdio.h>
19 #endif
21 #ifndef _STDLIB
22 #include <stdlib.h>
23 #endif
25 #ifndef _STRING
26 #include <string.h>
27 #endif
29 #include "memory.h"
31 int memory_errors = 0;
33 void memory_seterrors(int e)
35 memory_errors = (e!=0);
38 void memory_error(char *from, char *func, int val)
40 unsigned int key;
41 char info[20];
42 if(!memory_errors) return;
44 sprintf(info,"%d",val);
45 PopUpWin(6);
47 locate(4,2); Print((unsigned char *)"Memory ERROR !!");
48 locate(3,4); Print((unsigned char *)"FROM:");
49 locate(8,4); Print((unsigned char *)from);
50 locate(3,5); Print((unsigned char *)"FUNC:");
51 locate(8,5); Print((unsigned char *)func);
52 locate(3,6); Print((unsigned char *)"INFO:");
53 locate(8,6); Print((unsigned char *)info);
54 locate(3,7); Print((unsigned char *)"META:");
55 locate(8,7);
57 switch(val)
59 case 1: Print((unsigned char *)"NotEnoughRAM"); break;
60 case -1: Print((unsigned char *)"Nonexisting"); break;
61 case -5: Print((unsigned char *)"WrongDevice"); break;
62 case -8: Print((unsigned char *)"AccessDenied"); break;
63 case -14: Print((unsigned char *)"ReadOnly"); break;
64 case -31: Print((unsigned char *)"DeviceError"); break;
65 case -35: Print((unsigned char *)"NotEmpty"); break;
66 default: Print((unsigned char *)"Other"); break;
68 GetKey(&key);
71 FONTCHARACTER *memory_char2font(char *adresse)
73 FONTCHARACTER *adr;
74 int i;
76 adr = calloc((strlen(adresse)+1),sizeof(FONTCHARACTER));
77 for(i=0;i<strlen(adresse);i++) *(adr+i) = *(adresse+i);
78 return adr;
81 int memory_createfile(char *adresse, int size)
83 FONTCHARACTER *adr = memory_char2font(adresse);
84 int i = Bfile_CreateFile(adr,size);
85 if(i<0) memory_error("createfile()","CreateFile()",i);
87 free(adr);
88 return i;
91 int memory_createdir(char *adresse)
93 FONTCHARACTER *adr = memory_char2font(adresse);
94 int i = Bfile_CreateDirectory(adr);
95 if(i<0) memory_error("createdir()","CreateDir.()",i);
97 free(adr);
98 return 1;
101 int memory_openfile(char *adresse, int mode)
103 FONTCHARACTER *adr = memory_char2font(adresse);
104 int i = Bfile_OpenFile(adr,mode);
105 if(i<0) memory_error("openfile()","OpenFile()",i);
107 free(adr);
108 return i;
111 int memory_deletefile(char *adresse)
113 FONTCHARACTER *adr = memory_char2font(adresse);
114 int i = Bfile_DeleteFile(adr);
115 if(i<0) memory_error("deletefil.()","DeleteFil.()",i);
117 free(adr);
118 return i;
121 char **memory_alloc(int l)
123 char **p = calloc(l,sizeof(char *));
124 int i; for(i=0;i<l;i++) *(p+i) = calloc(20,1);
125 return p;
128 void memory_free(char **p, int l)
130 int i; for(i=0;i<l;i++) free(*(p+i));
131 free(p);
134 int memory_find(char *adresse, char **files, int max)
136 FONTCHARACTER *adr = memory_char2font(adresse);
137 FONTCHARACTER found[30];
138 FILE_INFO fileInfo;
139 int searchHandle,i=1,j,x;
141 if(x = Bfile_FindFirst(adr,&searchHandle,found,&fileInfo)) return 0;
142 for(j=0;j<14 && *(found+j);j++) *(*files+j) = *(found+j);
144 while(Bfile_FindNext(searchHandle,found,&fileInfo)==0 && i<max) {
145 for(j=0;j<14 && *(found+j);j++) *(*(files+i)+j) = *(found+j);
146 i++; }
148 Bfile_FindClose(searchHandle);
149 free(adr);
150 return i;
153 int memory_exists(char *adresse)
155 char *file[1];
156 int x;
158 *file = malloc(14); **file=0;
159 x = memory_find(adresse,file,1);
160 free(*file);
162 return x!=0;
165 void *memory_load(char *adresse)
167 FONTCHARACTER *adr = memory_char2font(adresse);
168 int handle, x, size;
169 void *p;
171 if((handle=Bfile_OpenFile(adr,_OPENMODE_READ))<0) { memory_error("load()","OpenFile()",handle); return NULL; }
172 size = Bfile_GetFileSize(handle)+1;
173 p = calloc(size,1);
175 if(!p) {
176 memory_error("load()","malloc()",1);
177 Bfile_CloseFile(handle); free(adr); return NULL; }
178 if((x=Bfile_ReadFile(handle,p,size,0))<0) {
179 memory_error("load()","ReadFile()",x);
180 Bfile_CloseFile(handle); free(adr); return NULL; }
182 Bfile_CloseFile(handle);
183 free(adr);
184 return p;
187 int memory_save(char *adresse, void *data, int l)
189 FONTCHARACTER *adr = memory_char2font(adresse);
190 int x=0, handle;
192 if(memory_exists(adresse)) x = Bfile_DeleteFile(adr);
193 if(x<0) { memory_error("save()","DeleteFile()",x); free(adr); return x; }
194 x = Bfile_CreateFile(adr,l+1);
195 if(x<0) { memory_error("save()","CreateFile()",x); free(adr); return x; }
196 handle = Bfile_OpenFile(adr,0x02);
197 if(handle<0) { memory_error("save()","OpenFile()",handle); free(adr); return handle; }
198 x = memory_writefile(handle,data,l);
199 if(x<0) { memory_error("save()","WriteFile()",x); free(adr); return x; }
200 memory_closefile(handle);
202 free(adr);
203 return 0;
206 int memory_user_select(char **files, int n, int extension, int exit)
208 const unsigned char icons[7][32] = {
209 { 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 },
210 { 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 },
211 { 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 },
212 { 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 },
213 { 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 },
214 { 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 },
215 { 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 } };
216 char *exts[19] = { ".txt", ".c", ".h", ".cpp", ".hpp", ".bmp",".jpg",".png",".gif", ".sav", ".g1m",".g2m",".g1r",".g2r", ".g1e",".g2e",".g1a", ".hex",".bin" };
217 unsigned char indexs[19] = { 1,1,1,1,1, 2,2,2,2, 3, 4,4,4,4, 5,5,5, 6,6 };
218 unsigned char *icoind = malloc(n);
219 unsigned int key;
221 int i,j,k,t;
222 int p=0, offset=0;
224 if(!icoind) { memory_error("user_sele.()","malloc()",1); return -2; }
226 for(i=0;i<n;i++)
228 for(t=-1,j=0;*(*(files+i)+j);j++) if(*(*(files+i)+j) == '.') t = j;
229 icoind[i] = (t==-1?1:0);
230 for(k=0;k<19;k++)
231 if(!strcmp(*(files+i)+t,exts[k])) { icoind[i]=indexs[k]; break; }
232 if(!extension && t+1) *(*(files+i)+t) = 0;
235 while(1)
237 Bdisp_AllClr_VRAM();
239 for(t=0;t<(n>3?3:n);t++)
241 if(icoind[offset+i]!=255) for(i=0;i<32;i++) {
242 k = icons[icoind[offset+t]][i];
243 for(j=0;j<8;j++) {
244 if(k&1) Bdisp_SetPoint_VRAM(11-j+8*(i&1),20*t+4+(i>>1),1);
245 k >>= 1; } }
246 PrintXY(24,20*t+9,(const unsigned char *)*(files+offset+t),0);
248 Bdisp_DrawLineVRAM(2,20*p+3,2,20*p+20);
249 Bdisp_DrawLineVRAM(3,20*p+2,99,20*p+2);
250 Bdisp_DrawLineVRAM(3,20*p+21,99,20*p+21);
251 Bdisp_DrawLineVRAM(100,20*p+3,100,20*p+20);
252 if(offset>0) PrintXY(114,6,(const unsigned char *)"\346\234",0);
253 if(offset+3<n) PrintXY(114,51,(const unsigned char *)"\346\235",0);
255 while(1)
257 GetKey(&key);
258 if(key==30002 && exit) { free(icoind); return -1; }
259 if(key==30004) break;
260 if(key==30018 && (offset||p)) { if(p==2) p--; else if(offset) offset--; else p--; break; }
261 if(key==30023 && (offset+p+1<n)) { if(p==0) p++; else if(offset+3<n) offset++; else p++; break; }
264 if(key==30004) break;
267 free(icoind);
268 return offset+p;
271 void *memory_user_autoload(char *prefix, char *selector, int l, int extension, int exit)
273 char **files = memory_alloc(l);
274 char *adr = malloc(strlen(prefix)+strlen(selector)+1);
275 void *data;
276 int x;
278 sprintf(adr,"%s%s",prefix,selector);
279 x = memory_find(adr,files,l);
280 free(adr);
281 x = memory_user_select(files,x,extension,exit);
283 adr = malloc(strlen(prefix)+strlen(files[x])+1);
284 sprintf(adr,"%s%s",prefix,files[x]);
285 data = memory_load(adr);
287 free(adr);
288 memory_free(files,l);
289 return data;