*/}}

la_util.c 63 KB

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