*/}}

la_util.c 60 KB

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