*/}}

la_util.c 60 KB

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