*/}}

la_util.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  1. /*
  2. * LaGUI: A graphical application framework.
  3. * Copyright (C) 2022-2023 Wu Yiming
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define _CRT_SEQURE_NO_WARNINGS
  19. #include "la_util.h"
  20. #include "la_interface.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. laSafeStringCollection SSC;
  24. extern LA MAIN;
  25. #define BYTE unsigned char
  26. uint32_t laToUnicode(const unsigned char* ch, int* advance){
  27. if((*ch)<0x80) { *advance=1; return *ch; }
  28. uint32_t u=0;
  29. if(((*ch)>>5)==0x06){ *advance=2; u|=((*(ch+1))&0x3f)|((*(ch)&0x1f)<<6); return u; }
  30. if(((*ch)>>4)==0x0e){ *advance=3; u|=((*(ch+2))&0x3f)|((*(ch+1)&0x3f)<<6)|((*(ch)&0x0f)<<12); return u; }
  31. if(((*ch)>>3)==0x1e){ *advance=4; u|=((*(ch+3))&0x3f)|((*(ch+2)&0x3f)<<6)|((*(ch+1)&0x3f)<<12)|((*(ch)&0x07)<<18); return u; }
  32. *advance=1; return '?';
  33. }
  34. int laToUTF8(const uint32_t ch, char* out, char** next){
  35. if(ch>=0x10000){ out[0]=0xf0|(ch>>18); out[1]=0x80|(0x3f&(ch>>12)); out[2]=0x80|(0x3f&(ch>>6)); out[3]=0x80|(0x3f&ch); (*next)+=4;}
  36. elif(ch>=0x800){ out[0]=0xe0|(ch>>12); out[1]=0x80|(0x3f&(ch>>6)); out[2]=0x80|(0x3f&ch); (*next)+=3;}
  37. elif(ch>=0x80){ out[0]=0xc0|(ch>>6); out[1]=0x80|(0x3f&ch); (*next)+=2;}
  38. else { if(!ch){return 0;} out[0]=ch&0x7f; (*next)++;} return 1;
  39. }
  40. int strToUnicode(uint32_t* target, unsigned char* const src){
  41. uint32_t UC,adv,i=0; unsigned char* source=src; while(target[i]=laToUnicode(source, &adv)) { source+=adv; i++; } target[i]=0; return i;
  42. }
  43. int strToUTF8Lim(unsigned char* target, uint32_t* const src, int count){
  44. uint32_t* source=src; unsigned char* out=target; int i=0; while(laToUTF8(*source, out, &out)){ source++; i++; if(i>=count) break; } *out=0; return out-target;
  45. }
  46. int strToUTF8(unsigned char* target, uint32_t* const src){
  47. strToUTF8Lim(target,src,INT_MAX);
  48. }
  49. int strlenU(uint32_t* str){ int i=0; while(str[i]!=0) i++; return i; }
  50. int strcpyU(uint32_t* target, uint32_t* const source ){ int i=0; while(source[i]!=0){ target[i]=source[i]; i++; } target[i]=0; }
  51. int strcatU(uint32_t* target, uint32_t* const source ){ int i=0,tl=strlenU(target); while(source[i]!=0){ target[i+tl]=source[i]; i++; } target[i+tl]=0; }
  52. struct tm *laGetFullTime(){
  53. time_t t = time(0);
  54. return localtime(&t);
  55. }
  56. void laRecordTime(laTimeRecorder *tr){
  57. clock_gettime(CLOCK_REALTIME, &tr->ts);
  58. }
  59. real laTimeElapsedSecondsf(laTimeRecorder *End, laTimeRecorder *Begin){
  60. real sec=End->ts.tv_sec-Begin->ts.tv_sec; sec+=((End->ts.tv_nsec-Begin->ts.tv_nsec)/1e9);
  61. return sec;
  62. }
  63. void laSetAuthorInfo(char *Name, char *CopyrightString){
  64. strSafeSet(&MAIN.Author.Name, Name);
  65. strSafeSet(&MAIN.Author.CopyrightString, CopyrightString);
  66. }
  67. void memCreateNUID(char* buf,laMemNodeHyper* hyper){
  68. sprintf(buf, "%08X-%hd%02hd%02hd%02hd%02hd%02hd", hyper, LA_HYPER_CREATED_TIME(hyper));
  69. }
  70. void memHyperInfo(laPropPack* pp, char* buf){
  71. int level=0;void* head=0;
  72. laMemNodeHyper* hi; laMemNode* mn;
  73. int a=0, count=0, pc;
  74. laProp* p=pp->LastPs->p;
  75. laPropContainer* c=p->Container;
  76. if(c->OtherAlloc){
  77. count=lstCountElements(&c->LocalUsers);
  78. }else{
  79. head=memGetHead(pp->LastPs->UseInstance, &level);
  80. if(!level){
  81. sprintf(buf,"Not HyperData.\n");
  82. }elif(level==1){
  83. mn=head; count=lstCountElements(&mn->Users);
  84. }elif(level==2){
  85. hi=head; count=lstCountElements(&hi->Users);
  86. }
  87. }
  88. a=sprintf(buf,"HyperData:\n\tProperty:%s\n\tContainer:%s (%d users)\n",
  89. pp->LastPs->p->Identifier, pp->LastPs->p->Container->Identifier, count);
  90. if(level==2){
  91. sprintf(buf+a,"\tCreated:%hd-%02hd-%02hd %02hd:%02hd:%02hd\n",LA_HYPER_CREATED_TIME(hi));
  92. }
  93. }
  94. void memMakeHyperData(laMemNodeHyper* hi){
  95. struct tm *time;
  96. hi->Modified = 1;
  97. time = laGetFullTime();
  98. //hi->CreatedBy = &MAIN.Author;
  99. hi->TimeCreated.Year = time->tm_year + 1900;
  100. hi->TimeCreated.Month = time->tm_mon + 1;
  101. hi->TimeCreated.Day = time->tm_mday;
  102. hi->TimeCreated.Hour = time->tm_hour;
  103. hi->TimeCreated.Minute = time->tm_min;
  104. hi->TimeCreated.Second = time->tm_sec;
  105. //memcpy(&hi->TimeModified, &hi->TimeCreated, sizeof(laTimeInfo));
  106. memCreateNUID(hi->NUID.String,hi);
  107. }
  108. void memMarkClean(void* HyperUserMem){
  109. int hyper=0;
  110. laMemNodeHyper* h = memGetHead(HyperUserMem, &hyper);
  111. if(hyper!=2) return;
  112. h->Modified=0;
  113. }
  114. void nutFreeMem(void **ptr){
  115. //free_total+=1;
  116. if (!*ptr) return;
  117. free(*ptr);
  118. *ptr = 0;
  119. }
  120. int nutFloatCompare(real l, real r){
  121. return (l > r - 0.00005 && l < r + 0.00005);
  122. }
  123. int nutSameAddress(void *l, void *r){
  124. return (l == r);
  125. }
  126. //===================================================================[list]
  127. void* arrElement(void* head, int i, int size){
  128. return ((char*)head+size*i);
  129. }
  130. int arrEnsureLength(void** head, int next, int* max, size_t ElementSize){
  131. int UseMax=*max; int nocopy=(!UseMax);
  132. if(next>=UseMax){
  133. if(!UseMax){ UseMax=50; }
  134. int AllocMax=next>(UseMax*2)?(next+16):(UseMax*2);
  135. void* data = CreateNew_Size(ElementSize* AllocMax);
  136. if(((*head) || next)&&(!nocopy)){ memcpy(data, *head, ElementSize*UseMax); }
  137. if(*head) free(*head);
  138. *head=data;
  139. *max=AllocMax;
  140. return 1;
  141. }
  142. return 0;
  143. }
  144. int arrInitLength(void** head, int max, int* pmax, size_t ElementSize){
  145. if(*head){ free(head); }
  146. *head=CreateNew_Size(ElementSize*max);
  147. *pmax=max;
  148. }
  149. void arrFree(void** head, int* max){ free(*head); *head=0; *max=0; }
  150. void lstPushSingle(void **Head, laListSingle *Item){
  151. Item->pNext = *Head;
  152. *Head = Item;
  153. }
  154. void *lstPopSingle(void **Head, laListSingle *Item){
  155. *Head = ((laListSingle *)(*Head))->pNext;
  156. Item->pNext = 0;
  157. return *Head;
  158. }
  159. int lstCountElements(laListHandle* Handle){
  160. int count=0; if(!Handle) return 0;
  161. for(laListItem* i=Handle->pFirst;i;i=i->pNext){count++;}
  162. return count;
  163. }
  164. void lstAppendItem(laListHandle* Handle, void* Item){
  165. laListItem* li = Item;
  166. li->pNext = li->pPrev = 0;
  167. if (!Handle->pFirst) Handle->pFirst = Item;
  168. if (Handle->pLast) ((laListItem*)Handle->pLast)->pNext = li;
  169. li->pPrev = Handle->pLast;
  170. li->pNext = 0;
  171. Handle->pLast = li;
  172. };
  173. void lstPushItem(laListHandle* Handle, void* Item){
  174. laListItem* li = Item;
  175. li->pNext = li->pPrev = 0;
  176. if (!Handle->pLast) Handle->pLast = Item;
  177. li->pNext = Handle->pFirst;
  178. if (Handle->pFirst) ((laListItem*)Handle->pFirst)->pPrev = Item;
  179. Handle->pFirst = li;
  180. };
  181. void* lstPopItem(laListHandle* Handle){
  182. laListItem* popitem;
  183. laListItem* next;
  184. if (!Handle->pFirst) return 0;
  185. popitem = Handle->pFirst;
  186. next = ((laListItem*)Handle->pFirst)->pNext;
  187. if (!next){
  188. Handle->pFirst = 0;
  189. Handle->pLast = 0;
  190. }else{
  191. Handle->pFirst = next;
  192. if (next) next->pPrev = 0;
  193. };
  194. popitem->pNext=popitem->pPrev=0;
  195. return popitem;
  196. };
  197. int lstHaveItemInList(laListHandle *Handle){
  198. if (Handle->pFirst) return 1;
  199. return 0;
  200. };
  201. void lstAppendItem2(laListHandle *Handle, void *Item){
  202. laListItem2 *li = Item;
  203. li->pNext = li->pPrev = 0;
  204. if (!Handle->pFirst) Handle->pFirst = Item;
  205. if (Handle->pLast) ((laListItem2 *)Handle->pLast)->pNext = li;
  206. li->pPrev = Handle->pLast;
  207. li->pNext = 0;
  208. Handle->pLast = li;
  209. };
  210. void lstPushItem2(laListHandle *Handle, void *Item){
  211. laListItem2 *li = Item;
  212. li->pNext = li->pPrev = 0;
  213. if (!Handle->pLast) Handle->pLast = Item;
  214. li->pNext = Handle->pFirst;
  215. if (Handle->pFirst) ((laListItem2 *)Handle->pFirst)->pPrev = Item;
  216. Handle->pFirst = li;
  217. };
  218. void *lstPopItem2(laListHandle *Handle){
  219. void *popitem;
  220. laListItem2 *next;
  221. if (!Handle->pFirst) return 0;
  222. popitem = Handle->pFirst;
  223. next = ((laListItem2 *)Handle->pFirst)->pNext;
  224. if (!next){
  225. Handle->pFirst = 0;
  226. Handle->pLast = 0;
  227. }else{
  228. Handle->pFirst = next;
  229. if (next) next->pPrev = 0;
  230. };
  231. return popitem;
  232. };
  233. void lstAppendItem3(laListHandle *Handle, void *Item){
  234. laListItem3 *li = Item;
  235. li->pNext = li->pPrev = 0;
  236. if (!Handle->pFirst) Handle->pFirst = Item;
  237. if (Handle->pLast) ((laListItem3 *)Handle->pLast)->pNext = li;
  238. li->pPrev = Handle->pLast;
  239. li->pNext = 0;
  240. Handle->pLast = li;
  241. };
  242. void lstPushItem3(laListHandle *Handle, void *Item){
  243. laListItem3 *li = Item;
  244. li->pNext = li->pPrev = 0;
  245. if (!Handle->pLast) Handle->pLast = Item;
  246. li->pNext = Handle->pFirst;
  247. if (Handle->pFirst) ((laListItem3 *)Handle->pFirst)->pPrev = Item;
  248. Handle->pFirst = li;
  249. };
  250. void *lstPopItem3(laListHandle *Handle){
  251. void *popitem;
  252. laListItem3 *next;
  253. if (!Handle->pFirst) return 0;
  254. popitem = Handle->pFirst;
  255. next = ((laListItem3 *)Handle->pFirst)->pNext;
  256. if (!next){
  257. Handle->pFirst = 0;
  258. Handle->pLast = 0;
  259. }else{
  260. Handle->pFirst = next;
  261. if (next) next->pPrev = 0;
  262. };
  263. return popitem;
  264. };
  265. void *lstGetTop(laListHandle *Handle){
  266. return Handle->pFirst;
  267. };
  268. int lstRemoveItem(laListHandle* Handle, laListItem* li) {
  269. if (!li->pPrev && Handle->pFirst != li) return 0;
  270. if (!li->pPrev) Handle->pFirst = li->pNext;
  271. else
  272. ((laListItem*)li->pPrev)->pNext = li->pNext;
  273. if (!li->pNext) Handle->pLast = li->pPrev;
  274. else
  275. ((laListItem*)li->pNext)->pPrev = li->pPrev;
  276. li->pNext = li->pPrev = 0;
  277. }
  278. int lstRemoveItem2(laListHandle *Handle, laListItem2 *li){
  279. if (!li->pPrev) Handle->pFirst = li->pNext;
  280. else
  281. ((laListItem2 *)li->pPrev)->pNext = li->pNext;
  282. if (!li->pNext) Handle->pLast = li->pPrev;
  283. else
  284. ((laListItem2 *)li->pNext)->pPrev = li->pPrev;
  285. li->pNext = li->pPrev = 0;
  286. };
  287. int lstRemoveItem3(laListHandle *Handle, laListItem2 *li){
  288. if (!li->pPrev) Handle->pFirst = li->pNext;
  289. else
  290. ((laListItem3 *)li->pPrev)->pNext = li->pNext;
  291. if (!li->pNext) Handle->pLast = li->pPrev;
  292. else
  293. ((laListItem3 *)li->pNext)->pPrev = li->pPrev;
  294. li->pNext = li->pPrev = 0;
  295. };
  296. int lstRemoveSegment(laListHandle *Handle, laListItem *Begin, laListItem *End){
  297. if (!Begin->pPrev) Handle->pFirst = End->pNext;
  298. else
  299. ((laListItem *)Begin->pPrev)->pNext = End->pNext;
  300. if (!End->pNext) Handle->pLast = Begin->pPrev;
  301. else
  302. ((laListItem *)End->pNext)->pPrev = Begin->pPrev;
  303. End->pNext = Begin->pPrev = 0;
  304. };
  305. void lstInsertItemBefore(laListHandle *Handle, laListItem *toIns, laListItem *pivot){
  306. if (!pivot){
  307. lstPushItem(Handle, toIns);
  308. return;
  309. }
  310. if (pivot->pPrev){
  311. ((laListItem *)pivot->pPrev)->pNext = toIns;
  312. toIns->pPrev = pivot->pPrev;
  313. }else{
  314. Handle->pFirst = toIns;
  315. }
  316. toIns->pNext = pivot;
  317. pivot->pPrev = toIns;
  318. };
  319. void lstInsertItemAfter(laListHandle *Handle, laListItem *toIns, laListItem *pivot){
  320. if (!pivot){
  321. lstAppendItem(Handle, toIns);
  322. return;
  323. }
  324. if (pivot->pNext){
  325. ((laListItem *)pivot->pNext)->pPrev = toIns;
  326. toIns->pNext = pivot->pNext;
  327. }else{
  328. Handle->pLast = toIns;
  329. }
  330. toIns->pPrev = pivot;
  331. pivot->pNext = toIns;
  332. }
  333. void lstInsertSegmentBefore(laListHandle *Handle, laListItem *Begin, laListItem *End, laListItem *pivot){
  334. if (pivot->pPrev){
  335. ((laListItem *)pivot->pPrev)->pNext = Begin;
  336. Begin->pPrev = pivot->pPrev;
  337. }else{
  338. Handle->pFirst = Begin;
  339. }
  340. End->pNext = pivot;
  341. pivot->pPrev = End;
  342. };
  343. void lstInsertSegmentAfter(laListHandle *Handle, laListItem *Begin, laListItem *End, laListItem *pivot){
  344. if (pivot->pNext){
  345. ((laListItem *)pivot->pNext)->pPrev = End;
  346. End->pNext = pivot->pNext;
  347. }else{
  348. Handle->pLast = End;
  349. }
  350. Begin->pPrev = pivot;
  351. pivot->pNext = Begin;
  352. }
  353. void *lstAppendPointerOnly(laListHandle *h, void *p){
  354. laListItemPointer *lip;
  355. if (!h) return 0;
  356. lip = CreateNew(laListItemPointer);
  357. lip->p = p;
  358. lstAppendItem(h, lip);
  359. return lip;
  360. }
  361. void *lstAppendPointerSizedOnly(laListHandle *h, void *p, int size){
  362. laListItemPointer *lip;
  363. if (!h) return 0;
  364. lip = calloc(1, size);
  365. lip->p = p;
  366. lstAppendItem(h, lip);
  367. return lip;
  368. }
  369. void *lstPushPointerOnly(laListHandle *h, void *p){
  370. laListItemPointer *lip = 0;
  371. if (!h) return 0;
  372. lip = CreateNew(laListItemPointer);
  373. lip->p = p;
  374. lstPushItem(h, lip);
  375. return lip;
  376. }
  377. void *lstPushPointerSizedOnly(laListHandle *h, void *p, int size){
  378. laListItemPointer *lip = 0;
  379. if (!h) return 0;
  380. lip = calloc(1, size);
  381. lip->p = p;
  382. lstPushItem(h, lip);
  383. return lip;
  384. }
  385. void lstReverse(laListHandle* h){
  386. laListHandle l={0}; void* i; while(i=lstPopItem(h)){ lstPushItem(&l,i); }
  387. memcpy(h,&l,sizeof(laListHandle));
  388. }
  389. int lstHasPointer(laListHandle* h, void *p){
  390. laListItemPointer *i; for (i = h->pFirst; i; i = i->pNext){
  391. if (i->p == p){return 1;}
  392. } return 0;
  393. }
  394. void *lstAppendPointer(laListHandle *h, void *p){
  395. laListItemPointer *lip;
  396. if (!h) return 0;
  397. lip = memAcquireSimple(sizeof(laListItemPointer));
  398. lip->p = p;
  399. lstAppendItem(h, lip);
  400. return lip;
  401. }
  402. void *lstAppendPointerSized(laListHandle *h, void *p, int size){
  403. laListItemPointer *lip;
  404. if (!h) return 0;
  405. lip = memAcquireSimple(size);
  406. lip->p = p;
  407. lstAppendItem(h, lip);
  408. return lip;
  409. }
  410. void *lstPushPointer(laListHandle *h, void *p){
  411. laListItemPointer *lip = 0;
  412. if (!h) return 0;
  413. lip = memAcquireSimple(sizeof(laListItemPointer));
  414. lip->p = p;
  415. lstPushItem(h, lip);
  416. return lip;
  417. }
  418. void *lstPushPointerSized(laListHandle *h, void *p, int size){
  419. laListItemPointer *lip = 0;
  420. if (!h) return 0;
  421. lip = memAcquireSimple(size);
  422. lip->p = p;
  423. lstPushItem(h, lip);
  424. return lip;
  425. }
  426. void *lstAppendPointerStatic(laListHandle *h, laStaticMemoryPool *smp, void *p){
  427. laListItemPointer *lip;
  428. if (!h) return 0;
  429. lip = memStaticAcquire(smp, sizeof(laListItemPointer));
  430. lip->p = p;
  431. lstAppendItem(h, lip);
  432. return lip;
  433. }
  434. void *lstAppendPointerStaticSized(laListHandle *h, laStaticMemoryPool *smp, void *p, int size){
  435. laListItemPointer *lip;
  436. if (!h) return 0;
  437. lip = memStaticAcquire(smp, size);
  438. lip->p = p;
  439. lstAppendItem(h, lip);
  440. return lip;
  441. }
  442. void *lstPushPointerStatic(laListHandle *h, laStaticMemoryPool *smp, void *p){
  443. laListItemPointer *lip = 0;
  444. if (!h) return 0;
  445. lip = memStaticAcquire(smp, sizeof(laListItemPointer));
  446. lip->p = p;
  447. lstPushItem(h, lip);
  448. return lip;
  449. }
  450. void *lstPushPointerStaticSized(laListHandle *h, laStaticMemoryPool *smp, void *p, int size){
  451. laListItemPointer *lip = 0;
  452. if (!h) return 0;
  453. lip = memStaticAcquire(smp, size);
  454. lip->p = p;
  455. lstPushItem(h, lip);
  456. return lip;
  457. }
  458. void *lstPopPointerOnly(laListHandle *h){
  459. laListItemPointer *lip;
  460. void *rev = 0;
  461. if (!h) return 0;
  462. lip = lstPopItem(h);
  463. rev = lip ? lip->p : 0;
  464. FreeMem(lip);
  465. return rev;
  466. }
  467. void lstRemovePointerItemOnly(laListHandle *h, laListItemPointer *lip){
  468. lstRemoveItem(h, lip);
  469. FreeMem(lip);
  470. }
  471. void lstRemovePointerOnly(laListHandle *h, void *p){
  472. laListItemPointer *i;
  473. for (i = h->pFirst; i; i = i->pNext){
  474. if (i->p == p){
  475. lstRemovePointerItem(h, i);
  476. break;
  477. }
  478. }
  479. }
  480. void lstClearPointerOnly(laListHandle *h){
  481. laListItemPointer *i;
  482. while (h && h->pFirst){
  483. lstPopPointer(h);
  484. }
  485. }
  486. void lstGeneratePointerListOnly(laListHandle *from1, laListHandle *from2, laListHandle *to){
  487. laListItemPointer *lip = from2 ? from2->pLast : 0;
  488. while (lip){
  489. lstPushPointer(to, lip->p);
  490. lip = lip->pPrev;
  491. }
  492. lip = from1 ? from1->pLast : 0;
  493. while (lip){
  494. lstPushPointer(to, lip->p);
  495. lip = lip->pPrev;
  496. }
  497. }
  498. void *lstPopPointer(laListHandle *h){
  499. laListItemPointer *lip;
  500. void *rev = 0;
  501. if (!h) return 0;
  502. lip = lstPopItem(h);
  503. rev = lip ? lip->p : 0;
  504. memFree(lip);
  505. return rev;
  506. }
  507. void lstRemovePointerItem(laListHandle *h, laListItemPointer *lip){
  508. lstRemoveItem(h, lip);
  509. memFree(lip);
  510. }
  511. void lstRemovePointer(laListHandle *h, void *p){
  512. laListItemPointer *i;
  513. for (i = h->pFirst; i; i = i->pNext){
  514. if (i->p == p){ lstRemovePointerItem(h, i); break; }
  515. }
  516. }
  517. void lstRemovePointerLeave(laListHandle *h, void *p){
  518. laListItemPointer *i;
  519. for (i = h->pFirst; i; i = i->pNext){
  520. if (i->p == p){ lstRemoveItem(h, i); memLeave(i); break; }
  521. }
  522. }
  523. void lstClearPointer(laListHandle *h){
  524. laListItemPointer *i;
  525. while (h && h->pFirst){
  526. lstPopPointer(h);
  527. }
  528. }
  529. void lstGeneratePointerList(laListHandle *from1, laListHandle *from2, laListHandle *to){
  530. laListItemPointer *lip = from2 ? from2->pLast : 0;
  531. while (lip){
  532. lstPushPointer(to, lip->p);
  533. lip = lip->pPrev;
  534. }
  535. lip = from1 ? from1->pLast : 0;
  536. while (lip){
  537. lstPushPointer(to, lip->p);
  538. lip = lip->pPrev;
  539. }
  540. }
  541. void *lstAppendPointerStaticPool(laStaticMemoryPool *mph, laListHandle *h, void *p){
  542. laListItemPointer *lip;
  543. if (!h) return 0;
  544. lip = memStaticAcquire(mph, sizeof(laListItemPointer));
  545. lip->p = p;
  546. lstAppendItem(h, lip);
  547. return lip;
  548. }
  549. void *lstPopPointerLeave(laListHandle *h){
  550. laListItemPointer *lip;
  551. void *rev = 0;
  552. if (!h) return 0;
  553. lip = lstPopItem(h); memLeave(lip);
  554. rev = lip ? lip->p : 0;
  555. return rev;
  556. }
  557. void lstRemovePointerItemNoFree(laListHandle *h, laListItemPointer *lip){
  558. lstRemoveItem(h, lip);
  559. }
  560. void lstCopyHandle(laListHandle *target, laListHandle *src){
  561. target->pFirst = src->pFirst;
  562. target->pLast = src->pLast;
  563. };
  564. void lstClearHandle(laListHandle *h){
  565. h->pFirst = 0;
  566. h->pLast = 0;
  567. }
  568. void lstClearPrevNext(laListItem *li){
  569. li->pNext = 0;
  570. li->pPrev = 0;
  571. }
  572. void lstMoveUp(laListHandle *h, laListItem *li){
  573. void *pprev = li->pPrev ? ((laListItem *)li->pPrev)->pPrev : 0;
  574. if (!h || !li) return;
  575. if (li == h->pFirst) return;
  576. else{
  577. if (li == h->pLast) h->pLast = li->pPrev;
  578. ((laListItem *)li->pPrev)->pNext = li->pNext;
  579. ((laListItem *)li->pPrev)->pPrev = li;
  580. if (li->pNext) ((laListItem *)li->pNext)->pPrev = li->pPrev;
  581. li->pNext = li->pPrev;
  582. li->pPrev = pprev;
  583. if (pprev) ((laListItem *)pprev)->pNext = li;
  584. }
  585. if (!li->pPrev) h->pFirst = li;
  586. }
  587. void lstMoveDown(laListHandle *h, laListItem *li){
  588. void *ppnext = li->pNext ? ((laListItem *)li->pNext)->pNext : 0;
  589. if (!h || !li) return;
  590. if (li == h->pLast) return;
  591. else{
  592. if (li == h->pFirst) h->pFirst = li->pNext;
  593. ((laListItem *)li->pNext)->pPrev = li->pPrev;
  594. ((laListItem *)li->pNext)->pNext = li;
  595. if (li->pPrev) ((laListItem *)li->pPrev)->pNext = li->pNext;
  596. li->pPrev = li->pNext;
  597. li->pNext = ppnext;
  598. if (ppnext) ((laListItem *)ppnext)->pPrev = li;
  599. }
  600. if (!li->pNext) h->pLast = li;
  601. }
  602. void lstForAllItemsDo(laListDoFunc func, laListHandle *hList){
  603. laListItem *it = hList->pFirst;
  604. for (; it; it = it->pNext){
  605. func(it);
  606. }
  607. };
  608. void lstForAllItemsDoLNRR(laListNonRecursiveDoFunc func, laListHandle *hList){
  609. laListItem *it = hList->pFirst;
  610. for (; it; it = it->pNext){
  611. func(0, it, 0);
  612. }
  613. };
  614. void lstForAllItemsDo_DirectFree(laListDoFunc func, laListHandle *hList){
  615. laListItem *it;
  616. while (it = lstPopItem(hList)){
  617. if (func) func(it);
  618. FreeMem(it);
  619. }
  620. };
  621. void lstForAllItemsDo_arg_ptr(laListDoFuncArgp func, laListHandle *hList, void *arg){
  622. laListItem *it = hList->pFirst;
  623. for (; it; it = it->pNext){
  624. func(it, arg);
  625. };
  626. };
  627. void lstForAllItemsDo_NonRecursive_Root(laListHandle *FirstHandle, laListNonRecursiveDoFunc func, int bFreeItem, void *custom_data, laListCustomDataRemover remover){
  628. laListItem *li = 0, *NextLi;
  629. laListNonRecursiveRoot root = {0};
  630. laListNonRecursiveItem *nrItem = CreateNew(laListNonRecursiveItem);
  631. nrItem->bFreeList = bFreeItem;
  632. nrItem->func = func;
  633. nrItem->CustomData = custom_data;
  634. nrItem->remover = remover;
  635. lstCopyHandle(&nrItem->handle, FirstHandle);
  636. lstAppendItem(&root.NSItems, nrItem);
  637. while (lstHaveItemInList(&root.NSItems)){
  638. nrItem = lstPopItem(&root.NSItems);
  639. for (li = nrItem->handle.pFirst; li /*!=nrItem->handle.pLast*/; li = NextLi){
  640. if (nrItem->func) nrItem->func(&root, li, custom_data);
  641. NextLi = li->pNext;
  642. if (nrItem->bFreeList){
  643. laListItem *fli = li;
  644. FreeMem(fli);
  645. }
  646. if (li == nrItem->handle.pLast) break;
  647. }
  648. if (nrItem->remover) nrItem->remover(nrItem->CustomData);
  649. FreeMem(nrItem);
  650. }
  651. };
  652. void lstAddNonRecursiveListHandle(laListNonRecursiveRoot *root, laListHandle *newHandle, laListNonRecursiveDoFunc nrFunc, int bFreeList, void *custom_data, laListCustomDataRemover remover){
  653. laListNonRecursiveItem *nrItem = CreateNew(laListNonRecursiveItem);
  654. nrItem->bFreeList = bFreeList;
  655. nrItem->func = nrFunc;
  656. nrItem->CustomData = custom_data;
  657. nrItem->remover = remover;
  658. lstCopyHandle(&nrItem->handle, newHandle);
  659. lstAppendItem(&root->NSItems, nrItem);
  660. };
  661. void lstCopy_NonRecursive_Root(laListHandle *FromHandle, laListHandle *ToHandle, int SizeEachNode, laListNonRecursiveCopyFunc func, void *custom_data, laListCustomDataRemover remover){
  662. laListItem *li = 0, *tli = 0;
  663. laListNonRecursiveRoot root = {0};
  664. laListNonRecursiveItem *nrItem = CreateNew(laListNonRecursiveItem);
  665. laListItem *NextLi;
  666. nrItem->CopyFunc = func;
  667. lstCopyHandle(&nrItem->handle, FromHandle);
  668. nrItem->ToHandle = ToHandle; //Pointer
  669. lstClearHandle(ToHandle);
  670. nrItem->CustomData = custom_data;
  671. nrItem->remover = remover;
  672. nrItem->SizeEachNode = SizeEachNode;
  673. lstAppendItem(&root.NSItems, nrItem);
  674. while (lstHaveItemInList(&root.NSItems)){
  675. nrItem = lstPopItem(&root.NSItems);
  676. if (nrItem->CopyFunc){
  677. for (li = nrItem->handle.pFirst; li; li = li->pNext){
  678. tli = CreateNew_Size(nrItem->SizeEachNode);
  679. nrItem->CopyFunc(&root, li, tli, nrItem->CustomData);
  680. lstClearPrevNext(tli);
  681. lstAppendItem(nrItem->ToHandle, tli);
  682. }
  683. if (nrItem->remover) nrItem->remover(nrItem->CustomData);
  684. }else if (nrItem->func){
  685. for (li = nrItem->handle.pFirst; li /*!=nrItem->handle.pLast*/; li = NextLi){
  686. if (nrItem->func) nrItem->func(&root, li, custom_data);
  687. NextLi = li->pNext;
  688. if (nrItem->bFreeList){
  689. laListItem *fli = li;
  690. FreeMem(fli);
  691. }
  692. if (li == nrItem->handle.pLast) break;
  693. }
  694. if (nrItem->remover) nrItem->remover(nrItem->CustomData);
  695. }
  696. FreeMem(nrItem);
  697. }
  698. };
  699. void lstAddNonRecursiveListCopier(laListNonRecursiveRoot *root, laListHandle *oldHandle, laListHandle *newHandle, int sizeEach, laListNonRecursiveCopyFunc nrCpyFunc, void *custom_data, laListCustomDataRemover remover){
  700. laListNonRecursiveItem *nrItem = CreateNew(laListNonRecursiveItem);
  701. nrItem->CopyFunc = nrCpyFunc;
  702. lstCopyHandle(&nrItem->handle, oldHandle);
  703. nrItem->ToHandle = newHandle;
  704. nrItem->CustomData = custom_data;
  705. nrItem->remover = remover;
  706. nrItem->SizeEachNode = sizeEach;
  707. lstAppendItem(&root->NSItems, nrItem);
  708. };
  709. void *lstFindItem(void *CmpData, laCompareFunc func, laListHandle *hList){
  710. laListItem *it;
  711. if (!CmpData || !hList) return 0;
  712. it = hList->pFirst;
  713. for (; it; it = it->pNext){
  714. if (func(it, CmpData)) return it;
  715. };
  716. return 0;
  717. };
  718. void lstCombineLists(laListHandle *dest, laListHandle *src){
  719. if ((!dest) || (!src)) return;
  720. if ((!dest->pFirst) && (!dest->pLast)){
  721. dest->pFirst = src->pFirst;
  722. dest->pLast = src->pLast;
  723. }else{
  724. if (src->pLast){
  725. ((laListItem *)src->pFirst)->pPrev = dest->pLast;
  726. ((laListItem *)dest->pLast)->pNext = src->pFirst;
  727. dest->pLast = src->pLast;
  728. }
  729. }
  730. src->pFirst = 0;
  731. src->pLast = 0;
  732. }
  733. void lstDestroyList(laListHandle *hlst){
  734. laListItem *li, *nextli;
  735. for (li = hlst->pFirst; li; li = nextli){
  736. nextli = li->pNext;
  737. memFree(li);
  738. }
  739. }
  740. void lstDestroyListA(laListHandle *hlst){
  741. laListItem *li, *nextli;
  742. for (li = hlst->pFirst; li; li = nextli){
  743. nextli = li->pNext;
  744. FreeMem(li);
  745. }
  746. }
  747. void lstDestroyList_User(laListHandle *hlst, laListDoFunc func){
  748. laListItem *it = hlst->pFirst;
  749. for (; it; it = it->pNext){
  750. func(it);
  751. FreeMem(it);
  752. }
  753. };
  754. void lstCopyList(laListHandle *hOldlst, laListHandle *hNewList, int SizeEachNode, laCopyListFunc func){
  755. laListItem *li, *nextli, *newli;
  756. for (li = hOldlst->pFirst; li; li = nextli){
  757. newli = (laListItem *)CreateNew_Size(SizeEachNode);
  758. func(li, newli);
  759. lstAppendItem(hNewList, newli);
  760. nextli = li->pNext;
  761. }
  762. }
  763. void *lstReMatch(laListHandle *SearchHandle, laListHandle *CurrentHandle, void *ItemToFind){
  764. laListItem *sl = 0, *rl = 0;
  765. if (!SearchHandle || !CurrentHandle || !ItemToFind) return 0;
  766. sl = SearchHandle->pFirst;
  767. rl = CurrentHandle->pFirst;
  768. while (sl && rl){
  769. if (ItemToFind == sl){
  770. return rl;
  771. }else{
  772. sl = sl->pNext;
  773. rl = rl->pNext;
  774. }
  775. }
  776. return 0;
  777. }
  778. //void* lstReMatchEx(laListHandle* SearchHandle, laListHandle* CurrentHandle, void* ItemToFind, MatcherFunc func){
  779. // laListItem* sl = 0, *rl = 0;
  780. //
  781. // if (!SearchHandle || !CurrentHandle || !ItemToFind) return 0;
  782. //
  783. // sl = SearchHandle->pFirst; rl = CurrentHandle->pFirst;
  784. //
  785. // while (sl && rl){
  786. // if (func(ItemToFind, sl)){
  787. // return rl;
  788. // }
  789. // else{
  790. // sl = sl->pNext;
  791. // rl = rl->pNext;
  792. // }
  793. // }
  794. // return 0;
  795. //}
  796. void lstAddElement(laListHandle *hlst, void *ext){
  797. laElementListItem *eli = CreateNew(laElementListItem);
  798. eli->Ext = ext;
  799. lstAppendItem(hlst, eli);
  800. }
  801. void lstDestroyElementList(laListHandle *hlst){
  802. laElementListItem *eli, *NextEli;
  803. for (eli = hlst->pFirst; eli; eli = NextEli){
  804. lstRemoveItem(hlst, eli);
  805. NextEli = eli->Item.pNext;
  806. FreeMem(eli);
  807. }
  808. }
  809. void hsh65536Init(laHash65536** h){
  810. if(!h) return; *h=calloc(1,sizeof(laHash65536));
  811. }
  812. void hshFree(laHash65536** h){
  813. if(!h || !*h) return; free(*h); *h=0;
  814. }
  815. laListHandle* hsh65536DoHashLongPtr(laHash65536* hash, unsigned long long buckle) {
  816. return &hash->Entries[(unsigned short)((buckle >> 10))];
  817. }
  818. laListHandle* hsh65536DoHashNUID(laHash65536* hash, char * NUID) {
  819. u64bit Hash;
  820. sscanf(NUID, "%ld", &Hash);
  821. return hsh65536DoHashLongPtr(hash, (long)Hash);
  822. }
  823. laListHandle* hsh16MDoHashLongPtr(laHash16M* hash, long long buckle) {
  824. return &hash->Entries[(buckle>>6)&0x00FFFFFF];
  825. }
  826. laListHandle* hsh16MDoHashNUID(laHash16M* hash, char * NUID) {
  827. u64bit Hash=0;
  828. for(char* c=NUID;*c;c++){ Hash=Hash*3+(*c); }
  829. return hsh65536DoHashLongPtr(hash, (long)Hash);
  830. }
  831. unsigned char hsh256DoHashSTR(char *buckle){
  832. int i, len = 0;
  833. unsigned char rev = 0;
  834. if (buckle) len = strlen(buckle);
  835. for (i = 0; i < len; i++){
  836. rev = rev * 31 + (unsigned char)buckle[i];
  837. }
  838. return (unsigned char)rev;
  839. }
  840. void hsh256InsertItemCSTR(laHash256 *hash, laListItem *li, char *buckle){
  841. unsigned char a = hsh256DoHashSTR(buckle);
  842. lstAppendItem(&hash->Entries[a], li);
  843. };
  844. void hsh256InsertItem(laHash256 *hash, laListItem *li, char buckle){
  845. lstAppendItem(&hash->Entries[(unsigned char)buckle], li);
  846. };
  847. void hsh65536InsertItem(laHash65536 *hash, laListItem *li, long buckle){
  848. lstAppendItem(&hash->Entries[(unsigned short)((buckle >> 10))], li);
  849. //hsh256InsertItem(&hash->HashHandles[(char)((buckle >> 8) / 8)], li, (char)(buckle/8));
  850. //printf("%d %d\n", (char)(buckle >> 5), (char)(buckle >> 6));
  851. };
  852. laListItem *hsh256FindItemSTR(laHash256 *hash, laCompareFunc func, char *buckle){
  853. unsigned char hsh;
  854. hsh = hsh256DoHashSTR(buckle);
  855. //if(hash->Entries[hsh].pFirst == hash->Entries[hsh].pLast)
  856. // return hash->Entries[hsh].pFirst;
  857. laListItem* item=lstFindItem(buckle, func, &hash->Entries[hsh]);
  858. return item;
  859. }
  860. //================================================================ [mem]
  861. void* memGetHead(void* UserMem, int* HyperLevel){
  862. laMemoryPoolPart **mpp = (laMemoryPoolPart**)(((char*)UserMem)-sizeof(void*));
  863. if(!(*mpp)) return 0;
  864. laMemoryPool* mp = (*mpp)->PoolRoot;
  865. if(HyperLevel) *HyperLevel= mp->Hyperlevel;
  866. if(mp->Hyperlevel==2) return ((char*)UserMem)-sizeof(laMemNodeHyper);
  867. if(mp->Hyperlevel==1) return ((char*)UserMem)-sizeof(laMemNode);
  868. if(mp->Hyperlevel==0) return ((char*)UserMem)-sizeof(laMemNode0);
  869. return 0;
  870. }
  871. laListHandle* memGetUserList(void* UserMem){
  872. int level; void* head=memGetHead(UserMem, &level);
  873. if(level==2) return &((laMemNodeHyper*)head)->Users;
  874. if(level==1) return &((laMemNode*)head)->Users;
  875. return 0;
  876. }
  877. laMemoryPool *memInitPool(int NodeSize, int HyperLevel){
  878. if (!NodeSize) return 0;
  879. laMemoryPool *mph = calloc(1, sizeof(laMemoryPool));
  880. mph->NodeSize = NodeSize;
  881. mph->NextCount = 1;
  882. mph->Hyperlevel = HyperLevel;
  883. u8bit Buckle = NodeSize;
  884. lstAppendItem(&MAIN.GlobalMemPool.Entries[Buckle], mph);
  885. return mph;
  886. }
  887. laMemoryPoolPart *memNewPoolPart(laMemoryPool *mph){
  888. if (!mph->NodeSize) return 0;
  889. int MemNodeSize=(mph->Hyperlevel==0)?sizeof(laMemNode0):((mph->Hyperlevel==1)?sizeof(laMemNode):sizeof(laMemNodeHyper));
  890. int PoolRefOffset=MemNodeSize-sizeof(void*);
  891. int RealNodeSize = mph->NodeSize + MemNodeSize;
  892. int NodeCount = mph->NextCount;
  893. int TotalSize = sizeof(laMemoryPoolPart) + NodeCount * RealNodeSize;
  894. laMemoryPoolPart *mp = calloc(1, TotalSize);
  895. void *BeginMem = ((BYTE *)mp) + sizeof(laMemoryPoolPart);
  896. mp->PoolRoot = mph;
  897. mp->FreeMemoryNodes.pFirst = mp->FreeMemoryNodes.pLast = 0;
  898. for (int i = 0; i < NodeCount; i++){
  899. void* mpn = ((BYTE *)BeginMem) + RealNodeSize * i;
  900. void** ref = ((BYTE *)mpn) + PoolRefOffset;
  901. (*ref)=mp;
  902. lstAppendItem(&mp->FreeMemoryNodes, mpn);
  903. }
  904. lstPushItem(&mph->Pools, mp);
  905. return mp;
  906. }
  907. void *memAcquireH(laMemoryPool *Handle){
  908. laMemoryPoolPart *mp = Handle->Pools.pFirst;
  909. laMemNode *mpn;
  910. if (!mp || !mp->FreeMemoryNodes.pFirst){
  911. mp = memNewPoolPart(Handle);
  912. }
  913. if (!mp) return 0;
  914. mpn = mp->FreeMemoryNodes.pFirst;
  915. lstRemoveItem(&mp->FreeMemoryNodes, mpn);
  916. mp->UsedCount++;
  917. //lstAppendItem(&mp->MemoryNodes, mpn);
  918. return mpn;
  919. }
  920. void *memAcquire_(int Size, int Hyper){
  921. laMemoryPool *mp;
  922. u8bit Buckle = Size;
  923. mp = MAIN.GlobalMemPool.Entries[Buckle].pFirst;
  924. while (mp && (mp->NodeSize != Size || mp->Hyperlevel!=Hyper))
  925. mp = mp->Item.pNext;
  926. if (!mp) mp = memInitPool(Size, Hyper);
  927. return memAcquireH(mp);
  928. }
  929. void *memAcquireSimple(int Size){
  930. void *mpn = memAcquire_(Size, 0);
  931. return ((char*)mpn)+sizeof(laMemNode0);
  932. }
  933. void *memAcquire(int Size){
  934. laMemNode *mpn = memAcquire_(Size, 1);
  935. void* mem = ((char*)mpn)+sizeof(laMemNode);
  936. return mem;
  937. }
  938. void *memAcquireHyperNoAppend(int Size){
  939. laMemNodeHyper *mpn = memAcquire_(Size, 2);
  940. void* mem = ((char*)mpn)+sizeof(laMemNodeHyper);
  941. memMakeHyperData(mpn);
  942. return mem;
  943. }
  944. void *memAcquireHyper(int Size){
  945. laMemNodeHyper *mpn = memAcquire_(Size, 2);
  946. void* mem = ((char*)mpn)+sizeof(laMemNodeHyper);
  947. memMakeHyperData(mpn);
  948. laListHandle* l=hsh16MDoHashNUID(&MAIN.DBInst2,mpn->NUID.String);
  949. lstAppendItem(l,mpn);
  950. return mem;
  951. }
  952. void memFree(void *Data){
  953. if (!Data) return;
  954. int level; void* head = memGetHead(Data, &level);
  955. laMemoryPoolPart *mp;
  956. if(level==2) { mp = ((laMemNodeHyper*)head)->InPool; laDataBlockNoLongerExists(Data,&((laMemNodeHyper*)head)->Users);
  957. laListHandle* l=hsh16MDoHashNUID(&MAIN.DBInst2,((laMemNodeHyper*)head)->NUID.String); lstRemoveItem(l,head);}
  958. if(level==1) { mp = ((laMemNode*)head)->InPool; laDataBlockNoLongerExists(Data,&((laMemNode*)head)->Users); }
  959. if(level==0) { mp = ((laMemNode0*)head)->InPool; }
  960. laMemoryPool *mph = mp->PoolRoot;
  961. //lstRemoveItem(&mp->MemoryNodes, head);
  962. mp->UsedCount--;
  963. void* head_except_item = ((char*)head)+sizeof(laListItem);
  964. //memset(head_except_item, 0, ((level==2)?sizeof(laMemNodeHyper):((level==1)?sizeof(laMemNode):sizeof(laMemNode0)))+mph->NodeSize-sizeof(laListItem));
  965. lstAppendItem(&mp->FreeMemoryNodes, head);
  966. memset(Data, 0, mph->NodeSize);
  967. MAIN.ByteCount -= mph->NodeSize;
  968. if (!mp->UsedCount){
  969. lstRemoveItem(&mph->Pools, mp);
  970. FreeMem(mp);
  971. }
  972. //if (!mph->Pools.pFirst) {
  973. // mph->CountPerPool = 0;
  974. // mph->NodeSize = 0;
  975. //}
  976. }
  977. void memDestroyPool(laMemoryPool *mph){
  978. laMemoryPool *mp;
  979. while ((mp = lstPopItem(&mph->Pools))){
  980. FreeMem(mp);
  981. }
  982. FreeMem(mph);
  983. }
  984. // Leave memory in an temporary place and if when push difference these are still not acquired, free them.
  985. void memLeave(void *Data){
  986. laListHandle* l=hsh65536DoHashLongPtr(MAIN.DBInstMemLeft,Data); lstAppendPointer(l,Data);
  987. }
  988. void memTake(void *Data){
  989. laListHandle* l=hsh65536DoHashLongPtr(MAIN.DBInstMemLeft,Data); lstRemovePointer(l,Data);
  990. }
  991. void memFreeRemainingLeftNodes(){
  992. laListHandle* l; void* m;
  993. for(int i=0;i<65536;i++){
  994. l=&MAIN.DBInstMemLeft->Entries[i]; while(m=lstPopPointer(l)){ memFree(m);
  995. #ifdef DEBUG
  996. printf("left freed %x\n",m);
  997. #endif
  998. }
  999. }
  1000. }
  1001. void memNoLonger(){
  1002. for(int i=0;i<256;i++){
  1003. laMemoryPool* mp; while(mp=lstPopItem(&MAIN.GlobalMemPool.Entries[i])){ memDestroyPool(mp); }
  1004. }
  1005. }
  1006. laStaticMemoryPoolNode *memNewStaticPool(laStaticMemoryPool *smp){
  1007. laStaticMemoryPoolNode *smpn = calloc(1, LA_MEMORY_POOL_128MB);
  1008. smpn->UsedByte = sizeof(laStaticMemoryPoolNode);
  1009. lstPushItem(&smp->Pools, smpn);
  1010. return smpn;
  1011. }
  1012. void *memStaticAcquire(laStaticMemoryPool *smp, int size){
  1013. laStaticMemoryPoolNode *smpn = smp->Pools.pFirst;
  1014. void *ret;
  1015. if (!smpn || (smpn->UsedByte + size) > LA_MEMORY_POOL_128MB) smpn = memNewStaticPool(smp);
  1016. ret = ((BYTE *)smpn) + smpn->UsedByte;
  1017. smpn->UsedByte += size;
  1018. return ret;
  1019. }
  1020. void *memStaticAcquireThread(laStaticMemoryPool *smp, int size){
  1021. laStaticMemoryPoolNode *smpn = smp->Pools.pFirst;
  1022. void *ret;
  1023. //pthread_spin_lock(&smp->csMem);
  1024. if (!smpn || (smpn->UsedByte + size) > LA_MEMORY_POOL_128MB) smpn = memNewStaticPool(smp);
  1025. ret = ((BYTE *)smpn) + smpn->UsedByte;
  1026. smpn->UsedByte += size;
  1027. //pthread_spin_unlock(&smp->csMem);
  1028. return ret;
  1029. }
  1030. void *memStaticDestroy(laStaticMemoryPool *smp){
  1031. laStaticMemoryPoolNode *smpn;
  1032. void *ret;
  1033. while (smpn = lstPopItem(&smp->Pools)){
  1034. FreeMem(smpn);
  1035. }
  1036. smp->EachSize = 0;
  1037. return ret;
  1038. }
  1039. void la_ReferencedBlockDeleted(void* This, laItemUserLinker* iul){
  1040. void** user=iul->Pointer.p; if(*user==This){ (*user)=0; laStopUsingDataBlock(iul->Additional, 0, This); }
  1041. }
  1042. void la_ReferrerDeleted(void* This, laItemUserLinker* iul){
  1043. void* instance=iul->Pointer.p; if(instance!=This){ laStopUsingDataBlock(instance, 0, This); }
  1044. }
  1045. void memAssignRef(void* This, void** ptr, void* instance){
  1046. laItemUserLinker* iul;
  1047. if((!This)||(!ptr)) return;
  1048. if(instance){
  1049. laItemUserLinker*iul=laUseDataBlock(instance, 0, 0, ptr, la_ReferencedBlockDeleted, 0); if(iul){iul->Additional=This;}
  1050. laUseDataBlock(This, 0, 0, instance, la_ReferrerDeleted, 0);
  1051. }else{
  1052. laStopUsingDataBlock((*ptr), 0, This);
  1053. laStopUsingDataBlock(This, 0, (*ptr));
  1054. }
  1055. (*ptr)=instance;
  1056. }
  1057. void memAssignRefSafe(laSubProp* sp, void* This, void** ptr, void* instance){
  1058. laPropContainer* pc=sp?la_EnsureSubTarget(sp,instance):0;
  1059. if(pc&&!pc->OtherAlloc) memAssignRef(This,ptr,instance);
  1060. else (*ptr)=instance;
  1061. }
  1062. //=======================================================================[str]
  1063. char *strGetNextString(char **pivot, char *NextMark){
  1064. int lenth = 0;
  1065. char *countP = *pivot;
  1066. char *result = 0;
  1067. int FloatArg = 0;
  1068. int i,advance;
  1069. if (**pivot == L'\0') return 0;
  1070. if (*NextMark == L'~') FloatArg = 1;
  1071. // container@identifier=window container#window contianer%
  1072. int UC=1;
  1073. while (!lenth){
  1074. for (countP; *countP != L'.' && *(*pivot) != L'\0' && UC && *countP && *countP != L'@' && *countP != L'=' && *countP != L'#' && *countP != L'$';){
  1075. if((*countP)=='\\'){
  1076. countP++; lenth++; }
  1077. UC = laToUnicode(countP, &advance);
  1078. lenth+=advance;
  1079. countP+=advance;
  1080. }
  1081. if (lenth || (*countP) == 0) break;
  1082. (*pivot)++; countP++;
  1083. }
  1084. *NextMark = (*pivot)[lenth];
  1085. if (!(*NextMark)) *NextMark = L'.';
  1086. if (lenth){
  1087. result = CreateNewBuffer(char, lenth + 1);
  1088. int pi=0; for (i = 0; i < lenth; i++){
  1089. if((*pivot)[i]=='\\'){ continue; }
  1090. result[pi] = (*pivot)[i]; pi++;
  1091. }
  1092. result[pi] = L'\0';
  1093. if ((*pivot)[lenth] == L'\0') *pivot = &((*pivot)[lenth]);
  1094. else
  1095. (*pivot) += lenth + 1;
  1096. return result;
  1097. }else{
  1098. return 0;
  1099. }
  1100. };
  1101. int strGetStringTerminateBy(char *content, char terminator, char *Out){
  1102. int Ofst = 0;
  1103. int Skip = 0;
  1104. int i = 0, advance;
  1105. if ((!content) || (*content == L'\0')) return 0;
  1106. int UC;
  1107. for (Ofst; content[Ofst] != terminator && content[Ofst] != L'\0'; ){
  1108. UC = laToUnicode(&content[Ofst], &advance);
  1109. for(int a=0;a<advance;a++){
  1110. Out[i] = content[Ofst];
  1111. Ofst++; i++;
  1112. }
  1113. }
  1114. Out[i] = 0;
  1115. return i;
  1116. };
  1117. char *strGetNewStringTerminateBy_PivotOver(char *content, char terminator, char **NewPivot, int IgnoreSpace){
  1118. int Ofst = 0;
  1119. int Skip = 0;
  1120. int i = 0;
  1121. char *NewString;
  1122. if (!content || *content == L'\0') return 0;
  1123. if (IgnoreSpace) for (i; content[i] == L' '; i++) ;
  1124. int UC,advance;
  1125. for (Ofst; content[Ofst] != terminator && content[Ofst] != L'\0';){
  1126. UC = laToUnicode(&content[Ofst], &advance);
  1127. Ofst+=advance;
  1128. }
  1129. NewString = CreateNewBuffer(char, Ofst + 1 - i);
  1130. memcpy(NewString, &content[i], sizeof(char) * (Ofst - i));
  1131. NewString[Ofst - i] = L'\0';
  1132. *NewPivot = content[Ofst]?&content[Ofst + 1]:&content[Ofst];
  1133. return NewString;
  1134. };
  1135. int strHeadOfStringMatch(char *Str, char *SubStr){
  1136. int len = strlen(SubStr);
  1137. int i = 0;
  1138. for (i; i < len; i++){
  1139. if (Str[i] != SubStr[i]) return 0;
  1140. }
  1141. return 1;
  1142. }
  1143. int strSkipSegmet(char **pivot, char *content){
  1144. if (!pivot || !(*pivot) || !(*(*pivot)) || !content) return 0;
  1145. if (strHeadOfStringMatch(*pivot, content)){
  1146. (*pivot) += strlen(content);
  1147. return 1;
  1148. }
  1149. return 0;
  1150. }
  1151. char *strGetLastSegment(char *Content, char Seperator){
  1152. char *p = Content;
  1153. char *pn = Content;
  1154. while (1){
  1155. while (*pn != Seperator){
  1156. if (!(*pn)) return p;
  1157. pn++;
  1158. }
  1159. pn++;
  1160. p = pn;
  1161. }
  1162. }
  1163. void strDiscardLastSegmentSeperateBy(char *Content, char Seperator){
  1164. char *p = Content;
  1165. char *pn = Content;
  1166. while (1){
  1167. while (*pn != Seperator){
  1168. if (!(*pn)){
  1169. *p = 0;
  1170. return;
  1171. }
  1172. pn++;
  1173. }
  1174. p = pn;
  1175. pn++;
  1176. }
  1177. }
  1178. void strDiscardSameBeginningSeperatedBy(char *s1, char *s2, char **Result1, char **Result2, char Seperator){
  1179. int i = 0;
  1180. int p = 0;
  1181. while (s1[i] == s2[i]){
  1182. i++;
  1183. if (s1[i] == Seperator) p = i;
  1184. if (!s1[i]){
  1185. p = i;
  1186. break;
  1187. }
  1188. if (!s2[i]){
  1189. p = i;
  1190. break;
  1191. }
  1192. }
  1193. *Result1 = &s1[p];
  1194. *Result2 = &s2[p];
  1195. }
  1196. int strCountSegmentSeperateBy(char *Content, char Seperator){
  1197. char *p = Content;
  1198. char *pn = Content;
  1199. int c = Content[0] ? (Content[0] == Seperator ? 0 : 1) : 0;
  1200. while (1){
  1201. while (*pn != Seperator){
  1202. if (!(*pn)){
  1203. if ((*p) == Seperator) c--;
  1204. return c;
  1205. }
  1206. p = pn;
  1207. pn++;
  1208. }
  1209. c++;
  1210. pn++;
  1211. }
  1212. return c;
  1213. }
  1214. void strMakeDifferentName(char *Target){
  1215. char *p = strGetLastSegment(Target, '.');
  1216. int Temp;
  1217. if (!sscanf(p, "%d", &Temp)){
  1218. int l = strlen(p);
  1219. if (p[l - 1] != L'.') strcat(p, ".");
  1220. strPrintIntAfter(Target, 0, 001);
  1221. }else{
  1222. sprintf(p, "%d", Temp + 1);
  1223. };
  1224. }
  1225. void strReplaceCharacter(char *Str, char Find, char Replace){
  1226. char *p = Str;
  1227. if (!p) return;
  1228. while (*p){
  1229. if (*p == Find) *p = Replace;
  1230. p++;
  1231. }
  1232. }
  1233. void strToUpper(char *Str){
  1234. char *p = Str; if (!p) return;
  1235. while (*p){
  1236. if (*p >= 'a' && *p <= 'z') *p += 'A' - 'a';
  1237. p++;
  1238. }
  1239. }
  1240. void strToLower(char *Str){
  1241. char *p = Str; if (!p) return;
  1242. while (*p){
  1243. if (*p >= 'A' && *p <= 'Z') *p -= 'A' - 'a';
  1244. p++;
  1245. }
  1246. }
  1247. laStringSplitor *strSplitPath(char *path,char terminator){
  1248. laStringPart *sp;
  1249. laStringSplitor *ss;
  1250. char *pivot = path;
  1251. char *temp_result;
  1252. char Type = terminator?terminator:'.';
  1253. char NextType = '.';
  1254. if (!path || !path[0]) return 0;
  1255. ss = memAcquireSimple(sizeof(laStringSplitor));
  1256. while (temp_result = strGetNextString(&pivot, &NextType)){
  1257. if (*temp_result != L'\0'){
  1258. sp = memAcquireSimple(sizeof(laStringPart));
  1259. sp->Content = temp_result;
  1260. lstAppendItem(&ss->parts, sp);
  1261. ss->NumberParts += 1;
  1262. if (NextType == L'$') sp->Type = L'$';
  1263. else
  1264. sp->Type = Type;
  1265. if (sp->Type == L'='){
  1266. if (sp->Content[0] >= L'0' && sp->Content[0] <= 9){
  1267. sscanf(sp->Content, "%d", &sp->IntValue);
  1268. }
  1269. }
  1270. if (NextType == L'$') NextType = L'.';
  1271. Type = NextType;
  1272. }
  1273. }
  1274. if (ss->NumberParts == 0){
  1275. strDestroyStringSplitor(&ss);
  1276. return 0;
  1277. }
  1278. return ss;
  1279. };
  1280. void DF_ClearStingParts(laStringPart *sp){
  1281. FreeMem(sp->Content);
  1282. };
  1283. int strDestroyStringSplitor(laStringSplitor **ss){
  1284. if (!(*ss)) return 0;
  1285. lstForAllItemsDo(DF_ClearStingParts, &(*ss)->parts);
  1286. lstDestroyList(&(*ss)->parts);
  1287. memFree(*ss);
  1288. *ss = 0;
  1289. return 1;
  1290. }
  1291. char * strSub(char *input, char *substring, char *replace){
  1292. int number_of_matches = 0;
  1293. size_t substring_size = strlen(substring), replace_size = strlen(replace), buffer_size;
  1294. char *buffer, *bp, *ip;
  1295. if (substring_size){
  1296. ip = strstr(input, substring);
  1297. while (ip != NULL){
  1298. number_of_matches++;
  1299. ip = strstr(ip+substring_size, substring);
  1300. }
  1301. }
  1302. else number_of_matches = strlen (input) + 1;
  1303. buffer_size = strlen(input) + number_of_matches*(replace_size - substring_size) + 1;
  1304. if ((buffer = ((char *) malloc(buffer_size))) == NULL){
  1305. return NULL;
  1306. }
  1307. bp = buffer;
  1308. ip = strstr(input, substring);
  1309. while ((ip != NULL) && (*input != '\0')){
  1310. if (ip == input){
  1311. memcpy (bp, replace, replace_size+1);
  1312. bp += replace_size;
  1313. if (substring_size)input += substring_size;
  1314. else*(bp++) = *(input++);
  1315. ip = strstr(input, substring);
  1316. }
  1317. else while (input != ip) *(bp++) = *(input++);
  1318. }
  1319. if (substring_size)strcpy (bp, input);
  1320. else memcpy (bp, replace, replace_size+1);
  1321. return buffer;
  1322. }
  1323. char buff[128]={0};
  1324. int strMakeInstructions(laStringSplitor **result, char *content){
  1325. laStringPart *sp;
  1326. laStringSplitor *ss = *result;
  1327. char *pivot = content;
  1328. unsigned char *temp_result;
  1329. if (!content || !content[0]) return 0;
  1330. if (!ss) ss = *result = memAcquireSimple(sizeof(laStringSplitor));
  1331. while (temp_result = strGetNewStringTerminateBy_PivotOver(pivot, '=', &pivot, 0)){
  1332. if (*temp_result != L'\0'){
  1333. sp = memAcquireSimple(sizeof(laStringPart));
  1334. sp->Content = temp_result;
  1335. lstAppendItem(&ss->parts, sp);
  1336. ss->NumberParts += 1;
  1337. }
  1338. temp_result = strGetNewStringTerminateBy_PivotOver(pivot, ';', &pivot, 0);
  1339. if (!temp_result) break;
  1340. if (*temp_result != L'\0'){
  1341. sp = memAcquireSimple(sizeof(laStringPart));
  1342. sp->Content = temp_result;
  1343. lstAppendItem(&ss->parts, sp);
  1344. ss->NumberParts += 1;
  1345. if (temp_result[0] >= L'0' && temp_result[0] <= L'9' || temp_result[0]>=128){
  1346. sscanf(temp_result, "%d", &sp->IntValue);
  1347. sscanf(temp_result, "%lf", &sp->FloatValue);
  1348. }
  1349. }
  1350. }
  1351. if (ss->NumberParts == 0){
  1352. strDestroyStringSplitor(&ss);
  1353. return 0;
  1354. }
  1355. return 1;
  1356. }
  1357. laStringPart *strGetArgument(laStringSplitor *ss, char *content){
  1358. laStringPart *sp;
  1359. if (!ss) return 0;
  1360. for (sp = ss->parts.pFirst; sp; sp = sp->Item.pNext ? ((laListItem *)sp->Item.pNext)->pNext : 0){
  1361. if (strSame(content, sp->Content)) return sp->Item.pNext;
  1362. }
  1363. return 0;
  1364. }
  1365. char *strGetArgumentString(laStringSplitor *ss, char *content){
  1366. laStringPart *sp;
  1367. if (!ss) return 0;
  1368. for (sp = ss->parts.pFirst; sp; sp = sp->Item.pNext ? ((laListItem *)sp->Item.pNext)->pNext : 0){
  1369. if (strSame(content, sp->Content)) return sp->Item.pNext ? ((laStringPart *)sp->Item.pNext)->Content : 0;
  1370. }
  1371. return 0;
  1372. }
  1373. int strArgumentMatch(laStringSplitor *ss, char *id, char *value){
  1374. laStringPart *sp;
  1375. if (!ss) return 0;
  1376. for (sp = ss->parts.pFirst; sp; sp = sp->Item.pNext ? ((laListItem *)sp->Item.pNext)->pNext : 0){
  1377. if (strSame(id, sp->Content)) return (strSame(((laStringPart *)sp->Item.pNext)->Content, value));
  1378. }
  1379. return 0;
  1380. }
  1381. int strGetIntSimple(char *content){
  1382. int a;
  1383. sscanf(content, "%d", &a);
  1384. return a;
  1385. }
  1386. real strGetFloatSimple(char *content){
  1387. real a;
  1388. sscanf(content, "%lf", &a);
  1389. return a;
  1390. }
  1391. void strConvInt_CString(int src, char *dest, int lenth){
  1392. sprintf(dest, "%d", src);
  1393. };
  1394. void strConvFloat_CString(real src, char *dest, int lenth){
  1395. sprintf(dest, "%lf", src);
  1396. };
  1397. void strCopyFull(char *dest, char *src){
  1398. if (src && dest) strcpy(dest, src);
  1399. }
  1400. void strCopySized(char *dest, int LenthLim, char *src){
  1401. if (src && dest) strcpy(dest, src);
  1402. }
  1403. void strPrintFloatAfter(char *dest, int LenthLim, int bits, real data){
  1404. char temp[64]={0};
  1405. sprintf(temp, "%.*lf", bits, data);
  1406. strcat(dest, temp);
  1407. }
  1408. void strPrintIntAfter(char *dest, int LenthLim, int data){
  1409. char temp[64]={0};
  1410. sprintf(&temp[0], "%d", data);
  1411. strcat(dest, temp);
  1412. }
  1413. void strEscapePath(char* OutCanBeSame, char* path){
  1414. char t[256]={0}; int ti=0;
  1415. for(int i=0;path[i];i++,ti++){
  1416. if(path[i]=='.'){ t[ti]='\\'; ti++; }
  1417. t[ti]=path[i];
  1418. }
  1419. strcpy(OutCanBeSame,t);
  1420. }
  1421. int strSame(char *src, char *dest){
  1422. return (src && dest && !strcmp(src, dest));
  1423. }
  1424. void strSafeDestroy(laSafeString **ss){
  1425. if (!*ss) return; lstRemoveItem(&SSC.SafeStrings, *ss);
  1426. if((*ss)->Ptr) memFree((*ss)->Ptr);
  1427. memFree(*ss); *ss=0;
  1428. }
  1429. void strSafeSet(laSafeString **ss, char *Content){
  1430. int len;
  1431. if (!Content||!Content[0]){ strSafeDestroy(ss); return; }
  1432. len = strlen(Content);
  1433. if (len < 1) return;
  1434. if (*ss){
  1435. char* mem=memAcquireSimple(sizeof(char)*(len+1));
  1436. strcpy(mem, Content);
  1437. memFree((*ss)->Ptr);
  1438. (*ss)->Ptr=mem;
  1439. return;
  1440. }
  1441. (*ss) = memAcquireSimple(sizeof(laSafeString));
  1442. (*ss)->Ptr = memAcquireSimple(sizeof(char)*(len+1));
  1443. strcpy((*ss)->Ptr, Content);
  1444. lstAppendItem(&SSC.SafeStrings, *ss);
  1445. }
  1446. void strSafeAppend(laSafeString **ss, char *Content){
  1447. if(!ss || !(*ss) || !Content){ strSafeSet(ss, Content); return; }
  1448. int OrigLen=strlen((*ss)->Ptr), ContentLen=strlen(Content);
  1449. char* mem=memAcquireSimple(sizeof(char)*(OrigLen+ContentLen+1));
  1450. memcpy(mem, (*ss)->Ptr, sizeof(char)*OrigLen);
  1451. memcpy(mem+sizeof(char)*OrigLen, Content, sizeof(char)*ContentLen);
  1452. mem[OrigLen+ContentLen]=0;
  1453. memFree((*ss)->Ptr);
  1454. (*ss)->Ptr=mem;
  1455. }
  1456. void strSafePrint(laSafeString **ss, char *Format, ...){
  1457. char content[512]; va_list va; va_start(va, Format); vsprintf(content, Format, va); va_end(va);
  1458. strSafeAppend(ss,content);
  1459. }
  1460. void strSafePrintV(laSafeString **ss, char *Format, va_list args){
  1461. char content[512]; va_list va; vsprintf(content, Format, args);
  1462. strSafeAppend(ss,content);
  1463. }
  1464. void strSafeDump(){
  1465. laSafeString*ss;
  1466. while(ss=lstPopItem(&SSC.SafeStrings)){
  1467. //if(ss->Ptr) printf("[String not freed] \"%s\"\n", ss->Ptr);
  1468. }
  1469. }
  1470. void strBeginEdit(laStringEdit **se, char *FullStr){
  1471. char *p = FullStr;
  1472. char buf[1024];
  1473. laStringEdit *nse = CreateNew(laStringEdit);
  1474. if(*se){ memcpy(nse,*se,sizeof(laStringEdit)); nse->Lines.pFirst=nse->Lines.pLast=0; nse->TotalLines=0; }
  1475. strEndEdit(se, 1);
  1476. nse->_BeginLine = -1; nse->_BeginBefore = -1;
  1477. if (FullStr && FullStr[0]){
  1478. while ((*p)){
  1479. laStringLine *sl = memAcquireSimple(sizeof(laStringLine));
  1480. p += strGetStringTerminateBy(p, '\n', buf);
  1481. strToUnicode(sl->Buf, buf);
  1482. lstAppendItem(&nse->Lines, sl); nse->TotalLines++;
  1483. if(*p){ p+=1; }
  1484. }
  1485. }
  1486. if (!nse->Lines.pFirst){
  1487. laStringLine *sl = memAcquireSimple(sizeof(laStringLine));
  1488. lstAppendItem(&nse->Lines, sl); nse->TotalLines++;
  1489. }
  1490. *se=nse;
  1491. }
  1492. char* strGetEditString(laStringEdit *se, int SelectionOnly){
  1493. if(!se) return 0; char* result=0; int next=0, max=0, len=0;
  1494. arrEnsureLength(&result, 0, &max, sizeof(char)); int NextChar=0;
  1495. int Line=0, starti=0, endat=INT_MAX;
  1496. for(laStringLine* sl=se->Lines.pFirst;sl;sl=sl->Item.pNext,Line++){
  1497. starti=0;
  1498. if(SelectionOnly && Line<se->BeginLine){ continue; }
  1499. if(SelectionOnly && Line==se->BeginLine){ starti=se->BeginBefore; }
  1500. int tlen=strlenU(&sl->Buf[starti]); int Extra=sl->Item.pNext?2:1;
  1501. arrEnsureLength(&result, (len+tlen)*4+Extra, &max, sizeof(char));
  1502. if(SelectionOnly && Line==se->EndLine){ endat=NextChar+se->EndBefore-starti; }
  1503. NextChar+=strToUTF8Lim(&result[NextChar], &sl->Buf[starti], endat); len+=tlen;
  1504. if(Extra==2){ result[NextChar]='\n'; NextChar+=1; }
  1505. if(SelectionOnly && Line==se->EndLine){ break; }
  1506. }
  1507. return result;
  1508. }
  1509. char* strEndEdit(laStringEdit **se, int FreeString){
  1510. char *p=0; laStringLine *sl, *NextSl;
  1511. if (!se || !(*se)) return 0;
  1512. p=strGetEditString(*se, 0);
  1513. while (sl=lstPopItem(&(*se)->Lines)){ memFree(sl); }
  1514. FreeMem(*se); *se=0;
  1515. if(FreeString && p){ free(p); p=0; }
  1516. return p;
  1517. }
  1518. void strSetEditViewRange(laStringEdit* se, int Lines, int Cols){
  1519. se->ViewHeight = Lines; se->ViewWidth = Cols;
  1520. }
  1521. void strEnsureCursorVisible(laStringEdit* se){
  1522. if(!se->ViewHeight || !se->ViewWidth || se->CursorLine<0 || se->CursorBefore<0 ){return;}
  1523. if(se->CursorLine>se->ViewHeight+se->ViewStartLine-1){ se->ViewStartLine=se->CursorLine-se->ViewHeight+1; }
  1524. if(se->CursorLine<se->ViewStartLine){ se->ViewStartLine=se->CursorLine; }
  1525. if(se->CursorBefore>se->ViewStartCol+se->ViewWidth-1){ se->ViewStartCol=se->CursorBefore-se->ViewWidth+1; }
  1526. if(se->CursorBefore<se->ViewStartCol){ se->ViewStartCol=se->CursorBefore; }
  1527. }
  1528. void strRemoveLine(laStringEdit *se, laStringLine *sl){
  1529. lstRemoveItem(&se->Lines, sl);
  1530. memFree(sl); se->TotalLines--;
  1531. }
  1532. void strRemoveLineI(laStringEdit *se, int LineIndex){
  1533. int i = 0;
  1534. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1535. while (sl){
  1536. NextSl = sl->Item.pNext;
  1537. if (i == LineIndex){ strRemoveLine(se, sl); break; }
  1538. i++;
  1539. sl = NextSl;
  1540. }
  1541. }
  1542. void strSetCursor(laStringEdit *se, int LineIndex, int BeforeIndex){
  1543. int maxbefore;
  1544. if (!se) return;
  1545. if(LineIndex<0){LineIndex=0;}
  1546. se->CursorLine = LineIndex;
  1547. maxbefore = strlenU(strGetCursorLine(se, &se->CursorLine)->Buf);
  1548. BeforeIndex = BeforeIndex < 0 ? 0 : BeforeIndex > maxbefore ? maxbefore : BeforeIndex;
  1549. se->CursorBefore = BeforeIndex;
  1550. se->BeginLine = -1;
  1551. se->BeginBefore = -1;
  1552. se->EndLine = -1;
  1553. se->EndBefore = -1;
  1554. strEnsureCursorVisible(se);
  1555. }
  1556. void strMoveCursor(laStringEdit *se, int Left, int Select){
  1557. int maxbefore;
  1558. int BeforeIndex;
  1559. int width = 1;
  1560. laStringLine *sl;
  1561. if (!se) return;
  1562. if(Select){ strLazySelect(se); } else { strCancelSelect(se); }
  1563. sl = strGetCursorLine(se, 0);
  1564. maxbefore = strlenU(sl->Buf);
  1565. BeforeIndex = se->CursorBefore - (Left ? 1 : -1);
  1566. if(BeforeIndex<0){
  1567. if(se->CursorLine>0) strSetCursor(se, se->CursorLine-1, INT_MAX);
  1568. }elif(BeforeIndex>maxbefore && se->CursorLine<se->TotalLines-1){
  1569. if(se->CursorLine>0) strSetCursor(se, se->CursorLine+1, 0);
  1570. }else{
  1571. se->CursorBefore = BeforeIndex>=maxbefore?maxbefore:BeforeIndex;
  1572. }
  1573. se->CursorPreferBefore = se->CursorBefore;
  1574. se->BeginLine = -1;
  1575. se->BeginBefore = -1;
  1576. se->EndLine = -1;
  1577. se->EndBefore = -1;
  1578. if(Select){ strEndSelect(se); }
  1579. strEnsureCursorVisible(se);
  1580. }
  1581. void strMoveCursorLine(laStringEdit *se, int Up, int Select){
  1582. int Line, maxbefore, LastIndex=-1;
  1583. laStringLine *sl;
  1584. if (!se) return;
  1585. if(Select){ strLazySelect(se); } else { strCancelSelect(se); }
  1586. Line=se->CursorLine - (Up? 1:-1);
  1587. if(Line<0) {Line=0;}
  1588. se->CursorLine = Line;
  1589. sl = strGetCursorLine(se, &LastIndex);
  1590. if(LastIndex>=0){ se->CursorLine = LastIndex; se->CursorPreferBefore=10000; }
  1591. maxbefore = strlenU(sl->Buf);
  1592. se->CursorBefore = se->CursorPreferBefore;
  1593. if(se->CursorBefore>maxbefore){ se->CursorBefore = maxbefore; }
  1594. if(LastIndex>=0){se->CursorPreferBefore=se->CursorBefore;}
  1595. if(Select){ strEndSelect(se); }
  1596. strEnsureCursorVisible(se);
  1597. }
  1598. int strHasSelection(laStringEdit* se){
  1599. return se->BeginBefore!=se->EndBefore||se->BeginLine!=se->EndLine;
  1600. }
  1601. void strCancelSelect(laStringEdit *se){
  1602. if (!se) return;
  1603. se->_BeginLine = -1;
  1604. se->_BeginBefore = -1;
  1605. se->BeginLine = -1;
  1606. se->EndLine = -1;
  1607. se->BeginBefore = -1;
  1608. se->EndBefore = -1;
  1609. }
  1610. void strLazySelect(laStringEdit *se){
  1611. if (!se || se->_BeginLine>=0) return;
  1612. se->_BeginLine = TNS_MAX2(se->CursorLine,0);
  1613. se->_BeginBefore = se->CursorBefore;
  1614. }
  1615. void strEndSelect(laStringEdit *se){
  1616. if (!se) return;
  1617. se->_EndLine = se->CursorLine;
  1618. se->_EndBefore = se->CursorBefore;
  1619. se->BeginLine = se->_BeginLine;
  1620. se->EndLine = se->_EndLine;
  1621. se->BeginBefore = se->_BeginBefore;
  1622. se->EndBefore = se->_EndBefore;
  1623. if(se->BeginLine>se->EndLine || (se->BeginLine==se->EndLine && se->BeginBefore>se->EndBefore))
  1624. { LA_SWAP(int,se->BeginLine,se->EndLine); LA_SWAP(int,se->BeginBefore,se->EndBefore); }
  1625. }
  1626. void strSelectLineAll(laStringEdit *se){
  1627. if (!se) return;
  1628. laStringLine *sl;
  1629. int len;
  1630. if (se->CursorLine == -1) sl = strGetBeginLine(se);
  1631. else
  1632. sl = strGetCursorLine(se, 0);
  1633. len = strlenU(sl->Buf);
  1634. se->EndBefore = len; se->EndLine=0;
  1635. se->BeginBefore = 0; se->BeginLine=0;
  1636. se->CursorBefore = len;
  1637. se->CursorLine = 0;
  1638. }
  1639. void strDeselectAll(laStringEdit *se){
  1640. if (!se) return;
  1641. laStringLine *sl;
  1642. int len;
  1643. if (se->CursorLine == -1) sl = strGetBeginLine(se);
  1644. else
  1645. sl = strGetCursorLine(se, 0);
  1646. len = strlenU(sl->Buf);
  1647. se->EndBefore = -1;
  1648. se->BeginBefore = -1;
  1649. se->BeginLine = -1;
  1650. se->EndLine = -1;
  1651. se->CursorBefore = len;
  1652. se->CursorLine = -1;
  1653. }
  1654. void strPanFoward(uint32_t *str, int Before, int Offset){
  1655. int len = strlenU(str);
  1656. int i = len + 1;
  1657. for (i; i >= Before; i--){
  1658. str[i + Offset] = str[i];
  1659. }
  1660. }
  1661. void strSquishBackward(uint32_t *str, int Before, int EndBefore){
  1662. int len = strlenU(str);
  1663. int i = Before;
  1664. int Offset = Before - EndBefore;
  1665. if (Before <= 0) return;
  1666. for (i; i <= len; i++){
  1667. str[i - Offset] = str[i];
  1668. }
  1669. }
  1670. void strClearSelection(laStringEdit *se){
  1671. //if (se->EndLine == -1) return;
  1672. if (se->BeginLine != se->EndLine){
  1673. int i = 0; int RemovedLines=0;
  1674. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1675. while (sl){
  1676. NextSl = sl->Item.pNext;
  1677. if (i == se->BeginLine){
  1678. sl->Buf[se->BeginBefore] = L'\0';
  1679. }else if (i > se->BeginLine && i < se->EndLine){
  1680. strRemoveLine(se, sl); RemovedLines++;
  1681. }else if (i == se->EndLine){
  1682. strSquishBackward(sl->Buf, se->EndBefore, 0);
  1683. se->CursorLine = i-RemovedLines;
  1684. se->CursorBefore = 0;
  1685. se->BeginLine = -1;
  1686. se->BeginBefore = -1;
  1687. se->EndLine = -1;
  1688. se->EndBefore = -1;
  1689. strBackspace(se);
  1690. }
  1691. if (i > se->EndLine) break;
  1692. i++;
  1693. sl = NextSl;
  1694. }
  1695. }else{
  1696. int i = 0;
  1697. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1698. while (sl){
  1699. NextSl = sl->Item.pNext;
  1700. if (i == se->EndLine) {
  1701. strSquishBackward(sl->Buf, se->EndBefore, se->BeginBefore);
  1702. se->CursorLine = i;
  1703. se->CursorBefore = se->BeginBefore;
  1704. se->BeginLine = -1;
  1705. se->BeginBefore = -1;
  1706. se->EndLine = -1;
  1707. se->EndBefore = -1;
  1708. break;
  1709. }
  1710. i++; sl = NextSl;
  1711. }
  1712. }
  1713. strEnsureCursorVisible(se);
  1714. }
  1715. laStringLine *strGetCursorLine(laStringEdit *se, int* ReturnIndexIfLast){
  1716. if (!se || se->CursorBefore <= -1) return se->Lines.pFirst;
  1717. int i = 0;
  1718. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1719. while (sl){
  1720. NextSl = sl->Item.pNext;
  1721. if (i == se->CursorLine){
  1722. return sl;
  1723. }
  1724. i++;
  1725. sl = NextSl;
  1726. }
  1727. if(ReturnIndexIfLast){ *ReturnIndexIfLast=i-1;}
  1728. return se->Lines.pLast;
  1729. }
  1730. laStringLine *strGetBeginLine(laStringEdit *se){
  1731. if (!se || se->BeginLine <= -1) return se->Lines.pFirst;
  1732. int i = 0;
  1733. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1734. while (sl){
  1735. NextSl = sl->Item.pNext;
  1736. if (i == se->BeginLine){
  1737. return sl;
  1738. }
  1739. i++;
  1740. sl = NextSl;
  1741. }
  1742. return se->Lines.pFirst;
  1743. }
  1744. void strInsertChar(laStringEdit *se, uint32_t a){
  1745. laStringLine *sl;
  1746. strClearSelection(se);
  1747. sl = strGetCursorLine(se, 0);
  1748. if(a==L'\n'){
  1749. laStringLine* nl=memAcquireSimple(sizeof(laStringLine));
  1750. if(sl->Buf[se->CursorBefore]!=L'\0') strcpyU(nl->Buf, &sl->Buf[se->CursorBefore]);
  1751. sl->Buf[se->CursorBefore]=L'\0';
  1752. se->CursorLine++; se->CursorBefore=0;
  1753. lstInsertItemAfter(&se->Lines, nl, sl); se->TotalLines++;
  1754. }else{
  1755. strPanFoward(sl->Buf, se->CursorBefore, 1);
  1756. sl->Buf[se->CursorBefore] = a;
  1757. se->CursorBefore += 1;
  1758. }
  1759. se->CursorPreferBefore = se->CursorBefore;
  1760. strEnsureCursorVisible(se);
  1761. }
  1762. void strBackspace(laStringEdit *se){
  1763. laStringLine *sl;
  1764. int width = 1;
  1765. if (se->CursorBefore == -1){
  1766. strClearSelection(se);
  1767. }else{
  1768. laStringLine *sl;
  1769. sl = strGetCursorLine(se, 0);
  1770. if (se->CursorBefore > 1 && sl->Buf[se->CursorBefore - 2] < 0) width = 2;
  1771. strSquishBackward(sl->Buf, se->CursorBefore, se->CursorBefore - width);
  1772. se->CursorBefore -= width;
  1773. if (se->CursorBefore <= -1){
  1774. if(sl->Item.pPrev){
  1775. laStringLine* ol=sl->Item.pPrev;
  1776. se->CursorBefore = strlenU(ol->Buf);
  1777. se->CursorLine--;
  1778. strcatU(ol->Buf, sl->Buf);
  1779. strRemoveLine(se, sl);
  1780. }
  1781. else {se->CursorBefore = 0;}
  1782. }
  1783. }
  1784. se->CursorPreferBefore = se->CursorBefore;
  1785. strEnsureCursorVisible(se);
  1786. }
  1787. void strMoveView(laStringEdit *se, int DownLines, int RightCharacters){
  1788. se->ViewStartLine+=DownLines;
  1789. se->ViewStartCol+=RightCharacters;
  1790. if(se->ViewStartLine>=se->TotalLines-1) se->ViewStartLine=se->TotalLines-1;
  1791. if(se->ViewStartLine<0) se->ViewStartLine=0;
  1792. if(se->ViewStartCol<0) se->ViewStartCol=0;
  1793. }
  1794. int laCopyFile(char *to, char *from){
  1795. int fd_to, fd_from; char buf[4096];
  1796. ssize_t nread; int saved_errno;
  1797. fd_from = open(from, O_RDONLY); if (fd_from < 0) return -1;
  1798. fd_to = open(to, O_WRONLY|O_CREAT|O_EXCL, 0666); if (fd_to < 0) goto out_error;
  1799. while (nread=read(fd_from,buf,sizeof(buf)), nread>0) {
  1800. char *out_ptr = buf; ssize_t nwritten;
  1801. do { nwritten = write(fd_to, out_ptr, nread);
  1802. if (nwritten >= 0){ nread -= nwritten; out_ptr += nwritten;}
  1803. else if (errno != EINTR){ goto out_error; }
  1804. }while (nread > 0);
  1805. }
  1806. if (nread == 0){
  1807. if (close(fd_to)<0){ fd_to = -1; goto out_error;}
  1808. close(fd_from);
  1809. /* Success! */
  1810. return 0;
  1811. }
  1812. out_error:
  1813. saved_errno = errno;
  1814. close(fd_from); if (fd_to >= 0) close(fd_to);
  1815. errno = saved_errno;
  1816. return -1;
  1817. }
  1818. //======================================================[ translation ]
  1819. void transNewLanguage(const char *LanguageID){
  1820. laTranslationNode *tn = memAcquire(sizeof(laTranslationNode));
  1821. strSafeSet(&tn->LanguageName, LanguageID);
  1822. lstAppendItem(&MAIN.Translation.Languages, tn);
  1823. MAIN.Translation.CurrentLanguage = tn;
  1824. }
  1825. void transSetLanguage(const char *LanguageID){
  1826. laTranslationNode *tn;
  1827. if (!LanguageID){
  1828. MAIN.Translation.CurrentLanguage = 0;
  1829. return;
  1830. }
  1831. for (tn = MAIN.Translation.Languages.pFirst; tn; tn = tn->Item.pNext){
  1832. if (!strcmp(tn->LanguageName->Ptr, LanguageID)){
  1833. MAIN.Translation.CurrentLanguage = tn; return;
  1834. }
  1835. }
  1836. transNewLanguage(LanguageID);
  1837. }
  1838. void transDumpMissMatchRecord(const char *filename){
  1839. laTranslationMatch *tm;
  1840. laListHandle *lst;
  1841. int i;
  1842. FILE *f = fopen(filename, "w"); if (!f) return;
  1843. for (i = 0; i < 256; i++){
  1844. lst = &MAIN.Translation.MisMatches.Entries[i];
  1845. for (tm = lst->pFirst; tm; tm = tm->Item.pNext){ if(tm->Target) fprintf(f, "%s | \n", tm->Target); }
  1846. }
  1847. fclose(f);
  1848. }
  1849. int IsThisTranslationMatch(laTranslationMatch *tm, char *p){
  1850. return (tm->Target && (!strcmp(tm->Target, p)));
  1851. }
  1852. void transNewEntry(const char *Target, const char *replacement){
  1853. laTranslationMatch *tm = memAcquireSimple(sizeof(laTranslationMatch));
  1854. tm->Target = Target; tm->Replacement = replacement;
  1855. hsh256InsertItemCSTR(&MAIN.Translation.CurrentLanguage->Matches, tm, Target);
  1856. }
  1857. void transNewMissEntry(const char *Target){
  1858. if (!hsh256FindItemSTR(&MAIN.Translation.MisMatches, IsThisTranslationMatch, Target)){
  1859. laTranslationMatch *tm = memAcquireSimple(sizeof(laTranslationMatch));
  1860. int len=strlen(Target); tm->Target=memAcquireSimple(len*sizeof(char)+1);
  1861. strcpy(tm->Target,Target);
  1862. hsh256InsertItemCSTR(&MAIN.Translation.MisMatches, tm, Target);
  1863. }
  1864. }
  1865. char *transLate(char *Target){
  1866. if (!MAIN.Translation.CurrentLanguage || !MAIN.Translation.EnableTranslation || !Target || !Target[0]) return Target;
  1867. laTranslationMatch *tm = hsh256FindItemSTR(&MAIN.Translation.CurrentLanguage->Matches, IsThisTranslationMatch, Target);
  1868. if (!tm){ transNewMissEntry(Target); return Target; }
  1869. return tm->Replacement;
  1870. }
  1871. void transState(void *UNUSED, int val){
  1872. if (val) MAIN.Translation.EnableTranslation = 1;
  1873. else MAIN.Translation.EnableTranslation = 0;
  1874. laRedrawCurrentWindow();
  1875. }
  1876. void laOpenInternetLink(char *url){
  1877. laSafeString* s=0; strSafePrint(&s, "xdg-open %s", url);
  1878. system(s->Ptr);
  1879. strSafeDestroy(&s);
  1880. //these were windows stuff
  1881. //HKEY hkRoot, hSubKey;
  1882. //char ValueName[256]={0};
  1883. //char DataValue[256]={0};
  1884. //unsigned long cbValueName = 256;
  1885. //unsigned long cbDataValue = 256;
  1886. //char ShellChar[512]={0};
  1887. //DWORD dwType;
  1888. //
  1889. //ShellExecute(0, "open", link, 0, 0, SW_SHOWNORMAL);
  1890. //
  1891. //return;
  1892. }