hschumann2/TempleOS-Source-Code
0847
1 2U0 BgtDataRead()3{4 CBgtEntry *tmpb;5 CBgtTemplate *tmpt;6 I64 i,cnt,size;7 U8 *b,*ptr;8 I64 max_num;9 U8 **s=StrFileRead(bgt_string_file,&max_num);10 11 MemSet(&b_head,0,sizeof(CBgtEntry));12 QueInit(&b_head);13 b_head.date=Now;14 MemSet(&t_head,0,sizeof(CBgtTemplate));15 QueInit(&t_head);16 t_head.b.date=Now;17 18 if (ptr=b=FileRead(bgt_data_file,&size)) {19 cnt=*ptr(I64 *)++;20 for (i=0;i<cnt;i++) {21 tmpb=CAlloc(sizeof(CBgtEntry));22 MemCpy(&tmpb->start,ptr,BE_SIZE);23 tmpb->credit=StrNew(s[tmpb->credit_idx]);24 tmpb->debit =StrNew(s[tmpb->debit_idx]);25 tmpb->desc =StrNew(s[tmpb->desc_idx]);26 QueIns(tmpb,b_head.last);27 ptr+=BE_SIZE;28 }29 30 cnt=*ptr(I64 *)++;31 for (i=0;i<cnt;i++) {32 tmpt=CAlloc(sizeof(CBgtTemplate));33 MemCpy(&tmpt->start,ptr,BT_SIZE);34 ptr+=BT_SIZE;35 MemCpy(&tmpt->b.start,ptr,BE_SIZE);36 ptr+=BE_SIZE;37 tmpt->b.credit=StrNew(s[tmpt->b.credit_idx]);38 tmpt->b.debit =StrNew(s[tmpt->b.debit_idx]);39 tmpt->b.desc =StrNew(s[tmpt->b.desc_idx]);40 QueIns(tmpt,t_head.last);41 }42 }43 44 StrFileArrDel(s,max_num);45 Free(b);46 47 BgtAcctsUpdate;48}49 50U0 BgtDataWrite()51{52 I64 i,num=0,size,cnt1,cnt2;53 CHashTable *table=HashTableNew(1024);54 CBgtEntry *tmpb;55 CBgtTemplate *tmpt;56 CHashGeneric *tmph;57 U8 *buf,*ptr;58 59 for (i=0;i<=accts_table->mask;i++) {60 tmph=accts_table->body[i];61 while (tmph) {62 StrFileAdd(tmph->str,&num,table); //Cosmetics -- make accts appear first.63 tmph=tmph->next;64 }65 }66 67 tmpb=b_head.next;68 cnt1=0;69 while (tmpb!=&b_head) {70 if (tmpb->type!=BE_TEMPLATE_COPY) {71 tmpb->credit_idx =StrFileAdd(tmpb->credit,&num,table);72 tmpb->debit_idx =StrFileAdd(tmpb->debit,&num,table);73 tmpb->desc_idx =StrFileAdd(tmpb->desc,&num,table);74 cnt1++;75 }76 tmpb=tmpb->next;77 }78 79 tmpt=t_head.next;80 cnt2=0;81 while (tmpt!=&t_head) {82 tmpt->b.credit_idx =StrFileAdd(tmpt->b.credit,&num,table);83 tmpt->b.debit_idx =StrFileAdd(tmpt->b.debit,&num,table);84 tmpt->b.desc_idx =StrFileAdd(tmpt->b.desc,&num,table);85 cnt2++;86 tmpt=tmpt->next;87 }88 StrFileWrite(bgt_string_file,table);89 StrFileDel(table);90 91 size=sizeof(I64)*2+cnt1*BE_SIZE+cnt2*(BT_SIZE+BE_SIZE);92 buf=ptr=MAlloc(size);93 94 MemCpy(ptr,&cnt1,sizeof(I64));95 ptr+=sizeof(I64);96 tmpb=b_head.next;97 while (tmpb!=&b_head) {98 if (tmpb->type!=BE_TEMPLATE_COPY) {99 MemCpy(ptr,&tmpb->start,BE_SIZE);100 ptr+=BE_SIZE;101 }102 tmpb=tmpb->next;103 }104 105 MemCpy(ptr,&cnt2,sizeof(I64));106 ptr+=sizeof(I64);107 tmpt=t_head.next;108 while (tmpt!=&t_head) {109 MemCpy(ptr,&tmpt->start,BT_SIZE);110 ptr+=BT_SIZE;111 MemCpy(ptr,&tmpt->b.start,BE_SIZE);112 ptr+=BE_SIZE;113 tmpt=tmpt->next;114 }115 116 FileWrite(bgt_data_file,buf,size);117 Free(buf);118}119 120U0 BgtDel()121{122 CBgtEntry *tmpb,*tmpb1;123 CBgtTemplate *tmpt,*tmpt1;124 tmpb=b_head.next;125 while (tmpb!=&b_head) {126 tmpb1=tmpb->next;127 BgtEntryDel2(tmpb);128 Free(tmpb);129 tmpb=tmpb1;130 }131 tmpt=t_head.next;132 while (tmpt!=&t_head) {133 tmpt1=tmpt->next;134 BgtEntryDel2(&tmpt->b);135 Free(tmpt);136 tmpt=tmpt1;137 }138 StrFileDel(accts_table);139 accts_table=NULL;140 accts_table_strs=0;141}142 