*/}}

la_util.c 64 KB

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