*/}}
1
0

la_util.c 62 KB

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