*/}}

la_util.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443
  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. mp = MAIN.GlobalMemPool.Entries[Buckle].pFirst;
  990. while (mp && (mp->NodeSize != Size || mp->Hyperlevel!=Hyper))
  991. mp = mp->Item.pNext;
  992. if (!mp) mp = memInitPool(Size, Hyper);
  993. return memAcquireH(mp);
  994. }
  995. void *memAcquireSimple(int Size){
  996. void *mpn = memAcquire_(Size, 0);
  997. return ((char*)mpn)+sizeof(laMemNode0);
  998. }
  999. void *memAcquire(int Size){
  1000. laMemNode *mpn = memAcquire_(Size, 1);
  1001. void* mem = ((char*)mpn)+sizeof(laMemNode);
  1002. return mem;
  1003. }
  1004. void *memAcquireHyperNoAppend(int Size){
  1005. laMemNodeHyper *mpn = memAcquire_(Size, 2);
  1006. void* mem = ((char*)mpn)+sizeof(laMemNodeHyper);
  1007. memMakeHyperData(mpn);
  1008. return mem;
  1009. }
  1010. void *memAcquireHyper(int Size){
  1011. laMemNodeHyper *mpn = memAcquire_(Size, 2);
  1012. void* mem = ((char*)mpn)+sizeof(laMemNodeHyper);
  1013. memMakeHyperData(mpn);
  1014. laListHandle* l=hsh65536DoHashNUID(&MAIN.DBInst2,mpn->NUID.String);
  1015. lstAppendItem(l,mpn);
  1016. return mem;
  1017. }
  1018. void memFree(void *Data){
  1019. if (!Data) return;
  1020. int level; void* head = memGetHead(Data, &level);
  1021. laMemoryPoolPart *mp;
  1022. if(level==2) { mp = ((laMemNodeHyper*)head)->InPool; laDataBlockNoLongerExists(Data,&((laMemNodeHyper*)head)->Users);
  1023. laListHandle* l=hsh65536DoHashNUID(&MAIN.DBInst2,((laMemNodeHyper*)head)->NUID.String); lstRemoveItem(l,head);}
  1024. if(level==1) { mp = ((laMemNode*)head)->InPool; laDataBlockNoLongerExists(Data,&((laMemNode*)head)->Users); }
  1025. if(level==0) { mp = ((laMemNode0*)head)->InPool; }
  1026. laMemoryPool *mph = mp->PoolRoot;
  1027. //lstRemoveItem(&mp->MemoryNodes, head);
  1028. mp->UsedCount--;
  1029. void* head_except_item = ((char*)head)+sizeof(laListItem);
  1030. //memset(head_except_item, 0, ((level==2)?sizeof(laMemNodeHyper):((level==1)?sizeof(laMemNode):sizeof(laMemNode0)))+mph->NodeSize-sizeof(laListItem));
  1031. lstAppendItem(&mp->FreeMemoryNodes, head);
  1032. memset(Data, 0, mph->NodeSize);
  1033. MAIN.ByteCount -= mph->NodeSize;
  1034. if (!mp->UsedCount){
  1035. lstRemoveItem(&mph->Pools, mp);
  1036. FreeMem(mp);
  1037. }
  1038. //if (!mph->Pools.pFirst) {
  1039. // mph->CountPerPool = 0;
  1040. // mph->NodeSize = 0;
  1041. //}
  1042. }
  1043. void memDestroyPool(laMemoryPool *mph){
  1044. laMemoryPool *mp;
  1045. while ((mp = lstPopItem(&mph->Pools))){
  1046. FreeMem(mp);
  1047. }
  1048. FreeMem(mph);
  1049. }
  1050. // Leave memory in an temporary place and if when push difference these are still not acquired, free them.
  1051. void memLeave(void *Data){
  1052. laListHandle* l=hsh65536DoHashLongPtr(MAIN.DBInstMemLeft,Data); lstAppendPointer(l,Data);
  1053. }
  1054. void memTake(void *Data){
  1055. laListHandle* l=hsh65536DoHashLongPtr(MAIN.DBInstMemLeft,Data); lstRemovePointer(l,Data);
  1056. }
  1057. void memFreeRemainingLeftNodes(){
  1058. laListHandle* l; void* m;
  1059. for(int i=0;i<65536;i++){
  1060. l=&MAIN.DBInstMemLeft->Entries[i]; while(m=lstPopPointer(l)){ memFree(m);
  1061. #ifdef DEBUG
  1062. printf("left freed %x\n",m);
  1063. #endif
  1064. }
  1065. }
  1066. }
  1067. void memNoLonger(){
  1068. for(int i=0;i<256;i++){
  1069. laMemoryPool* mp; while(mp=lstPopItem(&MAIN.GlobalMemPool.Entries[i])){ memDestroyPool(mp); }
  1070. }
  1071. }
  1072. laStaticMemoryPoolNode *memNewStaticPool(laStaticMemoryPool *smp){
  1073. laStaticMemoryPoolNode *smpn = calloc(1, LA_MEMORY_POOL_128MB);
  1074. smpn->UsedByte = sizeof(laStaticMemoryPoolNode);
  1075. lstPushItem(&smp->Pools, smpn);
  1076. return smpn;
  1077. }
  1078. void *memStaticAcquire(laStaticMemoryPool *smp, int size){
  1079. laStaticMemoryPoolNode *smpn = smp->Pools.pFirst;
  1080. void *ret;
  1081. if (!smpn || (smpn->UsedByte + size) > LA_MEMORY_POOL_128MB) smpn = memNewStaticPool(smp);
  1082. ret = ((BYTE *)smpn) + smpn->UsedByte;
  1083. smpn->UsedByte += size;
  1084. return ret;
  1085. }
  1086. void *memStaticAcquireThread(laStaticMemoryPool *smp, int size){
  1087. laStaticMemoryPoolNode *smpn = smp->Pools.pFirst;
  1088. void *ret;
  1089. //laSpinLock(&smp->csMem);
  1090. if (!smpn || (smpn->UsedByte + size) > LA_MEMORY_POOL_128MB) smpn = memNewStaticPool(smp);
  1091. ret = ((BYTE *)smpn) + smpn->UsedByte;
  1092. smpn->UsedByte += size;
  1093. //laSpinUnlock(&smp->csMem);
  1094. return ret;
  1095. }
  1096. void *memStaticDestroy(laStaticMemoryPool *smp){
  1097. laStaticMemoryPoolNode *smpn;
  1098. void *ret;
  1099. while (smpn = lstPopItem(&smp->Pools)){
  1100. FreeMem(smpn);
  1101. }
  1102. smp->EachSize = 0;
  1103. return ret;
  1104. }
  1105. void la_ReferencedBlockDeleted(void* This, laItemUserLinker* iul){
  1106. void** user=iul->Pointer.p; if(*user==This){ (*user)=0; } laStopUsingDataBlock(iul->Additional, 0, This); // <<< should always remove.
  1107. }
  1108. void la_ReferrerDeleted(void* This, laItemUserLinker* iul){
  1109. void* instance=iul->Pointer.p; if(instance!=This){ laStopUsingDataBlock(instance, 0, This); }
  1110. }
  1111. void memAssignRef(void* This, void** ptr, void* instance){
  1112. laItemUserLinker* iul;
  1113. if((!This)||(!ptr)) return;
  1114. if(instance){
  1115. laItemUserLinker*iul=laUseDataBlock(instance, 0, 0, ptr, la_ReferencedBlockDeleted, 0); if(iul){iul->Additional=This;}
  1116. laUseDataBlock(This, 0, 0, instance, la_ReferrerDeleted, 0);
  1117. }else{
  1118. laStopUsingDataBlock((*ptr), 0, This);
  1119. laStopUsingDataBlock(This, 0, (*ptr));
  1120. }
  1121. (*ptr)=instance;
  1122. }
  1123. void memAssignRefSafe(laSubProp* sp, void* This, void** ptr, void* instance){
  1124. laPropContainer* pc=sp?la_EnsureSubTarget(sp,instance):0;
  1125. if(pc&&!pc->OtherAlloc) memAssignRef(This,ptr,instance);
  1126. else (*ptr)=instance;
  1127. }
  1128. //=======================================================================[str]
  1129. char *strGetNextString(char **pivot, char *NextMark){
  1130. int lenth = 0;
  1131. char *countP = *pivot;
  1132. char *result = 0;
  1133. int FloatArg = 0;
  1134. int i,advance;
  1135. if (**pivot == U'\0') return 0;
  1136. if (*NextMark == U'~') FloatArg = 1;
  1137. // container@identifier=window container#window contianer%
  1138. int UC=1;
  1139. while (!lenth){
  1140. for (countP; *countP != U'.' && *(*pivot) != U'\0' && UC && *countP && *countP != U'@' && *countP != U'=' && *countP != U'#' && *countP != U'$';){
  1141. if((*countP)=='\\'){
  1142. countP++; lenth++; }
  1143. UC = laToUnicode(countP, &advance);
  1144. lenth+=advance;
  1145. countP+=advance;
  1146. }
  1147. if (lenth || (*countP) == 0) break;
  1148. (*pivot)++; countP++;
  1149. }
  1150. *NextMark = (*pivot)[lenth];
  1151. if (!(*NextMark)) *NextMark = U'.';
  1152. if (lenth){
  1153. result = CreateNewBuffer(char, lenth + 1);
  1154. int pi=0; for (i = 0; i < lenth; i++){
  1155. if((*pivot)[i]=='\\'){ continue; }
  1156. result[pi] = (*pivot)[i]; pi++;
  1157. }
  1158. result[pi] = U'\0';
  1159. if ((*pivot)[lenth] == U'\0') *pivot = &((*pivot)[lenth]);
  1160. else
  1161. (*pivot) += lenth + 1;
  1162. return result;
  1163. }else{
  1164. return 0;
  1165. }
  1166. };
  1167. int strGetStringTerminateBy(char *content, char terminator, char *Out){
  1168. int Ofst = 0;
  1169. int Skip = 0;
  1170. int i = 0, advance;
  1171. if ((!content) || (*content == U'\0')) return 0;
  1172. int UC;
  1173. for (Ofst; content[Ofst] != terminator && content[Ofst] != U'\0'; ){
  1174. UC = laToUnicode(&content[Ofst], &advance);
  1175. for(int a=0;a<advance;a++){
  1176. Out[i] = content[Ofst];
  1177. Ofst++; i++;
  1178. }
  1179. }
  1180. Out[i] = 0;
  1181. return i;
  1182. };
  1183. char *strGetNewStringTerminateBy_PivotOver(char *content, char terminator, char **NewPivot, int IgnoreSpace){
  1184. int Ofst = 0;
  1185. int Skip = 0;
  1186. int i = 0;
  1187. char *NewString;
  1188. if (!content || *content == U'\0') return 0;
  1189. if (IgnoreSpace) for (i; content[i] == U' '; i++) ;
  1190. int UC,advance;
  1191. for (Ofst; content[Ofst] != terminator && content[Ofst] != U'\0';){
  1192. UC = laToUnicode(&content[Ofst], &advance);
  1193. Ofst+=advance;
  1194. }
  1195. NewString = CreateNewBuffer(char, Ofst + 1 - i);
  1196. memcpy(NewString, &content[i], sizeof(char) * (Ofst - i));
  1197. NewString[Ofst - i] = U'\0';
  1198. *NewPivot = content[Ofst]?&content[Ofst + 1]:&content[Ofst];
  1199. return NewString;
  1200. };
  1201. int strHeadOfStringMatch(char *Str, char *SubStr){
  1202. int len = strlen(SubStr);
  1203. int i = 0;
  1204. for (i; i < len; i++){
  1205. if (Str[i] != SubStr[i]) return 0;
  1206. }
  1207. return 1;
  1208. }
  1209. int strSkipSegmet(char **pivot, char *content){
  1210. if (!pivot || !(*pivot) || !(*(*pivot)) || !content) return 0;
  1211. if (strHeadOfStringMatch(*pivot, content)){
  1212. (*pivot) += strlen(content);
  1213. return 1;
  1214. }
  1215. return 0;
  1216. }
  1217. char *strGetLastSegment(char *Content, char Seperator){
  1218. char *p = Content;
  1219. char *pn = Content;
  1220. while (1){
  1221. while (*pn != Seperator){
  1222. if (!(*pn)) return p;
  1223. pn++;
  1224. }
  1225. pn++;
  1226. p = pn;
  1227. }
  1228. }
  1229. void strDiscardLastSegmentSeperateBy(char *Content, char Seperator){
  1230. char *p = Content;
  1231. char *pn = Content;
  1232. while (1){
  1233. while (*pn != Seperator){
  1234. if (!(*pn)){
  1235. *p = 0;
  1236. return;
  1237. }
  1238. pn++;
  1239. }
  1240. p = pn;
  1241. pn++;
  1242. }
  1243. }
  1244. void strDiscardSameBeginningSeperatedBy(char *s1, char *s2, char **Result1, char **Result2, char Seperator){
  1245. int i = 0;
  1246. int p = 0;
  1247. while (s1[i] == s2[i]){
  1248. i++;
  1249. if (s1[i] == Seperator) p = i;
  1250. if (!s1[i]){
  1251. p = i;
  1252. break;
  1253. }
  1254. if (!s2[i]){
  1255. p = i;
  1256. break;
  1257. }
  1258. }
  1259. *Result1 = &s1[p];
  1260. *Result2 = &s2[p];
  1261. }
  1262. int strCountSegmentSeperateBy(char *Content, char Seperator){
  1263. char *p = Content;
  1264. char *pn = Content;
  1265. int c = Content[0] ? (Content[0] == Seperator ? 0 : 1) : 0;
  1266. while (1){
  1267. while (*pn != Seperator){
  1268. if (!(*pn)){
  1269. if ((*p) == Seperator) c--;
  1270. return c;
  1271. }
  1272. p = pn;
  1273. pn++;
  1274. }
  1275. c++;
  1276. pn++;
  1277. }
  1278. return c;
  1279. }
  1280. void strMakeDifferentName(char *Target){
  1281. char *p = strGetLastSegment(Target, '.');
  1282. int Temp;
  1283. if (!sscanf(p, "%d", &Temp)){
  1284. int l = strlen(p);
  1285. if (p[l - 1] != U'.') strcat(p, ".");
  1286. strPrintIntAfter(Target, 0, 001);
  1287. }else{
  1288. sprintf(p, "%d", Temp + 1);
  1289. };
  1290. }
  1291. void strReplaceCharacter(char *Str, char Find, char Replace){
  1292. char *p = Str;
  1293. if (!p) return;
  1294. while (*p){
  1295. if (*p == Find) *p = Replace;
  1296. p++;
  1297. }
  1298. }
  1299. void strToUpper(char *Str){
  1300. char *p = Str; if (!p) return;
  1301. while (*p){
  1302. if (*p >= 'a' && *p <= 'z') *p += 'A' - 'a';
  1303. p++;
  1304. }
  1305. }
  1306. void strToLower(char *Str){
  1307. char *p = Str; if (!p) return;
  1308. while (*p){
  1309. if (*p >= 'A' && *p <= 'Z') *p -= 'A' - 'a';
  1310. p++;
  1311. }
  1312. }
  1313. int tolowerGuarded(int a) {
  1314. if (a >= 'A' && a <= 'Z') a -= 'A' - 'a';
  1315. return a;
  1316. }
  1317. laStringSplitor *strSplitPath(char *path,char terminator){
  1318. laStringPart *sp;
  1319. laStringSplitor *ss;
  1320. char *pivot = path;
  1321. char *temp_result;
  1322. char Type = terminator?terminator:'.';
  1323. char NextType = '.';
  1324. if (!path || !path[0]) return 0;
  1325. ss = memAcquireSimple(sizeof(laStringSplitor));
  1326. while (temp_result = strGetNextString(&pivot, &NextType)){
  1327. if (*temp_result != U'\0'){
  1328. sp = memAcquireSimple(sizeof(laStringPart));
  1329. sp->Content = temp_result;
  1330. lstAppendItem(&ss->parts, sp);
  1331. ss->NumberParts += 1;
  1332. if (NextType == U'$') sp->Type = U'$';
  1333. else
  1334. sp->Type = Type;
  1335. if (sp->Type == U'='){
  1336. if (sp->Content[0] >= U'0' && sp->Content[0] <= 9){
  1337. sscanf(sp->Content, "%d", &sp->IntValue);
  1338. }
  1339. }
  1340. if (NextType == U'$') NextType = U'.';
  1341. Type = NextType;
  1342. }
  1343. }
  1344. if (ss->NumberParts == 0){
  1345. strDestroyStringSplitor(&ss);
  1346. return 0;
  1347. }
  1348. return ss;
  1349. };
  1350. void DF_ClearStingParts(laStringPart *sp){
  1351. FreeMem(sp->Content);
  1352. };
  1353. int strDestroyStringSplitor(laStringSplitor **ss){
  1354. if (!(*ss)) return 0;
  1355. lstForAllItemsDo(DF_ClearStingParts, &(*ss)->parts);
  1356. lstDestroyList(&(*ss)->parts);
  1357. memFree(*ss);
  1358. *ss = 0;
  1359. return 1;
  1360. }
  1361. char * strSub(char *input, char *substring, char *replace){
  1362. int number_of_matches = 0;
  1363. size_t substring_size = strlen(substring), replace_size = strlen(replace), buffer_size;
  1364. char *buffer, *bp, *ip;
  1365. if (substring_size){
  1366. ip = strstr(input, substring);
  1367. while (ip != NULL){
  1368. number_of_matches++;
  1369. ip = strstr(ip+substring_size, substring);
  1370. }
  1371. }
  1372. else number_of_matches = strlen (input) + 1;
  1373. buffer_size = strlen(input) + number_of_matches*(replace_size - substring_size) + 1;
  1374. if ((buffer = ((char *) malloc(buffer_size))) == NULL){
  1375. return NULL;
  1376. }
  1377. bp = buffer;
  1378. ip = strstr(input, substring);
  1379. while ((ip != NULL) && (*input != '\0')){
  1380. if (ip == input){
  1381. memcpy (bp, replace, replace_size+1);
  1382. bp += replace_size;
  1383. if (substring_size)input += substring_size;
  1384. else*(bp++) = *(input++);
  1385. ip = strstr(input, substring);
  1386. }
  1387. else while (input != ip) *(bp++) = *(input++);
  1388. }
  1389. if (substring_size)strcpy (bp, input);
  1390. else memcpy (bp, replace, replace_size+1);
  1391. return buffer;
  1392. }
  1393. char buff[128]={0};
  1394. int strMakeInstructions(laStringSplitor **result, char *content){
  1395. laStringPart *sp;
  1396. laStringSplitor *ss = *result;
  1397. char *pivot = content;
  1398. unsigned char *temp_result;
  1399. if (!content || !content[0]) return 0;
  1400. if (!ss) ss = *result = memAcquireSimple(sizeof(laStringSplitor));
  1401. while (temp_result = strGetNewStringTerminateBy_PivotOver(pivot, '=', &pivot, 0)){
  1402. if (*temp_result != U'\0'){
  1403. sp = memAcquireSimple(sizeof(laStringPart));
  1404. sp->Content = temp_result;
  1405. lstAppendItem(&ss->parts, sp);
  1406. ss->NumberParts += 1;
  1407. }
  1408. temp_result = strGetNewStringTerminateBy_PivotOver(pivot, ';', &pivot, 0);
  1409. if (!temp_result) break;
  1410. if (*temp_result != U'\0'){
  1411. sp = memAcquireSimple(sizeof(laStringPart));
  1412. sp->Content = temp_result;
  1413. lstAppendItem(&ss->parts, sp);
  1414. ss->NumberParts += 1;
  1415. if (temp_result[0] >= U'0' && temp_result[0] <= U'9' || temp_result[0]>=128){
  1416. sscanf(temp_result, "%d", &sp->IntValue);
  1417. sscanf(temp_result, "%lf", &sp->FloatValue);
  1418. }
  1419. }
  1420. }
  1421. if (ss->NumberParts == 0){
  1422. strDestroyStringSplitor(&ss);
  1423. return 0;
  1424. }
  1425. return 1;
  1426. }
  1427. laStringPart *strGetArgument(laStringSplitor *ss, char *content){
  1428. laStringPart *sp;
  1429. if (!ss) return 0;
  1430. for (sp = ss->parts.pFirst; sp; sp = sp->Item.pNext ? ((laListItem *)sp->Item.pNext)->pNext : 0){
  1431. if (strSame(content, sp->Content)) return sp->Item.pNext;
  1432. }
  1433. return 0;
  1434. }
  1435. char *strGetArgumentString(laStringSplitor *ss, char *content){
  1436. laStringPart *sp;
  1437. if (!ss) return 0;
  1438. for (sp = ss->parts.pFirst; sp; sp = sp->Item.pNext ? ((laListItem *)sp->Item.pNext)->pNext : 0){
  1439. if (strSame(content, sp->Content)) return sp->Item.pNext ? ((laStringPart *)sp->Item.pNext)->Content : 0;
  1440. }
  1441. return 0;
  1442. }
  1443. int strArgumentMatch(laStringSplitor *ss, char *id, char *value){
  1444. laStringPart *sp;
  1445. if (!ss) return 0;
  1446. for (sp = ss->parts.pFirst; sp; sp = sp->Item.pNext ? ((laListItem *)sp->Item.pNext)->pNext : 0){
  1447. if (strSame(id, sp->Content)) return (strSame(((laStringPart *)sp->Item.pNext)->Content, value));
  1448. }
  1449. return 0;
  1450. }
  1451. int strGetIntSimple(char *content){
  1452. int a;
  1453. sscanf(content, "%d", &a);
  1454. return a;
  1455. }
  1456. real strGetFloatSimple(char *content){
  1457. real a;
  1458. sscanf(content, "%lf", &a);
  1459. return a;
  1460. }
  1461. void strConvInt_CString(int src, char *dest, int lenth){
  1462. sprintf(dest, "%d", src);
  1463. };
  1464. void strConvFloat_CString(real src, char *dest, int lenth){
  1465. sprintf(dest, "%lf", src);
  1466. };
  1467. void strCopyFull(char *dest, char *src){
  1468. if (src && dest) strcpy(dest, src);
  1469. }
  1470. void strCopySized(char *dest, int LenthLim, char *src){
  1471. if (src && dest) strcpy(dest, src);
  1472. }
  1473. void strPrintFloatAfter(char *dest, int LenthLim, int bits, real data){
  1474. char temp[64]={0};
  1475. sprintf(temp, "%.*lf", bits, data);
  1476. strcat(dest, temp);
  1477. }
  1478. void strPrintIntAfter(char *dest, int LenthLim, int data){
  1479. char temp[64]={0};
  1480. sprintf(&temp[0], "%d", data);
  1481. strcat(dest, temp);
  1482. }
  1483. void strEscapePath(char* OutCanBeSame, char* path){
  1484. char t[256]={0}; int ti=0;
  1485. for(int i=0;path[i];i++,ti++){
  1486. if(path[i]=='.'){ t[ti]='\\'; ti++; }
  1487. t[ti]=path[i];
  1488. }
  1489. strcpy(OutCanBeSame,t);
  1490. }
  1491. int strSame(char *src, char *dest){
  1492. return (src && dest && !strcmp(src, dest));
  1493. }
  1494. void strSafeDestroy(laSafeString **ss){
  1495. if (!*ss) return; lstRemoveItem(&SSC.SafeStrings, *ss);
  1496. if((*ss)->Ptr) memFree((*ss)->Ptr);
  1497. memFree(*ss); *ss=0;
  1498. }
  1499. void strSafeSet(laSafeString **ss, char *Content){
  1500. int len;
  1501. if (!Content||!Content[0]){ strSafeDestroy(ss); return; }
  1502. len = strlen(Content);
  1503. if (len < 1) return;
  1504. if (*ss){
  1505. char* mem=memAcquireSimple(sizeof(char)*(len+1));
  1506. strcpy(mem, Content);
  1507. memFree((*ss)->Ptr);
  1508. (*ss)->Ptr=mem;
  1509. return;
  1510. }
  1511. (*ss) = memAcquireSimple(sizeof(laSafeString));
  1512. (*ss)->Ptr = memAcquireSimple(sizeof(char)*(len+1));
  1513. strcpy((*ss)->Ptr, Content);
  1514. lstAppendItem(&SSC.SafeStrings, *ss);
  1515. }
  1516. void strSafeAppend(laSafeString **ss, char *Content){
  1517. if(!ss || !(*ss) || !Content){ strSafeSet(ss, Content); return; }
  1518. int OrigLen=strlen((*ss)->Ptr), ContentLen=strlen(Content);
  1519. char* mem=memAcquireSimple(sizeof(char)*(OrigLen+ContentLen+1));
  1520. memcpy(mem, (*ss)->Ptr, sizeof(char)*OrigLen);
  1521. memcpy(mem+sizeof(char)*OrigLen, Content, sizeof(char)*ContentLen);
  1522. mem[OrigLen+ContentLen]=0;
  1523. memFree((*ss)->Ptr);
  1524. (*ss)->Ptr=mem;
  1525. }
  1526. void strSafePrint(laSafeString **ss, char *Format, ...){
  1527. char content[512]; va_list va; va_start(va, Format); vsprintf(content, Format, va); va_end(va);
  1528. strSafeAppend(ss,content);
  1529. }
  1530. void strSafePrintV(laSafeString **ss, char *Format, va_list args){
  1531. char content[512]; va_list va; vsprintf(content, Format, args);
  1532. strSafeAppend(ss,content);
  1533. }
  1534. void strSafeDump(){
  1535. laSafeString*ss;
  1536. while(ss=lstPopItem(&SSC.SafeStrings)){
  1537. //if(ss->Ptr) printf("[String not freed] \"%s\"\n", ss->Ptr);
  1538. }
  1539. }
  1540. void strBeginEdit(laStringEdit **se, char *FullStr){
  1541. char *p = FullStr;
  1542. char buf[1024];
  1543. laStringEdit *nse = CreateNew(laStringEdit);
  1544. if(*se){ memcpy(nse,*se,sizeof(laStringEdit)); nse->Lines.pFirst=nse->Lines.pLast=0; nse->TotalLines=0; }
  1545. strEndEdit(se, 1);
  1546. nse->_BeginLine = -1; nse->_BeginBefore = -1;
  1547. if (FullStr && FullStr[0]){
  1548. while ((*p)){
  1549. laStringLine *sl = memAcquireSimple(sizeof(laStringLine));
  1550. p += strGetStringTerminateBy(p, '\n', buf);
  1551. strToUnicode(sl->Buf, buf);
  1552. lstAppendItem(&nse->Lines, sl); nse->TotalLines++;
  1553. if(*p){ p+=1; }
  1554. }
  1555. }
  1556. if (!nse->Lines.pFirst){
  1557. laStringLine *sl = memAcquireSimple(sizeof(laStringLine));
  1558. lstAppendItem(&nse->Lines, sl); nse->TotalLines=1;
  1559. }
  1560. laStringLine *sl = strGetCursorLine(nse, 0); int len=strlen(sl->Buf);
  1561. if(len<nse->CursorBefore){ nse->CursorBefore=len; }
  1562. *se=nse;
  1563. }
  1564. char* strGetEditString(laStringEdit *se, int SelectionOnly){
  1565. if(!se) return 0; char* result=0; int next=0, max=0, len=0;
  1566. arrEnsureLength(&result, 0, &max, sizeof(char)); int NextChar=0;
  1567. int Line=0, starti=0, endat=INT_MAX;
  1568. for(laStringLine* sl=se->Lines.pFirst;sl;sl=sl->Item.pNext,Line++){
  1569. starti=0;
  1570. if(SelectionOnly && Line<se->BeginLine){ continue; }
  1571. if(SelectionOnly && Line==se->BeginLine){ starti=se->BeginBefore; }
  1572. int tlen=strlenU(&sl->Buf[starti]); int Extra=sl->Item.pNext?2:1;
  1573. arrEnsureLength(&result, (len+tlen)*4+Extra, &max, sizeof(char));
  1574. if(SelectionOnly && Line==se->EndLine){ endat=NextChar+se->EndBefore-starti; }
  1575. NextChar+=strToUTF8Lim(&result[NextChar], &sl->Buf[starti], endat); len+=tlen;
  1576. if(Extra==2){ result[NextChar]='\n'; NextChar+=1; }
  1577. if(SelectionOnly && Line==se->EndLine){ break; }
  1578. }
  1579. return result;
  1580. }
  1581. char* strEndEdit(laStringEdit **se, int FreeString){
  1582. char *p=0; laStringLine *sl, *NextSl;
  1583. if (!se || !(*se)) return 0;
  1584. p=strGetEditString(*se, 0);
  1585. while (sl=lstPopItem(&(*se)->Lines)){ memFree(sl); }
  1586. FreeMem(*se); *se=0;
  1587. if(FreeString && p){ free(p); p=0; }
  1588. return p;
  1589. }
  1590. void strSetEditViewRange(laStringEdit* se, int Lines, int Cols){
  1591. se->ViewHeight = Lines; se->ViewWidth = Cols;
  1592. }
  1593. void strEnsureCursorVisible(laStringEdit* se){
  1594. if(!se->ViewHeight || !se->ViewWidth || se->CursorLine<0 || se->CursorBefore<0 ){return;}
  1595. if(se->CursorLine>se->ViewHeight+se->ViewStartLine-1){ se->ViewStartLine=se->CursorLine-se->ViewHeight+1; }
  1596. if(se->CursorLine<se->ViewStartLine){ se->ViewStartLine=se->CursorLine; }
  1597. if(se->CursorBefore>se->ViewStartCol+se->ViewWidth-1){ se->ViewStartCol=se->CursorBefore-se->ViewWidth+1; }
  1598. if(se->CursorBefore<se->ViewStartCol){ se->ViewStartCol=se->CursorBefore; }
  1599. }
  1600. void strRemoveLine(laStringEdit *se, laStringLine *sl){
  1601. lstRemoveItem(&se->Lines, sl);
  1602. memFree(sl); se->TotalLines--;
  1603. }
  1604. void strRemoveLineI(laStringEdit *se, int LineIndex){
  1605. int i = 0;
  1606. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1607. while (sl){
  1608. NextSl = sl->Item.pNext;
  1609. if (i == LineIndex){ strRemoveLine(se, sl); break; }
  1610. i++;
  1611. sl = NextSl;
  1612. }
  1613. }
  1614. void strSetCursor(laStringEdit *se, int LineIndex, int BeforeIndex){
  1615. int maxbefore;
  1616. if (!se) return;
  1617. if(LineIndex<0){LineIndex=0;}
  1618. se->CursorLine = LineIndex;
  1619. maxbefore = strlenU(strGetCursorLine(se, &se->CursorLine)->Buf);
  1620. BeforeIndex = BeforeIndex < 0 ? 0 : BeforeIndex > maxbefore ? maxbefore : BeforeIndex;
  1621. se->CursorBefore = BeforeIndex;
  1622. se->BeginLine = -1;
  1623. se->BeginBefore = -1;
  1624. se->EndLine = -1;
  1625. se->EndBefore = -1;
  1626. strEnsureCursorVisible(se);
  1627. }
  1628. void strMoveCursor(laStringEdit *se, int Left, int Select){
  1629. int maxbefore;
  1630. int BeforeIndex;
  1631. int width = 1;
  1632. laStringLine *sl;
  1633. if (!se) return;
  1634. if(Select){ strLazySelect(se); } else { strCancelSelect(se); }
  1635. sl = strGetCursorLine(se, 0);
  1636. maxbefore = strlenU(sl->Buf);
  1637. BeforeIndex = se->CursorBefore - (Left ? 1 : -1);
  1638. if(BeforeIndex<0){
  1639. if(se->CursorLine>0) strSetCursor(se, se->CursorLine-1, INT_MAX);
  1640. }elif(BeforeIndex>maxbefore && se->CursorLine<se->TotalLines-1){
  1641. if(se->CursorLine>0) strSetCursor(se, se->CursorLine+1, 0);
  1642. }else{
  1643. se->CursorBefore = BeforeIndex>=maxbefore?maxbefore:BeforeIndex;
  1644. }
  1645. se->CursorPreferBefore = se->CursorBefore;
  1646. se->BeginLine = -1;
  1647. se->BeginBefore = -1;
  1648. se->EndLine = -1;
  1649. se->EndBefore = -1;
  1650. if(Select){ strEndSelect(se); }
  1651. strEnsureCursorVisible(se);
  1652. }
  1653. void strMoveCursorLine(laStringEdit *se, int Up, int Select){
  1654. int Line, maxbefore, LastIndex=-1;
  1655. laStringLine *sl;
  1656. if (!se) return;
  1657. if(Select){ strLazySelect(se); } else { strCancelSelect(se); }
  1658. Line=se->CursorLine - (Up? 1:-1);
  1659. if(Line<0) {Line=0;}
  1660. se->CursorLine = Line;
  1661. sl = strGetCursorLine(se, &LastIndex);
  1662. if(LastIndex>=0){ se->CursorLine = LastIndex; se->CursorPreferBefore=10000; }
  1663. maxbefore = strlenU(sl->Buf);
  1664. se->CursorBefore = se->CursorPreferBefore;
  1665. if(se->CursorBefore>maxbefore){ se->CursorBefore = maxbefore; }
  1666. if(LastIndex>=0){se->CursorPreferBefore=se->CursorBefore;}
  1667. if(Select){ strEndSelect(se); }
  1668. strEnsureCursorVisible(se);
  1669. }
  1670. int strHasSelection(laStringEdit* se){
  1671. return se->BeginBefore!=se->EndBefore||se->BeginLine!=se->EndLine;
  1672. }
  1673. void strCancelSelect(laStringEdit *se){
  1674. if (!se) return;
  1675. se->_BeginLine = -1;
  1676. se->_BeginBefore = -1;
  1677. se->BeginLine = -1;
  1678. se->EndLine = -1;
  1679. se->BeginBefore = -1;
  1680. se->EndBefore = -1;
  1681. }
  1682. void strLazySelect(laStringEdit *se){
  1683. if (!se || se->_BeginLine>=0) return;
  1684. se->_BeginLine = TNS_MAX2(se->CursorLine,0);
  1685. se->_BeginBefore = se->CursorBefore;
  1686. }
  1687. void strEndSelect(laStringEdit *se){
  1688. if (!se) return;
  1689. se->_EndLine = se->CursorLine;
  1690. se->_EndBefore = se->CursorBefore;
  1691. se->BeginLine = se->_BeginLine;
  1692. se->EndLine = se->_EndLine;
  1693. se->BeginBefore = se->_BeginBefore;
  1694. se->EndBefore = se->_EndBefore;
  1695. if(se->BeginLine>se->EndLine || (se->BeginLine==se->EndLine && se->BeginBefore>se->EndBefore))
  1696. { LA_SWAP(int,se->BeginLine,se->EndLine); LA_SWAP(int,se->BeginBefore,se->EndBefore); }
  1697. }
  1698. void strSelectLineAll(laStringEdit *se){
  1699. if (!se) return;
  1700. laStringLine *sl;
  1701. int len;
  1702. if (se->CursorLine == -1) sl = strGetBeginLine(se);
  1703. else
  1704. sl = strGetCursorLine(se, 0);
  1705. len = strlenU(sl->Buf);
  1706. se->EndBefore = len; se->EndLine=0;
  1707. se->BeginBefore = 0; se->BeginLine=0;
  1708. se->CursorBefore = len;
  1709. se->CursorLine = 0;
  1710. }
  1711. void strDeselectAll(laStringEdit *se){
  1712. if (!se) return;
  1713. laStringLine *sl;
  1714. int len;
  1715. if (se->CursorLine == -1) sl = strGetBeginLine(se);
  1716. else
  1717. sl = strGetCursorLine(se, 0);
  1718. len = strlenU(sl->Buf);
  1719. se->EndBefore = -1;
  1720. se->BeginBefore = -1;
  1721. se->BeginLine = -1;
  1722. se->EndLine = -1;
  1723. se->CursorBefore = len;
  1724. se->CursorLine = -1;
  1725. }
  1726. void strPanFoward(uint32_t *str, int Before, int Offset){
  1727. int len = strlenU(str);
  1728. int i = len + 1;
  1729. for (i; i >= Before; i--){
  1730. str[i + Offset] = str[i];
  1731. }
  1732. }
  1733. void strSquishBackward(uint32_t *str, int Before, int EndBefore){
  1734. int len = strlenU(str);
  1735. int i = Before;
  1736. int Offset = Before - EndBefore;
  1737. if (Before <= 0) return;
  1738. for (i; i <= len; i++){
  1739. str[i - Offset] = str[i];
  1740. }
  1741. }
  1742. void strClearSelection(laStringEdit *se){
  1743. //if (se->EndLine == -1) return;
  1744. if (se->BeginLine != se->EndLine){
  1745. int i = 0; int RemovedLines=0;
  1746. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1747. while (sl){
  1748. NextSl = sl->Item.pNext;
  1749. if (i == se->BeginLine){
  1750. sl->Buf[se->BeginBefore] = U'\0';
  1751. }else if (i > se->BeginLine && i < se->EndLine){
  1752. strRemoveLine(se, sl); RemovedLines++;
  1753. }else if (i == se->EndLine){
  1754. strSquishBackward(sl->Buf, se->EndBefore, 0);
  1755. se->CursorLine = i-RemovedLines;
  1756. se->CursorBefore = 0;
  1757. se->BeginLine = -1;
  1758. se->BeginBefore = -1;
  1759. se->EndLine = -1;
  1760. se->EndBefore = -1;
  1761. strBackspace(se);
  1762. }
  1763. if (i > se->EndLine) break;
  1764. i++;
  1765. sl = NextSl;
  1766. }
  1767. }else{
  1768. int i = 0;
  1769. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1770. while (sl){
  1771. NextSl = sl->Item.pNext;
  1772. if (i == se->EndLine) {
  1773. strSquishBackward(sl->Buf, se->EndBefore, se->BeginBefore);
  1774. se->CursorLine = i;
  1775. se->CursorBefore = se->BeginBefore;
  1776. se->BeginLine = -1;
  1777. se->BeginBefore = -1;
  1778. se->EndLine = -1;
  1779. se->EndBefore = -1;
  1780. break;
  1781. }
  1782. i++; sl = NextSl;
  1783. }
  1784. }
  1785. strEnsureCursorVisible(se);
  1786. }
  1787. laStringLine *strGetCursorLine(laStringEdit *se, int* ReturnIndexIfLast){
  1788. if (!se || se->CursorBefore <= -1) return se->Lines.pFirst;
  1789. int i = 0;
  1790. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1791. while (sl){
  1792. NextSl = sl->Item.pNext;
  1793. if (i == se->CursorLine){
  1794. return sl;
  1795. }
  1796. i++;
  1797. sl = NextSl;
  1798. }
  1799. if(ReturnIndexIfLast){ *ReturnIndexIfLast=i-1;}
  1800. return se->Lines.pLast;
  1801. }
  1802. laStringLine *strGetBeginLine(laStringEdit *se){
  1803. if (!se || se->BeginLine <= -1) return se->Lines.pFirst;
  1804. int i = 0;
  1805. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1806. while (sl){
  1807. NextSl = sl->Item.pNext;
  1808. if (i == se->BeginLine){
  1809. return sl;
  1810. }
  1811. i++;
  1812. sl = NextSl;
  1813. }
  1814. return se->Lines.pFirst;
  1815. }
  1816. void strInsertChar(laStringEdit *se, uint32_t a){
  1817. laStringLine *sl;
  1818. strClearSelection(se);
  1819. sl = strGetCursorLine(se, 0);
  1820. if(a==U'\n'){
  1821. laStringLine* nl=memAcquireSimple(sizeof(laStringLine));
  1822. if(sl->Buf[se->CursorBefore]!=U'\0') strcpyU(nl->Buf, &sl->Buf[se->CursorBefore]);
  1823. sl->Buf[se->CursorBefore]=U'\0';
  1824. se->CursorLine++; se->CursorBefore=0;
  1825. lstInsertItemAfter(&se->Lines, nl, sl); se->TotalLines++;
  1826. }else{
  1827. strPanFoward(sl->Buf, se->CursorBefore, 1);
  1828. sl->Buf[se->CursorBefore] = a;
  1829. se->CursorBefore += 1;
  1830. }
  1831. se->CursorPreferBefore = se->CursorBefore;
  1832. strEnsureCursorVisible(se);
  1833. }
  1834. void strBackspace(laStringEdit *se){
  1835. laStringLine *sl;
  1836. int width = 1;
  1837. if (se->CursorBefore == -1){
  1838. strClearSelection(se);
  1839. }else{
  1840. laStringLine *sl;
  1841. sl = strGetCursorLine(se, 0);
  1842. if (se->CursorBefore > 1 && sl->Buf[se->CursorBefore - 2] < 0) width = 2;
  1843. strSquishBackward(sl->Buf, se->CursorBefore, se->CursorBefore - width);
  1844. se->CursorBefore -= width;
  1845. if (se->CursorBefore <= -1){
  1846. if(sl->Item.pPrev){
  1847. laStringLine* ol=sl->Item.pPrev;
  1848. se->CursorBefore = strlenU(ol->Buf);
  1849. se->CursorLine--;
  1850. strcatU(ol->Buf, sl->Buf);
  1851. strRemoveLine(se, sl);
  1852. }
  1853. else {se->CursorBefore = 0;}
  1854. }
  1855. }
  1856. se->CursorPreferBefore = se->CursorBefore;
  1857. strEnsureCursorVisible(se);
  1858. }
  1859. void strMoveView(laStringEdit *se, int DownLines, int RightCharacters){
  1860. se->ViewStartLine+=DownLines;
  1861. se->ViewStartCol+=RightCharacters;
  1862. if(se->ViewStartLine>=se->TotalLines-1) se->ViewStartLine=se->TotalLines-1;
  1863. if(se->ViewStartLine<0) se->ViewStartLine=0;
  1864. if(se->ViewStartCol<0) se->ViewStartCol=0;
  1865. }
  1866. int laCopyFile(char *to, char *from){
  1867. #ifdef _WIN32
  1868. if(CopyFile(from, to, 0)) return 1; return 0;
  1869. #endif
  1870. #ifdef __linux__
  1871. int fd_to, fd_from; char buf[4096];
  1872. ssize_t nread; int saved_errno;
  1873. fd_from = open(from, O_RDONLY); if (fd_from < 0) return -1;
  1874. fd_to = open(to, O_WRONLY|O_CREAT|O_EXCL, 0666); if (fd_to < 0) goto out_error;
  1875. while (nread=read(fd_from,buf,sizeof(buf)), nread>0) {
  1876. char *out_ptr = buf; ssize_t nwritten;
  1877. do { nwritten = write(fd_to, out_ptr, nread);
  1878. if (nwritten >= 0){ nread -= nwritten; out_ptr += nwritten;}
  1879. else if (errno != EINTR){ goto out_error; }
  1880. }while (nread > 0);
  1881. }
  1882. if (nread == 0){
  1883. if (close(fd_to)<0){ fd_to = -1; goto out_error;}
  1884. close(fd_from);
  1885. /* Success! */
  1886. return 0;
  1887. }
  1888. out_error:
  1889. saved_errno = errno;
  1890. close(fd_from); if (fd_to >= 0) close(fd_to);
  1891. errno = saved_errno;
  1892. return -1;
  1893. #endif //linux
  1894. }
  1895. //======================================================[ translation ]
  1896. void transNewLanguage(const char *LanguageID){
  1897. laTranslationNode *tn = memAcquire(sizeof(laTranslationNode));
  1898. strSafeSet(&tn->LanguageName, LanguageID);
  1899. lstAppendItem(&MAIN.Translation.Languages, tn);
  1900. MAIN.Translation.CurrentLanguage = tn;
  1901. }
  1902. void transSetLanguage(const char *LanguageID){
  1903. laTranslationNode *tn;
  1904. if (!LanguageID){
  1905. MAIN.Translation.CurrentLanguage = 0;
  1906. return;
  1907. }
  1908. for (tn = MAIN.Translation.Languages.pFirst; tn; tn = tn->Item.pNext){
  1909. if (!strcmp(tn->LanguageName->Ptr, LanguageID)){
  1910. MAIN.Translation.CurrentLanguage = tn; return;
  1911. }
  1912. }
  1913. transNewLanguage(LanguageID);
  1914. }
  1915. void transDumpMissMatchRecord(const char *filename){
  1916. laTranslationMatch *tm;
  1917. laListHandle *lst;
  1918. int i;
  1919. FILE *f = fopen(filename, "w"); if (!f) return;
  1920. for (i = 0; i < 256; i++){
  1921. lst = &MAIN.Translation.MisMatches.Entries[i];
  1922. for (tm = lst->pFirst; tm; tm = tm->Item.pNext){ if(tm->Target) fprintf(f, "%s | \n", tm->Target); }
  1923. }
  1924. fclose(f);
  1925. }
  1926. int IsThisTranslationMatch(laTranslationMatch *tm, char *p){
  1927. return (tm->Target && (!strcmp(tm->Target, p)));
  1928. }
  1929. void transNewEntry(const char *Target, const char *replacement){
  1930. laTranslationMatch *tm = memAcquireSimple(sizeof(laTranslationMatch));
  1931. tm->Target = Target; tm->Replacement = replacement;
  1932. hsh256InsertItemCSTR(&MAIN.Translation.CurrentLanguage->Matches, tm, Target);
  1933. }
  1934. void transNewMissEntry(const char *Target){
  1935. if (!hsh256FindItemSTR(&MAIN.Translation.MisMatches, IsThisTranslationMatch, Target)){
  1936. laTranslationMatch *tm = memAcquireSimple(sizeof(laTranslationMatch));
  1937. int len=strlen(Target); tm->Target=memAcquireSimple(len*sizeof(char)+1);
  1938. strcpy(tm->Target,Target);
  1939. hsh256InsertItemCSTR(&MAIN.Translation.MisMatches, tm, Target);
  1940. }
  1941. }
  1942. char *transLate(char *Target){
  1943. if (!MAIN.Translation.CurrentLanguage || !MAIN.Translation.EnableTranslation || !Target || !Target[0]) return Target;
  1944. laTranslationMatch *tm = hsh256FindItemSTR(&MAIN.Translation.CurrentLanguage->Matches, IsThisTranslationMatch, Target);
  1945. if (!tm){ transNewMissEntry(Target); return Target; }
  1946. return tm->Replacement;
  1947. }
  1948. void transState(void *UNUSED, int val){
  1949. if (val) MAIN.Translation.EnableTranslation = 1;
  1950. else MAIN.Translation.EnableTranslation = 0;
  1951. laRedrawCurrentWindow();
  1952. }
  1953. void laOpenInternetLink(char *url){
  1954. laSafeString* s=0;
  1955. #ifdef __linux__
  1956. strSafePrint(&s, "xdg-open %s", url);
  1957. #endif
  1958. #ifdef _WIN32
  1959. strSafePrint(&s, "start %s", url);
  1960. #endif
  1961. system(s->Ptr);
  1962. strSafeDestroy(&s);
  1963. //these were windows stuff
  1964. //HKEY hkRoot, hSubKey;
  1965. //char ValueName[256]={0};
  1966. //char DataValue[256]={0};
  1967. //u64bit cbValueName = 256;
  1968. //u64bit cbDataValue = 256;
  1969. //char ShellChar[512]={0};
  1970. //DWORD dwType;
  1971. //
  1972. //ShellExecute(0, "open", link, 0, 0, SW_SHOWNORMAL);
  1973. //
  1974. //return;
  1975. }
  1976. #ifdef _WIN32
  1977. void usleep(unsigned int usec){
  1978. HANDLE timer;
  1979. LARGE_INTEGER ft;
  1980. static int init = 0;
  1981. if (init == 0){
  1982. init = 1;
  1983. const HINSTANCE ntdll = LoadLibrary("ntdll.dll");
  1984. if (ntdll != NULL){
  1985. typedef long(NTAPI* pNtQueryTimerResolution)(u64bit* MinimumResolution, u64bit* MaximumResolution, u64bit* CurrentResolution);
  1986. typedef long(NTAPI* pNtSetTimerResolution)(u64bit RequestedResolution, char SetResolution, u64bit* ActualResolution);
  1987. pNtQueryTimerResolution NtQueryTimerResolution = (pNtQueryTimerResolution)GetProcAddress(ntdll, "NtQueryTimerResolution");
  1988. pNtSetTimerResolution NtSetTimerResolution = (pNtSetTimerResolution)GetProcAddress(ntdll, "NtSetTimerResolution");
  1989. if (NtQueryTimerResolution != NULL && NtSetTimerResolution != NULL){
  1990. u64bit minimum, maximum, current;
  1991. NtQueryTimerResolution(&minimum, &maximum, &current);
  1992. NtSetTimerResolution(maximum, (char)1, &current);
  1993. }
  1994. FreeLibrary(ntdll);
  1995. }
  1996. }
  1997. ft.QuadPart = -(10 * (__int64)usec);
  1998. timer = CreateWaitableTimer(NULL, TRUE, NULL);
  1999. SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
  2000. WaitForSingleObject(timer, INFINITE);
  2001. CloseHandle(timer);
  2002. }
  2003. void laSpinInit(SYSLOCK* lock) {
  2004. InitializeCriticalSection(lock);
  2005. }
  2006. void laSpinDestroy(SYSLOCK* lock) {
  2007. DeleteCriticalSection(lock);
  2008. }
  2009. void laSpinLock(SYSLOCK* lock) {
  2010. EnterCriticalSection(lock);
  2011. }
  2012. void laSpinUnlock(SYSLOCK* lock) {
  2013. LeaveCriticalSection(lock);
  2014. }
  2015. #endif
  2016. #ifdef __linux__
  2017. void laSpinInit(SYSLOCK* lock) {
  2018. pthread_spin_init(lock, 0);
  2019. }
  2020. void laSpinDestroy(SYSLOCK* lock) {
  2021. pthread_spin_destroy(lock);
  2022. }
  2023. void laSpinLock(SYSLOCK* lock) {
  2024. pthread_spin_lock(lock);
  2025. }
  2026. void laSpinUnlock(SYSLOCK* lock) {
  2027. pthread_spin_unlock(lock);
  2028. }
  2029. #endif
  2030. //======================================= lua utils
  2031. #ifdef LA_WITH_LUAJIT
  2032. static const char *progname = LUA_PROGNAME;
  2033. static int la_luaTraceback(lua_State *L){
  2034. if (!lua_isstring(L, 1)) { /* Non-string error object? Try metamethod. */
  2035. if (lua_isnoneornil(L, 1) ||
  2036. !luaL_callmeta(L, 1, "__tostring") ||
  2037. !lua_isstring(L, -1))
  2038. return 1; /* Return non-string error object. */
  2039. lua_remove(L, 1); /* Replace object by result of __tostring metamethod. */
  2040. }
  2041. luaL_traceback(L, L, lua_tostring(L, 1), 1);
  2042. return 1;
  2043. }
  2044. static void la_luaMessage(const char *msg){
  2045. if (progname) { logPrint("%s: ",progname); } logPrint("%s\n",msg);
  2046. }
  2047. static int la_luaReport(lua_State *L, int status){
  2048. if (status && !lua_isnil(L, -1)) {
  2049. const char *msg = lua_tostring(L, -1);
  2050. if (msg == NULL) msg = "(error object is not a string)";
  2051. la_luaMessage(msg);
  2052. lua_pop(L, 1);
  2053. }
  2054. return status;
  2055. }
  2056. static int la_luaDoCall(lua_State *L, int narg, int clear){
  2057. int status;
  2058. int base = lua_gettop(L) - narg; /* function index */
  2059. lua_pushcfunction(L, la_luaTraceback); /* push la_luaTraceback function */
  2060. lua_insert(L, base); /* put it under chunk and args */
  2061. status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
  2062. lua_remove(L, base); /* remove la_luaTraceback function */
  2063. /* force a complete garbage collection in case of errors */
  2064. if (status != LUA_OK) lua_gc(L, LUA_GCCOLLECT, 0);
  2065. return status;
  2066. }
  2067. static int la_luaIncomplete(lua_State *L, int status){
  2068. if (status == LUA_ERRSYNTAX) {
  2069. size_t lmsg;
  2070. const char *msg = lua_tolstring(L, -1, &lmsg);
  2071. const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
  2072. if (strstr(msg, LUA_QL("<eof>")) == tp) { lua_pop(L, 1); return 1; }
  2073. }
  2074. return 0; /* else... */
  2075. }
  2076. int terLoadLine(char* buf, int firstline){
  2077. lua_State *L=MAIN.L;
  2078. if(!MAIN.TerminalIncomplete){ lua_settop(L, 0); }
  2079. size_t len = strlen(buf); if(len>=512){ buf[512]=0; }
  2080. if(len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
  2081. if(firstline && buf[0] == '=') lua_pushfstring(L, "return %s", buf+1);
  2082. else lua_pushstring(L, buf);
  2083. if(MAIN.TerminalIncomplete){
  2084. lua_pushliteral(L, "\n"); lua_insert(L, -2); lua_concat(L, 3);
  2085. }
  2086. int status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "terLoadLine");
  2087. if(la_luaIncomplete(L,status)){
  2088. MAIN.TerminalIncomplete=1;
  2089. }else{
  2090. MAIN.TerminalIncomplete=0;
  2091. lua_remove(L, 1);
  2092. }
  2093. if(status==LUA_OK && (!MAIN.TerminalIncomplete)){
  2094. status = la_luaDoCall(L, 0, 0);
  2095. la_luaReport(L, status);
  2096. if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */
  2097. lua_getglobal(L, "log");
  2098. lua_insert(L, 1);
  2099. if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
  2100. la_luaMessage(lua_pushfstring(L, "error calling " LUA_QL("log") " (%s)",
  2101. lua_tostring(L, -1)));
  2102. }
  2103. }
  2104. return status;
  2105. }
  2106. static int lalua_Log(lua_State *L) {
  2107. int n = lua_gettop(L); int i;
  2108. lua_getglobal(L, "tostring");
  2109. for (i=1; i<=n; i++) {
  2110. const char *s;
  2111. lua_pushvalue(L, -1); /* tostring function */
  2112. lua_pushvalue(L, i); /* value to print */
  2113. lua_call(L, 1, 1);
  2114. s = lua_tostring(L, -1); /* get result */
  2115. if (s == NULL) return luaL_error(L, LUA_QL("tostring") " must return a string to "LUA_QL("use `log`"));
  2116. if (i>1) logPrint(" ");
  2117. logPrint(s); lua_pop(L, 1); /* pop result */
  2118. }
  2119. logPrint("\n");
  2120. return 0;
  2121. }
  2122. void la_luaLoadLibs(lua_State *L){
  2123. lua_gc(L, LUA_GCSTOP, 0);
  2124. luaL_openlibs(L);
  2125. lua_register(L,"log",lalua_Log);
  2126. if(luaL_loadstring(L, LA_LUA_LIB_COMMON) || lua_pcall(L, 0, 0, 0)){ logPrint(" Error loading lagui lua libs.\n"); };
  2127. if(luaL_loadstring(L, LA_LUA_LIB_AUDIO) || lua_pcall(L, 0, 0, 0)){ logPrint(" Error loading lua libs for audio.\n"); };
  2128. lua_gc(L, LUA_GCRESTART, -1);
  2129. }
  2130. void la_luaPrintStatus(lua_State *L){
  2131. logPrint(LUAJIT_VERSION " -- " LUAJIT_COPYRIGHT ". " LUAJIT_URL "\n");
  2132. int n; const char *s;
  2133. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  2134. lua_getfield(L, -1, "jit"); lua_remove(L, -2); /* Get jit.* module table. */
  2135. lua_getfield(L, -1, "status"); lua_remove(L, -2);
  2136. n = lua_gettop(L);
  2137. lua_call(L, 0, LUA_MULTRET);
  2138. logPrint(lua_toboolean(L, n) ? "JIT: ON" : "JIT: OFF");
  2139. for (n++; (s = lua_tostring(L, n)); n++) { logPrint("%s ",s); }
  2140. logPrint("\n");
  2141. lua_settop(L, 0); /* clear stack */
  2142. }
  2143. void la_luaDumpStack(lua_State *L){
  2144. int top=lua_gettop(L); for (int i=1; i <= top; i++) {
  2145. printf("%d\t%s\t", i, luaL_typename(L,i));
  2146. switch (lua_type(L, i)) {
  2147. case LUA_TNUMBER: printf("%g\n",lua_tonumber(L,i)); break;
  2148. case LUA_TSTRING: printf("%s\n",lua_tostring(L,i)); break;
  2149. case LUA_TBOOLEAN: printf("%s\n", (lua_toboolean(L, i) ? "true" : "false")); break;
  2150. case LUA_TNIL: printf("%s\n", "nil"); break;
  2151. default: printf("%p\n",lua_topointer(L,i)); break;
  2152. }
  2153. }
  2154. }
  2155. #else //luajit
  2156. int terLoadLine(char* buf, int firstline){
  2157. logPrint(buf); return 0;
  2158. }
  2159. #endif //luajit