hschumann2/TempleOS-Source-Code
0847
1 2U8 *FileRead(U8 *filename,I64 *_size=NULL,I64 *_attr=NULL)3{//Read whole file from disk.4 CHashGeneric *tmph;5 U8 *absname,*altname,*curname,*res=NULL;6 I64 i,size=0,attr=0;7 CDirContext *dirc;8 CArcCompress *arc;9 absname=FileNameAbs(filename);10 altname=ToggleZorNotZ(absname);11 if ((tmph=HashFind(absname,adam_task->hash_table,HTT_FILE))||12 (tmph=HashFind(altname,adam_task->hash_table,HTT_FILE))) {13 size=tmph->user_data1;14 res=MAlloc(size+1);15 MemCpy(res,tmph->user_data0,size);16 res[size]=0; //Terminate17 attr=FileAttr(tmph->str,attr);18 } else {19 for (i=0;i<2 && !res;i++) {//Try name, then altname20 if (!i)21 curname=absname;22 else23 curname=altname;24 if (dirc=DirContextNew(curname)) {25 switch (dirc->dv->fs_type) {26 case FSt_REDSEA:27 res=RedSeaFileRead(dirc->dv,Fs->cur_dir,dirc->mask,&size,&attr);28 break;29 case FSt_FAT32:30 res=FAT32FileRead(dirc->dv,Fs->cur_dir,dirc->mask,&size,&attr);31 break;32 case FSt_ISO9660:33 res=ISO1FileRead(dirc->dv,Fs->cur_dir,dirc->mask,&size,&attr);34 break;35 default:36 PrintErr("File System Not Supported\n");37 }38 DirContextDel(dirc);39 }40 }41 42 //Search parent directories.43 for (i=0;i<2 && !res;i++) {//Try name, then altname44 if (!i)45 curname=absname;46 else47 curname=altname;48 if (dirc=DirContextNew(curname)) {49 while (!res && StrCmp(Fs->cur_dir,"/")) {50 Cd("..");51 switch (Fs->cur_dv->fs_type) {52 case FSt_REDSEA:53 res=RedSeaFileRead(dirc->dv,Fs->cur_dir,dirc->mask,&size,&attr);54 break;55 case FSt_FAT32:56 res=FAT32FileRead(dirc->dv,Fs->cur_dir,dirc->mask,&size,&attr);57 break;58 case FSt_ISO9660:59 res=ISO1FileRead(dirc->dv,Fs->cur_dir,dirc->mask,&size,&attr);60 break;61 default:62 PrintErr("File System Not Supported\n");63 }64 }65 DirContextDel(dirc);66 }67 }68 if (!res)69 PrintErr("File not found: \"%s\".\n",filename);70 if (res && attr & RS_ATTR_RESIDENT)71 HashGenericAdd(curname,HTT_FILE,AMAllocIdent(res),size,0,adam_task);72 }73 if (res && attr & RS_ATTR_COMPRESSED) {74 arc=res;75 size=arc->expanded_size;76 res=ExpandBuf(arc);77 Free(arc);78 }79 if (_attr) *_attr=attr;80 if (_size) *_size=size;81 Free(absname);82 Free(altname);83 return res;84}85 86I64 FileWrite(U8 *filename,U8 *fbuf,I64 size,CDate cdt=0,I64 attr=0)87{//Write whole file to disk.88 I64 c=0;89 CHashGeneric *tmph;90 CDirContext *dirc;91 U8 *fbuf2,*absname=FileNameAbs(filename);92 if (dirc=DirContextNew(filename,FALSE,TRUE)) {93 attr=FileAttr(dirc->mask,attr);94 if (attr&RS_ATTR_COMPRESSED) {95 fbuf=CompressBuf(fbuf,size);96 size=fbuf(CArcCompress *)->compressed_size;97 fbuf2=fbuf;98 } else99 fbuf2=NULL;100 if (!cdt) cdt=Now;101 switch (dirc->dv->fs_type) {102 case FSt_REDSEA:103 c=RedSeaFileWrite(dirc->dv,Fs->cur_dir,dirc->mask,fbuf,size,cdt,attr);104 break;105 case FSt_FAT32:106 c=FAT32FileWrite(dirc->dv,Fs->cur_dir,dirc->mask,fbuf,size,cdt,attr);107 break;108 case FSt_ISO9660:109 PrintErr("Not Writable\n");110 break;111 default:112 PrintErr("File System Not Supported\n");113 }114 if (tmph=HashFind(absname,adam_task->hash_table,HTT_FILE)) {115 if (attr & RS_ATTR_RESIDENT) {116 Free(tmph->user_data0);117 tmph->user_data0=AMAllocIdent(fbuf);118 tmph->user_data1=size;119 } else120 HashRemDel(tmph,adam_task->hash_table);121 } else if (attr & RS_ATTR_RESIDENT)122 HashGenericAdd(absname,HTT_FILE,AMAllocIdent(fbuf),size,0,adam_task);123 Free(fbuf2);124 DirContextDel(dirc);125 }126 Free(absname);127 return c;128}129 