*/}}

la_util.c 62 KB

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