1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194 |
- /*
- * LaGUI: A graphical application framework.
- * Copyright (C) 2022-2023 Wu Yiming
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #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 strToUTF8Lim(unsigned char* target, uint32_t* const src, int count){
- uint32_t* source=src; unsigned char* out=target; int i=0; while(laToUTF8(*source, out, &out)){ source++; i++; if(i>=count) break; } *out=0; return out-target;
- }
- int strToUTF8(unsigned char* target, uint32_t* const src){
- strToUTF8Lim(target,src,INT_MAX);
- }
- 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){
- #ifdef __linux__
- clock_gettime(CLOCK_REALTIME, &tr->ts);
- #endif
- #ifdef _WIN32
- QueryPerformanceCounter(&tr->tm);
- #endif
- }
- real laTimeElapsedSecondsf(laTimeRecorder *End, laTimeRecorder *Begin){
- #ifdef __linux__
- real sec=End->ts.tv_sec-Begin->ts.tv_sec; sec+=((End->ts.tv_nsec-Begin->ts.tv_nsec)/1e9);
- #endif
- #ifdef _WIN32
- LARGE_INTEGER perfCnt; QueryPerformanceFrequency(&perfCnt);
- real sec = ((real)(End->tm.QuadPart - Begin->tm.QuadPart))/ perfCnt.QuadPart;
- #endif
- return sec;
- }
- void laSetAuthorInfo(char *Name, char *CopyrightString){
- strSafeSet(&MAIN.Author.Name, Name);
- strSafeSet(&MAIN.Author.CopyrightString, CopyrightString);
- }
- void memCreateNUID(char* buf,laMemNodeHyper* Hyper){
- sprintf(buf, "%08X-%hd%02hd%02hd%02hd%02hd%02hd", Hyper, LA_HYPER_CREATED_TIME(Hyper));
- }
- 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->NUID.String,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){
- laListItem* 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;
- };
- popitem->pNext=popitem->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 0;
- 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){
- unsigned char 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;
- laListItem* item=lstFindItem(buckle, func, &hash->Entries[hsh]);
- return item;
- }
- //================================================================ [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;
- 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);
- #ifdef DEBUG
- printf("left freed %x\n",m);
- #endif
- }
- }
- }
- 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; if(*user==This){ (*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){
- laItemUserLinker*iul=laUseDataBlock(instance, 0, 0, ptr, la_ReferencedBlockDeleted, 0); if(iul){iul->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 == U'\0') return 0;
- if (*NextMark == U'~') FloatArg = 1;
- // container@identifier=window container#window contianer%
- int UC=1;
- while (!lenth){
- for (countP; *countP != U'.' && *(*pivot) != U'\0' && UC && *countP && *countP != U'@' && *countP != U'=' && *countP != U'#' && *countP != U'$';){
- 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 = U'.';
- 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] = U'\0';
- if ((*pivot)[lenth] == U'\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 == U'\0')) return 0;
- int UC;
- for (Ofst; content[Ofst] != terminator && content[Ofst] != U'\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 == U'\0') return 0;
- if (IgnoreSpace) for (i; content[i] == U' '; i++) ;
- int UC,advance;
- for (Ofst; content[Ofst] != terminator && content[Ofst] != U'\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] = U'\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 0;
- 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] != U'.') 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 strToUpper(char *Str){
- char *p = Str; if (!p) return;
- while (*p){
- if (*p >= 'a' && *p <= 'z') *p += 'A' - 'a';
- p++;
- }
- }
- void strToLower(char *Str){
- char *p = Str; if (!p) return;
- while (*p){
- if (*p >= 'A' && *p <= 'Z') *p -= 'A' - 'a';
- p++;
- }
- }
- laStringSplitor *strSplitPath(char *path,char terminator){
- laStringPart *sp;
- laStringSplitor *ss;
- char *pivot = path;
- char *temp_result;
- char Type = terminator?terminator:'.';
- char NextType = '.';
- if (!path || !path[0]) return 0;
- ss = memAcquireSimple(sizeof(laStringSplitor));
- while (temp_result = strGetNextString(&pivot, &NextType)){
- if (*temp_result != U'\0'){
- sp = memAcquireSimple(sizeof(laStringPart));
- sp->Content = temp_result;
- lstAppendItem(&ss->parts, sp);
- ss->NumberParts += 1;
- if (NextType == U'$') sp->Type = U'$';
- else
- sp->Type = Type;
- if (sp->Type == U'='){
- if (sp->Content[0] >= U'0' && sp->Content[0] <= 9){
- sscanf(sp->Content, "%d", &sp->IntValue);
- }
- }
- if (NextType == U'$') NextType = U'.';
- 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 != U'\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 != U'\0'){
- sp = memAcquireSimple(sizeof(laStringPart));
- sp->Content = temp_result;
- lstAppendItem(&ss->parts, sp);
- ss->NumberParts += 1;
- if (temp_result[0] >= U'0' && temp_result[0] <= U'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=INT_MAX;
- 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+=strToUTF8Lim(&result[NextChar], &sl->Buf[starti], endat); len+=tlen;
- if(Extra==2){ result[NextChar]='\n'; NextChar+=1; }
- if(SelectionOnly && Line==se->EndLine){ 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){
- if(se->CursorLine>0) strSetCursor(se, se->CursorLine-1, INT_MAX);
- }elif(BeforeIndex>maxbefore && se->CursorLine<se->TotalLines-1){
- if(se->CursorLine>0) 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 = TNS_MAX2(se->CursorLine,0);
- 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] = U'\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==U'\n'){
- laStringLine* nl=memAcquireSimple(sizeof(laStringLine));
- if(sl->Buf[se->CursorBefore]!=U'\0') strcpyU(nl->Buf, &sl->Buf[se->CursorBefore]);
- sl->Buf[se->CursorBefore]=U'\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;
- }
- int laCopyFile(char *to, char *from){
- #ifdef _WIN32
- if(CopyFile(from, to, 0)) return 1; return 0;
- #endif
- #ifdef __linux__
- int fd_to, fd_from; char buf[4096];
- ssize_t nread; int saved_errno;
- fd_from = open(from, O_RDONLY); if (fd_from < 0) return -1;
- fd_to = open(to, O_WRONLY|O_CREAT|O_EXCL, 0666); if (fd_to < 0) goto out_error;
- while (nread=read(fd_from,buf,sizeof(buf)), nread>0) {
- char *out_ptr = buf; ssize_t nwritten;
- do { nwritten = write(fd_to, out_ptr, nread);
- if (nwritten >= 0){ nread -= nwritten; out_ptr += nwritten;}
- else if (errno != EINTR){ goto out_error; }
- }while (nread > 0);
- }
- if (nread == 0){
- if (close(fd_to)<0){ fd_to = -1; goto out_error;}
- close(fd_from);
- /* Success! */
- return 0;
- }
- out_error:
- saved_errno = errno;
- close(fd_from); if (fd_to >= 0) close(fd_to);
- errno = saved_errno;
- return -1;
- #endif //linux
- }
- //======================================================[ translation ]
- void transNewLanguage(const char *LanguageID){
- laTranslationNode *tn = memAcquire(sizeof(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; return;
- }
- }
- transNewLanguage(LanguageID);
- }
- 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){ if(tm->Target) fprintf(f, "%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));
- int len=strlen(Target); tm->Target=memAcquireSimple(len*sizeof(char)+1);
- strcpy(tm->Target,Target);
- hsh256InsertItemCSTR(&MAIN.Translation.MisMatches, tm, Target);
- }
- }
- char *transLate(char *Target){
- if (!MAIN.Translation.CurrentLanguage || !MAIN.Translation.EnableTranslation || !Target || !Target[0]) 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;
- }
- #ifdef _WIN32
- void usleep(unsigned int usec){
- HANDLE timer;
- LARGE_INTEGER ft;
- static int init = 0;
- if (init == 0){
- init = 1;
- const HINSTANCE ntdll = LoadLibrary("ntdll.dll");
- if (ntdll != NULL){
- typedef long(NTAPI* pNtQueryTimerResolution)(unsigned long* MinimumResolution, unsigned long* MaximumResolution, unsigned long* CurrentResolution);
- typedef long(NTAPI* pNtSetTimerResolution)(unsigned long RequestedResolution, char SetResolution, unsigned long* ActualResolution);
- pNtQueryTimerResolution NtQueryTimerResolution = (pNtQueryTimerResolution)GetProcAddress(ntdll, "NtQueryTimerResolution");
- pNtSetTimerResolution NtSetTimerResolution = (pNtSetTimerResolution)GetProcAddress(ntdll, "NtSetTimerResolution");
- if (NtQueryTimerResolution != NULL && NtSetTimerResolution != NULL){
- unsigned long minimum, maximum, current;
- NtQueryTimerResolution(&minimum, &maximum, ¤t);
- NtSetTimerResolution(maximum, (char)1, ¤t);
- }
- FreeLibrary(ntdll);
- }
- }
- ft.QuadPart = -(10 * (__int64)usec);
- timer = CreateWaitableTimer(NULL, TRUE, NULL);
- SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
- WaitForSingleObject(timer, INFINITE);
- CloseHandle(timer);
- }
- #endif
|