hschumann2/TempleOS-Source-Code
0847
1 2#help_index "Cmd Line (Typically)"3 4I64 DEPtrCompare(CDocEntry **e1,CDocEntry **e2)5{6 return StrCmp((*e1)->tag,(*e2)->tag);7}8 9public I64 Sort(U8 *_in_name,U8 *_out_name=NULL,10 I64 entry_lines=1,Bool unique=FALSE)11{//Sort lines of a text file. Removes blank lines.12 U8 *in_name,*out_name,*st;13 CDoc *doc;14 CDocEntry *doc_e,*doc_e1,**a;15 I64 i,j,cnt=0,res;16 17 if (!_in_name) return 0;18 in_name=ExtDft(_in_name,"DD.Z");19 if (_out_name)20 out_name=ExtDft(_out_name,"DD.Z");21 else22 out_name=StrNew(in_name);23 24 doc=DocRead(in_name,DOCF_PLAIN_TEXT_TABS|DOCF_NO_CURSOR);25 doc_e=doc->head.next;26 while (doc_e!=doc) {27 if (doc_e->type_u8==DOCT_TEXT)28 cnt++;29 doc_e=doc_e->next;30 }31 a=MAlloc(cnt*sizeof(CDocEntry *));32 doc_e=doc->head.next;33 i=0;34 while (doc_e!=doc) {35 doc_e1=doc_e->next;36 if (doc_e->type_u8==DOCT_TEXT) {37 QueRem(doc_e);38 a[i++]=doc_e;39 } else40 DocEntryDel(doc,doc_e);41 doc_e=doc_e1;42 }43 QSort(a,cnt/entry_lines,entry_lines*sizeof(CDocEntry *),&DEPtrCompare);44 45 res=0;46 st=NULL;47 for (i=0;i<cnt;) {48 if (!unique || !st || StrCmp(a[i]->tag,st)) {49 st=a[i]->tag;50 for (j=0;j<entry_lines && i<cnt;j++,i++) {51 QueIns(a[i],doc->head.last);52 doc->cur_entry=&doc->head;53 doc->cur_col=0;54 DocPrint(doc,"\n");55 }56 res++;57 } else58 for (j=0;j<entry_lines && i<cnt;j++,i++) {59 QueIns(a[i],doc->head.last);60 DocEntryDel(doc,a[i]);61 }62 }63 StrCpy(doc->filename.name,out_name);64 DocWrite(doc);65 66 Free(a);67 DocDel(doc);68 Free(in_name);69 Free(out_name);70 return res; //Num Entries71}72 73I64 DocWordsFile(CDoc *doc_out=NULL,U8 *filename,U32 *char_bmp)74{75 U8 *ptr,*ptr2;76 I64 res=0,ch;77 CDoc *doc_in=DocRead(filename);78 CDocEntry *doc_e=doc_in->head.next;79 while (doc_e!=doc_in) {80 if (doc_e->de_flags & DOCEF_TAG) {81 ptr=doc_e->tag;82 while (*ptr) {83 while (*ptr && !Bt(char_bmp,*ptr))84 ptr++;85 86 ptr2=ptr;87 while (*ptr && Bt(char_bmp,*ptr))88 ptr++;89 90 ch=*ptr;91 *ptr=0;92 if (*ptr2) {93 DocPrint(doc_out,"%s\n",ptr2);94 res++;95 }96 *ptr=ch;97 }98 }99 doc_e=doc_e->next;100 }101 DocDel(doc_in);102 return res;103}104public I64 Words(U8 *files_find_mask="*",U32 *char_bmp=char_bmp_alpha,105 U8 *fu_flags=NULL)106{//Break file into list of not-unique words.107 I64 fuf_flags=0,res=0;108 CDoc *doc_out=DocNew;109 CDirEntry *tmpde,*tmpde1;110 ScanFlags(&fuf_flags,Define("ST_FILE_UTIL_FLAGS"),"+r+f+F+T");111 ScanFlags(&fuf_flags,Define("ST_FILE_UTIL_FLAGS"),fu_flags);112 tmpde=tmpde1=FilesFind(files_find_mask,fuf_flags);113 while (tmpde) {114 res+=DocWordsFile(doc_out,tmpde->full_name,char_bmp);115 tmpde=tmpde->next;116 }117 DirTreeDel(tmpde1);118 DocInsDoc(NULL,doc_out);119 DocDel(doc_out);120 return res;121}122 123I64 LongLinesFile(U8 *filename,I64 cols)124{125 I64 res=0;126 CDoc *doc=DocRead(filename);127 CDocEntry *doc_e=doc->head.next;128 while (doc_e!=doc) {129 if (doc_e->type_u8==DOCT_NEW_LINE && doc_e->x>=cols+1)130 res++;131 doc_e=doc_e->next;132 }133 DocDel(doc);134 if (res) {135 "%04d ",res;136 PutFileLink(filename);137 '\n';138 }139 return res;140}141public I64 LongLines(U8 *files_find_mask="*",I64 cols=80,U8 *fu_flags=NULL)142{//Report files with lines of too many cols.143 I64 res=0,fuf_flags=0;144 CDirEntry *tmpde,*tmpde1;145 ScanFlags(&fuf_flags,Define("ST_FILE_UTIL_FLAGS"),"+r+f+F+S");146 ScanFlags(&fuf_flags,Define("ST_FILE_UTIL_FLAGS"),fu_flags);147 tmpde=tmpde1=FilesFind(files_find_mask,fuf_flags);148 while (tmpde) {149 if (LongLinesFile(tmpde->full_name,cols))150 res++;151 tmpde=tmpde->next;152 }153 DirTreeDel(tmpde1);154 return res;155}156 157U0 SUFile(U8 *filename,I64 suf_flags,F64 indent_scale_factor)158{//String utility on a single file159//See SU Flags160 U8 *dst;161 Bool chged=FALSE;162 I64 reduced=0;163 CDoc *doc=DocRead(filename,DOCF_PLAIN_TEXT_TABS|DOCF_NO_CURSOR);164 CDocEntry *doc_e=doc->head.next;165 while (doc_e!=doc) {166 if (doc_e->type_u8==DOCT_TEXT) {167 dst=MStrUtil(doc_e->tag,suf_flags,indent_scale_factor);168 if (StrCmp(dst,doc_e->tag)) {169 reduced+=StrLen(doc_e->tag)-StrLen(dst);170 chged=TRUE;171 Free(doc_e->tag);172 doc_e->tag=dst;173 } else174 Free(dst);175 }176 doc_e=doc_e->next;177 }178 if (chged) {179 "Reduced %s by %d chars\n",filename,reduced;180 DocWrite(doc);181 }182 DocDel(doc);183}184public U0 SU(U8 *files_find_mask,I64 suf_flags,U8 *fu_flags=NULL,185 F64 indent_scale_factor=0)186{//Apply StrUtil() on files187//You can convert spaces to tabs, for example,188 //or removing trailing spaces on lines.189 //See SUF Flags.190 I64 fuf_flags=0;191 CDirEntry *tmpde,*tmpde1;192 ScanFlags(&fuf_flags,Define("ST_FILE_UTIL_FLAGS"),"+f+F+T");193 ScanFlags(&fuf_flags,Define("ST_FILE_UTIL_FLAGS"),fu_flags);194 tmpde=tmpde1=FilesFind(files_find_mask,fuf_flags);195 while (tmpde) {196 SUFile(tmpde->full_name,suf_flags,indent_scale_factor);197 tmpde=tmpde->next;198 }199 DirTreeDel(tmpde1);200}201 202public U0 S2T(U8 *files_find_mask,U8 *fu_flags=NULL)203{//Spaces to tabs.204//Use "Hard Space" (SHIFT-SPACE) for spaces205 //in string consts in your code.206 SU(files_find_mask,SUF_S2T|SUF_REM_TRAILING,fu_flags);207}208 