*/}}

la_util.c 73 KB

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