*/}}

la_util.c 65 KB

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