hschumann2/TempleOS-Source-Code
0832
1 2#help_index "DolDoc/Tree"3 4public Bool DocTreeFind(CDoc *haystack_doc,U8 *needle_path,5 CDocEntry **_tree_entry=NULL,6 CDocEntry **_start_indent=NULL, CDocEntry **_end_indent=NULL)7{//Find tree widget start and end.8 I64 i=0,k=0;9 U8 *st1=StrNew(needle_path),*st2=MAlloc(StrLen(needle_path)+1);10 Bool res=FALSE,unlock_doc=DocLock(haystack_doc);11 CDocEntry *doc_e=haystack_doc->head.next;12 if (_tree_entry) *_tree_entry=haystack_doc;13 if (_start_indent) *_start_indent=haystack_doc;14 if (_end_indent) *_end_indent=haystack_doc;15 while (*st1 && doc_e!=haystack_doc) {16 StrFirstRem(st1,"/",st2);17 if (*st2) {18 while (doc_e!=haystack_doc) {19 if (doc_e->type_u8==DOCT_INDENT)20 i+=doc_e->attr;21 else if (i==k && doc_e->de_flags&DOCEF_TREE &&22 !StrCmp(doc_e->tag+3,st2)) {23 if (*st1)24 break;25 else {26 if (_tree_entry) *_tree_entry=doc_e;27 i=0;28 while (doc_e!=haystack_doc && doc_e->type_u8!=DOCT_INDENT)29 doc_e=doc_e->next;30 if (doc_e!=haystack_doc) {31 i=doc_e->attr;32 if (_start_indent) *_start_indent=doc_e;33 doc_e=doc_e->next;34 while (doc_e!=haystack_doc && i>0) {35 if (doc_e->type_u8==DOCT_INDENT) {36 i+=doc_e->attr;37 if (i<=0) {38 if (_end_indent) *_end_indent=doc_e;39 res=TRUE;40 break;41 }42 }43 doc_e=doc_e->next;44 }45 }46 goto ft_done;47 }48 }49 doc_e=doc_e->next;50 }51 k+=2;52 }53 }54ft_done:55 if (unlock_doc)56 DocUnlock(haystack_doc);57 Free(st1);58 Free(st2);59 return res;60}61 62public Bool DocTreeFFind(U8 *name,U8 *path)63{//Find tree widget in file.64 CDoc *doc=DocRead(name);65 Bool res=DocTreeFind(doc,path);66 DocDel(doc);67 return res;68}69 70public Bool DocTreeMake(CDoc *doc,U8 *path)71{//Make tree widget.72 I64 i=0,j=I64_MIN,k=0;73 U8 *st1=StrNew(path),74 *st2=MAlloc(StrLen(path)+1),75 *st3=StrNew(path);76 Bool res=TRUE,unlock_doc=DocLock(doc);77 CDocEntry *doc_e=doc->head.next;78 doc->cur_entry=doc;79 doc->cur_col=0;80 while (*st1 && doc_e!=doc) {81 StrFirstRem(st1,"/",st2);82 if (*st2) {83 while (doc_e!=doc) {84 if (doc_e->type_u8==DOCT_INDENT) {85 i+=doc_e->attr;86 if (i==j) {87 doc->cur_entry=doc_e;88 doc->cur_col=0;89 goto mt_done;90 }91 } else if (i==k && doc_e->de_flags&DOCEF_TREE &&92 !StrCmp(doc_e->tag+3,st2)) {93 Free(st3);94 st3=StrNew(st1);95 j=i;96 if (!*st1)97 res=FALSE;98 else99 break;100 }101 doc_e=doc_e->next;102 }103 k+=2;104 }105 }106mt_done:107 if (res) {108 while (*st3) {109 StrFirstRem(st3,"/",st2);110 if (*st2) {111 DocPrint(doc,"$TR+C,\"%s\"$\n$ID,2$",st2);112 doc->cur_entry=DocPrint(doc,"$ID,-2$");113 doc->cur_col=0;114 }115 }116 }117 if (unlock_doc)118 DocUnlock(doc);119 Free(st1);120 Free(st2);121 Free(st3);122 return res;123}124 125Bool DocTreeWriteJoin(CDoc *doc,U8 *path,Bool write,U8 *fmt,I64 argc,I64 *argv)126{//Rewrite doc tree branch.127 CDocEntry *tree_branch,*start_indent,*end_indent;128 U8 *buf=StrPrintJoin(NULL,fmt,argc,argv);129 Bool res,unlock_doc=DocLock(doc);130 if (res=DocTreeFind(doc,path,131 &tree_branch,&start_indent,&end_indent)) {132 DocCut(doc,start_indent->next,end_indent->last);133 doc->cur_entry=start_indent->next;134 doc->cur_col=doc->cur_entry->min_col;135 } else136 DocTreeMake(doc,path);137 DocPrint(doc,"%s",buf);138 if (write && DrvIsWritable(*doc->filename.name))139 DocWrite(doc);140 if (unlock_doc)141 DocUnlock(doc);142 Free(buf);143 return res;144}145 146Bool DocTreeAppendJoin(CDoc *doc,U8 *path,Bool write,U8 *fmt,I64 argc,I64 *argv)147{//Append to doc tree branch.148 CDocEntry *tree_branch,*start_indent,*end_indent;149 U8 *buf=StrPrintJoin(NULL,fmt,argc,argv);150 Bool res,unlock_doc=DocLock(doc);151 if (res=DocTreeFind(doc,path,152 &tree_branch,&start_indent,&end_indent)) {153 doc->cur_entry=end_indent;154 doc->cur_col=doc->cur_entry->min_col;155 } else156 DocTreeMake(doc,path);157 DocPrint(doc,"%s",buf);158 if (write && DrvIsWritable(*doc->filename.name))159 DocWrite(doc);160 if (unlock_doc)161 DocUnlock(doc);162 Free(buf);163 return res;164}165 166public Bool DocTreeWrite(CDoc *doc,U8 *path,Bool write=TRUE,U8 *fmt,...)167{//Rewrite doc tree branch.168 return DocTreeWriteJoin(doc,path,write,fmt,argc,argv);169}170 171public Bool DocTreeAppend(CDoc *doc,U8 *path,Bool write=TRUE,U8 *fmt,...)172{//Append to doc tree branch.173 return DocTreeAppendJoin(doc,path,write,fmt,argc,argv);174}175 176public Bool DocTreeFWrite(U8 *name,U8 *path,U8 *fmt,...)177{//Rewrite doc tree branch in file.178 CDoc *doc=DocRead(name);179 Bool res=DocTreeWriteJoin(doc,path,TRUE,fmt,argc,argv);180 DocDel(doc);181 return res;182}183 184public Bool DocTreeFAppend(U8 *name,U8 *path,U8 *fmt,...)185{//Append to doc tree branch in file.186 CDoc *doc=DocRead(name);187 Bool res=DocTreeAppendJoin(doc,path,TRUE,fmt,argc,argv);188 DocDel(doc);189 return res;190}191 192#help_index "DolDoc/Compiler;Compiler"193public I64 ExeDoc(CDoc *doc,I64 ccf_flags=0)194{//JIT Compile and execute a document.195 I64 res;196 Bool okay=TRUE,unlock_doc=DocLock(doc);197 CCmpCtrl *cc=CmpCtrlNew(,ccf_flags|CCF_DONT_FREE_BUF);198 if (Fs->last_cc!=&Fs->next_cc)199 cc->opts=Fs->last_cc->opts;200 QueIns(cc,Fs->last_cc);201 LexAttachDoc(cc,,doc);202 try {203 Lex(cc);204 res=ExeCmdLine(cc);205 } catch {206 if (Fs->except_ch=='Compiler' || Fs->except_ch=='Break') {207 Fs->catch_except=TRUE;208 okay=FALSE;209 res=0;210 }211 }212 QueRem(cc);213 if (okay)214 CmpCtrlDel(cc); //TODO: can crash215 if (unlock_doc)216 DocUnlock(doc);217 return res;218}219 220#help_index "DolDoc/Tree;DolDoc/Compiler;Compiler"221public I64 DocTreeExe(CDoc *doc,U8 *path)222{//Execute doc tree branch.223 CDoc *doc2;224 Bool unlock_doc=DocLock(doc);225 CDocEntry *tree_branch,*start_indent,*end_indent;226 I64 res=0;227 if (DocTreeFind(doc,path,&tree_branch,&start_indent,&end_indent)) {228 doc2=DocCopy(doc,tree_branch,end_indent);229 res=ExeDoc(doc2);230 DocDel(doc2);231 }232 if (unlock_doc)233 DocUnlock(doc);234 return res;235}236 237public I64 DocTreeFExe(U8 *name,U8 *path)238{//Execute doc tree branch in file.239 I64 res;240 CDoc *doc=DocRead(name);241 res=DocTreeExe(doc,path);242 DocDel(doc);243 return res;244}245 