123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106 |
- /*
- NUL4.0 - Nick's Best - www.nicksbest.com
- Author(s):WuYiming - xp8110@outlook.com
- Want to join the development?
- Append your name in the author list above.
- Send feedback to la_support@nicksbest.com
- */
- #define _CRT_SEQURE_NO_WARNINGS
- #include "la_util.h"
- #include "la_interface.h"
- #include <stdio.h>
- #include <stdlib.h>
- laSafeStringCollection SSC;
- extern LA MAIN;
- #define BYTE unsigned char
- uint32_t laToUnicode(const unsigned char* ch, int* advance){
- if((*ch)<0x80) { *advance=1; return *ch; }
- uint32_t u=0;
- if(((*ch)>>5)==0x06){ *advance=2; u|=((*(ch+1))&0x3f)|((*(ch)&0x1f)<<6); return u; }
- if(((*ch)>>4)==0x0e){ *advance=3; u|=((*(ch+2))&0x3f)|((*(ch+1)&0x3f)<<6)|((*(ch)&0x0f)<<12); return u; }
- if(((*ch)>>3)==0x1e){ *advance=4; u|=((*(ch+3))&0x3f)|((*(ch+2)&0x3f)<<6)|((*(ch+1)&0x3f)<<12)|((*(ch)&0x07)<<18); return u; }
- *advance=1; return '?';
- }
- int laToUTF8(const uint32_t ch, char* out, char** next){
- 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;}
- elif(ch>=0x800){ out[0]=0xe0|(ch>>12); out[1]=0x80|(0x3f&(ch>>6)); out[2]=0x80|(0x3f&ch); (*next)+=3;}
- elif(ch>=0x80){ out[0]=0xc0|(ch>>6); out[1]=0x80|(0x3f&ch); (*next)+=2;}
- else { if(!ch){return 0;} out[0]=ch&0x7f; (*next)++;} return 1;
- }
- int strToUnicode(uint32_t* target, unsigned char* const src){
- uint32_t UC,adv,i=0; unsigned char* source=src; while(target[i]=laToUnicode(source, &adv)) { source+=adv; i++; } target[i]=0; return i;
- }
- int strToUTF8(unsigned char* target, uint32_t* const src){
- uint32_t* source=src; unsigned char* out=target; while(laToUTF8(*source, out, &out)){ source++; } *out=0; return out-target;
- }
- int strlenU(uint32_t* str){ int i=0; while(str[i]!=0) i++; return i; }
- int strcpyU(uint32_t* target, uint32_t* const source ){ int i=0; while(source[i]!=0){ target[i]=source[i]; i++; } target[i]=0; }
- 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; }
- struct tm *laGetFullTime(){
- time_t t = time(0);
- return localtime(&t);
- }
- void laRecordTime(laTimeRecorder *tr){
- clock_gettime(CLOCK_REALTIME, &tr->ts);
- }
- real laTimeElapsedSecondsf(laTimeRecorder *End, laTimeRecorder *Begin){
- real sec=End->ts.tv_sec-Begin->ts.tv_sec; sec+=((End->ts.tv_nsec-Begin->ts.tv_nsec)/1e9);
- return sec;
- }
- void laSetAuthorInfo(char *Name, char *CopyrightString){
- strSafeSet(&MAIN.Author.Name, Name);
- strSafeSet(&MAIN.Author.CopyrightString, CopyrightString);
- }
- void memCreateNUID(laMemNodeHyper* hi){
- sprintf(hi->NUID.String, "%08X-%hd%02hd%02hd%02hd%02hd%02hd", hi, LA_HYPER_CREATED_TIME(hi));
- }
- void memHyperInfo(laPropPack* pp, char* buf){
- int level=0;void* head=0;
- laMemNodeHyper* hi; laMemNode* mn;
- int a=0, count=0, pc;
- laProp* p=pp->LastPs->p;
- laPropContainer* c=p->Container;
- if(c->OtherAlloc){
- count=lstCountElements(&c->LocalUsers);
- }else{
- head=memGetHead(pp->LastPs->UseInstance, &level);
- if(!level){
- sprintf(buf,"Not HyperData.\n");
- }elif(level==1){
- mn=head; count=lstCountElements(&mn->Users);
- }elif(level==2){
- hi=head; count=lstCountElements(&hi->Users);
- }
- }
- a=sprintf(buf,"HyperData:\n\tProperty:%s\n\tContainer:%s (%d users)\n",
- pp->LastPs->p->Identifier, pp->LastPs->p->Container->Identifier, count);
-
- if(level==2){
- sprintf(buf+a,"\tCreated:%hd-%02hd-%02hd %02hd:%02hd:%02hd\n",LA_HYPER_CREATED_TIME(hi));
- }
- }
- void memMakeHyperData(laMemNodeHyper* hi){
- struct tm *time;
- hi->Modified = 1;
- time = laGetFullTime();
- //hi->CreatedBy = &MAIN.Author;
- hi->TimeCreated.Year = time->tm_year + 1900;
- hi->TimeCreated.Month = time->tm_mon + 1;
- hi->TimeCreated.Day = time->tm_mday;
- hi->TimeCreated.Hour = time->tm_hour;
- hi->TimeCreated.Minute = time->tm_min;
- hi->TimeCreated.Second = time->tm_sec;
- //memcpy(&hi->TimeModified, &hi->TimeCreated, sizeof(laTimeInfo));
- memCreateNUID(hi);
- }
- void memMarkClean(void* HyperUserMem){
- int hyper=0;
- laMemNodeHyper* h = memGetHead(HyperUserMem, &hyper);
- if(hyper!=2) return;
- h->Modified=0;
- }
- void nutFreeMem(void **ptr){
- //free_total+=1;
- if (!*ptr) return;
- free(*ptr);
- *ptr = 0;
- }
- int nutFloatCompare(real l, real r){
- return (l > r - 0.00005 && l < r + 0.00005);
- }
- int nutSameAddress(void *l, void *r){
- return (l == r);
- }
- //===================================================================[list]
- void* arrElement(void* head, int i, int size){
- return ((char*)head+size*i);
- }
- int arrEnsureLength(void** head, int next, int* max, size_t ElementSize){
- int UseMax=*max; int nocopy=(!UseMax);
- if(next>=UseMax){
- if(!UseMax){ UseMax=50; }
- int AllocMax=next>(UseMax*2)?(next+16):(UseMax*2);
- void* data = CreateNew_Size(ElementSize* AllocMax);
- if(((*head) || next)&&(!nocopy)){ memcpy(data, *head, ElementSize*UseMax); }
- if(*head) free(*head);
- *head=data;
- *max=AllocMax;
- return 1;
- }
- return 0;
- }
- int arrInitLength(void** head, int max, int* pmax, size_t ElementSize){
- if(*head){ free(head); }
- *head=CreateNew_Size(ElementSize*max);
- *pmax=max;
- }
- void arrFree(void** head, int* max){ free(*head); *head=0; *max=0; }
- void lstPushSingle(void **Head, laListSingle *Item){
- Item->pNext = *Head;
- *Head = Item;
- }
- void *lstPopSingle(void **Head, laListSingle *Item){
- *Head = ((laListSingle *)(*Head))->pNext;
- Item->pNext = 0;
- return *Head;
- }
- int lstCountElements(laListHandle* Handle){
- int count=0; if(!Handle) return 0;
- for(laListItem* i=Handle->pFirst;i;i=i->pNext){count++;}
- return count;
- }
- void lstAppendItem(laListHandle* Handle, void* Item){
- laListItem* li = Item;
- li->pNext = li->pPrev = 0;
- if (!Handle->pFirst) Handle->pFirst = Item;
- if (Handle->pLast) ((laListItem*)Handle->pLast)->pNext = li;
- li->pPrev = Handle->pLast;
- li->pNext = 0;
- Handle->pLast = li;
- };
- void lstPushItem(laListHandle* Handle, void* Item){
- laListItem* li = Item;
- li->pNext = li->pPrev = 0;
- if (!Handle->pLast) Handle->pLast = Item;
- li->pNext = Handle->pFirst;
- if (Handle->pFirst) ((laListItem*)Handle->pFirst)->pPrev = Item;
- Handle->pFirst = li;
- };
- void* lstPopItem(laListHandle* Handle){
- void* popitem;
- laListItem* next;
- if (!Handle->pFirst) return 0;
- popitem = Handle->pFirst;
- next = ((laListItem*)Handle->pFirst)->pNext;
- if (!next){
- Handle->pFirst = 0;
- Handle->pLast = 0;
- }else{
- Handle->pFirst = next;
- if (next) next->pPrev = 0;
- };
- return popitem;
- };
- int lstHaveItemInList(laListHandle *Handle){
- if (Handle->pFirst) return 1;
- return 0;
- };
- void lstAppendItem2(laListHandle *Handle, void *Item){
- laListItem2 *li = Item;
- li->pNext = li->pPrev = 0;
- if (!Handle->pFirst) Handle->pFirst = Item;
- if (Handle->pLast) ((laListItem2 *)Handle->pLast)->pNext = li;
- li->pPrev = Handle->pLast;
- li->pNext = 0;
- Handle->pLast = li;
- };
- void lstPushItem2(laListHandle *Handle, void *Item){
- laListItem2 *li = Item;
- li->pNext = li->pPrev = 0;
- if (!Handle->pLast) Handle->pLast = Item;
- li->pNext = Handle->pFirst;
- if (Handle->pFirst) ((laListItem2 *)Handle->pFirst)->pPrev = Item;
- Handle->pFirst = li;
- };
- void *lstPopItem2(laListHandle *Handle){
- void *popitem;
- laListItem2 *next;
- if (!Handle->pFirst) return 0;
- popitem = Handle->pFirst;
- next = ((laListItem2 *)Handle->pFirst)->pNext;
- if (!next){
- Handle->pFirst = 0;
- Handle->pLast = 0;
- }else{
- Handle->pFirst = next;
- if (next) next->pPrev = 0;
- };
- return popitem;
- };
- void lstAppendItem3(laListHandle *Handle, void *Item){
- laListItem3 *li = Item;
- li->pNext = li->pPrev = 0;
- if (!Handle->pFirst) Handle->pFirst = Item;
- if (Handle->pLast) ((laListItem3 *)Handle->pLast)->pNext = li;
- li->pPrev = Handle->pLast;
- li->pNext = 0;
- Handle->pLast = li;
- };
- void lstPushItem3(laListHandle *Handle, void *Item){
- laListItem3 *li = Item;
- li->pNext = li->pPrev = 0;
- if (!Handle->pLast) Handle->pLast = Item;
- li->pNext = Handle->pFirst;
- if (Handle->pFirst) ((laListItem3 *)Handle->pFirst)->pPrev = Item;
- Handle->pFirst = li;
- };
- void *lstPopItem3(laListHandle *Handle){
- void *popitem;
- laListItem3 *next;
- if (!Handle->pFirst) return 0;
- popitem = Handle->pFirst;
- next = ((laListItem3 *)Handle->pFirst)->pNext;
- if (!next){
- Handle->pFirst = 0;
- Handle->pLast = 0;
- }else{
- Handle->pFirst = next;
- if (next) next->pPrev = 0;
- };
- return popitem;
- };
- void *lstGetTop(laListHandle *Handle){
- return Handle->pFirst;
- };
- int lstRemoveItem(laListHandle* Handle, laListItem* li) {
- if (!li->pPrev && Handle->pFirst != li) return;
- if (!li->pPrev) Handle->pFirst = li->pNext;
- else
- ((laListItem*)li->pPrev)->pNext = li->pNext;
- if (!li->pNext) Handle->pLast = li->pPrev;
- else
- ((laListItem*)li->pNext)->pPrev = li->pPrev;
- li->pNext = li->pPrev = 0;
- }
- int lstRemoveItem2(laListHandle *Handle, laListItem2 *li){
- if (!li->pPrev) Handle->pFirst = li->pNext;
- else
- ((laListItem2 *)li->pPrev)->pNext = li->pNext;
- if (!li->pNext) Handle->pLast = li->pPrev;
- else
- ((laListItem2 *)li->pNext)->pPrev = li->pPrev;
- li->pNext = li->pPrev = 0;
- };
- int lstRemoveItem3(laListHandle *Handle, laListItem2 *li){
- if (!li->pPrev) Handle->pFirst = li->pNext;
- else
- ((laListItem3 *)li->pPrev)->pNext = li->pNext;
- if (!li->pNext) Handle->pLast = li->pPrev;
- else
- ((laListItem3 *)li->pNext)->pPrev = li->pPrev;
- li->pNext = li->pPrev = 0;
- };
- int lstRemoveSegment(laListHandle *Handle, laListItem *Begin, laListItem *End){
- if (!Begin->pPrev) Handle->pFirst = End->pNext;
- else
- ((laListItem *)Begin->pPrev)->pNext = End->pNext;
- if (!End->pNext) Handle->pLast = Begin->pPrev;
- else
- ((laListItem *)End->pNext)->pPrev = Begin->pPrev;
- End->pNext = Begin->pPrev = 0;
- };
- void lstInsertItemBefore(laListHandle *Handle, laListItem *toIns, laListItem *pivot){
- if (!pivot){
- lstPushItem(Handle, toIns);
- return;
- }
- if (pivot->pPrev){
- ((laListItem *)pivot->pPrev)->pNext = toIns;
- toIns->pPrev = pivot->pPrev;
- }else{
- Handle->pFirst = toIns;
- }
- toIns->pNext = pivot;
- pivot->pPrev = toIns;
- };
- void lstInsertItemAfter(laListHandle *Handle, laListItem *toIns, laListItem *pivot){
- if (!pivot){
- lstAppendItem(Handle, toIns);
- return;
- }
- if (pivot->pNext){
- ((laListItem *)pivot->pNext)->pPrev = toIns;
- toIns->pNext = pivot->pNext;
- }else{
- Handle->pLast = toIns;
- }
- toIns->pPrev = pivot;
- pivot->pNext = toIns;
- }
- void lstInsertSegmentBefore(laListHandle *Handle, laListItem *Begin, laListItem *End, laListItem *pivot){
- if (pivot->pPrev){
- ((laListItem *)pivot->pPrev)->pNext = Begin;
- Begin->pPrev = pivot->pPrev;
- }else{
- Handle->pFirst = Begin;
- }
- End->pNext = pivot;
- pivot->pPrev = End;
- };
- void lstInsertSegmentAfter(laListHandle *Handle, laListItem *Begin, laListItem *End, laListItem *pivot){
- if (pivot->pNext){
- ((laListItem *)pivot->pNext)->pPrev = End;
- End->pNext = pivot->pNext;
- }else{
- Handle->pLast = End;
- }
- Begin->pPrev = pivot;
- pivot->pNext = Begin;
- }
- void *lstAppendPointerOnly(laListHandle *h, void *p){
- laListItemPointer *lip;
- if (!h) return 0;
- lip = CreateNew(laListItemPointer);
- lip->p = p;
- lstAppendItem(h, lip);
- return lip;
- }
- void *lstAppendPointerSizedOnly(laListHandle *h, void *p, int size){
- laListItemPointer *lip;
- if (!h) return 0;
- lip = calloc(1, size);
- lip->p = p;
- lstAppendItem(h, lip);
- return lip;
- }
- void *lstPushPointerOnly(laListHandle *h, void *p){
- laListItemPointer *lip = 0;
- if (!h) return 0;
- lip = CreateNew(laListItemPointer);
- lip->p = p;
- lstPushItem(h, lip);
- return lip;
- }
- void *lstPushPointerSizedOnly(laListHandle *h, void *p, int size){
- laListItemPointer *lip = 0;
- if (!h) return 0;
- lip = calloc(1, size);
- lip->p = p;
- lstPushItem(h, lip);
- return lip;
- }
- void lstReverse(laListHandle* h){
- laListHandle l={0}; void* i; while(i=lstPopItem(h)){ lstPushItem(&l,i); }
- memcpy(h,&l,sizeof(laListHandle));
- }
- int lstHasPointer(laListHandle* h, void *p){
- laListItemPointer *i; for (i = h->pFirst; i; i = i->pNext){
- if (i->p == p){return 1;}
- } return 0;
- }
- void *lstAppendPointer(laListHandle *h, void *p){
- laListItemPointer *lip;
- if (!h) return 0;
- lip = memAcquireSimple(sizeof(laListItemPointer));
- lip->p = p;
- lstAppendItem(h, lip);
- return lip;
- }
- void *lstAppendPointerSized(laListHandle *h, void *p, int size){
- laListItemPointer *lip;
- if (!h) return 0;
- lip = memAcquireSimple(size);
- lip->p = p;
- lstAppendItem(h, lip);
- return lip;
- }
- void *lstPushPointer(laListHandle *h, void *p){
- laListItemPointer *lip = 0;
- if (!h) return 0;
- lip = memAcquireSimple(sizeof(laListItemPointer));
- lip->p = p;
- lstPushItem(h, lip);
- return lip;
- }
- void *lstPushPointerSized(laListHandle *h, void *p, int size){
- laListItemPointer *lip = 0;
- if (!h) return 0;
- lip = memAcquireSimple(size);
- lip->p = p;
- lstPushItem(h, lip);
- return lip;
- }
- void *lstAppendPointerStatic(laListHandle *h, laStaticMemoryPool *smp, void *p){
- laListItemPointer *lip;
- if (!h) return 0;
- lip = memStaticAcquire(smp, sizeof(laListItemPointer));
- lip->p = p;
- lstAppendItem(h, lip);
- return lip;
- }
- void *lstAppendPointerStaticSized(laListHandle *h, laStaticMemoryPool *smp, void *p, int size){
- laListItemPointer *lip;
- if (!h) return 0;
- lip = memStaticAcquire(smp, size);
- lip->p = p;
- lstAppendItem(h, lip);
- return lip;
- }
- void *lstPushPointerStatic(laListHandle *h, laStaticMemoryPool *smp, void *p){
- laListItemPointer *lip = 0;
- if (!h) return 0;
- lip = memStaticAcquire(smp, sizeof(laListItemPointer));
- lip->p = p;
- lstPushItem(h, lip);
- return lip;
- }
- void *lstPushPointerStaticSized(laListHandle *h, laStaticMemoryPool *smp, void *p, int size){
- laListItemPointer *lip = 0;
- if (!h) return 0;
- lip = memStaticAcquire(smp, size);
- lip->p = p;
- lstPushItem(h, lip);
- return lip;
- }
- void *lstPopPointerOnly(laListHandle *h){
- laListItemPointer *lip;
- void *rev = 0;
- if (!h) return 0;
- lip = lstPopItem(h);
- rev = lip ? lip->p : 0;
- FreeMem(lip);
- return rev;
- }
- void lstRemovePointerItemOnly(laListHandle *h, laListItemPointer *lip){
- lstRemoveItem(h, lip);
- FreeMem(lip);
- }
- void lstRemovePointerOnly(laListHandle *h, void *p){
- laListItemPointer *i;
- for (i = h->pFirst; i; i = i->pNext){
- if (i->p == p){
- lstRemovePointerItem(h, i);
- break;
- }
- }
- }
- void lstClearPointerOnly(laListHandle *h){
- laListItemPointer *i;
- while (h && h->pFirst){
- lstPopPointer(h);
- }
- }
- void lstGeneratePointerListOnly(laListHandle *from1, laListHandle *from2, laListHandle *to){
- laListItemPointer *lip = from2 ? from2->pLast : 0;
- while (lip){
- lstPushPointer(to, lip->p);
- lip = lip->pPrev;
- }
- lip = from1 ? from1->pLast : 0;
- while (lip){
- lstPushPointer(to, lip->p);
- lip = lip->pPrev;
- }
- }
- void *lstPopPointer(laListHandle *h){
- laListItemPointer *lip;
- void *rev = 0;
- if (!h) return 0;
- lip = lstPopItem(h);
- rev = lip ? lip->p : 0;
- memFree(lip);
- return rev;
- }
- void lstRemovePointerItem(laListHandle *h, laListItemPointer *lip){
- lstRemoveItem(h, lip);
- memFree(lip);
- }
- void lstRemovePointer(laListHandle *h, void *p){
- laListItemPointer *i;
- for (i = h->pFirst; i; i = i->pNext){
- if (i->p == p){ lstRemovePointerItem(h, i); break; }
- }
- }
- void lstRemovePointerLeave(laListHandle *h, void *p){
- laListItemPointer *i;
- for (i = h->pFirst; i; i = i->pNext){
- if (i->p == p){ lstRemoveItem(h, i); memLeave(i); break; }
- }
- }
- void lstClearPointer(laListHandle *h){
- laListItemPointer *i;
- while (h && h->pFirst){
- lstPopPointer(h);
- }
- }
- void lstGeneratePointerList(laListHandle *from1, laListHandle *from2, laListHandle *to){
- laListItemPointer *lip = from2 ? from2->pLast : 0;
- while (lip){
- lstPushPointer(to, lip->p);
- lip = lip->pPrev;
- }
- lip = from1 ? from1->pLast : 0;
- while (lip){
- lstPushPointer(to, lip->p);
- lip = lip->pPrev;
- }
- }
- void *lstAppendPointerStaticPool(laStaticMemoryPool *mph, laListHandle *h, void *p){
- laListItemPointer *lip;
- if (!h) return 0;
- lip = memStaticAcquire(mph, sizeof(laListItemPointer));
- lip->p = p;
- lstAppendItem(h, lip);
- return lip;
- }
- void *lstPopPointerLeave(laListHandle *h){
- laListItemPointer *lip;
- void *rev = 0;
- if (!h) return 0;
- lip = lstPopItem(h); memLeave(lip);
- rev = lip ? lip->p : 0;
- return rev;
- }
- void lstRemovePointerItemNoFree(laListHandle *h, laListItemPointer *lip){
- lstRemoveItem(h, lip);
- }
- void lstCopyHandle(laListHandle *target, laListHandle *src){
- target->pFirst = src->pFirst;
- target->pLast = src->pLast;
- };
- void lstClearHandle(laListHandle *h){
- h->pFirst = 0;
- h->pLast = 0;
- }
- void lstClearPrevNext(laListItem *li){
- li->pNext = 0;
- li->pPrev = 0;
- }
- void lstMoveUp(laListHandle *h, laListItem *li){
- void *pprev = li->pPrev ? ((laListItem *)li->pPrev)->pPrev : 0;
- if (!h || !li) return;
- if (li == h->pFirst) return;
- else{
- if (li == h->pLast) h->pLast = li->pPrev;
- ((laListItem *)li->pPrev)->pNext = li->pNext;
- ((laListItem *)li->pPrev)->pPrev = li;
- if (li->pNext) ((laListItem *)li->pNext)->pPrev = li->pPrev;
- li->pNext = li->pPrev;
- li->pPrev = pprev;
- if (pprev) ((laListItem *)pprev)->pNext = li;
- }
- if (!li->pPrev) h->pFirst = li;
- }
- void lstMoveDown(laListHandle *h, laListItem *li){
- void *ppnext = li->pNext ? ((laListItem *)li->pNext)->pNext : 0;
- if (!h || !li) return;
- if (li == h->pLast) return;
- else{
- if (li == h->pFirst) h->pFirst = li->pNext;
- ((laListItem *)li->pNext)->pPrev = li->pPrev;
- ((laListItem *)li->pNext)->pNext = li;
- if (li->pPrev) ((laListItem *)li->pPrev)->pNext = li->pNext;
- li->pPrev = li->pNext;
- li->pNext = ppnext;
- if (ppnext) ((laListItem *)ppnext)->pPrev = li;
- }
- if (!li->pNext) h->pLast = li;
- }
- void lstForAllItemsDo(laListDoFunc func, laListHandle *hList){
- laListItem *it = hList->pFirst;
- for (; it; it = it->pNext){
- func(it);
- }
- };
- void lstForAllItemsDoLNRR(laListNonRecursiveDoFunc func, laListHandle *hList){
- laListItem *it = hList->pFirst;
- for (; it; it = it->pNext){
- func(0, it, 0);
- }
- };
- void lstForAllItemsDo_DirectFree(laListDoFunc func, laListHandle *hList){
- laListItem *it;
- while (it = lstPopItem(hList)){
- if (func) func(it);
- FreeMem(it);
- }
- };
- void lstForAllItemsDo_arg_ptr(laListDoFuncArgp func, laListHandle *hList, void *arg){
- laListItem *it = hList->pFirst;
- for (; it; it = it->pNext){
- func(it, arg);
- };
- };
- void lstForAllItemsDo_NonRecursive_Root(laListHandle *FirstHandle, laListNonRecursiveDoFunc func, int bFreeItem, void *custom_data, laListCustomDataRemover remover){
- laListItem *li = 0, *NextLi;
- laListNonRecursiveRoot root = {0};
- laListNonRecursiveItem *nrItem = CreateNew(laListNonRecursiveItem);
- nrItem->bFreeList = bFreeItem;
- nrItem->func = func;
- nrItem->CustomData = custom_data;
- nrItem->remover = remover;
- lstCopyHandle(&nrItem->handle, FirstHandle);
- lstAppendItem(&root.NSItems, nrItem);
- while (lstHaveItemInList(&root.NSItems)){
- nrItem = lstPopItem(&root.NSItems);
- for (li = nrItem->handle.pFirst; li /*!=nrItem->handle.pLast*/; li = NextLi){
- if (nrItem->func) nrItem->func(&root, li, custom_data);
- NextLi = li->pNext;
- if (nrItem->bFreeList){
- laListItem *fli = li;
- FreeMem(fli);
- }
- if (li == nrItem->handle.pLast) break;
- }
- if (nrItem->remover) nrItem->remover(nrItem->CustomData);
- FreeMem(nrItem);
- }
- };
- void lstAddNonRecursiveListHandle(laListNonRecursiveRoot *root, laListHandle *newHandle, laListNonRecursiveDoFunc nrFunc, int bFreeList, void *custom_data, laListCustomDataRemover remover){
- laListNonRecursiveItem *nrItem = CreateNew(laListNonRecursiveItem);
- nrItem->bFreeList = bFreeList;
- nrItem->func = nrFunc;
- nrItem->CustomData = custom_data;
- nrItem->remover = remover;
- lstCopyHandle(&nrItem->handle, newHandle);
- lstAppendItem(&root->NSItems, nrItem);
- };
- void lstCopy_NonRecursive_Root(laListHandle *FromHandle, laListHandle *ToHandle, int SizeEachNode, laListNonRecursiveCopyFunc func, void *custom_data, laListCustomDataRemover remover){
- laListItem *li = 0, *tli = 0;
- laListNonRecursiveRoot root = {0};
- laListNonRecursiveItem *nrItem = CreateNew(laListNonRecursiveItem);
- laListItem *NextLi;
- nrItem->CopyFunc = func;
- lstCopyHandle(&nrItem->handle, FromHandle);
- nrItem->ToHandle = ToHandle; //Pointer
- lstClearHandle(ToHandle);
- nrItem->CustomData = custom_data;
- nrItem->remover = remover;
- nrItem->SizeEachNode = SizeEachNode;
- lstAppendItem(&root.NSItems, nrItem);
- while (lstHaveItemInList(&root.NSItems)){
- nrItem = lstPopItem(&root.NSItems);
- if (nrItem->CopyFunc){
- for (li = nrItem->handle.pFirst; li; li = li->pNext){
- tli = CreateNew_Size(nrItem->SizeEachNode);
- nrItem->CopyFunc(&root, li, tli, nrItem->CustomData);
- lstClearPrevNext(tli);
- lstAppendItem(nrItem->ToHandle, tli);
- }
- if (nrItem->remover) nrItem->remover(nrItem->CustomData);
- }else if (nrItem->func){
- for (li = nrItem->handle.pFirst; li /*!=nrItem->handle.pLast*/; li = NextLi){
- if (nrItem->func) nrItem->func(&root, li, custom_data);
- NextLi = li->pNext;
- if (nrItem->bFreeList){
- laListItem *fli = li;
- FreeMem(fli);
- }
- if (li == nrItem->handle.pLast) break;
- }
- if (nrItem->remover) nrItem->remover(nrItem->CustomData);
- }
- FreeMem(nrItem);
- }
- };
- void lstAddNonRecursiveListCopier(laListNonRecursiveRoot *root, laListHandle *oldHandle, laListHandle *newHandle, int sizeEach, laListNonRecursiveCopyFunc nrCpyFunc, void *custom_data, laListCustomDataRemover remover){
- laListNonRecursiveItem *nrItem = CreateNew(laListNonRecursiveItem);
- nrItem->CopyFunc = nrCpyFunc;
- lstCopyHandle(&nrItem->handle, oldHandle);
- nrItem->ToHandle = newHandle;
- nrItem->CustomData = custom_data;
- nrItem->remover = remover;
- nrItem->SizeEachNode = sizeEach;
- lstAppendItem(&root->NSItems, nrItem);
- };
- void *lstFindItem(void *CmpData, laCompareFunc func, laListHandle *hList){
- laListItem *it;
- if (!CmpData || !hList) return 0;
- it = hList->pFirst;
- for (; it; it = it->pNext){
- if (func(it, CmpData)) return it;
- };
- return 0;
- };
- void lstCombineLists(laListHandle *dest, laListHandle *src){
- if ((!dest) || (!src)) return;
- if ((!dest->pFirst) && (!dest->pLast)){
- dest->pFirst = src->pFirst;
- dest->pLast = src->pLast;
- }else{
- if (src->pLast){
- ((laListItem *)src->pFirst)->pPrev = dest->pLast;
- ((laListItem *)dest->pLast)->pNext = src->pFirst;
- dest->pLast = src->pLast;
- }
- }
- src->pFirst = 0;
- src->pLast = 0;
- }
- void lstDestroyList(laListHandle *hlst){
- laListItem *li, *nextli;
- for (li = hlst->pFirst; li; li = nextli){
- nextli = li->pNext;
- memFree(li);
- }
- }
- void lstDestroyListA(laListHandle *hlst){
- laListItem *li, *nextli;
- for (li = hlst->pFirst; li; li = nextli){
- nextli = li->pNext;
- FreeMem(li);
- }
- }
- void lstDestroyList_User(laListHandle *hlst, laListDoFunc func){
- laListItem *it = hlst->pFirst;
- for (; it; it = it->pNext){
- func(it);
- FreeMem(it);
- }
- };
- void lstCopyList(laListHandle *hOldlst, laListHandle *hNewList, int SizeEachNode, laCopyListFunc func){
- laListItem *li, *nextli, *newli;
- for (li = hOldlst->pFirst; li; li = nextli){
- newli = (laListItem *)CreateNew_Size(SizeEachNode);
- func(li, newli);
- lstAppendItem(hNewList, newli);
- nextli = li->pNext;
- }
- }
- void *lstReMatch(laListHandle *SearchHandle, laListHandle *CurrentHandle, void *ItemToFind){
- laListItem *sl = 0, *rl = 0;
- if (!SearchHandle || !CurrentHandle || !ItemToFind) return 0;
- sl = SearchHandle->pFirst;
- rl = CurrentHandle->pFirst;
- while (sl && rl){
- if (ItemToFind == sl){
- return rl;
- }else{
- sl = sl->pNext;
- rl = rl->pNext;
- }
- }
- return 0;
- }
- //void* lstReMatchEx(laListHandle* SearchHandle, laListHandle* CurrentHandle, void* ItemToFind, MatcherFunc func){
- // laListItem* sl = 0, *rl = 0;
- //
- // if (!SearchHandle || !CurrentHandle || !ItemToFind) return 0;
- //
- // sl = SearchHandle->pFirst; rl = CurrentHandle->pFirst;
- //
- // while (sl && rl){
- // if (func(ItemToFind, sl)){
- // return rl;
- // }
- // else{
- // sl = sl->pNext;
- // rl = rl->pNext;
- // }
- // }
- // return 0;
- //}
- void lstAddElement(laListHandle *hlst, void *ext){
- laElementListItem *eli = CreateNew(laElementListItem);
- eli->Ext = ext;
- lstAppendItem(hlst, eli);
- }
- void lstDestroyElementList(laListHandle *hlst){
- laElementListItem *eli, *NextEli;
- for (eli = hlst->pFirst; eli; eli = NextEli){
- lstRemoveItem(hlst, eli);
- NextEli = eli->Item.pNext;
- FreeMem(eli);
- }
- }
- void hsh65536Init(laHash65536** h){
- if(!h) return; *h=calloc(1,sizeof(laHash65536));
- }
- void hshFree(laHash65536** h){
- if(!h || !*h) return; free(*h); *h=0;
- }
- laListHandle* hsh65536DoHashLongPtr(laHash65536* hash, unsigned long long buckle) {
- return &hash->Entries[(unsigned short)((buckle >> 10))];
- }
- laListHandle* hsh65536DoHashNUID(laHash65536* hash, char * NUID) {
- u64bit Hash;
- sscanf(NUID, "%ld", &Hash);
- return hsh65536DoHashLongPtr(hash, (long)Hash);
- }
- laListHandle* hsh16MDoHashLongPtr(laHash16M* hash, long long buckle) {
- return &hash->Entries[(buckle>>6)&0x00FFFFFF];
- }
- laListHandle* hsh16MDoHashNUID(laHash16M* hash, char * NUID) {
- u64bit Hash=0;
- for(char* c=NUID;*c;c++){ Hash=Hash*3+(*c); }
- return hsh65536DoHashLongPtr(hash, (long)Hash);
- }
- unsigned char hsh256DoHashSTR(char *buckle){
- int i, len = 0;
- unsigned char rev = 0;
- if (buckle) len = strlen(buckle);
- for (i = 0; i < len; i++){
- rev = rev * 31 + (unsigned char)buckle[i];
- }
- return (unsigned char)rev;
- }
- void hsh256InsertItemCSTR(laHash256 *hash, laListItem *li, char *buckle){
- int a = hsh256DoHashSTR(buckle);
- lstAppendItem(&hash->Entries[a], li);
- };
- void hsh256InsertItem(laHash256 *hash, laListItem *li, char buckle){
- lstAppendItem(&hash->Entries[(unsigned char)buckle], li);
- };
- void hsh65536InsertItem(laHash65536 *hash, laListItem *li, long buckle){
- lstAppendItem(&hash->Entries[(unsigned short)((buckle >> 10))], li);
- //hsh256InsertItem(&hash->HashHandles[(char)((buckle >> 8) / 8)], li, (char)(buckle/8));
- //printf("%d %d\n", (char)(buckle >> 5), (char)(buckle >> 6));
- };
- laListItem *hsh256FindItemSTR(laHash256 *hash, laCompareFunc func, char *buckle){
- unsigned char hsh;
- hsh = hsh256DoHashSTR(buckle);
- //if(hash->Entries[hsh].pFirst == hash->Entries[hsh].pLast)
- // return hash->Entries[hsh].pFirst;
- return lstFindItem(buckle, func, &hash->Entries[hsh]);
- }
- //================================================================ [mem]
- void* memGetHead(void* UserMem, int* HyperLevel){
- laMemoryPoolPart **mpp = (laMemoryPoolPart**)(((char*)UserMem)-sizeof(void*));
- if(!(*mpp)) return 0;
- laMemoryPool* mp = (*mpp)->PoolRoot;
- if(HyperLevel) *HyperLevel= mp->Hyperlevel;
- if(mp->Hyperlevel==2) return ((char*)UserMem)-sizeof(laMemNodeHyper);
- if(mp->Hyperlevel==1) return ((char*)UserMem)-sizeof(laMemNode);
- if(mp->Hyperlevel==0) return ((char*)UserMem)-sizeof(laMemNode0);
- return 0;
- }
- laListHandle* memGetUserList(void* UserMem){
- int level; void* head=memGetHead(UserMem, &level);
- if(level==2) return &((laMemNodeHyper*)head)->Users;
- if(level==1) return &((laMemNode*)head)->Users;
- return 0;
- }
- laMemoryPool *memInitPool(int NodeSize, int HyperLevel){
- if (!NodeSize) return 0;
- laMemoryPool *mph = calloc(1, sizeof(laMemoryPool));
- mph->NodeSize = NodeSize;
- mph->NextCount = 1;
- mph->Hyperlevel = HyperLevel;
- u8bit Buckle = NodeSize;
- lstAppendItem(&MAIN.GlobalMemPool.Entries[Buckle], mph);
- return mph;
- }
- laMemoryPoolPart *memNewPoolPart(laMemoryPool *mph){
- if (!mph->NodeSize) return 0;
- int MemNodeSize=(mph->Hyperlevel==0)?sizeof(laMemNode0):((mph->Hyperlevel==1)?sizeof(laMemNode):sizeof(laMemNodeHyper));
- int PoolRefOffset=MemNodeSize-sizeof(void*);
- int RealNodeSize = mph->NodeSize + MemNodeSize;
- int NodeCount = mph->NextCount;
- int TotalSize = sizeof(laMemoryPoolPart) + NodeCount * RealNodeSize;
-
- laMemoryPoolPart *mp = calloc(1, TotalSize);
- void *BeginMem = ((BYTE *)mp) + sizeof(laMemoryPoolPart);
-
- mp->PoolRoot = mph;
- mp->FreeMemoryNodes.pFirst = mp->FreeMemoryNodes.pLast = 0;
- for (int i = 0; i < NodeCount; i++){
- void* mpn = ((BYTE *)BeginMem) + RealNodeSize * i;
- void** ref = ((BYTE *)mpn) + PoolRefOffset;
- (*ref)=mp;
- lstAppendItem(&mp->FreeMemoryNodes, mpn);
- }
- lstPushItem(&mph->Pools, mp);
- return mp;
- }
- void *memAcquireH(laMemoryPool *Handle){
- laMemoryPoolPart *mp = Handle->Pools.pFirst;
- laMemNode *mpn;
- if (!mp || !mp->FreeMemoryNodes.pFirst){
- mp = memNewPoolPart(Handle);
- }
- if (!mp) return 0;
- mpn = mp->FreeMemoryNodes.pFirst;
- lstRemoveItem(&mp->FreeMemoryNodes, mpn);
- mp->UsedCount++;
- //lstAppendItem(&mp->MemoryNodes, mpn);
- return mpn;
- }
- void *memAcquire_(int Size, int Hyper){
- laMemoryPool *mp;
- u8bit Buckle = Size;
- mp = MAIN.GlobalMemPool.Entries[Buckle].pFirst;
- while (mp && (mp->NodeSize != Size || mp->Hyperlevel!=Hyper))
- mp = mp->Item.pNext;
- if (!mp) mp = memInitPool(Size, Hyper);
- return memAcquireH(mp);
- }
- void *memAcquireSimple(int Size){
- void *mpn = memAcquire_(Size, 0);
- return ((char*)mpn)+sizeof(laMemNode0);
- }
- void *memAcquire(int Size){
- laMemNode *mpn = memAcquire_(Size, 1);
- void* mem = ((char*)mpn)+sizeof(laMemNode);
- return mem;
- }
- void *memAcquireHyperNoAppend(int Size){
- laMemNodeHyper *mpn = memAcquire_(Size, 2);
- void* mem = ((char*)mpn)+sizeof(laMemNodeHyper);
- memMakeHyperData(mpn);
- return mem;
- }
- void *memAcquireHyper(int Size){
- laMemNodeHyper *mpn = memAcquire_(Size, 2);
- void* mem = ((char*)mpn)+sizeof(laMemNodeHyper);
- memMakeHyperData(mpn);
- laListHandle* l=hsh16MDoHashNUID(&MAIN.DBInst2,mpn->NUID.String);
- lstAppendItem(l,mpn);
- return mem;
- }
- void memFree(void *Data){
- if (!Data) return 0;
- int level; void* head = memGetHead(Data, &level);
- laMemoryPoolPart *mp;
- if(level==2) { mp = ((laMemNodeHyper*)head)->InPool; laDataBlockNoLongerExists(Data,&((laMemNodeHyper*)head)->Users);
- laListHandle* l=hsh16MDoHashNUID(&MAIN.DBInst2,((laMemNodeHyper*)head)->NUID.String); lstRemoveItem(l,head);}
- if(level==1) { mp = ((laMemNode*)head)->InPool; laDataBlockNoLongerExists(Data,&((laMemNode*)head)->Users); }
- if(level==0) { mp = ((laMemNode0*)head)->InPool; }
- laMemoryPool *mph = mp->PoolRoot;
- //lstRemoveItem(&mp->MemoryNodes, head);
- mp->UsedCount--;
- void* head_except_item = ((char*)head)+sizeof(laListItem);
- //memset(head_except_item, 0, ((level==2)?sizeof(laMemNodeHyper):((level==1)?sizeof(laMemNode):sizeof(laMemNode0)))+mph->NodeSize-sizeof(laListItem));
- lstAppendItem(&mp->FreeMemoryNodes, head);
- memset(Data, 0, mph->NodeSize);
- MAIN.ByteCount -= mph->NodeSize;
- if (!mp->UsedCount){
- lstRemoveItem(&mph->Pools, mp);
- FreeMem(mp);
- }
- //if (!mph->Pools.pFirst) {
- // mph->CountPerPool = 0;
- // mph->NodeSize = 0;
- //}
- }
- void memDestroyPool(laMemoryPool *mph){
- laMemoryPool *mp;
- while ((mp = lstPopItem(&mph->Pools))){
- FreeMem(mp);
- }
- FreeMem(mph);
- }
- // Leave memory in an temporary place and if when push difference these are still not acquired, free them.
- void memLeave(void *Data){
- laListHandle* l=hsh65536DoHashLongPtr(MAIN.DBInstMemLeft,Data); lstAppendPointer(l,Data);
- }
- void memTake(void *Data){
- laListHandle* l=hsh65536DoHashLongPtr(MAIN.DBInstMemLeft,Data); lstRemovePointer(l,Data);
- }
- void memFreeRemainingLeftNodes(){
- laListHandle* l; void* m;
- for(int i=0;i<65536;i++){
- l=&MAIN.DBInstMemLeft->Entries[i]; while(m=lstPopPointer(l)){ memFree(m); printf("left freed %x\n",m); }
- }
- }
- void memNoLonger(){
- for(int i=0;i<256;i++){
- laMemoryPool* mp; while(mp=lstPopItem(&MAIN.GlobalMemPool.Entries[i])){ memDestroyPool(mp); }
- }
- }
- laStaticMemoryPoolNode *memNewStaticPool(laStaticMemoryPool *smp){
- laStaticMemoryPoolNode *smpn = calloc(1, LA_MEMORY_POOL_128MB);
- smpn->UsedByte = sizeof(laStaticMemoryPoolNode);
- lstPushItem(&smp->Pools, smpn);
- return smpn;
- }
- void *memStaticAcquire(laStaticMemoryPool *smp, int size){
- laStaticMemoryPoolNode *smpn = smp->Pools.pFirst;
- void *ret;
- if (!smpn || (smpn->UsedByte + size) > LA_MEMORY_POOL_128MB) smpn = memNewStaticPool(smp);
- ret = ((BYTE *)smpn) + smpn->UsedByte;
- smpn->UsedByte += size;
- return ret;
- }
- void *memStaticAcquireThread(laStaticMemoryPool *smp, int size){
- laStaticMemoryPoolNode *smpn = smp->Pools.pFirst;
- void *ret;
- //pthread_spin_lock(&smp->csMem);
- if (!smpn || (smpn->UsedByte + size) > LA_MEMORY_POOL_128MB) smpn = memNewStaticPool(smp);
- ret = ((BYTE *)smpn) + smpn->UsedByte;
- smpn->UsedByte += size;
- //pthread_spin_unlock(&smp->csMem);
- return ret;
- }
- void *memStaticDestroy(laStaticMemoryPool *smp){
- laStaticMemoryPoolNode *smpn;
- void *ret;
- while (smpn = lstPopItem(&smp->Pools)){
- FreeMem(smpn);
- }
- smp->EachSize = 0;
- return ret;
- }
- void la_ReferencedBlockDeleted(void* This, laItemUserLinker* iul){
- void** user=iul->Pointer.p; (*user)=0; laStopUsingDataBlock(iul->Additional, 0, This);
- }
- void la_ReferrerDeleted(void* This, laItemUserLinker* iul){
- void* instance=iul->Pointer.p; if(instance!=This){ laStopUsingDataBlock(instance, 0, This); }
- }
- void memAssignRef(void* This, void** ptr, void* instance){
- laItemUserLinker* iul;
- if(!This||!ptr) return;
- if(instance){
- laUseDataBlock(instance, 0, 0, ptr, la_ReferencedBlockDeleted, 0)->Additional=This;
- laUseDataBlock(This, 0, 0, instance, la_ReferrerDeleted, 0);
- }else{
- laStopUsingDataBlock((*ptr), 0, This);
- laStopUsingDataBlock(This, 0, (*ptr));
- }
- (*ptr)=instance;
- }
- void memAssignRefSafe(laSubProp* sp, void* This, void** ptr, void* instance){
- laPropContainer* pc=sp?la_EnsureSubTarget(sp,instance):0;
- if(pc&&!pc->OtherAlloc) memAssignRef(This,ptr,instance);
- else (*ptr)=instance;
- }
- //=======================================================================[str]
- char *strGetNextString(char **pivot, char *NextMark){
- int lenth = 0;
- char *countP = *pivot;
- char *result = 0;
- int FloatArg = 0;
- int i,advance;
- if (**pivot == L'\0') return 0;
- if (*NextMark == L'~') FloatArg = 1;
- // container@identifier=window container#window contianer%
- int UC=1;
- while (!lenth){
- for (countP; *countP != L'.' && *(*pivot) != L'\0' && UC && *countP && *countP != L'@' && *countP != L'=' && *countP != L'#' && *countP != L'$';){
- if((*countP)=='\\'){
- countP++; lenth++; }
- UC = laToUnicode(countP, &advance);
- lenth+=advance;
- countP+=advance;
- }
- if (lenth || (*countP) == 0) break;
- (*pivot)++; countP++;
- }
- *NextMark = (*pivot)[lenth];
- if (!(*NextMark)) *NextMark = L'.';
- if (lenth){
- result = CreateNewBuffer(char, lenth + 1);
- int pi=0; for (i = 0; i < lenth; i++){
- if((*pivot)[i]=='\\'){ continue; }
- result[pi] = (*pivot)[i]; pi++;
- }
- result[pi] = L'\0';
- if ((*pivot)[lenth] == L'\0') *pivot = &((*pivot)[lenth]);
- else
- (*pivot) += lenth + 1;
- return result;
- }else{
- return 0;
- }
- };
- int strGetStringTerminateBy(char *content, char terminator, char *Out){
- int Ofst = 0;
- int Skip = 0;
- int i = 0, advance;
- if ((!content) || (*content == L'\0')) return 0;
- int UC;
- for (Ofst; content[Ofst] != terminator && content[Ofst] != L'\0'; ){
- UC = laToUnicode(&content[Ofst], &advance);
- for(int a=0;a<advance;a++){
- Out[i] = content[Ofst];
- Ofst++; i++;
- }
- }
- Out[i] = 0;
- return i;
- };
- char *strGetNewStringTerminateBy_PivotOver(char *content, char terminator, char **NewPivot, int IgnoreSpace){
- int Ofst = 0;
- int Skip = 0;
- int i = 0;
- char *NewString;
- if (!content || *content == L'\0') return 0;
- if (IgnoreSpace) for (i; content[i] == L' '; i++) ;
- int UC,advance;
- for (Ofst; content[Ofst] != terminator && content[Ofst] != L'\0';){
- UC = laToUnicode(&content[Ofst], &advance);
- Ofst+=advance;
- }
- NewString = CreateNewBuffer(char, Ofst + 1 - i);
- memcpy(NewString, &content[i], sizeof(char) * (Ofst - i));
- NewString[Ofst - i] = L'\0';
- *NewPivot = content[Ofst]?&content[Ofst + 1]:&content[Ofst];
- return NewString;
- };
- int strHeadOfStringMatch(char *Str, char *SubStr){
- int len = strlen(SubStr);
- int i = 0;
- for (i; i < len; i++){
- if (Str[i] != SubStr[i]) return 0;
- }
- return 1;
- }
- int strSkipSegmet(char **pivot, char *content){
- if (!pivot || !(*pivot) || !(*(*pivot)) || !content) return;
- if (strHeadOfStringMatch(*pivot, content)){
- (*pivot) += strlen(content);
- return 1;
- }
- return 0;
- }
- char *strGetLastSegment(char *Content, char Seperator){
- char *p = Content;
- char *pn = Content;
- while (1){
- while (*pn != Seperator){
- if (!(*pn)) return p;
- pn++;
- }
- pn++;
- p = pn;
- }
- }
- void strDiscardLastSegmentSeperateBy(char *Content, char Seperator){
- char *p = Content;
- char *pn = Content;
- while (1){
- while (*pn != Seperator){
- if (!(*pn)){
- *p = 0;
- return;
- }
- pn++;
- }
- p = pn;
- pn++;
- }
- }
- void strDiscardSameBeginningSeperatedBy(char *s1, char *s2, char **Result1, char **Result2, char Seperator){
- int i = 0;
- int p = 0;
- while (s1[i] == s2[i]){
- i++;
- if (s1[i] == Seperator) p = i;
- if (!s1[i]){
- p = i;
- break;
- }
- if (!s2[i]){
- p = i;
- break;
- }
- }
- *Result1 = &s1[p];
- *Result2 = &s2[p];
- }
- int strCountSegmentSeperateBy(char *Content, char Seperator){
- char *p = Content;
- char *pn = Content;
- int c = Content[0] ? (Content[0] == Seperator ? 0 : 1) : 0;
- while (1){
- while (*pn != Seperator){
- if (!(*pn)){
- if ((*p) == Seperator) c--;
- return c;
- }
- p = pn;
- pn++;
- }
- c++;
- pn++;
- }
- return c;
- }
- void strMakeDifferentName(char *Target){
- char *p = strGetLastSegment(Target, '.');
- int Temp;
- if (!sscanf(p, "%d", &Temp)){
- int l = strlen(p);
- if (p[l - 1] != L'.') strcat(p, ".");
- strPrintIntAfter(Target, 0, 001);
- }else{
- sprintf(p, "%d", Temp + 1);
- };
- }
- void strReplaceCharacter(char *Str, char Find, char Replace){
- char *p = Str;
- if (!p) return;
- while (*p){
- if (*p == Find) *p = Replace;
- p++;
- }
- }
- void strToUpperCase(char *Str){
- char *p = Str;
- if (!p) return;
- while (*p){
- if (*p >= L'a' && *p <= L'z') *p += L'A' - L'a';
- p++;
- }
- }
- void strToLowerCase(char *Str){
- char *p = Str;
- if (!p) return;
- while (*p){
- if (*p >= L'A' && *p <= L'A') *p -= L'A' - L'a';
- p++;
- }
- }
- laStringSplitor *strSplitPath(char *path){
- laStringPart *sp;
- laStringSplitor *ss;
- char *pivot = path;
- char *temp_result;
- char Type = L'.';
- char NextType = L'.';
- if (!path || !path[0]) return 0;
- ss = memAcquireSimple(sizeof(laStringSplitor));
- while (temp_result = strGetNextString(&pivot, &NextType)){
- if (*temp_result != L'\0'){
- sp = memAcquireSimple(sizeof(laStringPart));
- sp->Content = temp_result;
- lstAppendItem(&ss->parts, sp);
- ss->NumberParts += 1;
- if (NextType == L'$') sp->Type = L'$';
- else
- sp->Type = Type;
- if (sp->Type == L'='){
- if (sp->Content[0] >= L'0' && sp->Content[0] <= 9){
- sscanf(sp->Content, "%d", &sp->IntValue);
- }
- }
- if (NextType == L'$') NextType = L'.';
- Type = NextType;
- }
- }
- if (ss->NumberParts == 0){
- strDestroyStringSplitor(&ss);
- return 0;
- }
- return ss;
- };
- void DF_ClearStingParts(laStringPart *sp){
- FreeMem(sp->Content);
- };
- int strDestroyStringSplitor(laStringSplitor **ss){
- if (!(*ss)) return 0;
- lstForAllItemsDo(DF_ClearStingParts, &(*ss)->parts);
- lstDestroyList(&(*ss)->parts);
- memFree(*ss);
- *ss = 0;
- return 1;
- }
- char * strSub(char *input, char *substring, char *replace){
- int number_of_matches = 0;
- size_t substring_size = strlen(substring), replace_size = strlen(replace), buffer_size;
- char *buffer, *bp, *ip;
- if (substring_size){
- ip = strstr(input, substring);
- while (ip != NULL){
- number_of_matches++;
- ip = strstr(ip+substring_size, substring);
- }
- }
- else number_of_matches = strlen (input) + 1;
- buffer_size = strlen(input) + number_of_matches*(replace_size - substring_size) + 1;
- if ((buffer = ((char *) malloc(buffer_size))) == NULL){
- return NULL;
- }
- bp = buffer;
- ip = strstr(input, substring);
- while ((ip != NULL) && (*input != '\0')){
- if (ip == input){
- memcpy (bp, replace, replace_size+1);
- bp += replace_size;
- if (substring_size)input += substring_size;
- else*(bp++) = *(input++);
- ip = strstr(input, substring);
- }
- else while (input != ip) *(bp++) = *(input++);
- }
- if (substring_size)strcpy (bp, input);
- else memcpy (bp, replace, replace_size+1);
- return buffer;
- }
- char buff[128]={0};
- int strMakeInstructions(laStringSplitor **result, char *content){
- laStringPart *sp;
- laStringSplitor *ss = *result;
- char *pivot = content;
- unsigned char *temp_result;
- if (!content || !content[0]) return 0;
- if (!ss) ss = *result = memAcquireSimple(sizeof(laStringSplitor));
- while (temp_result = strGetNewStringTerminateBy_PivotOver(pivot, '=', &pivot, 0)){
- if (*temp_result != L'\0'){
- sp = memAcquireSimple(sizeof(laStringPart));
- sp->Content = temp_result;
- lstAppendItem(&ss->parts, sp);
- ss->NumberParts += 1;
- }
- temp_result = strGetNewStringTerminateBy_PivotOver(pivot, ';', &pivot, 0);
- if (!temp_result) break;
- if (*temp_result != L'\0'){
- sp = memAcquireSimple(sizeof(laStringPart));
- sp->Content = temp_result;
- lstAppendItem(&ss->parts, sp);
- ss->NumberParts += 1;
- if (temp_result[0] >= L'0' && temp_result[0] <= L'9' || temp_result[0]>=128){
- sscanf(temp_result, "%d", &sp->IntValue);
- sscanf(temp_result, "%lf", &sp->FloatValue);
- }
- }
- }
- if (ss->NumberParts == 0){
- strDestroyStringSplitor(&ss);
- return 0;
- }
- return 1;
- }
- laStringPart *strGetArgument(laStringSplitor *ss, char *content){
- laStringPart *sp;
- if (!ss) return 0;
- for (sp = ss->parts.pFirst; sp; sp = sp->Item.pNext ? ((laListItem *)sp->Item.pNext)->pNext : 0){
- if (strSame(content, sp->Content)) return sp->Item.pNext;
- }
- return 0;
- }
- char *strGetArgumentString(laStringSplitor *ss, char *content){
- laStringPart *sp;
- if (!ss) return 0;
- for (sp = ss->parts.pFirst; sp; sp = sp->Item.pNext ? ((laListItem *)sp->Item.pNext)->pNext : 0){
- if (strSame(content, sp->Content)) return sp->Item.pNext ? ((laStringPart *)sp->Item.pNext)->Content : 0;
- }
- return 0;
- }
- int strArgumentMatch(laStringSplitor *ss, char *id, char *value){
- laStringPart *sp;
- if (!ss) return 0;
- for (sp = ss->parts.pFirst; sp; sp = sp->Item.pNext ? ((laListItem *)sp->Item.pNext)->pNext : 0){
- if (strSame(id, sp->Content)) return (strSame(((laStringPart *)sp->Item.pNext)->Content, value));
- }
- return 0;
- }
- int strGetIntSimple(char *content){
- int a;
- sscanf(content, "%d", &a);
- return a;
- }
- real strGetFloatSimple(char *content){
- real a;
- sscanf(content, "%lf", &a);
- return a;
- }
- void strConvInt_CString(int src, char *dest, int lenth){
- sprintf(dest, "%d", src);
- };
- void strConvFloat_CString(real src, char *dest, int lenth){
- sprintf(dest, "%lf", src);
- };
- void strCopyFull(char *dest, char *src){
- if (src && dest) strcpy(dest, src);
- }
- void strCopySized(char *dest, int LenthLim, char *src){
- if (src && dest) strcpy(dest, src);
- }
- void strPrintFloatAfter(char *dest, int LenthLim, int bits, real data){
- char temp[64]={0};
- sprintf(temp, "%.*lf", bits, data);
- strcat(dest, temp);
- }
- void strPrintIntAfter(char *dest, int LenthLim, int data){
- char temp[64]={0};
- sprintf(&temp[0], "%d", data);
- strcat(dest, temp);
- }
- void strEscapePath(char* OutCanBeSame, char* path){
- char t[256]={0}; int ti=0;
- for(int i=0;path[i];i++,ti++){
- if(path[i]=='.'){ t[ti]='\\'; ti++; }
- t[ti]=path[i];
- }
- strcpy(OutCanBeSame,t);
- }
- int strSame(char *src, char *dest){
- return (src && dest && !strcmp(src, dest));
- }
- void strSafeDestroy(laSafeString **ss){
- if (!*ss) return; lstRemoveItem(&SSC.SafeStrings, *ss);
- if((*ss)->Ptr) memFree((*ss)->Ptr);
- memFree(*ss); *ss=0;
- }
- void strSafeSet(laSafeString **ss, char *Content){
- int len;
- if (!Content||!Content[0]){ strSafeDestroy(ss); return; }
- len = strlen(Content);
- if (len < 1) return;
- if (*ss){
- char* mem=memAcquireSimple(sizeof(char)*(len+1));
- strcpy(mem, Content);
- memFree((*ss)->Ptr);
- (*ss)->Ptr=mem;
- return;
- }
- (*ss) = memAcquireSimple(sizeof(laSafeString));
- (*ss)->Ptr = memAcquireSimple(sizeof(char)*(len+1));
- strcpy((*ss)->Ptr, Content);
- lstAppendItem(&SSC.SafeStrings, *ss);
- }
- void strSafeAppend(laSafeString **ss, char *Content){
- if(!ss || !(*ss) || !Content){ strSafeSet(ss, Content); return; }
- int OrigLen=strlen((*ss)->Ptr), ContentLen=strlen(Content);
- char* mem=memAcquireSimple(sizeof(char)*(OrigLen+ContentLen+1));
- memcpy(mem, (*ss)->Ptr, sizeof(char)*OrigLen);
- memcpy(mem+sizeof(char)*OrigLen, Content, sizeof(char)*ContentLen);
- mem[OrigLen+ContentLen]=0;
- memFree((*ss)->Ptr);
- (*ss)->Ptr=mem;
- }
- void strSafePrint(laSafeString **ss, char *Format, ...){
- char content[512]; va_list va; va_start(va, Format); vsprintf(content, Format, va); va_end(va);
- strSafeAppend(ss,content);
- }
- void strSafePrintV(laSafeString **ss, char *Format, va_list args){
- char content[512]; va_list va; vsprintf(content, Format, args);
- strSafeAppend(ss,content);
- }
- void strSafeDump(){
- laSafeString*ss;
- while(ss=lstPopItem(&SSC.SafeStrings)){
- if(ss->Ptr) printf("[String not freed] \"%s\"\n", ss->Ptr);
- }
- }
- void strBeginEdit(laStringEdit **se, char *FullStr){
- char *p = FullStr;
- char buf[1024];
- laStringEdit *nse = CreateNew(laStringEdit);
- if(*se){ memcpy(nse,*se,sizeof(laStringEdit)); nse->Lines.pFirst=nse->Lines.pLast=0; nse->TotalLines=0; }
- strEndEdit(se, 1);
- nse->_BeginLine = -1; nse->_BeginBefore = -1;
- if (FullStr && FullStr[0]){
- while ((*p)){
- laStringLine *sl = memAcquireSimple(sizeof(laStringLine));
- p += strGetStringTerminateBy(p, '\n', buf);
- strToUnicode(sl->Buf, buf);
- lstAppendItem(&nse->Lines, sl); nse->TotalLines++;
- if(*p){ p+=1; }
- }
- }
- if (!nse->Lines.pFirst){
- laStringLine *sl = memAcquireSimple(sizeof(laStringLine));
- lstAppendItem(&nse->Lines, sl); nse->TotalLines++;
- }
- *se=nse;
- }
- char* strGetEditString(laStringEdit *se, int SelectionOnly){
- if(!se) return 0; char* result=0; int next=0, max=0, len=0;
- arrEnsureLength(&result, 0, &max, sizeof(char)); int NextChar=0;
- int Line=0, starti=0, endat=0;
- for(laStringLine* sl=se->Lines.pFirst;sl;sl=sl->Item.pNext,Line++){
- starti=0;
- if(SelectionOnly && Line<se->BeginLine){ continue; }
- if(SelectionOnly && Line==se->BeginLine){ starti=se->BeginBefore; }
- int tlen=strlenU(&sl->Buf[starti]); int Extra=sl->Item.pNext?2:1;
- arrEnsureLength(&result, (len+tlen)*4+Extra, &max, sizeof(char));
- if(SelectionOnly && Line==se->EndLine){ endat=NextChar+se->EndBefore-starti; }
- NextChar+=strToUTF8(&result[NextChar], &sl->Buf[starti]); len+=tlen;
- if(Extra==2){ result[NextChar]='\n'; NextChar+=1; }
- if(SelectionOnly && Line==se->EndLine){ result[endat]=0; break; }
- }
- return result;
- }
- char* strEndEdit(laStringEdit **se, int FreeString){
- char *p=0; laStringLine *sl, *NextSl;
- if (!se || !(*se)) return 0;
- p=strGetEditString(*se, 0);
- while (sl=lstPopItem(&(*se)->Lines)){ memFree(sl); }
- FreeMem(*se); *se=0;
- if(FreeString && p){ free(p); p=0; }
- return p;
- }
- void strSetEditViewRange(laStringEdit* se, int Lines, int Cols){
- se->ViewHeight = Lines; se->ViewWidth = Cols;
- }
- void strEnsureCursorVisible(laStringEdit* se){
- if(!se->ViewHeight || !se->ViewWidth || se->CursorLine<0 || se->CursorBefore<0 ){return;}
- if(se->CursorLine>se->ViewHeight+se->ViewStartLine-1){ se->ViewStartLine=se->CursorLine-se->ViewHeight+1; }
- if(se->CursorLine<se->ViewStartLine){ se->ViewStartLine=se->CursorLine; }
- if(se->CursorBefore>se->ViewStartCol+se->ViewWidth-1){ se->ViewStartCol=se->CursorBefore-se->ViewWidth+1; }
- if(se->CursorBefore<se->ViewStartCol){ se->ViewStartCol=se->CursorBefore; }
- }
- void strRemoveLine(laStringEdit *se, laStringLine *sl){
- lstRemoveItem(&se->Lines, sl);
- memFree(sl); se->TotalLines--;
- }
- void strRemoveLineI(laStringEdit *se, int LineIndex){
- int i = 0;
- laStringLine *sl = se->Lines.pFirst, *NextSl;
- while (sl){
- NextSl = sl->Item.pNext;
- if (i == LineIndex){ strRemoveLine(se, sl); break; }
- i++;
- sl = NextSl;
- }
- }
- void strSetCursor(laStringEdit *se, int LineIndex, int BeforeIndex){
- int maxbefore;
- if (!se) return;
-
- if(LineIndex<0){LineIndex=0;}
- se->CursorLine = LineIndex;
- maxbefore = strlenU(strGetCursorLine(se, &se->CursorLine)->Buf);
- BeforeIndex = BeforeIndex < 0 ? 0 : BeforeIndex > maxbefore ? maxbefore : BeforeIndex;
- se->CursorBefore = BeforeIndex;
- se->BeginLine = -1;
- se->BeginBefore = -1;
- se->EndLine = -1;
- se->EndBefore = -1;
- strEnsureCursorVisible(se);
- }
- void strMoveCursor(laStringEdit *se, int Left, int Select){
- int maxbefore;
- int BeforeIndex;
- int width = 1;
- laStringLine *sl;
- if (!se) return;
- if(Select){ strLazySelect(se); } else { strCancelSelect(se); }
- sl = strGetCursorLine(se, 0);
- maxbefore = strlenU(sl->Buf);
- BeforeIndex = se->CursorBefore - (Left ? 1 : -1);
- if(BeforeIndex<0){
- strSetCursor(se, se->CursorLine-1, INT_MAX);
- }elif(BeforeIndex>maxbefore && se->CursorLine<se->TotalLines-1){
- strSetCursor(se, se->CursorLine+1, 0);
- }else{
- se->CursorBefore = BeforeIndex>=maxbefore?maxbefore:BeforeIndex;
- }
- se->CursorPreferBefore = se->CursorBefore;
- se->BeginLine = -1;
- se->BeginBefore = -1;
- se->EndLine = -1;
- se->EndBefore = -1;
- if(Select){ strEndSelect(se); }
- strEnsureCursorVisible(se);
- }
- void strMoveCursorLine(laStringEdit *se, int Up, int Select){
- int Line, maxbefore, LastIndex=-1;
- laStringLine *sl;
- if (!se) return;
- if(Select){ strLazySelect(se); } else { strCancelSelect(se); }
- Line=se->CursorLine - (Up? 1:-1);
- if(Line<0) {Line=0;}
- se->CursorLine = Line;
- sl = strGetCursorLine(se, &LastIndex);
- if(LastIndex>=0){ se->CursorLine = LastIndex; se->CursorPreferBefore=10000; }
- maxbefore = strlenU(sl->Buf);
- se->CursorBefore = se->CursorPreferBefore;
- if(se->CursorBefore>maxbefore){ se->CursorBefore = maxbefore; }
- if(LastIndex>=0){se->CursorPreferBefore=se->CursorBefore;}
- if(Select){ strEndSelect(se); }
- strEnsureCursorVisible(se);
- }
- int strHasSelection(laStringEdit* se){
- return se->BeginBefore!=se->EndBefore||se->BeginLine!=se->EndLine;
- }
- void strCancelSelect(laStringEdit *se){
- if (!se) return;
- se->_BeginLine = -1;
- se->_BeginBefore = -1;
- se->BeginLine = -1;
- se->EndLine = -1;
- se->BeginBefore = -1;
- se->EndBefore = -1;
- }
- void strLazySelect(laStringEdit *se){
- if (!se || se->_BeginLine>=0) return;
- se->_BeginLine = se->CursorLine;
- se->_BeginBefore = se->CursorBefore;
- }
- void strEndSelect(laStringEdit *se){
- if (!se) return;
- se->_EndLine = se->CursorLine;
- se->_EndBefore = se->CursorBefore;
- se->BeginLine = se->_BeginLine;
- se->EndLine = se->_EndLine;
- se->BeginBefore = se->_BeginBefore;
- se->EndBefore = se->_EndBefore;
- if(se->BeginLine>se->EndLine || (se->BeginLine==se->EndLine && se->BeginBefore>se->EndBefore))
- { LA_SWAP(int,se->BeginLine,se->EndLine); LA_SWAP(int,se->BeginBefore,se->EndBefore); }
- }
- void strSelectLineAll(laStringEdit *se){
- if (!se) return;
- laStringLine *sl;
- int len;
- if (se->CursorLine == -1) sl = strGetBeginLine(se);
- else
- sl = strGetCursorLine(se, 0);
- len = strlenU(sl->Buf);
- se->EndBefore = len; se->EndLine=0;
- se->BeginBefore = 0; se->BeginLine=0;
- se->CursorBefore = len;
- se->CursorLine = 0;
- }
- void strDeselectAll(laStringEdit *se){
- if (!se) return;
- laStringLine *sl;
- int len;
- if (se->CursorLine == -1) sl = strGetBeginLine(se);
- else
- sl = strGetCursorLine(se, 0);
- len = strlenU(sl->Buf);
- se->EndBefore = -1;
- se->BeginBefore = -1;
- se->BeginLine = -1;
- se->EndLine = -1;
- se->CursorBefore = len;
- se->CursorLine = -1;
- }
- void strPanFoward(uint32_t *str, int Before, int Offset){
- int len = strlenU(str);
- int i = len + 1;
- for (i; i >= Before; i--){
- str[i + Offset] = str[i];
- }
- }
- void strSquishBackward(uint32_t *str, int Before, int EndBefore){
- int len = strlenU(str);
- int i = Before;
- int Offset = Before - EndBefore;
- if (Before <= 0) return;
- for (i; i <= len; i++){
- str[i - Offset] = str[i];
- }
- }
- void strClearSelection(laStringEdit *se){
- //if (se->EndLine == -1) return;
- if (se->BeginLine != se->EndLine){
- int i = 0; int RemovedLines=0;
- laStringLine *sl = se->Lines.pFirst, *NextSl;
- while (sl){
- NextSl = sl->Item.pNext;
- if (i == se->BeginLine){
- sl->Buf[se->BeginBefore] = L'\0';
- }else if (i > se->BeginLine && i < se->EndLine){
- strRemoveLine(se, sl); RemovedLines++;
- }else if (i == se->EndLine){
- strSquishBackward(sl->Buf, se->EndBefore, 0);
- se->CursorLine = i-RemovedLines;
- se->CursorBefore = 0;
- se->BeginLine = -1;
- se->BeginBefore = -1;
- se->EndLine = -1;
- se->EndBefore = -1;
- strBackspace(se);
- }
- if (i > se->EndLine) break;
- i++;
- sl = NextSl;
- }
- }else{
- int i = 0;
- laStringLine *sl = se->Lines.pFirst, *NextSl;
- while (sl){
- NextSl = sl->Item.pNext;
- if (i == se->EndLine) {
- strSquishBackward(sl->Buf, se->EndBefore, se->BeginBefore);
- se->CursorLine = i;
- se->CursorBefore = se->BeginBefore;
- se->BeginLine = -1;
- se->BeginBefore = -1;
- se->EndLine = -1;
- se->EndBefore = -1;
- break;
- }
- i++; sl = NextSl;
- }
- }
- strEnsureCursorVisible(se);
- }
- laStringLine *strGetCursorLine(laStringEdit *se, int* ReturnIndexIfLast){
- if (!se || se->CursorBefore <= -1) return se->Lines.pFirst;
- int i = 0;
- laStringLine *sl = se->Lines.pFirst, *NextSl;
- while (sl){
- NextSl = sl->Item.pNext;
- if (i == se->CursorLine){
- return sl;
- }
- i++;
- sl = NextSl;
- }
- if(ReturnIndexIfLast){ *ReturnIndexIfLast=i-1;}
- return se->Lines.pLast;
- }
- laStringLine *strGetBeginLine(laStringEdit *se){
- if (!se || se->BeginLine <= -1) return se->Lines.pFirst;
- int i = 0;
- laStringLine *sl = se->Lines.pFirst, *NextSl;
- while (sl){
- NextSl = sl->Item.pNext;
- if (i == se->BeginLine){
- return sl;
- }
- i++;
- sl = NextSl;
- }
- return se->Lines.pFirst;
- }
- void strInsertChar(laStringEdit *se, uint32_t a){
- laStringLine *sl;
- strClearSelection(se);
- sl = strGetCursorLine(se, 0);
- if(a==L'\n'){
- laStringLine* nl=memAcquireSimple(sizeof(laStringLine));
- if(sl->Buf[se->CursorBefore]!=L'\0') strcpyU(nl->Buf, &sl->Buf[se->CursorBefore]);
- sl->Buf[se->CursorBefore]=L'\0';
- se->CursorLine++; se->CursorBefore=0;
- lstInsertItemAfter(&se->Lines, nl, sl); se->TotalLines++;
- }else{
- strPanFoward(sl->Buf, se->CursorBefore, 1);
- sl->Buf[se->CursorBefore] = a;
- se->CursorBefore += 1;
- }
- se->CursorPreferBefore = se->CursorBefore;
- strEnsureCursorVisible(se);
- }
- void strBackspace(laStringEdit *se){
- laStringLine *sl;
- int width = 1;
- if (se->CursorBefore == -1){
- strClearSelection(se);
- }else{
- laStringLine *sl;
- sl = strGetCursorLine(se, 0);
- if (se->CursorBefore > 1 && sl->Buf[se->CursorBefore - 2] < 0) width = 2;
- strSquishBackward(sl->Buf, se->CursorBefore, se->CursorBefore - width);
- se->CursorBefore -= width;
- if (se->CursorBefore <= -1){
- if(sl->Item.pPrev){
- laStringLine* ol=sl->Item.pPrev;
- se->CursorBefore = strlenU(ol->Buf);
- se->CursorLine--;
- strcatU(ol->Buf, sl->Buf);
- strRemoveLine(se, sl);
- }
- else {se->CursorBefore = 0;}
- }
- }
- se->CursorPreferBefore = se->CursorBefore;
- strEnsureCursorVisible(se);
- }
- void strMoveView(laStringEdit *se, int DownLines, int RightCharacters){
- se->ViewStartLine+=DownLines;
- se->ViewStartCol+=RightCharacters;
- if(se->ViewStartLine>=se->TotalLines-1) se->ViewStartLine=se->TotalLines-1;
- if(se->ViewStartLine<0) se->ViewStartLine=0;
- if(se->ViewStartCol<0) se->ViewStartCol=0;
- }
- //======================================================[ translation ]
- void transNewLanguage(const char *LanguageID){
- laTranslationNode *tn = CreateNew(laTranslationNode);
- strSafeSet(&tn->LanguageName, LanguageID);
- lstAppendItem(&MAIN.Translation.Languages, tn);
- MAIN.Translation.CurrentLanguage = tn;
- }
- void transSetLanguage(const char *LanguageID){
- laTranslationNode *tn;
- if (!LanguageID){
- MAIN.Translation.CurrentLanguage = 0;
- return;
- }
- for (tn = MAIN.Translation.Languages.pFirst; tn; tn = tn->Item.pNext){
- if (!strcmp(tn->LanguageName->Ptr, LanguageID)){
- MAIN.Translation.CurrentLanguage = tn;
- break;
- }
- }
- }
- void transDumpMissMatchRecord(const char *filename){
- laTranslationMatch *tm;
- laListHandle *lst;
- int i;
- FILE *f = fopen(filename, "w");
- if (!f) return;
- for (i = 0; i < 256; i++){
- lst = &MAIN.Translation.MisMatches.Entries[i];
- for (tm = lst->pFirst; tm; tm = tm->Item.pNext){
- fprintf(f, "transNewEntry(\"%s\",\"\");\n", tm->Target);
- }
- }
- fclose(f);
- }
- int IsThisTranslationMatch(laTranslationMatch *tm, char *p){
- return (tm->Target && !strcmp(tm->Target, p));
- }
- void transNewEntry(const char *Target, const char *replacement){
- laTranslationMatch *tm = memAcquireSimple(sizeof(laTranslationMatch));
- tm->Target = Target;
- tm->Replacement = replacement;
- hsh256InsertItemCSTR(&MAIN.Translation.CurrentLanguage->Matches, tm, Target);
- }
- void transNewMissEntry(const char *Target){
- if (!hsh256FindItemSTR(&MAIN.Translation.MisMatches, IsThisTranslationMatch, Target)){
- laTranslationMatch *tm = memAcquireSimple(sizeof(laTranslationMatch));
- tm->Target = Target;
- hsh256InsertItemCSTR(&MAIN.Translation.MisMatches, tm, Target);
- }
- }
- char *transLate(char *Target){
- if (!MAIN.Translation.CurrentLanguage || !MAIN.Translation.EnableTranslation) return Target;
- laTranslationMatch *tm = hsh256FindItemSTR(&MAIN.Translation.CurrentLanguage->Matches, IsThisTranslationMatch, Target);
- if (!tm){
- transNewMissEntry(Target);
- return Target;
- }
- return tm->Replacement;
- }
- void transState(void *UNUSED, int val){
- if (val) MAIN.Translation.EnableTranslation = 1;
- else
- MAIN.Translation.EnableTranslation = 0;
- laRedrawCurrentWindow();
- }
- void laOpenInternetLink(char *url){
- laSafeString* s=0; strSafePrint(&s, "xdg-open %s", url);
- system(s->Ptr);
- strSafeDestroy(&s);
- //these were windows stuff
- //HKEY hkRoot, hSubKey;
- //char ValueName[256]={0};
- //char DataValue[256]={0};
- //unsigned long cbValueName = 256;
- //unsigned long cbDataValue = 256;
- //char ShellChar[512]={0};
- //DWORD dwType;
- //
- //ShellExecute(0, "open", link, 0, 0, SW_SHOWNORMAL);
- //
- //return;
- }
|