*/}}

la_util.c 62 KB

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