*/}}

la_util.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143
  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 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); printf("left freed %x\n",m); }
  995. }
  996. }
  997. void memNoLonger(){
  998. for(int i=0;i<256;i++){
  999. laMemoryPool* mp; while(mp=lstPopItem(&MAIN.GlobalMemPool.Entries[i])){ memDestroyPool(mp); }
  1000. }
  1001. }
  1002. laStaticMemoryPoolNode *memNewStaticPool(laStaticMemoryPool *smp){
  1003. laStaticMemoryPoolNode *smpn = calloc(1, LA_MEMORY_POOL_128MB);
  1004. smpn->UsedByte = sizeof(laStaticMemoryPoolNode);
  1005. lstPushItem(&smp->Pools, smpn);
  1006. return smpn;
  1007. }
  1008. void *memStaticAcquire(laStaticMemoryPool *smp, int size){
  1009. laStaticMemoryPoolNode *smpn = smp->Pools.pFirst;
  1010. void *ret;
  1011. if (!smpn || (smpn->UsedByte + size) > LA_MEMORY_POOL_128MB) smpn = memNewStaticPool(smp);
  1012. ret = ((BYTE *)smpn) + smpn->UsedByte;
  1013. smpn->UsedByte += size;
  1014. return ret;
  1015. }
  1016. void *memStaticAcquireThread(laStaticMemoryPool *smp, int size){
  1017. laStaticMemoryPoolNode *smpn = smp->Pools.pFirst;
  1018. void *ret;
  1019. //pthread_spin_lock(&smp->csMem);
  1020. if (!smpn || (smpn->UsedByte + size) > LA_MEMORY_POOL_128MB) smpn = memNewStaticPool(smp);
  1021. ret = ((BYTE *)smpn) + smpn->UsedByte;
  1022. smpn->UsedByte += size;
  1023. //pthread_spin_unlock(&smp->csMem);
  1024. return ret;
  1025. }
  1026. void *memStaticDestroy(laStaticMemoryPool *smp){
  1027. laStaticMemoryPoolNode *smpn;
  1028. void *ret;
  1029. while (smpn = lstPopItem(&smp->Pools)){
  1030. FreeMem(smpn);
  1031. }
  1032. smp->EachSize = 0;
  1033. return ret;
  1034. }
  1035. void la_ReferencedBlockDeleted(void* This, laItemUserLinker* iul){
  1036. void** user=iul->Pointer.p; if(*user==This){ (*user)=0; laStopUsingDataBlock(iul->Additional, 0, This); }
  1037. }
  1038. void la_ReferrerDeleted(void* This, laItemUserLinker* iul){
  1039. void* instance=iul->Pointer.p; if(instance!=This){ laStopUsingDataBlock(instance, 0, This); }
  1040. }
  1041. void memAssignRef(void* This, void** ptr, void* instance){
  1042. laItemUserLinker* iul;
  1043. if(!This||!ptr) return;
  1044. if(instance){
  1045. laUseDataBlock(instance, 0, 0, ptr, la_ReferencedBlockDeleted, 0)->Additional=This;
  1046. laUseDataBlock(This, 0, 0, instance, la_ReferrerDeleted, 0);
  1047. }else{
  1048. laStopUsingDataBlock((*ptr), 0, This);
  1049. laStopUsingDataBlock(This, 0, (*ptr));
  1050. }
  1051. (*ptr)=instance;
  1052. }
  1053. void memAssignRefSafe(laSubProp* sp, void* This, void** ptr, void* instance){
  1054. laPropContainer* pc=sp?la_EnsureSubTarget(sp,instance):0;
  1055. if(pc&&!pc->OtherAlloc) memAssignRef(This,ptr,instance);
  1056. else (*ptr)=instance;
  1057. }
  1058. //=======================================================================[str]
  1059. char *strGetNextString(char **pivot, char *NextMark){
  1060. int lenth = 0;
  1061. char *countP = *pivot;
  1062. char *result = 0;
  1063. int FloatArg = 0;
  1064. int i,advance;
  1065. if (**pivot == L'\0') return 0;
  1066. if (*NextMark == L'~') FloatArg = 1;
  1067. // container@identifier=window container#window contianer%
  1068. int UC=1;
  1069. while (!lenth){
  1070. for (countP; *countP != L'.' && *(*pivot) != L'\0' && UC && *countP && *countP != L'@' && *countP != L'=' && *countP != L'#' && *countP != L'$';){
  1071. if((*countP)=='\\'){
  1072. countP++; lenth++; }
  1073. UC = laToUnicode(countP, &advance);
  1074. lenth+=advance;
  1075. countP+=advance;
  1076. }
  1077. if (lenth || (*countP) == 0) break;
  1078. (*pivot)++; countP++;
  1079. }
  1080. *NextMark = (*pivot)[lenth];
  1081. if (!(*NextMark)) *NextMark = L'.';
  1082. if (lenth){
  1083. result = CreateNewBuffer(char, lenth + 1);
  1084. int pi=0; for (i = 0; i < lenth; i++){
  1085. if((*pivot)[i]=='\\'){ continue; }
  1086. result[pi] = (*pivot)[i]; pi++;
  1087. }
  1088. result[pi] = L'\0';
  1089. if ((*pivot)[lenth] == L'\0') *pivot = &((*pivot)[lenth]);
  1090. else
  1091. (*pivot) += lenth + 1;
  1092. return result;
  1093. }else{
  1094. return 0;
  1095. }
  1096. };
  1097. int strGetStringTerminateBy(char *content, char terminator, char *Out){
  1098. int Ofst = 0;
  1099. int Skip = 0;
  1100. int i = 0, advance;
  1101. if ((!content) || (*content == L'\0')) return 0;
  1102. int UC;
  1103. for (Ofst; content[Ofst] != terminator && content[Ofst] != L'\0'; ){
  1104. UC = laToUnicode(&content[Ofst], &advance);
  1105. for(int a=0;a<advance;a++){
  1106. Out[i] = content[Ofst];
  1107. Ofst++; i++;
  1108. }
  1109. }
  1110. Out[i] = 0;
  1111. return i;
  1112. };
  1113. char *strGetNewStringTerminateBy_PivotOver(char *content, char terminator, char **NewPivot, int IgnoreSpace){
  1114. int Ofst = 0;
  1115. int Skip = 0;
  1116. int i = 0;
  1117. char *NewString;
  1118. if (!content || *content == L'\0') return 0;
  1119. if (IgnoreSpace) for (i; content[i] == L' '; i++) ;
  1120. int UC,advance;
  1121. for (Ofst; content[Ofst] != terminator && content[Ofst] != L'\0';){
  1122. UC = laToUnicode(&content[Ofst], &advance);
  1123. Ofst+=advance;
  1124. }
  1125. NewString = CreateNewBuffer(char, Ofst + 1 - i);
  1126. memcpy(NewString, &content[i], sizeof(char) * (Ofst - i));
  1127. NewString[Ofst - i] = L'\0';
  1128. *NewPivot = content[Ofst]?&content[Ofst + 1]:&content[Ofst];
  1129. return NewString;
  1130. };
  1131. int strHeadOfStringMatch(char *Str, char *SubStr){
  1132. int len = strlen(SubStr);
  1133. int i = 0;
  1134. for (i; i < len; i++){
  1135. if (Str[i] != SubStr[i]) return 0;
  1136. }
  1137. return 1;
  1138. }
  1139. int strSkipSegmet(char **pivot, char *content){
  1140. if (!pivot || !(*pivot) || !(*(*pivot)) || !content) return 0;
  1141. if (strHeadOfStringMatch(*pivot, content)){
  1142. (*pivot) += strlen(content);
  1143. return 1;
  1144. }
  1145. return 0;
  1146. }
  1147. char *strGetLastSegment(char *Content, char Seperator){
  1148. char *p = Content;
  1149. char *pn = Content;
  1150. while (1){
  1151. while (*pn != Seperator){
  1152. if (!(*pn)) return p;
  1153. pn++;
  1154. }
  1155. pn++;
  1156. p = pn;
  1157. }
  1158. }
  1159. void strDiscardLastSegmentSeperateBy(char *Content, char Seperator){
  1160. char *p = Content;
  1161. char *pn = Content;
  1162. while (1){
  1163. while (*pn != Seperator){
  1164. if (!(*pn)){
  1165. *p = 0;
  1166. return;
  1167. }
  1168. pn++;
  1169. }
  1170. p = pn;
  1171. pn++;
  1172. }
  1173. }
  1174. void strDiscardSameBeginningSeperatedBy(char *s1, char *s2, char **Result1, char **Result2, char Seperator){
  1175. int i = 0;
  1176. int p = 0;
  1177. while (s1[i] == s2[i]){
  1178. i++;
  1179. if (s1[i] == Seperator) p = i;
  1180. if (!s1[i]){
  1181. p = i;
  1182. break;
  1183. }
  1184. if (!s2[i]){
  1185. p = i;
  1186. break;
  1187. }
  1188. }
  1189. *Result1 = &s1[p];
  1190. *Result2 = &s2[p];
  1191. }
  1192. int strCountSegmentSeperateBy(char *Content, char Seperator){
  1193. char *p = Content;
  1194. char *pn = Content;
  1195. int c = Content[0] ? (Content[0] == Seperator ? 0 : 1) : 0;
  1196. while (1){
  1197. while (*pn != Seperator){
  1198. if (!(*pn)){
  1199. if ((*p) == Seperator) c--;
  1200. return c;
  1201. }
  1202. p = pn;
  1203. pn++;
  1204. }
  1205. c++;
  1206. pn++;
  1207. }
  1208. return c;
  1209. }
  1210. void strMakeDifferentName(char *Target){
  1211. char *p = strGetLastSegment(Target, '.');
  1212. int Temp;
  1213. if (!sscanf(p, "%d", &Temp)){
  1214. int l = strlen(p);
  1215. if (p[l - 1] != L'.') strcat(p, ".");
  1216. strPrintIntAfter(Target, 0, 001);
  1217. }else{
  1218. sprintf(p, "%d", Temp + 1);
  1219. };
  1220. }
  1221. void strReplaceCharacter(char *Str, char Find, char Replace){
  1222. char *p = Str;
  1223. if (!p) return;
  1224. while (*p){
  1225. if (*p == Find) *p = Replace;
  1226. p++;
  1227. }
  1228. }
  1229. void strToUpperCase(char *Str){
  1230. char *p = Str;
  1231. if (!p) return;
  1232. while (*p){
  1233. if (*p >= L'a' && *p <= L'z') *p += L'A' - L'a';
  1234. p++;
  1235. }
  1236. }
  1237. void strToLowerCase(char *Str){
  1238. char *p = Str;
  1239. if (!p) return;
  1240. while (*p){
  1241. if (*p >= L'A' && *p <= L'A') *p -= L'A' - L'a';
  1242. p++;
  1243. }
  1244. }
  1245. laStringSplitor *strSplitPath(char *path,char terminator){
  1246. laStringPart *sp;
  1247. laStringSplitor *ss;
  1248. char *pivot = path;
  1249. char *temp_result;
  1250. char Type = terminator?terminator:'.';
  1251. char NextType = '.';
  1252. if (!path || !path[0]) return 0;
  1253. ss = memAcquireSimple(sizeof(laStringSplitor));
  1254. while (temp_result = strGetNextString(&pivot, &NextType)){
  1255. if (*temp_result != L'\0'){
  1256. sp = memAcquireSimple(sizeof(laStringPart));
  1257. sp->Content = temp_result;
  1258. lstAppendItem(&ss->parts, sp);
  1259. ss->NumberParts += 1;
  1260. if (NextType == L'$') sp->Type = L'$';
  1261. else
  1262. sp->Type = Type;
  1263. if (sp->Type == L'='){
  1264. if (sp->Content[0] >= L'0' && sp->Content[0] <= 9){
  1265. sscanf(sp->Content, "%d", &sp->IntValue);
  1266. }
  1267. }
  1268. if (NextType == L'$') NextType = L'.';
  1269. Type = NextType;
  1270. }
  1271. }
  1272. if (ss->NumberParts == 0){
  1273. strDestroyStringSplitor(&ss);
  1274. return 0;
  1275. }
  1276. return ss;
  1277. };
  1278. void DF_ClearStingParts(laStringPart *sp){
  1279. FreeMem(sp->Content);
  1280. };
  1281. int strDestroyStringSplitor(laStringSplitor **ss){
  1282. if (!(*ss)) return 0;
  1283. lstForAllItemsDo(DF_ClearStingParts, &(*ss)->parts);
  1284. lstDestroyList(&(*ss)->parts);
  1285. memFree(*ss);
  1286. *ss = 0;
  1287. return 1;
  1288. }
  1289. char * strSub(char *input, char *substring, char *replace){
  1290. int number_of_matches = 0;
  1291. size_t substring_size = strlen(substring), replace_size = strlen(replace), buffer_size;
  1292. char *buffer, *bp, *ip;
  1293. if (substring_size){
  1294. ip = strstr(input, substring);
  1295. while (ip != NULL){
  1296. number_of_matches++;
  1297. ip = strstr(ip+substring_size, substring);
  1298. }
  1299. }
  1300. else number_of_matches = strlen (input) + 1;
  1301. buffer_size = strlen(input) + number_of_matches*(replace_size - substring_size) + 1;
  1302. if ((buffer = ((char *) malloc(buffer_size))) == NULL){
  1303. return NULL;
  1304. }
  1305. bp = buffer;
  1306. ip = strstr(input, substring);
  1307. while ((ip != NULL) && (*input != '\0')){
  1308. if (ip == input){
  1309. memcpy (bp, replace, replace_size+1);
  1310. bp += replace_size;
  1311. if (substring_size)input += substring_size;
  1312. else*(bp++) = *(input++);
  1313. ip = strstr(input, substring);
  1314. }
  1315. else while (input != ip) *(bp++) = *(input++);
  1316. }
  1317. if (substring_size)strcpy (bp, input);
  1318. else memcpy (bp, replace, replace_size+1);
  1319. return buffer;
  1320. }
  1321. char buff[128]={0};
  1322. int strMakeInstructions(laStringSplitor **result, char *content){
  1323. laStringPart *sp;
  1324. laStringSplitor *ss = *result;
  1325. char *pivot = content;
  1326. unsigned char *temp_result;
  1327. if (!content || !content[0]) return 0;
  1328. if (!ss) ss = *result = memAcquireSimple(sizeof(laStringSplitor));
  1329. while (temp_result = strGetNewStringTerminateBy_PivotOver(pivot, '=', &pivot, 0)){
  1330. if (*temp_result != L'\0'){
  1331. sp = memAcquireSimple(sizeof(laStringPart));
  1332. sp->Content = temp_result;
  1333. lstAppendItem(&ss->parts, sp);
  1334. ss->NumberParts += 1;
  1335. }
  1336. temp_result = strGetNewStringTerminateBy_PivotOver(pivot, ';', &pivot, 0);
  1337. if (!temp_result) break;
  1338. if (*temp_result != L'\0'){
  1339. sp = memAcquireSimple(sizeof(laStringPart));
  1340. sp->Content = temp_result;
  1341. lstAppendItem(&ss->parts, sp);
  1342. ss->NumberParts += 1;
  1343. if (temp_result[0] >= L'0' && temp_result[0] <= L'9' || temp_result[0]>=128){
  1344. sscanf(temp_result, "%d", &sp->IntValue);
  1345. sscanf(temp_result, "%lf", &sp->FloatValue);
  1346. }
  1347. }
  1348. }
  1349. if (ss->NumberParts == 0){
  1350. strDestroyStringSplitor(&ss);
  1351. return 0;
  1352. }
  1353. return 1;
  1354. }
  1355. laStringPart *strGetArgument(laStringSplitor *ss, char *content){
  1356. laStringPart *sp;
  1357. if (!ss) return 0;
  1358. for (sp = ss->parts.pFirst; sp; sp = sp->Item.pNext ? ((laListItem *)sp->Item.pNext)->pNext : 0){
  1359. if (strSame(content, sp->Content)) return sp->Item.pNext;
  1360. }
  1361. return 0;
  1362. }
  1363. char *strGetArgumentString(laStringSplitor *ss, char *content){
  1364. laStringPart *sp;
  1365. if (!ss) return 0;
  1366. for (sp = ss->parts.pFirst; sp; sp = sp->Item.pNext ? ((laListItem *)sp->Item.pNext)->pNext : 0){
  1367. if (strSame(content, sp->Content)) return sp->Item.pNext ? ((laStringPart *)sp->Item.pNext)->Content : 0;
  1368. }
  1369. return 0;
  1370. }
  1371. int strArgumentMatch(laStringSplitor *ss, char *id, char *value){
  1372. laStringPart *sp;
  1373. if (!ss) return 0;
  1374. for (sp = ss->parts.pFirst; sp; sp = sp->Item.pNext ? ((laListItem *)sp->Item.pNext)->pNext : 0){
  1375. if (strSame(id, sp->Content)) return (strSame(((laStringPart *)sp->Item.pNext)->Content, value));
  1376. }
  1377. return 0;
  1378. }
  1379. int strGetIntSimple(char *content){
  1380. int a;
  1381. sscanf(content, "%d", &a);
  1382. return a;
  1383. }
  1384. real strGetFloatSimple(char *content){
  1385. real a;
  1386. sscanf(content, "%lf", &a);
  1387. return a;
  1388. }
  1389. void strConvInt_CString(int src, char *dest, int lenth){
  1390. sprintf(dest, "%d", src);
  1391. };
  1392. void strConvFloat_CString(real src, char *dest, int lenth){
  1393. sprintf(dest, "%lf", src);
  1394. };
  1395. void strCopyFull(char *dest, char *src){
  1396. if (src && dest) strcpy(dest, src);
  1397. }
  1398. void strCopySized(char *dest, int LenthLim, char *src){
  1399. if (src && dest) strcpy(dest, src);
  1400. }
  1401. void strPrintFloatAfter(char *dest, int LenthLim, int bits, real data){
  1402. char temp[64]={0};
  1403. sprintf(temp, "%.*lf", bits, data);
  1404. strcat(dest, temp);
  1405. }
  1406. void strPrintIntAfter(char *dest, int LenthLim, int data){
  1407. char temp[64]={0};
  1408. sprintf(&temp[0], "%d", data);
  1409. strcat(dest, temp);
  1410. }
  1411. void strEscapePath(char* OutCanBeSame, char* path){
  1412. char t[256]={0}; int ti=0;
  1413. for(int i=0;path[i];i++,ti++){
  1414. if(path[i]=='.'){ t[ti]='\\'; ti++; }
  1415. t[ti]=path[i];
  1416. }
  1417. strcpy(OutCanBeSame,t);
  1418. }
  1419. int strSame(char *src, char *dest){
  1420. return (src && dest && !strcmp(src, dest));
  1421. }
  1422. void strSafeDestroy(laSafeString **ss){
  1423. if (!*ss) return; lstRemoveItem(&SSC.SafeStrings, *ss);
  1424. if((*ss)->Ptr) memFree((*ss)->Ptr);
  1425. memFree(*ss); *ss=0;
  1426. }
  1427. void strSafeSet(laSafeString **ss, char *Content){
  1428. int len;
  1429. if (!Content||!Content[0]){ strSafeDestroy(ss); return; }
  1430. len = strlen(Content);
  1431. if (len < 1) return;
  1432. if (*ss){
  1433. char* mem=memAcquireSimple(sizeof(char)*(len+1));
  1434. strcpy(mem, Content);
  1435. memFree((*ss)->Ptr);
  1436. (*ss)->Ptr=mem;
  1437. return;
  1438. }
  1439. (*ss) = memAcquireSimple(sizeof(laSafeString));
  1440. (*ss)->Ptr = memAcquireSimple(sizeof(char)*(len+1));
  1441. strcpy((*ss)->Ptr, Content);
  1442. lstAppendItem(&SSC.SafeStrings, *ss);
  1443. }
  1444. void strSafeAppend(laSafeString **ss, char *Content){
  1445. if(!ss || !(*ss) || !Content){ strSafeSet(ss, Content); return; }
  1446. int OrigLen=strlen((*ss)->Ptr), ContentLen=strlen(Content);
  1447. char* mem=memAcquireSimple(sizeof(char)*(OrigLen+ContentLen+1));
  1448. memcpy(mem, (*ss)->Ptr, sizeof(char)*OrigLen);
  1449. memcpy(mem+sizeof(char)*OrigLen, Content, sizeof(char)*ContentLen);
  1450. mem[OrigLen+ContentLen]=0;
  1451. memFree((*ss)->Ptr);
  1452. (*ss)->Ptr=mem;
  1453. }
  1454. void strSafePrint(laSafeString **ss, char *Format, ...){
  1455. char content[512]; va_list va; va_start(va, Format); vsprintf(content, Format, va); va_end(va);
  1456. strSafeAppend(ss,content);
  1457. }
  1458. void strSafePrintV(laSafeString **ss, char *Format, va_list args){
  1459. char content[512]; va_list va; vsprintf(content, Format, args);
  1460. strSafeAppend(ss,content);
  1461. }
  1462. void strSafeDump(){
  1463. laSafeString*ss;
  1464. while(ss=lstPopItem(&SSC.SafeStrings)){
  1465. //if(ss->Ptr) printf("[String not freed] \"%s\"\n", ss->Ptr);
  1466. }
  1467. }
  1468. void strBeginEdit(laStringEdit **se, char *FullStr){
  1469. char *p = FullStr;
  1470. char buf[1024];
  1471. laStringEdit *nse = CreateNew(laStringEdit);
  1472. if(*se){ memcpy(nse,*se,sizeof(laStringEdit)); nse->Lines.pFirst=nse->Lines.pLast=0; nse->TotalLines=0; }
  1473. strEndEdit(se, 1);
  1474. nse->_BeginLine = -1; nse->_BeginBefore = -1;
  1475. if (FullStr && FullStr[0]){
  1476. while ((*p)){
  1477. laStringLine *sl = memAcquireSimple(sizeof(laStringLine));
  1478. p += strGetStringTerminateBy(p, '\n', buf);
  1479. strToUnicode(sl->Buf, buf);
  1480. lstAppendItem(&nse->Lines, sl); nse->TotalLines++;
  1481. if(*p){ p+=1; }
  1482. }
  1483. }
  1484. if (!nse->Lines.pFirst){
  1485. laStringLine *sl = memAcquireSimple(sizeof(laStringLine));
  1486. lstAppendItem(&nse->Lines, sl); nse->TotalLines++;
  1487. }
  1488. *se=nse;
  1489. }
  1490. char* strGetEditString(laStringEdit *se, int SelectionOnly){
  1491. if(!se) return 0; char* result=0; int next=0, max=0, len=0;
  1492. arrEnsureLength(&result, 0, &max, sizeof(char)); int NextChar=0;
  1493. int Line=0, starti=0, endat=INT_MAX;
  1494. for(laStringLine* sl=se->Lines.pFirst;sl;sl=sl->Item.pNext,Line++){
  1495. starti=0;
  1496. if(SelectionOnly && Line<se->BeginLine){ continue; }
  1497. if(SelectionOnly && Line==se->BeginLine){ starti=se->BeginBefore; }
  1498. int tlen=strlenU(&sl->Buf[starti]); int Extra=sl->Item.pNext?2:1;
  1499. arrEnsureLength(&result, (len+tlen)*4+Extra, &max, sizeof(char));
  1500. if(SelectionOnly && Line==se->EndLine){ endat=NextChar+se->EndBefore-starti; }
  1501. NextChar+=strToUTF8Lim(&result[NextChar], &sl->Buf[starti], endat); len+=tlen;
  1502. if(Extra==2){ result[NextChar]='\n'; NextChar+=1; }
  1503. if(SelectionOnly && Line==se->EndLine){ break; }
  1504. }
  1505. return result;
  1506. }
  1507. char* strEndEdit(laStringEdit **se, int FreeString){
  1508. char *p=0; laStringLine *sl, *NextSl;
  1509. if (!se || !(*se)) return 0;
  1510. p=strGetEditString(*se, 0);
  1511. while (sl=lstPopItem(&(*se)->Lines)){ memFree(sl); }
  1512. FreeMem(*se); *se=0;
  1513. if(FreeString && p){ free(p); p=0; }
  1514. return p;
  1515. }
  1516. void strSetEditViewRange(laStringEdit* se, int Lines, int Cols){
  1517. se->ViewHeight = Lines; se->ViewWidth = Cols;
  1518. }
  1519. void strEnsureCursorVisible(laStringEdit* se){
  1520. if(!se->ViewHeight || !se->ViewWidth || se->CursorLine<0 || se->CursorBefore<0 ){return;}
  1521. if(se->CursorLine>se->ViewHeight+se->ViewStartLine-1){ se->ViewStartLine=se->CursorLine-se->ViewHeight+1; }
  1522. if(se->CursorLine<se->ViewStartLine){ se->ViewStartLine=se->CursorLine; }
  1523. if(se->CursorBefore>se->ViewStartCol+se->ViewWidth-1){ se->ViewStartCol=se->CursorBefore-se->ViewWidth+1; }
  1524. if(se->CursorBefore<se->ViewStartCol){ se->ViewStartCol=se->CursorBefore; }
  1525. }
  1526. void strRemoveLine(laStringEdit *se, laStringLine *sl){
  1527. lstRemoveItem(&se->Lines, sl);
  1528. memFree(sl); se->TotalLines--;
  1529. }
  1530. void strRemoveLineI(laStringEdit *se, int LineIndex){
  1531. int i = 0;
  1532. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1533. while (sl){
  1534. NextSl = sl->Item.pNext;
  1535. if (i == LineIndex){ strRemoveLine(se, sl); break; }
  1536. i++;
  1537. sl = NextSl;
  1538. }
  1539. }
  1540. void strSetCursor(laStringEdit *se, int LineIndex, int BeforeIndex){
  1541. int maxbefore;
  1542. if (!se) return;
  1543. if(LineIndex<0){LineIndex=0;}
  1544. se->CursorLine = LineIndex;
  1545. maxbefore = strlenU(strGetCursorLine(se, &se->CursorLine)->Buf);
  1546. BeforeIndex = BeforeIndex < 0 ? 0 : BeforeIndex > maxbefore ? maxbefore : BeforeIndex;
  1547. se->CursorBefore = BeforeIndex;
  1548. se->BeginLine = -1;
  1549. se->BeginBefore = -1;
  1550. se->EndLine = -1;
  1551. se->EndBefore = -1;
  1552. strEnsureCursorVisible(se);
  1553. }
  1554. void strMoveCursor(laStringEdit *se, int Left, int Select){
  1555. int maxbefore;
  1556. int BeforeIndex;
  1557. int width = 1;
  1558. laStringLine *sl;
  1559. if (!se) return;
  1560. if(Select){ strLazySelect(se); } else { strCancelSelect(se); }
  1561. sl = strGetCursorLine(se, 0);
  1562. maxbefore = strlenU(sl->Buf);
  1563. BeforeIndex = se->CursorBefore - (Left ? 1 : -1);
  1564. if(BeforeIndex<0){
  1565. strSetCursor(se, se->CursorLine-1, INT_MAX);
  1566. }elif(BeforeIndex>maxbefore && se->CursorLine<se->TotalLines-1){
  1567. strSetCursor(se, se->CursorLine+1, 0);
  1568. }else{
  1569. se->CursorBefore = BeforeIndex>=maxbefore?maxbefore:BeforeIndex;
  1570. }
  1571. se->CursorPreferBefore = se->CursorBefore;
  1572. se->BeginLine = -1;
  1573. se->BeginBefore = -1;
  1574. se->EndLine = -1;
  1575. se->EndBefore = -1;
  1576. if(Select){ strEndSelect(se); }
  1577. strEnsureCursorVisible(se);
  1578. }
  1579. void strMoveCursorLine(laStringEdit *se, int Up, int Select){
  1580. int Line, maxbefore, LastIndex=-1;
  1581. laStringLine *sl;
  1582. if (!se) return;
  1583. if(Select){ strLazySelect(se); } else { strCancelSelect(se); }
  1584. Line=se->CursorLine - (Up? 1:-1);
  1585. if(Line<0) {Line=0;}
  1586. se->CursorLine = Line;
  1587. sl = strGetCursorLine(se, &LastIndex);
  1588. if(LastIndex>=0){ se->CursorLine = LastIndex; se->CursorPreferBefore=10000; }
  1589. maxbefore = strlenU(sl->Buf);
  1590. se->CursorBefore = se->CursorPreferBefore;
  1591. if(se->CursorBefore>maxbefore){ se->CursorBefore = maxbefore; }
  1592. if(LastIndex>=0){se->CursorPreferBefore=se->CursorBefore;}
  1593. if(Select){ strEndSelect(se); }
  1594. strEnsureCursorVisible(se);
  1595. }
  1596. int strHasSelection(laStringEdit* se){
  1597. return se->BeginBefore!=se->EndBefore||se->BeginLine!=se->EndLine;
  1598. }
  1599. void strCancelSelect(laStringEdit *se){
  1600. if (!se) return;
  1601. se->_BeginLine = -1;
  1602. se->_BeginBefore = -1;
  1603. se->BeginLine = -1;
  1604. se->EndLine = -1;
  1605. se->BeginBefore = -1;
  1606. se->EndBefore = -1;
  1607. }
  1608. void strLazySelect(laStringEdit *se){
  1609. if (!se || se->_BeginLine>=0) return;
  1610. se->_BeginLine = se->CursorLine;
  1611. se->_BeginBefore = se->CursorBefore;
  1612. }
  1613. void strEndSelect(laStringEdit *se){
  1614. if (!se) return;
  1615. se->_EndLine = se->CursorLine;
  1616. se->_EndBefore = se->CursorBefore;
  1617. se->BeginLine = se->_BeginLine;
  1618. se->EndLine = se->_EndLine;
  1619. se->BeginBefore = se->_BeginBefore;
  1620. se->EndBefore = se->_EndBefore;
  1621. if(se->BeginLine>se->EndLine || (se->BeginLine==se->EndLine && se->BeginBefore>se->EndBefore))
  1622. { LA_SWAP(int,se->BeginLine,se->EndLine); LA_SWAP(int,se->BeginBefore,se->EndBefore); }
  1623. }
  1624. void strSelectLineAll(laStringEdit *se){
  1625. if (!se) return;
  1626. laStringLine *sl;
  1627. int len;
  1628. if (se->CursorLine == -1) sl = strGetBeginLine(se);
  1629. else
  1630. sl = strGetCursorLine(se, 0);
  1631. len = strlenU(sl->Buf);
  1632. se->EndBefore = len; se->EndLine=0;
  1633. se->BeginBefore = 0; se->BeginLine=0;
  1634. se->CursorBefore = len;
  1635. se->CursorLine = 0;
  1636. }
  1637. void strDeselectAll(laStringEdit *se){
  1638. if (!se) return;
  1639. laStringLine *sl;
  1640. int len;
  1641. if (se->CursorLine == -1) sl = strGetBeginLine(se);
  1642. else
  1643. sl = strGetCursorLine(se, 0);
  1644. len = strlenU(sl->Buf);
  1645. se->EndBefore = -1;
  1646. se->BeginBefore = -1;
  1647. se->BeginLine = -1;
  1648. se->EndLine = -1;
  1649. se->CursorBefore = len;
  1650. se->CursorLine = -1;
  1651. }
  1652. void strPanFoward(uint32_t *str, int Before, int Offset){
  1653. int len = strlenU(str);
  1654. int i = len + 1;
  1655. for (i; i >= Before; i--){
  1656. str[i + Offset] = str[i];
  1657. }
  1658. }
  1659. void strSquishBackward(uint32_t *str, int Before, int EndBefore){
  1660. int len = strlenU(str);
  1661. int i = Before;
  1662. int Offset = Before - EndBefore;
  1663. if (Before <= 0) return;
  1664. for (i; i <= len; i++){
  1665. str[i - Offset] = str[i];
  1666. }
  1667. }
  1668. void strClearSelection(laStringEdit *se){
  1669. //if (se->EndLine == -1) return;
  1670. if (se->BeginLine != se->EndLine){
  1671. int i = 0; int RemovedLines=0;
  1672. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1673. while (sl){
  1674. NextSl = sl->Item.pNext;
  1675. if (i == se->BeginLine){
  1676. sl->Buf[se->BeginBefore] = L'\0';
  1677. }else if (i > se->BeginLine && i < se->EndLine){
  1678. strRemoveLine(se, sl); RemovedLines++;
  1679. }else if (i == se->EndLine){
  1680. strSquishBackward(sl->Buf, se->EndBefore, 0);
  1681. se->CursorLine = i-RemovedLines;
  1682. se->CursorBefore = 0;
  1683. se->BeginLine = -1;
  1684. se->BeginBefore = -1;
  1685. se->EndLine = -1;
  1686. se->EndBefore = -1;
  1687. strBackspace(se);
  1688. }
  1689. if (i > se->EndLine) break;
  1690. i++;
  1691. sl = NextSl;
  1692. }
  1693. }else{
  1694. int i = 0;
  1695. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1696. while (sl){
  1697. NextSl = sl->Item.pNext;
  1698. if (i == se->EndLine) {
  1699. strSquishBackward(sl->Buf, se->EndBefore, se->BeginBefore);
  1700. se->CursorLine = i;
  1701. se->CursorBefore = se->BeginBefore;
  1702. se->BeginLine = -1;
  1703. se->BeginBefore = -1;
  1704. se->EndLine = -1;
  1705. se->EndBefore = -1;
  1706. break;
  1707. }
  1708. i++; sl = NextSl;
  1709. }
  1710. }
  1711. strEnsureCursorVisible(se);
  1712. }
  1713. laStringLine *strGetCursorLine(laStringEdit *se, int* ReturnIndexIfLast){
  1714. if (!se || se->CursorBefore <= -1) return se->Lines.pFirst;
  1715. int i = 0;
  1716. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1717. while (sl){
  1718. NextSl = sl->Item.pNext;
  1719. if (i == se->CursorLine){
  1720. return sl;
  1721. }
  1722. i++;
  1723. sl = NextSl;
  1724. }
  1725. if(ReturnIndexIfLast){ *ReturnIndexIfLast=i-1;}
  1726. return se->Lines.pLast;
  1727. }
  1728. laStringLine *strGetBeginLine(laStringEdit *se){
  1729. if (!se || se->BeginLine <= -1) return se->Lines.pFirst;
  1730. int i = 0;
  1731. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1732. while (sl){
  1733. NextSl = sl->Item.pNext;
  1734. if (i == se->BeginLine){
  1735. return sl;
  1736. }
  1737. i++;
  1738. sl = NextSl;
  1739. }
  1740. return se->Lines.pFirst;
  1741. }
  1742. void strInsertChar(laStringEdit *se, uint32_t a){
  1743. laStringLine *sl;
  1744. strClearSelection(se);
  1745. sl = strGetCursorLine(se, 0);
  1746. if(a==L'\n'){
  1747. laStringLine* nl=memAcquireSimple(sizeof(laStringLine));
  1748. if(sl->Buf[se->CursorBefore]!=L'\0') strcpyU(nl->Buf, &sl->Buf[se->CursorBefore]);
  1749. sl->Buf[se->CursorBefore]=L'\0';
  1750. se->CursorLine++; se->CursorBefore=0;
  1751. lstInsertItemAfter(&se->Lines, nl, sl); se->TotalLines++;
  1752. }else{
  1753. strPanFoward(sl->Buf, se->CursorBefore, 1);
  1754. sl->Buf[se->CursorBefore] = a;
  1755. se->CursorBefore += 1;
  1756. }
  1757. se->CursorPreferBefore = se->CursorBefore;
  1758. strEnsureCursorVisible(se);
  1759. }
  1760. void strBackspace(laStringEdit *se){
  1761. laStringLine *sl;
  1762. int width = 1;
  1763. if (se->CursorBefore == -1){
  1764. strClearSelection(se);
  1765. }else{
  1766. laStringLine *sl;
  1767. sl = strGetCursorLine(se, 0);
  1768. if (se->CursorBefore > 1 && sl->Buf[se->CursorBefore - 2] < 0) width = 2;
  1769. strSquishBackward(sl->Buf, se->CursorBefore, se->CursorBefore - width);
  1770. se->CursorBefore -= width;
  1771. if (se->CursorBefore <= -1){
  1772. if(sl->Item.pPrev){
  1773. laStringLine* ol=sl->Item.pPrev;
  1774. se->CursorBefore = strlenU(ol->Buf);
  1775. se->CursorLine--;
  1776. strcatU(ol->Buf, sl->Buf);
  1777. strRemoveLine(se, sl);
  1778. }
  1779. else {se->CursorBefore = 0;}
  1780. }
  1781. }
  1782. se->CursorPreferBefore = se->CursorBefore;
  1783. strEnsureCursorVisible(se);
  1784. }
  1785. void strMoveView(laStringEdit *se, int DownLines, int RightCharacters){
  1786. se->ViewStartLine+=DownLines;
  1787. se->ViewStartCol+=RightCharacters;
  1788. if(se->ViewStartLine>=se->TotalLines-1) se->ViewStartLine=se->TotalLines-1;
  1789. if(se->ViewStartLine<0) se->ViewStartLine=0;
  1790. if(se->ViewStartCol<0) se->ViewStartCol=0;
  1791. }
  1792. int laCopyFile(char *to, char *from){
  1793. int fd_to, fd_from; char buf[4096];
  1794. ssize_t nread; int saved_errno;
  1795. fd_from = open(from, O_RDONLY); if (fd_from < 0) return -1;
  1796. fd_to = open(to, O_WRONLY|O_CREAT|O_EXCL, 0666); if (fd_to < 0) goto out_error;
  1797. while (nread=read(fd_from,buf,sizeof(buf)), nread>0) {
  1798. char *out_ptr = buf; ssize_t nwritten;
  1799. do { nwritten = write(fd_to, out_ptr, nread);
  1800. if (nwritten >= 0){ nread -= nwritten; out_ptr += nwritten;}
  1801. else if (errno != EINTR){ goto out_error; }
  1802. }while (nread > 0);
  1803. }
  1804. if (nread == 0){
  1805. if (close(fd_to)<0){ fd_to = -1; goto out_error;}
  1806. close(fd_from);
  1807. /* Success! */
  1808. return 0;
  1809. }
  1810. out_error:
  1811. saved_errno = errno;
  1812. close(fd_from); if (fd_to >= 0) close(fd_to);
  1813. errno = saved_errno;
  1814. return -1;
  1815. }
  1816. //======================================================[ translation ]
  1817. void transNewLanguage(const char *LanguageID){
  1818. laTranslationNode *tn = memAcquire(sizeof(laTranslationNode));
  1819. strSafeSet(&tn->LanguageName, LanguageID);
  1820. lstAppendItem(&MAIN.Translation.Languages, tn);
  1821. MAIN.Translation.CurrentLanguage = tn;
  1822. }
  1823. void transSetLanguage(const char *LanguageID){
  1824. laTranslationNode *tn;
  1825. if (!LanguageID){
  1826. MAIN.Translation.CurrentLanguage = 0;
  1827. return;
  1828. }
  1829. for (tn = MAIN.Translation.Languages.pFirst; tn; tn = tn->Item.pNext){
  1830. if (!strcmp(tn->LanguageName->Ptr, LanguageID)){
  1831. MAIN.Translation.CurrentLanguage = tn; return;
  1832. }
  1833. }
  1834. transNewLanguage(LanguageID);
  1835. }
  1836. void transDumpMissMatchRecord(const char *filename){
  1837. laTranslationMatch *tm;
  1838. laListHandle *lst;
  1839. int i;
  1840. FILE *f = fopen(filename, "w"); if (!f) return;
  1841. for (i = 0; i < 256; i++){
  1842. lst = &MAIN.Translation.MisMatches.Entries[i];
  1843. for (tm = lst->pFirst; tm; tm = tm->Item.pNext){ if(tm->Target) fprintf(f, "%s | \n", tm->Target); }
  1844. }
  1845. fclose(f);
  1846. }
  1847. int IsThisTranslationMatch(laTranslationMatch *tm, char *p){
  1848. return (tm->Target && (!strcmp(tm->Target, p)));
  1849. }
  1850. void transNewEntry(const char *Target, const char *replacement){
  1851. laTranslationMatch *tm = memAcquireSimple(sizeof(laTranslationMatch));
  1852. tm->Target = Target; tm->Replacement = replacement;
  1853. hsh256InsertItemCSTR(&MAIN.Translation.CurrentLanguage->Matches, tm, Target);
  1854. }
  1855. void transNewMissEntry(const char *Target){
  1856. if (!hsh256FindItemSTR(&MAIN.Translation.MisMatches, IsThisTranslationMatch, Target)){
  1857. laTranslationMatch *tm = memAcquireSimple(sizeof(laTranslationMatch));
  1858. int len=strlen(Target); tm->Target=memAcquireSimple(len*sizeof(char)+1);
  1859. strcpy(tm->Target,Target);
  1860. hsh256InsertItemCSTR(&MAIN.Translation.MisMatches, tm, Target);
  1861. }
  1862. }
  1863. char *transLate(char *Target){
  1864. if (!MAIN.Translation.CurrentLanguage || !MAIN.Translation.EnableTranslation || !Target || !Target[0]) return Target;
  1865. laTranslationMatch *tm = hsh256FindItemSTR(&MAIN.Translation.CurrentLanguage->Matches, IsThisTranslationMatch, Target);
  1866. if (!tm){ transNewMissEntry(Target); return Target; }
  1867. return tm->Replacement;
  1868. }
  1869. void transState(void *UNUSED, int val){
  1870. if (val) MAIN.Translation.EnableTranslation = 1;
  1871. else MAIN.Translation.EnableTranslation = 0;
  1872. laRedrawCurrentWindow();
  1873. }
  1874. void laOpenInternetLink(char *url){
  1875. laSafeString* s=0; strSafePrint(&s, "xdg-open %s", url);
  1876. system(s->Ptr);
  1877. strSafeDestroy(&s);
  1878. //these were windows stuff
  1879. //HKEY hkRoot, hSubKey;
  1880. //char ValueName[256]={0};
  1881. //char DataValue[256]={0};
  1882. //unsigned long cbValueName = 256;
  1883. //unsigned long cbDataValue = 256;
  1884. //char ShellChar[512]={0};
  1885. //DWORD dwType;
  1886. //
  1887. //ShellExecute(0, "open", link, 0, 0, SW_SHOWNORMAL);
  1888. //
  1889. //return;
  1890. }