*/}}

la_util.c 61 KB

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