*/}}

la_util.c 62 KB

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