*/}}

la_util.c 72 KB

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