*/}}

la_util.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723
  1. /*
  2. * LaGUI: A graphical application framework.
  3. * Copyright (C) 2022-2023 Wu Yiming
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define _CRT_SEQURE_NO_WARNINGS
  19. #include "la_util.h"
  20. #include "la_interface.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. laSafeStringCollection SSC;
  24. extern LA MAIN;
  25. #define BYTE unsigned char
  26. uint32_t laToUnicode(const unsigned char* ch, int* advance){
  27. if((*ch)<0x80) { *advance=1; return *ch; }
  28. uint32_t u=0;
  29. if(((*ch)>>5)==0x06){ *advance=2; u|=((*(ch+1))&0x3f)|((*(ch)&0x1f)<<6); return u; }
  30. if(((*ch)>>4)==0x0e){ *advance=3; u|=((*(ch+2))&0x3f)|((*(ch+1)&0x3f)<<6)|((*(ch)&0x0f)<<12); return u; }
  31. if(((*ch)>>3)==0x1e){ *advance=4; u|=((*(ch+3))&0x3f)|((*(ch+2)&0x3f)<<6)|((*(ch+1)&0x3f)<<12)|((*(ch)&0x07)<<18); return u; }
  32. *advance=1; return '?';
  33. }
  34. int laToUTF8(const uint32_t ch, unsigned char* out, unsigned char** next){
  35. 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;}
  36. elif(ch>=0x800){ out[0]=0xe0|(ch>>12); out[1]=0x80|(0x3f&(ch>>6)); out[2]=0x80|(0x3f&ch); (*next)+=3;}
  37. elif(ch>=0x80){ out[0]=0xc0|(ch>>6); out[1]=0x80|(0x3f&ch); (*next)+=2;}
  38. else { if(!ch){return 0;} out[0]=ch&0x7f; (*next)++;} return 1;
  39. }
  40. int strToUnicode(uint32_t* target, unsigned char* const src){
  41. uint32_t UC,adv,i=0; unsigned char* source=src; while(target[i]=laToUnicode(source, &adv)) { source+=adv; i++; } target[i]=0; return i;
  42. }
  43. int strToUTF8Lim(unsigned char* target, uint32_t* const src, int count){
  44. 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;
  45. }
  46. int strToUTF8(unsigned char* target, uint32_t* const src){
  47. strToUTF8Lim(target,src,INT_MAX);
  48. }
  49. int strlenU(uint32_t* str){ int i=0; while(str[i]!=0) i++; return i; }
  50. int strcpyU(uint32_t* target, uint32_t* const source ){ int i=0; while(source[i]!=0){ target[i]=source[i]; i++; } target[i]=0; }
  51. 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; }
  52. struct tm *laGetFullTime(){
  53. time_t t = time(0);
  54. return localtime(&t);
  55. }
  56. void laRecordTime(laTimeRecorder *tr){
  57. #if (defined(LA_LINUX) || defined(LAGUI_ANDROID))
  58. clock_gettime(CLOCK_REALTIME, &tr->ts);
  59. #endif
  60. #ifdef _WIN32
  61. QueryPerformanceCounter(&tr->tm);
  62. #endif
  63. }
  64. real laTimeElapsedSecondsf(laTimeRecorder *End, laTimeRecorder *Begin){
  65. real sec=0;
  66. #if (defined(LA_LINUX) || defined(LAGUI_ANDROID))
  67. sec=End->ts.tv_sec-Begin->ts.tv_sec; sec+=((End->ts.tv_nsec-Begin->ts.tv_nsec)/1e9);
  68. #endif
  69. #ifdef _WIN32
  70. LARGE_INTEGER perfCnt; QueryPerformanceFrequency(&perfCnt);
  71. sec = ((real)(End->tm.QuadPart - Begin->tm.QuadPart))/ perfCnt.QuadPart;
  72. #endif
  73. return sec;
  74. }
  75. void laSetAuthorInfo(char *Name, char *CopyrightString){
  76. strSafeSet(&MAIN.Author.Name, Name);
  77. strSafeSet(&MAIN.Author.CopyrightString, CopyrightString);
  78. }
  79. void memCreateNUID(char* buf,laMemNodeHyper* Hyper){
  80. sprintf(buf, "%08X-%hd%02hd%02hd%02hd%02hd%02hd", Hyper, LA_HYPER_CREATED_TIME(Hyper));
  81. }
  82. void memHyperInfo(laPropPack* pp, char* buf){
  83. int level=0;void* head=0;
  84. laMemNodeHyper* hi; laMemNode* mn;
  85. int a=0, count=0, pc;
  86. laProp* p=pp->LastPs->p;
  87. laPropContainer* c=p->Container;
  88. if(c->OtherAlloc){
  89. count=lstCountElements(&c->LocalUsers);
  90. }else{
  91. head=memGetHead(pp->LastPs->UseInstance, &level);
  92. if(!level){
  93. sprintf(buf,"Not HyperData.\n");
  94. }elif(level==1){
  95. mn=head; count=lstCountElements(&mn->Users);
  96. }elif(level==2){
  97. hi=head; count=lstCountElements(&hi->Users);
  98. }
  99. }
  100. a=sprintf(buf,"HyperData:\n\tProperty:%s\n\tContainer:%s (%d users)\n",
  101. pp->LastPs->p->Identifier, pp->LastPs->p->Container->Identifier, count);
  102. if(level==2){
  103. sprintf(buf+a,"\tCreated:%hd-%02hd-%02hd %02hd:%02hd:%02hd\n",LA_HYPER_CREATED_TIME(hi));
  104. }
  105. }
  106. void memMakeHyperData(laMemNodeHyper* hi){
  107. struct tm *time;
  108. hi->Modified = 1;
  109. time = laGetFullTime();
  110. //hi->CreatedBy = &MAIN.Author;
  111. hi->TimeCreated.Year = time->tm_year + 1900;
  112. hi->TimeCreated.Month = time->tm_mon + 1;
  113. hi->TimeCreated.Day = time->tm_mday;
  114. hi->TimeCreated.Hour = time->tm_hour;
  115. hi->TimeCreated.Minute = time->tm_min;
  116. hi->TimeCreated.Second = time->tm_sec;
  117. //memcpy(&hi->TimeModified, &hi->TimeCreated, sizeof(laTimeInfo));
  118. memCreateNUID(hi->NUID.String,hi);
  119. }
  120. void memMarkClean(void* HyperUserMem){
  121. int Hyper=0;
  122. laMemNodeHyper* h = memGetHead(HyperUserMem, &Hyper);
  123. if(Hyper!=2) return;
  124. h->Modified=0;
  125. }
  126. void nutFreeMem(void **ptr){
  127. //free_total+=1;
  128. if (!*ptr) return;
  129. free(*ptr);
  130. *ptr = 0;
  131. }
  132. int nutFloatCompare(real l, real r){
  133. return (l > r - 0.00005 && l < r + 0.00005);
  134. }
  135. int nutSameAddress(void *l, void *r){
  136. return (l == r);
  137. }
  138. //===================================================================[barray]
  139. #ifdef _MSC_VER
  140. # include <intrin.h>
  141. # define __builtin_popcountll __popcnt64
  142. static inline int __builtin_ctzl(u64bit x)
  143. {
  144. #ifdef _WIN64
  145. return (int)_tzcnt_u64(x);
  146. #else
  147. return !!unsigned(x) ? __builtin_ctz((unsigned)x) : 32 + __builtin_ctz((unsigned)(x >> 32));
  148. #endif
  149. }
  150. #endif
  151. barray_t *barray_init(size_t num_bits)
  152. {
  153. size_t num_longs = BITS_TO_LONGS(num_bits);
  154. barray_t *barray = calloc(1,sizeof(u64bit) * num_longs + sizeof(barray_t));
  155. barray->num_bits = num_bits;
  156. barray->num_longs = num_longs;
  157. return barray;
  158. }
  159. void barray_free(barray_t *barray)
  160. {
  161. free(barray);
  162. }
  163. u64bit *barray_data(barray_t *barray)
  164. {
  165. return barray->data;
  166. }
  167. size_t barray_count_set(barray_t *barray)
  168. {
  169. size_t count = 0;
  170. for (int i = 0; i < barray->num_longs; i++)
  171. count += __builtin_popcountll(barray->data[i]);
  172. return count;
  173. }
  174. void barray_set(barray_t *barray, bit_t bit)
  175. {
  176. if (bit >= barray->num_bits)
  177. return;
  178. int index = bit / BITS_PER_LONG;
  179. int shift = bit % BITS_PER_LONG;
  180. barray->data[index] |= ((u64bit)1 << shift);
  181. }
  182. void barray_clear(barray_t *barray, bit_t bit)
  183. {
  184. if (bit >= barray->num_bits)
  185. return;
  186. int index = bit / BITS_PER_LONG;
  187. int shift = bit % BITS_PER_LONG;
  188. barray->data[index] &= ~((u64bit)1 << shift);
  189. }
  190. bool barray_is_set(barray_t *barray, bit_t bit)
  191. {
  192. if (bit >= barray->num_bits)
  193. return false;
  194. int index = bit / BITS_PER_LONG;
  195. int shift = bit % BITS_PER_LONG;
  196. return (barray->data[index] & ((u64bit)1 << shift)) != 0;
  197. }
  198. void barray_foreach_set(barray_t *barray, barray_callback_t callback, void *arg)
  199. {
  200. for (int i = 0; i < barray->num_longs; i++)
  201. {
  202. u64bit bits = barray->data[i];
  203. while (bits != 0)
  204. {
  205. callback(i * BITS_PER_LONG + __builtin_ctzl(bits), arg);
  206. bits ^= (bits & -bits);
  207. }
  208. }
  209. }
  210. //===================================================================[md5]
  211. #define A 0x67452301
  212. #define B 0xefcdab89
  213. #define C 0x98badcfe
  214. #define D 0x10325476
  215. static uint32_t S[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
  216. 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
  217. 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
  218. 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};
  219. static uint32_t K[] = {0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
  220. 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
  221. 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
  222. 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
  223. 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
  224. 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
  225. 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
  226. 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
  227. 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
  228. 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
  229. 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
  230. 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
  231. 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
  232. 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
  233. 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
  234. 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391};
  235. static uint8_t PADDING[] = {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  236. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  237. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  238. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  239. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  240. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  241. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  242. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  243. #define F(X, Y, Z) ((X & Y) | (~X & Z))
  244. #define G(X, Y, Z) ((X & Z) | (Y & ~Z))
  245. #define H(X, Y, Z) (X ^ Y ^ Z)
  246. #define I(X, Y, Z) (Y ^ (X | ~Z))
  247. uint32_t rotateLeft(uint32_t x, uint32_t n){
  248. return (x << n) | (x >> (32 - n));
  249. }
  250. void md5Init(MD5Context *ctx){
  251. ctx->size = (uint64_t)0;
  252. ctx->buffer[0] = (uint32_t)A;
  253. ctx->buffer[1] = (uint32_t)B;
  254. ctx->buffer[2] = (uint32_t)C;
  255. ctx->buffer[3] = (uint32_t)D;
  256. }
  257. void md5Update(MD5Context *ctx, uint8_t *input_buffer, size_t input_len){
  258. uint32_t input[16];
  259. unsigned int offset = ctx->size % 64;
  260. ctx->size += (uint64_t)input_len;
  261. // Copy each byte in input_buffer into the next space in our context input
  262. for(unsigned int i = 0; i < input_len; ++i){
  263. ctx->input[offset++] = (uint8_t)*(input_buffer + i);
  264. // If we've filled our context input, copy it into our local array input
  265. // then reset the offset to 0 and fill in a new buffer.
  266. // Every time we fill out a chunk, we run it through the algorithm
  267. // to enable some back and forth between cpu and i/o
  268. if(offset % 64 == 0){
  269. for(unsigned int j = 0; j < 16; ++j){
  270. // Convert to little-endian
  271. // The local variable `input` our 512-bit chunk separated into 32-bit words
  272. // we can use in calculations
  273. input[j] = (uint32_t)(ctx->input[(j * 4) + 3]) << 24 |
  274. (uint32_t)(ctx->input[(j * 4) + 2]) << 16 |
  275. (uint32_t)(ctx->input[(j * 4) + 1]) << 8 |
  276. (uint32_t)(ctx->input[(j * 4)]);
  277. }
  278. md5Step(ctx->buffer, input);
  279. offset = 0;
  280. }
  281. }
  282. }
  283. void md5Finalize(MD5Context *ctx){
  284. uint32_t input[16];
  285. unsigned int offset = ctx->size % 64;
  286. unsigned int padding_length = offset < 56 ? 56 - offset : (56 + 64) - offset;
  287. // Fill in the padding and undo the changes to size that resulted from the update
  288. md5Update(ctx, PADDING, padding_length);
  289. ctx->size -= (uint64_t)padding_length;
  290. // Do a final update (internal to this function)
  291. // Last two 32-bit words are the two halves of the size (converted from bytes to bits)
  292. for(unsigned int j = 0; j < 14; ++j){
  293. input[j] = (uint32_t)(ctx->input[(j * 4) + 3]) << 24 |
  294. (uint32_t)(ctx->input[(j * 4) + 2]) << 16 |
  295. (uint32_t)(ctx->input[(j * 4) + 1]) << 8 |
  296. (uint32_t)(ctx->input[(j * 4)]);
  297. }
  298. input[14] = (uint32_t)(ctx->size * 8);
  299. input[15] = (uint32_t)((ctx->size * 8) >> 32);
  300. md5Step(ctx->buffer, input);
  301. // Move the result into digest (convert from little-endian)
  302. for(unsigned int i = 0; i < 4; ++i){
  303. ctx->digest[(i * 4) + 0] = (uint8_t)((ctx->buffer[i] & 0x000000FF));
  304. ctx->digest[(i * 4) + 1] = (uint8_t)((ctx->buffer[i] & 0x0000FF00) >> 8);
  305. ctx->digest[(i * 4) + 2] = (uint8_t)((ctx->buffer[i] & 0x00FF0000) >> 16);
  306. ctx->digest[(i * 4) + 3] = (uint8_t)((ctx->buffer[i] & 0xFF000000) >> 24);
  307. }
  308. }
  309. void md5Step(uint32_t *buffer, uint32_t *input){
  310. uint32_t AA = buffer[0];
  311. uint32_t BB = buffer[1];
  312. uint32_t CC = buffer[2];
  313. uint32_t DD = buffer[3];
  314. uint32_t E;
  315. unsigned int j;
  316. for(unsigned int i = 0; i < 64; ++i){
  317. switch(i / 16){
  318. case 0:
  319. E = F(BB, CC, DD);
  320. j = i;
  321. break;
  322. case 1:
  323. E = G(BB, CC, DD);
  324. j = ((i * 5) + 1) % 16;
  325. break;
  326. case 2:
  327. E = H(BB, CC, DD);
  328. j = ((i * 3) + 5) % 16;
  329. break;
  330. default:
  331. E = I(BB, CC, DD);
  332. j = (i * 7) % 16;
  333. break;
  334. }
  335. uint32_t temp = DD;
  336. DD = CC;
  337. CC = BB;
  338. BB = BB + rotateLeft(AA + E + K[i] + input[j], S[i]);
  339. AA = temp;
  340. }
  341. buffer[0] += AA;
  342. buffer[1] += BB;
  343. buffer[2] += CC;
  344. buffer[3] += DD;
  345. }
  346. void md5String(char *input, uint8_t *result){
  347. MD5Context ctx;
  348. md5Init(&ctx);
  349. md5Update(&ctx, (uint8_t *)input, strlen(input));
  350. md5Finalize(&ctx);
  351. memcpy(result, ctx.digest, 16);
  352. }
  353. void md5File(FILE *file, uint8_t *result){
  354. char *input_buffer = malloc(1024);
  355. size_t input_size = 0;
  356. MD5Context ctx;
  357. md5Init(&ctx);
  358. while((input_size = fread(input_buffer, 1, 1024, file)) > 0){
  359. md5Update(&ctx, (uint8_t *)input_buffer, input_size);
  360. }
  361. md5Finalize(&ctx);
  362. free(input_buffer);
  363. memcpy(result, ctx.digest, 16);
  364. }
  365. void toHexString(char* text, char* hex){
  366. int len = strlen(text);
  367. for (int i = 0, j = 0; i < len; ++i, j += 2)
  368. sprintf(hex + j, "%02x", text[i] & 0xff);
  369. }
  370. #undef A
  371. #undef B
  372. #undef C
  373. #undef D
  374. //===================================================================[list]
  375. void* arrElement(void* head, int i, int size){
  376. return ((char*)head+size*i);
  377. }
  378. int arrEnsureLength(void** head, int next, int* max, size_t ElementSize){
  379. int UseMax=*max; int nocopy=(!UseMax);
  380. if(next>=UseMax){
  381. if(!UseMax){ UseMax=50; }
  382. int AllocMax=next>(UseMax*2)?(next+16):(UseMax*2);
  383. void* data = CreateNew_Size(ElementSize* AllocMax);
  384. if(((*head) || next)&&(!nocopy)){ memcpy(data, *head, ElementSize*UseMax); }
  385. if(*head) free(*head);
  386. *head=data;
  387. *max=AllocMax;
  388. return 1;
  389. }
  390. return 0;
  391. }
  392. int arrInitLength(void** head, int max, int* pmax, size_t ElementSize){
  393. if(*head){ free(head); }
  394. *head=CreateNew_Size(ElementSize*max);
  395. *pmax=max;
  396. }
  397. void arrFree(void** head, int* max){ free(*head); *head=0; *max=0; }
  398. void lstPushSingle(void **Head, laListSingle *Item){
  399. Item->pNext = *Head;
  400. *Head = Item;
  401. }
  402. void *lstPopSingle(void **Head, laListSingle *Item){
  403. *Head = ((laListSingle *)(*Head))->pNext;
  404. Item->pNext = 0;
  405. return *Head;
  406. }
  407. int lstCountElements(laListHandle* Handle){
  408. int count=0; if(!Handle) return 0;
  409. for(laListItem* i=Handle->pFirst;i;i=i->pNext){count++;}
  410. return count;
  411. }
  412. void lstAppendItem(laListHandle* Handle, void* Item){
  413. laListItem* li = Item;
  414. li->pNext = li->pPrev = 0;
  415. if (!Handle->pFirst) Handle->pFirst = Item;
  416. if (Handle->pLast) ((laListItem*)Handle->pLast)->pNext = li;
  417. li->pPrev = Handle->pLast;
  418. li->pNext = 0;
  419. Handle->pLast = li;
  420. };
  421. void lstPushItem(laListHandle* Handle, void* Item){
  422. laListItem* li = Item;
  423. li->pNext = li->pPrev = 0;
  424. if (!Handle->pLast) Handle->pLast = Item;
  425. li->pNext = Handle->pFirst;
  426. if (Handle->pFirst) ((laListItem*)Handle->pFirst)->pPrev = Item;
  427. Handle->pFirst = li;
  428. };
  429. void* lstPopItem(laListHandle* Handle){
  430. laListItem* popitem;
  431. laListItem* next;
  432. if (!Handle->pFirst) return 0;
  433. popitem = Handle->pFirst;
  434. next = ((laListItem*)Handle->pFirst)->pNext;
  435. if (!next){
  436. Handle->pFirst = 0;
  437. Handle->pLast = 0;
  438. }else{
  439. Handle->pFirst = next;
  440. if (next) next->pPrev = 0;
  441. };
  442. popitem->pNext=popitem->pPrev=0;
  443. return popitem;
  444. };
  445. int lstHaveItemInList(laListHandle *Handle){
  446. if (Handle->pFirst) return 1;
  447. return 0;
  448. };
  449. void lstAppendItem2(laListHandle *Handle, void *Item){
  450. laListItem2 *li = Item;
  451. li->pNext = li->pPrev = 0;
  452. if (!Handle->pFirst) Handle->pFirst = Item;
  453. if (Handle->pLast) ((laListItem2 *)Handle->pLast)->pNext = li;
  454. li->pPrev = Handle->pLast;
  455. li->pNext = 0;
  456. Handle->pLast = li;
  457. };
  458. void lstPushItem2(laListHandle *Handle, void *Item){
  459. laListItem2 *li = Item;
  460. li->pNext = li->pPrev = 0;
  461. if (!Handle->pLast) Handle->pLast = Item;
  462. li->pNext = Handle->pFirst;
  463. if (Handle->pFirst) ((laListItem2 *)Handle->pFirst)->pPrev = Item;
  464. Handle->pFirst = li;
  465. };
  466. void *lstPopItem2(laListHandle *Handle){
  467. void *popitem;
  468. laListItem2 *next;
  469. if (!Handle->pFirst) return 0;
  470. popitem = Handle->pFirst;
  471. next = ((laListItem2 *)Handle->pFirst)->pNext;
  472. if (!next){
  473. Handle->pFirst = 0;
  474. Handle->pLast = 0;
  475. }else{
  476. Handle->pFirst = next;
  477. if (next) next->pPrev = 0;
  478. };
  479. return popitem;
  480. };
  481. void lstAppendItem3(laListHandle *Handle, void *Item){
  482. laListItem3 *li = Item;
  483. li->pNext = li->pPrev = 0;
  484. if (!Handle->pFirst) Handle->pFirst = Item;
  485. if (Handle->pLast) ((laListItem3 *)Handle->pLast)->pNext = li;
  486. li->pPrev = Handle->pLast;
  487. li->pNext = 0;
  488. Handle->pLast = li;
  489. };
  490. void lstPushItem3(laListHandle *Handle, void *Item){
  491. laListItem3 *li = Item;
  492. li->pNext = li->pPrev = 0;
  493. if (!Handle->pLast) Handle->pLast = Item;
  494. li->pNext = Handle->pFirst;
  495. if (Handle->pFirst) ((laListItem3 *)Handle->pFirst)->pPrev = Item;
  496. Handle->pFirst = li;
  497. };
  498. void *lstPopItem3(laListHandle *Handle){
  499. void *popitem;
  500. laListItem3 *next;
  501. if (!Handle->pFirst) return 0;
  502. popitem = Handle->pFirst;
  503. next = ((laListItem3 *)Handle->pFirst)->pNext;
  504. if (!next){
  505. Handle->pFirst = 0;
  506. Handle->pLast = 0;
  507. }else{
  508. Handle->pFirst = next;
  509. if (next) next->pPrev = 0;
  510. };
  511. return popitem;
  512. };
  513. void *lstGetTop(laListHandle *Handle){
  514. return Handle->pFirst;
  515. };
  516. int lstRemoveItem(laListHandle* Handle, laListItem* li) {
  517. if (!li->pPrev && Handle->pFirst != li) return 0;
  518. if (!li->pPrev) Handle->pFirst = li->pNext;
  519. else
  520. ((laListItem*)li->pPrev)->pNext = li->pNext;
  521. if (!li->pNext) Handle->pLast = li->pPrev;
  522. else
  523. ((laListItem*)li->pNext)->pPrev = li->pPrev;
  524. li->pNext = li->pPrev = 0;
  525. }
  526. int lstRemoveItem2(laListHandle *Handle, laListItem2 *li){
  527. if (!li->pPrev) Handle->pFirst = li->pNext;
  528. else
  529. ((laListItem2 *)li->pPrev)->pNext = li->pNext;
  530. if (!li->pNext) Handle->pLast = li->pPrev;
  531. else
  532. ((laListItem2 *)li->pNext)->pPrev = li->pPrev;
  533. li->pNext = li->pPrev = 0;
  534. };
  535. int lstRemoveItem3(laListHandle *Handle, laListItem2 *li){
  536. if (!li->pPrev) Handle->pFirst = li->pNext;
  537. else
  538. ((laListItem3 *)li->pPrev)->pNext = li->pNext;
  539. if (!li->pNext) Handle->pLast = li->pPrev;
  540. else
  541. ((laListItem3 *)li->pNext)->pPrev = li->pPrev;
  542. li->pNext = li->pPrev = 0;
  543. };
  544. int lstRemoveSegment(laListHandle *Handle, laListItem *Begin, laListItem *End){
  545. if (!Begin->pPrev) Handle->pFirst = End->pNext;
  546. else
  547. ((laListItem *)Begin->pPrev)->pNext = End->pNext;
  548. if (!End->pNext) Handle->pLast = Begin->pPrev;
  549. else
  550. ((laListItem *)End->pNext)->pPrev = Begin->pPrev;
  551. End->pNext = Begin->pPrev = 0;
  552. };
  553. void lstInsertItemBefore(laListHandle *Handle, laListItem *toIns, laListItem *pivot){
  554. if (!pivot){
  555. lstPushItem(Handle, toIns);
  556. return;
  557. }
  558. if (pivot->pPrev){
  559. ((laListItem *)pivot->pPrev)->pNext = toIns;
  560. toIns->pPrev = pivot->pPrev;
  561. }else{
  562. Handle->pFirst = toIns;
  563. }
  564. toIns->pNext = pivot;
  565. pivot->pPrev = toIns;
  566. };
  567. void lstInsertItemAfter(laListHandle *Handle, laListItem *toIns, laListItem *pivot){
  568. if (!pivot){
  569. lstAppendItem(Handle, toIns);
  570. return;
  571. }
  572. if (pivot->pNext){
  573. ((laListItem *)pivot->pNext)->pPrev = toIns;
  574. toIns->pNext = pivot->pNext;
  575. }else{
  576. Handle->pLast = toIns;
  577. }
  578. toIns->pPrev = pivot;
  579. pivot->pNext = toIns;
  580. }
  581. void lstInsertSegmentBefore(laListHandle *Handle, laListItem *Begin, laListItem *End, laListItem *pivot){
  582. if (pivot->pPrev){
  583. ((laListItem *)pivot->pPrev)->pNext = Begin;
  584. Begin->pPrev = pivot->pPrev;
  585. }else{
  586. Handle->pFirst = Begin;
  587. }
  588. End->pNext = pivot;
  589. pivot->pPrev = End;
  590. };
  591. void lstInsertSegmentAfter(laListHandle *Handle, laListItem *Begin, laListItem *End, laListItem *pivot){
  592. if (pivot->pNext){
  593. ((laListItem *)pivot->pNext)->pPrev = End;
  594. End->pNext = pivot->pNext;
  595. }else{
  596. Handle->pLast = End;
  597. }
  598. Begin->pPrev = pivot;
  599. pivot->pNext = Begin;
  600. }
  601. void *lstAppendPointerOnly(laListHandle *h, void *p){
  602. laListItemPointer *lip;
  603. if (!h) return 0;
  604. lip = CreateNew(laListItemPointer);
  605. lip->p = p;
  606. lstAppendItem(h, lip);
  607. return lip;
  608. }
  609. void *lstAppendPointerSizedOnly(laListHandle *h, void *p, int size){
  610. laListItemPointer *lip;
  611. if (!h) return 0;
  612. lip = calloc(1, size);
  613. lip->p = p;
  614. lstAppendItem(h, lip);
  615. return lip;
  616. }
  617. void *lstPushPointerOnly(laListHandle *h, void *p){
  618. laListItemPointer *lip = 0;
  619. if (!h) return 0;
  620. lip = CreateNew(laListItemPointer);
  621. lip->p = p;
  622. lstPushItem(h, lip);
  623. return lip;
  624. }
  625. void *lstPushPointerSizedOnly(laListHandle *h, void *p, int size){
  626. laListItemPointer *lip = 0;
  627. if (!h) return 0;
  628. lip = calloc(1, size);
  629. lip->p = p;
  630. lstPushItem(h, lip);
  631. return lip;
  632. }
  633. void lstReverse(laListHandle* h){
  634. laListHandle l={0}; void* i; while(i=lstPopItem(h)){ lstPushItem(&l,i); }
  635. memcpy(h,&l,sizeof(laListHandle));
  636. }
  637. int lstHasPointer(laListHandle* h, void *p){
  638. laListItemPointer *i; for (i = h->pFirst; i; i = i->pNext){
  639. if (i->p == p){return 1;}
  640. } return 0;
  641. }
  642. void *lstAppendPointer(laListHandle *h, void *p){
  643. laListItemPointer *lip;
  644. if (!h) return 0;
  645. lip = memAcquireSimple(sizeof(laListItemPointer));
  646. lip->p = p;
  647. lstAppendItem(h, lip);
  648. return lip;
  649. }
  650. void *lstAppendPointerSized(laListHandle *h, void *p, int size){
  651. laListItemPointer *lip;
  652. if (!h) return 0;
  653. lip = memAcquireSimple(size);
  654. lip->p = p;
  655. lstAppendItem(h, lip);
  656. return lip;
  657. }
  658. void *lstPushPointer(laListHandle *h, void *p){
  659. laListItemPointer *lip = 0;
  660. if (!h) return 0;
  661. lip = memAcquireSimple(sizeof(laListItemPointer));
  662. lip->p = p;
  663. lstPushItem(h, lip);
  664. return lip;
  665. }
  666. void *lstPushPointerSized(laListHandle *h, void *p, int size){
  667. laListItemPointer *lip = 0;
  668. if (!h) return 0;
  669. lip = memAcquireSimple(size);
  670. lip->p = p;
  671. lstPushItem(h, lip);
  672. return lip;
  673. }
  674. void *lstAppendPointerStatic(laListHandle *h, laStaticMemoryPool *smp, void *p){
  675. laListItemPointer *lip;
  676. if (!h) return 0;
  677. lip = memStaticAcquire(smp, sizeof(laListItemPointer));
  678. lip->p = p;
  679. lstAppendItem(h, lip);
  680. return lip;
  681. }
  682. void *lstAppendPointerStaticSized(laListHandle *h, laStaticMemoryPool *smp, void *p, int size){
  683. laListItemPointer *lip;
  684. if (!h) return 0;
  685. lip = memStaticAcquire(smp, size);
  686. lip->p = p;
  687. lstAppendItem(h, lip);
  688. return lip;
  689. }
  690. void *lstPushPointerStatic(laListHandle *h, laStaticMemoryPool *smp, void *p){
  691. laListItemPointer *lip = 0;
  692. if (!h) return 0;
  693. lip = memStaticAcquire(smp, sizeof(laListItemPointer));
  694. lip->p = p;
  695. lstPushItem(h, lip);
  696. return lip;
  697. }
  698. void *lstPushPointerStaticSized(laListHandle *h, laStaticMemoryPool *smp, void *p, int size){
  699. laListItemPointer *lip = 0;
  700. if (!h) return 0;
  701. lip = memStaticAcquire(smp, size);
  702. lip->p = p;
  703. lstPushItem(h, lip);
  704. return lip;
  705. }
  706. void *lstPopPointerOnly(laListHandle *h){
  707. laListItemPointer *lip;
  708. void *rev = 0;
  709. if (!h) return 0;
  710. lip = lstPopItem(h);
  711. rev = lip ? lip->p : 0;
  712. FreeMem(lip);
  713. return rev;
  714. }
  715. void lstRemovePointerItemOnly(laListHandle *h, laListItemPointer *lip){
  716. lstRemoveItem(h, lip);
  717. FreeMem(lip);
  718. }
  719. void lstRemovePointerOnly(laListHandle *h, void *p){
  720. laListItemPointer *i;
  721. for (i = h->pFirst; i; i = i->pNext){
  722. if (i->p == p){
  723. lstRemovePointerItem(h, i);
  724. break;
  725. }
  726. }
  727. }
  728. void lstClearPointerOnly(laListHandle *h){
  729. laListItemPointer *i;
  730. while (h && h->pFirst){
  731. lstPopPointer(h);
  732. }
  733. }
  734. void lstGeneratePointerListOnly(laListHandle *from1, laListHandle *from2, laListHandle *to){
  735. laListItemPointer *lip = from2 ? from2->pLast : 0;
  736. while (lip){
  737. lstPushPointer(to, lip->p);
  738. lip = lip->pPrev;
  739. }
  740. lip = from1 ? from1->pLast : 0;
  741. while (lip){
  742. lstPushPointer(to, lip->p);
  743. lip = lip->pPrev;
  744. }
  745. }
  746. void *lstPopPointer(laListHandle *h){
  747. laListItemPointer *lip;
  748. void *rev = 0;
  749. if (!h) return 0;
  750. lip = lstPopItem(h);
  751. rev = lip ? lip->p : 0;
  752. memFree(lip);
  753. return rev;
  754. }
  755. void lstRemovePointerItem(laListHandle *h, laListItemPointer *lip){
  756. lstRemoveItem(h, lip);
  757. memFree(lip);
  758. }
  759. void lstRemovePointer(laListHandle *h, void *p){
  760. laListItemPointer *i;
  761. for (i = h->pFirst; i; i = i->pNext){
  762. if (i->p == p){ lstRemovePointerItem(h, i); break; }
  763. }
  764. }
  765. void lstRemovePointerLeave(laListHandle *h, void *p){
  766. laListItemPointer *i;
  767. for (i = h->pFirst; i; i = i->pNext){
  768. if (i->p == p){ lstRemoveItem(h, i); memLeave(i); break; }
  769. }
  770. }
  771. void lstClearPointer(laListHandle *h){
  772. laListItemPointer *i;
  773. while (h && h->pFirst){
  774. lstPopPointer(h);
  775. }
  776. }
  777. void lstGeneratePointerList(laListHandle *from1, laListHandle *from2, laListHandle *to){
  778. laListItemPointer *lip = from1 ? from1->pLast : 0;
  779. while (lip){
  780. lstPushPointer(to, lip->p);
  781. lip = lip->pPrev;
  782. }
  783. lip = from2 ? from2->pLast : 0;
  784. while (lip){
  785. lstPushPointer(to, lip->p);
  786. lip = lip->pPrev;
  787. }
  788. }
  789. void *lstAppendPointerStaticPool(laStaticMemoryPool *mph, laListHandle *h, void *p){
  790. laListItemPointer *lip;
  791. if (!h) return 0;
  792. lip = memStaticAcquire(mph, sizeof(laListItemPointer));
  793. lip->p = p;
  794. lstAppendItem(h, lip);
  795. return lip;
  796. }
  797. void *lstPopPointerLeave(laListHandle *h){
  798. laListItemPointer *lip;
  799. void *rev = 0;
  800. if (!h) return 0;
  801. lip = lstPopItem(h); memLeave(lip);
  802. rev = lip ? lip->p : 0;
  803. return rev;
  804. }
  805. void lstRemovePointerItemNoFree(laListHandle *h, laListItemPointer *lip){
  806. lstRemoveItem(h, lip);
  807. }
  808. void lstCopyHandle(laListHandle *target, laListHandle *src){
  809. target->pFirst = src->pFirst;
  810. target->pLast = src->pLast;
  811. };
  812. void lstClearHandle(laListHandle *h){
  813. h->pFirst = 0;
  814. h->pLast = 0;
  815. }
  816. void lstClearPrevNext(laListItem *li){
  817. li->pNext = 0;
  818. li->pPrev = 0;
  819. }
  820. void lstMoveUp(laListHandle *h, laListItem *li){
  821. void *pprev = li->pPrev ? ((laListItem *)li->pPrev)->pPrev : 0;
  822. if (!h || !li) return;
  823. if (li == h->pFirst) return;
  824. else{
  825. if (li == h->pLast) h->pLast = li->pPrev;
  826. ((laListItem *)li->pPrev)->pNext = li->pNext;
  827. ((laListItem *)li->pPrev)->pPrev = li;
  828. if (li->pNext) ((laListItem *)li->pNext)->pPrev = li->pPrev;
  829. li->pNext = li->pPrev;
  830. li->pPrev = pprev;
  831. if (pprev) ((laListItem *)pprev)->pNext = li;
  832. }
  833. if (!li->pPrev) h->pFirst = li;
  834. }
  835. void lstMoveDown(laListHandle *h, laListItem *li){
  836. void *ppnext = li->pNext ? ((laListItem *)li->pNext)->pNext : 0;
  837. if (!h || !li) return;
  838. if (li == h->pLast) return;
  839. else{
  840. if (li == h->pFirst) h->pFirst = li->pNext;
  841. ((laListItem *)li->pNext)->pPrev = li->pPrev;
  842. ((laListItem *)li->pNext)->pNext = li;
  843. if (li->pPrev) ((laListItem *)li->pPrev)->pNext = li->pNext;
  844. li->pPrev = li->pNext;
  845. li->pNext = ppnext;
  846. if (ppnext) ((laListItem *)ppnext)->pPrev = li;
  847. }
  848. if (!li->pNext) h->pLast = li;
  849. }
  850. void lstForAllItemsDo(laListDoFunc func, laListHandle *hList){
  851. laListItem *it = hList->pFirst;
  852. for (; it; it = it->pNext){
  853. func(it);
  854. }
  855. };
  856. void lstForAllItemsDoLNRR(laListNonRecursiveDoFunc func, laListHandle *hList){
  857. laListItem *it = hList->pFirst;
  858. for (; it; it = it->pNext){
  859. func(0, it, 0);
  860. }
  861. };
  862. void lstForAllItemsDo_DirectFree(laListDoFunc func, laListHandle *hList){
  863. laListItem *it;
  864. while (it = lstPopItem(hList)){
  865. if (func) func(it);
  866. FreeMem(it);
  867. }
  868. };
  869. void lstForAllItemsDo_arg_ptr(laListDoFuncArgp func, laListHandle *hList, void *arg){
  870. laListItem *it = hList->pFirst;
  871. for (; it; it = it->pNext){
  872. func(it, arg);
  873. };
  874. };
  875. void lstForAllItemsDo_NonRecursive_Root(laListHandle *FirstHandle, laListNonRecursiveDoFunc func, int bFreeItem, void *custom_data, laListCustomDataRemover remover){
  876. laListItem *li = 0, *NextLi;
  877. laListNonRecursiveRoot root = {0};
  878. laListNonRecursiveItem *nrItem = CreateNew(laListNonRecursiveItem);
  879. nrItem->bFreeList = bFreeItem;
  880. nrItem->func = func;
  881. nrItem->CustomData = custom_data;
  882. nrItem->remover = remover;
  883. lstCopyHandle(&nrItem->handle, FirstHandle);
  884. lstAppendItem(&root.NSItems, nrItem);
  885. while (lstHaveItemInList(&root.NSItems)){
  886. nrItem = lstPopItem(&root.NSItems);
  887. for (li = nrItem->handle.pFirst; li /*!=nrItem->handle.pLast*/; li = NextLi){
  888. if (nrItem->func) nrItem->func(&root, li, custom_data);
  889. NextLi = li->pNext;
  890. if (nrItem->bFreeList){
  891. laListItem *fli = li;
  892. FreeMem(fli);
  893. }
  894. if (li == nrItem->handle.pLast) break;
  895. }
  896. if (nrItem->remover) nrItem->remover(nrItem->CustomData);
  897. FreeMem(nrItem);
  898. }
  899. };
  900. void lstAddNonRecursiveListHandle(laListNonRecursiveRoot *root, laListHandle *newHandle, laListNonRecursiveDoFunc nrFunc, int bFreeList, void *custom_data, laListCustomDataRemover remover){
  901. laListNonRecursiveItem *nrItem = CreateNew(laListNonRecursiveItem);
  902. nrItem->bFreeList = bFreeList;
  903. nrItem->func = nrFunc;
  904. nrItem->CustomData = custom_data;
  905. nrItem->remover = remover;
  906. lstCopyHandle(&nrItem->handle, newHandle);
  907. lstAppendItem(&root->NSItems, nrItem);
  908. };
  909. void lstCopy_NonRecursive_Root(laListHandle *FromHandle, laListHandle *ToHandle, int SizeEachNode, laListNonRecursiveCopyFunc func, void *custom_data, laListCustomDataRemover remover){
  910. laListItem *li = 0, *tli = 0;
  911. laListNonRecursiveRoot root = {0};
  912. laListNonRecursiveItem *nrItem = CreateNew(laListNonRecursiveItem);
  913. laListItem *NextLi;
  914. nrItem->CopyFunc = func;
  915. lstCopyHandle(&nrItem->handle, FromHandle);
  916. nrItem->ToHandle = ToHandle; //Pointer
  917. lstClearHandle(ToHandle);
  918. nrItem->CustomData = custom_data;
  919. nrItem->remover = remover;
  920. nrItem->SizeEachNode = SizeEachNode;
  921. lstAppendItem(&root.NSItems, nrItem);
  922. while (lstHaveItemInList(&root.NSItems)){
  923. nrItem = lstPopItem(&root.NSItems);
  924. if (nrItem->CopyFunc){
  925. for (li = nrItem->handle.pFirst; li; li = li->pNext){
  926. tli = CreateNew_Size(nrItem->SizeEachNode);
  927. nrItem->CopyFunc(&root, li, tli, nrItem->CustomData);
  928. lstClearPrevNext(tli);
  929. lstAppendItem(nrItem->ToHandle, tli);
  930. }
  931. if (nrItem->remover) nrItem->remover(nrItem->CustomData);
  932. }else if (nrItem->func){
  933. for (li = nrItem->handle.pFirst; li /*!=nrItem->handle.pLast*/; li = NextLi){
  934. if (nrItem->func) nrItem->func(&root, li, custom_data);
  935. NextLi = li->pNext;
  936. if (nrItem->bFreeList){
  937. laListItem *fli = li;
  938. FreeMem(fli);
  939. }
  940. if (li == nrItem->handle.pLast) break;
  941. }
  942. if (nrItem->remover) nrItem->remover(nrItem->CustomData);
  943. }
  944. FreeMem(nrItem);
  945. }
  946. };
  947. void lstAddNonRecursiveListCopier(laListNonRecursiveRoot *root, laListHandle *oldHandle, laListHandle *newHandle, int sizeEach, laListNonRecursiveCopyFunc nrCpyFunc, void *custom_data, laListCustomDataRemover remover){
  948. laListNonRecursiveItem *nrItem = CreateNew(laListNonRecursiveItem);
  949. nrItem->CopyFunc = nrCpyFunc;
  950. lstCopyHandle(&nrItem->handle, oldHandle);
  951. nrItem->ToHandle = newHandle;
  952. nrItem->CustomData = custom_data;
  953. nrItem->remover = remover;
  954. nrItem->SizeEachNode = sizeEach;
  955. lstAppendItem(&root->NSItems, nrItem);
  956. };
  957. void *lstFindItem(void *CmpData, laCompareFunc func, laListHandle *hList){
  958. laListItem *it;
  959. if (!CmpData || !hList) return 0;
  960. it = hList->pFirst;
  961. for (; it; it = it->pNext){
  962. if (func(it, CmpData)) return it;
  963. };
  964. return 0;
  965. };
  966. void lstCombineLists(laListHandle *dest, laListHandle *src){
  967. if ((!dest) || (!src)) return;
  968. if ((!dest->pFirst) && (!dest->pLast)){
  969. dest->pFirst = src->pFirst;
  970. dest->pLast = src->pLast;
  971. }else{
  972. if (src->pLast){
  973. ((laListItem *)src->pFirst)->pPrev = dest->pLast;
  974. ((laListItem *)dest->pLast)->pNext = src->pFirst;
  975. dest->pLast = src->pLast;
  976. }
  977. }
  978. src->pFirst = 0;
  979. src->pLast = 0;
  980. }
  981. void lstDestroyList(laListHandle *hlst){
  982. laListItem *li, *nextli;
  983. for (li = hlst->pFirst; li; li = nextli){
  984. nextli = li->pNext;
  985. memFree(li);
  986. }
  987. }
  988. void lstDestroyListA(laListHandle *hlst){
  989. laListItem *li, *nextli;
  990. for (li = hlst->pFirst; li; li = nextli){
  991. nextli = li->pNext;
  992. FreeMem(li);
  993. }
  994. }
  995. void lstDestroyList_User(laListHandle *hlst, laListDoFunc func){
  996. laListItem *it = hlst->pFirst;
  997. for (; it; it = it->pNext){
  998. func(it);
  999. FreeMem(it);
  1000. }
  1001. };
  1002. void lstCopyList(laListHandle *hOldlst, laListHandle *hNewList, int SizeEachNode, laCopyListFunc func){
  1003. laListItem *li, *nextli, *newli;
  1004. for (li = hOldlst->pFirst; li; li = nextli){
  1005. newli = (laListItem *)CreateNew_Size(SizeEachNode);
  1006. func(li, newli);
  1007. lstAppendItem(hNewList, newli);
  1008. nextli = li->pNext;
  1009. }
  1010. }
  1011. void *lstReMatch(laListHandle *SearchHandle, laListHandle *CurrentHandle, void *ItemToFind){
  1012. laListItem *sl = 0, *rl = 0;
  1013. if (!SearchHandle || !CurrentHandle || !ItemToFind) return 0;
  1014. sl = SearchHandle->pFirst;
  1015. rl = CurrentHandle->pFirst;
  1016. while (sl && rl){
  1017. if (ItemToFind == sl){
  1018. return rl;
  1019. }else{
  1020. sl = sl->pNext;
  1021. rl = rl->pNext;
  1022. }
  1023. }
  1024. return 0;
  1025. }
  1026. //void* lstReMatchEx(laListHandle* SearchHandle, laListHandle* CurrentHandle, void* ItemToFind, MatcherFunc func){
  1027. // laListItem* sl = 0, *rl = 0;
  1028. //
  1029. // if (!SearchHandle || !CurrentHandle || !ItemToFind) return 0;
  1030. //
  1031. // sl = SearchHandle->pFirst; rl = CurrentHandle->pFirst;
  1032. //
  1033. // while (sl && rl){
  1034. // if (func(ItemToFind, sl)){
  1035. // return rl;
  1036. // }
  1037. // else{
  1038. // sl = sl->pNext;
  1039. // rl = rl->pNext;
  1040. // }
  1041. // }
  1042. // return 0;
  1043. //}
  1044. void lstAddElement(laListHandle *hlst, void *ext){
  1045. laElementListItem *eli = CreateNew(laElementListItem);
  1046. eli->Ext = ext;
  1047. lstAppendItem(hlst, eli);
  1048. }
  1049. void lstDestroyElementList(laListHandle *hlst){
  1050. laElementListItem *eli, *NextEli;
  1051. for (eli = hlst->pFirst; eli; eli = NextEli){
  1052. lstRemoveItem(hlst, eli);
  1053. NextEli = eli->Item.pNext;
  1054. FreeMem(eli);
  1055. }
  1056. }
  1057. uint16_t BKDRHash16bit(char* str){
  1058. unsigned int seed = 131, hash = 0;
  1059. while (*str) { hash = hash * seed + (*str++); }
  1060. return (hash & 0xFFFF);
  1061. }
  1062. void hsh65536Init(laHash65536** h){
  1063. if(!h) return; *h=calloc(1,sizeof(laHash65536));
  1064. }
  1065. void hshFree(laHash65536** h){
  1066. if(!h || !*h) return; free(*h); *h=0;
  1067. }
  1068. laListHandle* hsh65536DoHashLongPtr(laHash65536* hash, u64bit buckle) {
  1069. return &hash->Entries[(unsigned short)(buckle*13)];
  1070. }
  1071. laListHandle* hsh65536DoHashNUID(laHash65536* hash, char * NUID) {
  1072. u64bit Hash;
  1073. return &hash->Entries[BKDRHash16bit(NUID)];
  1074. }
  1075. unsigned char hsh256DoHashSTR(char *buckle){
  1076. int i, len = 0;
  1077. unsigned char rev = 0;
  1078. if (buckle) len = strlen(buckle);
  1079. for (i = 0; i < len; i++){
  1080. rev = rev * 31 + (unsigned char)buckle[i];
  1081. }
  1082. return (unsigned char)rev;
  1083. }
  1084. void hsh256InsertItemCSTR(laHash256 *hash, laListItem *li, char *buckle){
  1085. unsigned char a = hsh256DoHashSTR(buckle);
  1086. lstAppendItem(&hash->Entries[a], li);
  1087. };
  1088. void hsh256InsertItem(laHash256 *hash, laListItem *li, char buckle){
  1089. lstAppendItem(&hash->Entries[(unsigned char)buckle], li);
  1090. };
  1091. void hsh65536InsertItem(laHash65536 *hash, laListItem *li, long buckle){
  1092. lstAppendItem(&hash->Entries[(unsigned short)((buckle >> 10))], li);
  1093. //hsh256InsertItem(&hash->HashHandles[(char)((buckle >> 8) / 8)], li, (char)(buckle/8));
  1094. //printf("%d %d\n", (char)(buckle >> 5), (char)(buckle >> 6));
  1095. };
  1096. laListItem *hsh256FindItemSTR(laHash256 *hash, laCompareFunc func, char *buckle){
  1097. unsigned char hsh;
  1098. hsh = hsh256DoHashSTR(buckle);
  1099. //if(hash->Entries[hsh].pFirst == hash->Entries[hsh].pLast)
  1100. // return hash->Entries[hsh].pFirst;
  1101. laListItem* item=lstFindItem(buckle, func, &hash->Entries[hsh]);
  1102. return item;
  1103. }
  1104. //================================================================ [mem]
  1105. void* memGetHead(void* UserMem, int* HyperLevel){
  1106. laMemoryPoolPart **mpp = (laMemoryPoolPart**)(((char*)UserMem)-sizeof(void*));
  1107. if(!(*mpp)) return 0;
  1108. laMemoryPool* mp = (*mpp)->PoolRoot;
  1109. if(HyperLevel) *HyperLevel= mp->Hyperlevel;
  1110. if(mp->Hyperlevel==2) return ((char*)UserMem)-sizeof(laMemNodeHyper);
  1111. if(mp->Hyperlevel==1) return ((char*)UserMem)-sizeof(laMemNode);
  1112. if(mp->Hyperlevel==0) return ((char*)UserMem)-sizeof(laMemNode0);
  1113. return 0;
  1114. }
  1115. laListHandle* memGetUserList(void* UserMem){
  1116. int level; void* head=memGetHead(UserMem, &level);
  1117. if(level==2) return &((laMemNodeHyper*)head)->Users;
  1118. if(level==1) return &((laMemNode*)head)->Users;
  1119. return 0;
  1120. }
  1121. laMemoryPool *memInitPool(int NodeSize, int HyperLevel){
  1122. if (!NodeSize) return 0;
  1123. laMemoryPool *mph = calloc(1, sizeof(laMemoryPool));
  1124. mph->NodeSize = NodeSize;
  1125. mph->NextCount = 1;
  1126. mph->Hyperlevel = HyperLevel;
  1127. u8bit Buckle = NodeSize;
  1128. lstAppendItem(&MAIN.GlobalMemPool.Entries[Buckle], mph);
  1129. return mph;
  1130. }
  1131. laMemoryPoolPart *memNewPoolPart(laMemoryPool *mph){
  1132. if (!mph->NodeSize) return 0;
  1133. int MemNodeSize=(mph->Hyperlevel==0)?sizeof(laMemNode0):((mph->Hyperlevel==1)?sizeof(laMemNode):sizeof(laMemNodeHyper));
  1134. int PoolRefOffset=MemNodeSize-sizeof(void*);
  1135. int RealNodeSize = mph->NodeSize + MemNodeSize;
  1136. int NodeCount = mph->NextCount;
  1137. int TotalSize = sizeof(laMemoryPoolPart) + NodeCount * RealNodeSize;
  1138. laMemoryPoolPart *mp = calloc(1, TotalSize);
  1139. void *BeginMem = ((BYTE *)mp) + sizeof(laMemoryPoolPart);
  1140. mp->PoolRoot = mph;
  1141. mp->FreeMemoryNodes.pFirst = mp->FreeMemoryNodes.pLast = 0;
  1142. for (int i = 0; i < NodeCount; i++){
  1143. void* mpn = ((BYTE *)BeginMem) + RealNodeSize * i;
  1144. void** ref = ((BYTE *)mpn) + PoolRefOffset;
  1145. (*ref)=mp;
  1146. lstAppendItem(&mp->FreeMemoryNodes, mpn);
  1147. }
  1148. lstPushItem(&mph->Pools, mp);
  1149. return mp;
  1150. }
  1151. void *memAcquireH(laMemoryPool *Handle){
  1152. laMemoryPoolPart *mp = Handle->Pools.pFirst;
  1153. laMemNode *mpn;
  1154. if (!mp || !mp->FreeMemoryNodes.pFirst){
  1155. mp = memNewPoolPart(Handle);
  1156. }
  1157. if (!mp) return 0;
  1158. mpn = mp->FreeMemoryNodes.pFirst;
  1159. lstRemoveItem(&mp->FreeMemoryNodes, mpn);
  1160. mp->UsedCount++;
  1161. //lstAppendItem(&mp->MemoryNodes, mpn);
  1162. return mpn;
  1163. }
  1164. void *memAcquire_(int Size, int Hyper){
  1165. laMemoryPool *mp;
  1166. u8bit Buckle = Size;
  1167. laSpinLock(&MAIN.MemLock);
  1168. mp = MAIN.GlobalMemPool.Entries[Buckle].pFirst;
  1169. while (mp && (mp->NodeSize != Size || mp->Hyperlevel!=Hyper))
  1170. mp = mp->Item.pNext;
  1171. if (!mp) mp = memInitPool(Size, Hyper);
  1172. void* ret=memAcquireH(mp);
  1173. laSpinUnlock(&MAIN.MemLock);
  1174. return ret;
  1175. }
  1176. void *memAcquireSimple(int Size){
  1177. void *mpn = memAcquire_(Size, 0);
  1178. return ((char*)mpn)+sizeof(laMemNode0);
  1179. }
  1180. void *memAcquire(int Size){
  1181. laMemNode *mpn = memAcquire_(Size, 1);
  1182. void* mem = ((char*)mpn)+sizeof(laMemNode);
  1183. return mem;
  1184. }
  1185. void *memAcquireHyperNoAppend(int Size){
  1186. laMemNodeHyper *mpn = memAcquire_(Size, 2);
  1187. void* mem = ((char*)mpn)+sizeof(laMemNodeHyper);
  1188. memMakeHyperData(mpn);
  1189. return mem;
  1190. }
  1191. void *memAcquireHyper(int Size){
  1192. laMemNodeHyper *mpn = memAcquire_(Size, 2);
  1193. void* mem = ((char*)mpn)+sizeof(laMemNodeHyper);
  1194. memMakeHyperData(mpn);
  1195. laListHandle* l=hsh65536DoHashNUID(&MAIN.DBInst2,mpn->NUID.String);
  1196. lstAppendItem(l,mpn);
  1197. return mem;
  1198. }
  1199. void memFree(void *Data){
  1200. if (!Data) return;
  1201. int level; void* head = memGetHead(Data, &level);
  1202. laMemoryPoolPart *mp;
  1203. if(level==2) { mp = ((laMemNodeHyper*)head)->InPool; laDataBlockNoLongerExists(Data,&((laMemNodeHyper*)head)->Users);
  1204. laListHandle* l=hsh65536DoHashNUID(&MAIN.DBInst2,((laMemNodeHyper*)head)->NUID.String); lstRemoveItem(l,head);}
  1205. if(level==1) { mp = ((laMemNode*)head)->InPool; laDataBlockNoLongerExists(Data,&((laMemNode*)head)->Users); }
  1206. if(level==0) { mp = ((laMemNode0*)head)->InPool; }
  1207. laMemoryPool *mph = mp->PoolRoot;
  1208. laSpinLock(&MAIN.MemLock);
  1209. //lstRemoveItem(&mp->MemoryNodes, head);
  1210. mp->UsedCount--;
  1211. void* head_except_item = ((char*)head)+sizeof(laListItem);
  1212. //memset(head_except_item, 0, ((level==2)?sizeof(laMemNodeHyper):((level==1)?sizeof(laMemNode):sizeof(laMemNode0)))+mph->NodeSize-sizeof(laListItem));
  1213. lstAppendItem(&mp->FreeMemoryNodes, head);
  1214. memset(Data, 0, mph->NodeSize);
  1215. MAIN.ByteCount -= mph->NodeSize;
  1216. if (!mp->UsedCount){
  1217. lstRemoveItem(&mph->Pools, mp);
  1218. FreeMem(mp);
  1219. }
  1220. laSpinUnlock(&MAIN.MemLock);
  1221. //if (!mph->Pools.pFirst) {
  1222. // mph->CountPerPool = 0;
  1223. // mph->NodeSize = 0;
  1224. //}
  1225. }
  1226. void memDestroyPool(laMemoryPool *mph){
  1227. laMemoryPool *mp;
  1228. while ((mp = lstPopItem(&mph->Pools))){
  1229. FreeMem(mp);
  1230. }
  1231. FreeMem(mph);
  1232. }
  1233. // Leave memory in an temporary place and if when push difference these are still not acquired, free them.
  1234. void memLeave(void *Data){
  1235. laListHandle* l=hsh65536DoHashLongPtr(MAIN.DBInstMemLeft,Data); lstAppendPointer(l,Data);
  1236. }
  1237. void memTake(void *Data){
  1238. laListHandle* l=hsh65536DoHashLongPtr(MAIN.DBInstMemLeft,Data); lstRemovePointer(l,Data);
  1239. }
  1240. void memFreeRemainingLeftNodes(){
  1241. laListHandle* l; void* m;
  1242. for(int i=0;i<65536;i++){
  1243. l=&MAIN.DBInstMemLeft->Entries[i]; while(m=lstPopPointer(l)){ memFree(m);
  1244. #ifdef DEBUG
  1245. printf("left freed %x\n",m);
  1246. #endif
  1247. }
  1248. }
  1249. }
  1250. void memNoLonger(){
  1251. for(int i=0;i<256;i++){
  1252. laMemoryPool* mp; while(mp=lstPopItem(&MAIN.GlobalMemPool.Entries[i])){ memDestroyPool(mp); }
  1253. }
  1254. }
  1255. laStaticMemoryPoolNode *memNewStaticPool(laStaticMemoryPool *smp){
  1256. laStaticMemoryPoolNode *smpn = calloc(1, LA_MEMORY_POOL_128MB);
  1257. smpn->UsedByte = sizeof(laStaticMemoryPoolNode);
  1258. lstPushItem(&smp->Pools, smpn);
  1259. return smpn;
  1260. }
  1261. void *memStaticAcquire(laStaticMemoryPool *smp, int size){
  1262. laStaticMemoryPoolNode *smpn = smp->Pools.pFirst;
  1263. void *ret;
  1264. if (!smpn || (smpn->UsedByte + size) > LA_MEMORY_POOL_128MB) smpn = memNewStaticPool(smp);
  1265. ret = ((BYTE *)smpn) + smpn->UsedByte;
  1266. smpn->UsedByte += size;
  1267. return ret;
  1268. }
  1269. void *memStaticAcquireThread(laStaticMemoryPool *smp, int size){
  1270. laStaticMemoryPoolNode *smpn = smp->Pools.pFirst;
  1271. void *ret;
  1272. //laSpinLock(&smp->csMem);
  1273. if (!smpn || (smpn->UsedByte + size) > LA_MEMORY_POOL_128MB) smpn = memNewStaticPool(smp);
  1274. ret = ((BYTE *)smpn) + smpn->UsedByte;
  1275. smpn->UsedByte += size;
  1276. //laSpinUnlock(&smp->csMem);
  1277. return ret;
  1278. }
  1279. void *memStaticDestroy(laStaticMemoryPool *smp){
  1280. laStaticMemoryPoolNode *smpn;
  1281. void *ret;
  1282. while (smpn = lstPopItem(&smp->Pools)){
  1283. FreeMem(smpn);
  1284. }
  1285. smp->EachSize = 0;
  1286. return ret;
  1287. }
  1288. void la_ReferencedBlockDeleted(void* This, laItemUserLinker* iul){
  1289. void** user=iul->Pointer.p; if(*user==This){ (*user)=0; } laStopUsingDataBlock(iul->Additional, 0, This); // <<< should always remove.
  1290. }
  1291. void la_ReferrerDeleted(void* This, laItemUserLinker* iul){
  1292. void* instance=iul->Pointer.p; if(instance!=This){ laStopUsingDataBlock(instance, 0, This); }
  1293. }
  1294. void memAssignRef(void* This, void** ptr, void* instance){
  1295. laItemUserLinker* iul;
  1296. if((!This)||(!ptr)) return;
  1297. if(instance){
  1298. laItemUserLinker*iul=laUseDataBlock(instance, 0, 0, ptr, la_ReferencedBlockDeleted, 0); if(iul){iul->Additional=This;}
  1299. laUseDataBlock(This, 0, 0, instance, la_ReferrerDeleted, 0);
  1300. }else{
  1301. laStopUsingDataBlock((*ptr), 0, This);
  1302. laStopUsingDataBlock(This, 0, (*ptr));
  1303. }
  1304. (*ptr)=instance;
  1305. }
  1306. void memAssignRefSafe(laSubProp* sp, void* This, void** ptr, void* instance){
  1307. laPropContainer* pc=sp?la_EnsureSubTarget(sp,instance):0;
  1308. if(pc&&!pc->OtherAlloc) memAssignRef(This,ptr,instance);
  1309. else (*ptr)=instance;
  1310. }
  1311. //=======================================================================[str]
  1312. char *strGetNextString(char **pivot, char *NextMark){
  1313. int lenth = 0;
  1314. char *countP = *pivot;
  1315. char *result = 0;
  1316. int FloatArg = 0;
  1317. int i,advance;
  1318. if (**pivot == U'\0') return 0;
  1319. if (*NextMark == U'~') FloatArg = 1;
  1320. // container@identifier=window container#window contianer%
  1321. int UC=1;
  1322. while (!lenth){
  1323. for (countP; *countP != U'.' && *(*pivot) != U'\0' && UC && *countP && *countP != U'@' && *countP != U'=' && *countP != U'#' && *countP != U'$';){
  1324. if((*countP)=='\\'){
  1325. countP++; lenth++; }
  1326. UC = laToUnicode(countP, &advance);
  1327. lenth+=advance;
  1328. countP+=advance;
  1329. }
  1330. if (lenth || (*countP) == 0) break;
  1331. (*pivot)++; countP++;
  1332. }
  1333. *NextMark = (*pivot)[lenth];
  1334. if (!(*NextMark)) *NextMark = U'.';
  1335. if (lenth){
  1336. result = CreateNewBuffer(char, lenth + 1);
  1337. int pi=0; for (i = 0; i < lenth; i++){
  1338. if((*pivot)[i]=='\\'){ continue; }
  1339. result[pi] = (*pivot)[i]; pi++;
  1340. }
  1341. result[pi] = U'\0';
  1342. if ((*pivot)[lenth] == U'\0') *pivot = &((*pivot)[lenth]);
  1343. else
  1344. (*pivot) += lenth + 1;
  1345. return result;
  1346. }else{
  1347. return 0;
  1348. }
  1349. };
  1350. int strGetStringTerminateBy(char *content, char terminator, char *Out){
  1351. int Ofst = 0;
  1352. int Skip = 0;
  1353. int i = 0, advance;
  1354. if ((!content) || (*content == U'\0')) return 0;
  1355. int UC;
  1356. for (Ofst; content[Ofst] != terminator && content[Ofst] != U'\0'; ){
  1357. UC = laToUnicode(&content[Ofst], &advance);
  1358. for(int a=0;a<advance;a++){
  1359. Out[i] = content[Ofst];
  1360. Ofst++; i++;
  1361. }
  1362. }
  1363. Out[i] = 0;
  1364. return i;
  1365. };
  1366. char *strGetNewStringTerminateBy_PivotOver(char *content, char terminator, char **NewPivot, int IgnoreSpace){
  1367. int Ofst = 0;
  1368. int Skip = 0;
  1369. int i = 0;
  1370. char *NewString;
  1371. if (!content || *content == U'\0') return 0;
  1372. if (IgnoreSpace) for (i; content[i] == U' '; i++) ;
  1373. int UC,advance;
  1374. for (Ofst; content[Ofst] != terminator && content[Ofst] != U'\0';){
  1375. UC = laToUnicode(&content[Ofst], &advance);
  1376. Ofst+=advance;
  1377. }
  1378. NewString = CreateNewBuffer(char, Ofst + 1 - i);
  1379. memcpy(NewString, &content[i], sizeof(char) * (Ofst - i));
  1380. NewString[Ofst - i] = U'\0';
  1381. *NewPivot = content[Ofst]?&content[Ofst + 1]:&content[Ofst];
  1382. return NewString;
  1383. };
  1384. int strHeadOfStringMatch(char *Str, char *SubStr){
  1385. int len = strlen(SubStr);
  1386. int i = 0;
  1387. for (i; i < len; i++){
  1388. if (Str[i] != SubStr[i]) return 0;
  1389. }
  1390. return 1;
  1391. }
  1392. int strSkipSegmet(char **pivot, char *content){
  1393. if (!pivot || !(*pivot) || !(*(*pivot)) || !content) return 0;
  1394. if (strHeadOfStringMatch(*pivot, content)){
  1395. (*pivot) += strlen(content);
  1396. return 1;
  1397. }
  1398. return 0;
  1399. }
  1400. char *strGetLastSegment(char *Content, char Seperator){
  1401. char *p = Content;
  1402. char *pn = Content;
  1403. while (1){
  1404. while (*pn != Seperator){
  1405. if (!(*pn)) return p;
  1406. pn++;
  1407. }
  1408. pn++;
  1409. p = pn;
  1410. }
  1411. }
  1412. void strDiscardLastSegmentSeperateBy(char *Content, char Seperator){
  1413. char *p = Content;
  1414. char *pn = Content;
  1415. while (1){
  1416. while (*pn != Seperator){
  1417. if (!(*pn)){
  1418. *p = 0;
  1419. return;
  1420. }
  1421. pn++;
  1422. }
  1423. p = pn;
  1424. pn++;
  1425. }
  1426. }
  1427. void strDiscardSameBeginningSeperatedBy(char *s1, char *s2, char **Result1, char **Result2, char Seperator){
  1428. int i = 0;
  1429. int p = 0;
  1430. while (s1[i] == s2[i]){
  1431. i++;
  1432. if (s1[i] == Seperator) p = i;
  1433. if (!s1[i]){
  1434. p = i;
  1435. break;
  1436. }
  1437. if (!s2[i]){
  1438. p = i;
  1439. break;
  1440. }
  1441. }
  1442. *Result1 = &s1[p];
  1443. *Result2 = &s2[p];
  1444. }
  1445. int strCountSegmentSeperateBy(char *Content, char Seperator){
  1446. char *p = Content;
  1447. char *pn = Content;
  1448. int c = Content[0] ? (Content[0] == Seperator ? 0 : 1) : 0;
  1449. while (1){
  1450. while (*pn != Seperator){
  1451. if (!(*pn)){
  1452. if ((*p) == Seperator) c--;
  1453. return c;
  1454. }
  1455. p = pn;
  1456. pn++;
  1457. }
  1458. c++;
  1459. pn++;
  1460. }
  1461. return c;
  1462. }
  1463. void strMakeDifferentName(char *Target){
  1464. char *p = strGetLastSegment(Target, '.');
  1465. int Temp;
  1466. if (!sscanf(p, "%d", &Temp)){
  1467. int l = strlen(p);
  1468. if (p[l - 1] != U'.') strcat(p, ".");
  1469. strPrintIntAfter(Target, 0, 001);
  1470. }else{
  1471. sprintf(p, "%d", Temp + 1);
  1472. };
  1473. }
  1474. void strReplaceCharacter(char *Str, char Find, char Replace){
  1475. char *p = Str;
  1476. if (!p) return;
  1477. while (*p){
  1478. if (*p == Find) *p = Replace;
  1479. p++;
  1480. }
  1481. }
  1482. void strToUpper(char *Str){
  1483. char *p = Str; if (!p) return;
  1484. while (*p){
  1485. if (*p >= 'a' && *p <= 'z') *p += 'A' - 'a';
  1486. p++;
  1487. }
  1488. }
  1489. void strToLower(char *Str){
  1490. char *p = Str; if (!p) return;
  1491. while (*p){
  1492. if (*p >= 'A' && *p <= 'Z') *p -= 'A' - 'a';
  1493. p++;
  1494. }
  1495. }
  1496. int tolowerGuarded(int a) {
  1497. if (a >= 'A' && a <= 'Z') a -= 'A' - 'a';
  1498. return a;
  1499. }
  1500. laStringSplitor *strSplitPath(char *path,char terminator){
  1501. laStringPart *sp;
  1502. laStringSplitor *ss;
  1503. char *pivot = path;
  1504. char *temp_result;
  1505. char Type = terminator?terminator:'.';
  1506. char NextType = '.';
  1507. if (!path || !path[0]) return 0;
  1508. ss = memAcquireSimple(sizeof(laStringSplitor));
  1509. while (temp_result = strGetNextString(&pivot, &NextType)){
  1510. if (*temp_result != U'\0'){
  1511. sp = memAcquireSimple(sizeof(laStringPart));
  1512. sp->Content = temp_result;
  1513. lstAppendItem(&ss->parts, sp);
  1514. ss->NumberParts += 1;
  1515. if (NextType == U'$') sp->Type = U'$';
  1516. else
  1517. sp->Type = Type;
  1518. if (sp->Type == U'='){
  1519. if (sp->Content[0] >= U'0' && sp->Content[0] <= 9){
  1520. sscanf(sp->Content, "%d", &sp->IntValue);
  1521. }
  1522. }
  1523. if (NextType == U'$') NextType = U'.';
  1524. Type = NextType;
  1525. }
  1526. }
  1527. if (ss->NumberParts == 0){
  1528. strDestroyStringSplitor(&ss);
  1529. return 0;
  1530. }
  1531. return ss;
  1532. };
  1533. void DF_ClearStingParts(laStringPart *sp){
  1534. FreeMem(sp->Content);
  1535. };
  1536. int strDestroyStringSplitor(laStringSplitor **ss){
  1537. if (!(*ss)) return 0;
  1538. lstForAllItemsDo(DF_ClearStingParts, &(*ss)->parts);
  1539. lstDestroyList(&(*ss)->parts);
  1540. memFree(*ss);
  1541. *ss = 0;
  1542. return 1;
  1543. }
  1544. char * strSub(char *input, char *substring, char *replace){
  1545. int number_of_matches = 0;
  1546. size_t substring_size = strlen(substring), replace_size = strlen(replace), buffer_size;
  1547. char *buffer, *bp, *ip;
  1548. if (substring_size){
  1549. ip = strstr(input, substring);
  1550. while (ip != NULL){
  1551. number_of_matches++;
  1552. ip = strstr(ip+substring_size, substring);
  1553. }
  1554. }
  1555. else number_of_matches = strlen (input) + 1;
  1556. buffer_size = strlen(input) + number_of_matches*(replace_size - substring_size) + 1;
  1557. if ((buffer = ((char *) malloc(buffer_size))) == NULL){
  1558. return NULL;
  1559. }
  1560. bp = buffer;
  1561. ip = strstr(input, substring);
  1562. while ((ip != NULL) && (*input != '\0')){
  1563. if (ip == input){
  1564. memcpy (bp, replace, replace_size+1);
  1565. bp += replace_size;
  1566. if (substring_size)input += substring_size;
  1567. else*(bp++) = *(input++);
  1568. ip = strstr(input, substring);
  1569. }
  1570. else while (input != ip) *(bp++) = *(input++);
  1571. }
  1572. if (substring_size)strcpy (bp, input);
  1573. else memcpy (bp, replace, replace_size+1);
  1574. return buffer;
  1575. }
  1576. char buff[128]={0};
  1577. int strMakeInstructions(laStringSplitor **result, char *content){
  1578. laStringPart *sp;
  1579. laStringSplitor *ss = *result;
  1580. char *pivot = content;
  1581. unsigned char *temp_result;
  1582. if (!content || !content[0]) return 0;
  1583. if (!ss) ss = *result = memAcquireSimple(sizeof(laStringSplitor));
  1584. while (temp_result = strGetNewStringTerminateBy_PivotOver(pivot, '=', &pivot, 0)){
  1585. if (*temp_result != U'\0'){
  1586. sp = memAcquireSimple(sizeof(laStringPart));
  1587. sp->Content = temp_result;
  1588. lstAppendItem(&ss->parts, sp);
  1589. ss->NumberParts += 1;
  1590. }
  1591. temp_result = strGetNewStringTerminateBy_PivotOver(pivot, ';', &pivot, 0);
  1592. if (!temp_result) break;
  1593. if (*temp_result != U'\0'){
  1594. sp = memAcquireSimple(sizeof(laStringPart));
  1595. sp->Content = temp_result;
  1596. lstAppendItem(&ss->parts, sp);
  1597. ss->NumberParts += 1;
  1598. if (temp_result[0] >= U'0' && temp_result[0] <= U'9' || temp_result[0]>=128){
  1599. sscanf(temp_result, "%d", &sp->IntValue);
  1600. sscanf(temp_result, "%lf", &sp->FloatValue);
  1601. }
  1602. }
  1603. }
  1604. if (ss->NumberParts == 0){
  1605. strDestroyStringSplitor(&ss);
  1606. return 0;
  1607. }
  1608. return 1;
  1609. }
  1610. laStringPart *strGetArgument(laStringSplitor *ss, char *content){
  1611. laStringPart *sp;
  1612. if (!ss) return 0;
  1613. for (sp = ss->parts.pFirst; sp; sp = sp->Item.pNext ? ((laListItem *)sp->Item.pNext)->pNext : 0){
  1614. if (strSame(content, sp->Content)) return sp->Item.pNext;
  1615. }
  1616. return 0;
  1617. }
  1618. char *strGetArgumentString(laStringSplitor *ss, char *content){
  1619. laStringPart *sp;
  1620. if (!ss) return 0;
  1621. for (sp = ss->parts.pFirst; sp; sp = sp->Item.pNext ? ((laListItem *)sp->Item.pNext)->pNext : 0){
  1622. if (strSame(content, sp->Content)) return sp->Item.pNext ? ((laStringPart *)sp->Item.pNext)->Content : 0;
  1623. }
  1624. return 0;
  1625. }
  1626. int strArgumentMatch(laStringSplitor *ss, char *id, char *value){
  1627. laStringPart *sp;
  1628. if (!ss) return 0;
  1629. for (sp = ss->parts.pFirst; sp; sp = sp->Item.pNext ? ((laListItem *)sp->Item.pNext)->pNext : 0){
  1630. if (strSame(id, sp->Content)) return (strSame(((laStringPart *)sp->Item.pNext)->Content, value));
  1631. }
  1632. return 0;
  1633. }
  1634. int strGetIntSimple(char *content){
  1635. int a;
  1636. sscanf(content, "%d", &a);
  1637. return a;
  1638. }
  1639. real strGetFloatSimple(char *content){
  1640. real a;
  1641. sscanf(content, "%lf", &a);
  1642. return a;
  1643. }
  1644. void strConvInt_CString(int src, char *dest, int lenth){
  1645. sprintf(dest, "%d", src);
  1646. };
  1647. void strConvFloat_CString(real src, char *dest, int lenth){
  1648. sprintf(dest, "%lf", src);
  1649. };
  1650. void strCopyFull(char *dest, char *src){
  1651. if (src && dest) strcpy(dest, src);
  1652. }
  1653. void strCopySized(char *dest, int LenthLim, char *src){
  1654. if (src && dest) strcpy(dest, src);
  1655. }
  1656. void strPrintFloatAfter(char *dest, int LenthLim, int bits, real data){
  1657. char temp[64]={0};
  1658. sprintf(temp, "%.*lf", bits, data);
  1659. strcat(dest, temp);
  1660. }
  1661. void strPrintIntAfter(char *dest, int LenthLim, int data){
  1662. char temp[64]={0};
  1663. sprintf(&temp[0], "%d", data);
  1664. strcat(dest, temp);
  1665. }
  1666. void strEscapePath(char* OutCanBeSame, char* path){
  1667. char t[256]={0}; int ti=0;
  1668. for(int i=0;path[i];i++,ti++){
  1669. if(path[i]=='.'){ t[ti]='\\'; ti++; }
  1670. t[ti]=path[i];
  1671. }
  1672. strcpy(OutCanBeSame,t);
  1673. }
  1674. int strSame(char *src, char *dest){
  1675. return (src && dest && !strcmp(src, dest));
  1676. }
  1677. void strSafeDestroy(laSafeString **ss){
  1678. if (!*ss) return; lstRemoveItem(&SSC.SafeStrings, *ss);
  1679. if((*ss)->Ptr) memFree((*ss)->Ptr);
  1680. memFree(*ss); *ss=0;
  1681. }
  1682. void strSafeSet(laSafeString **ss, char *Content){
  1683. int len;
  1684. if (!Content||!Content[0]){ strSafeDestroy(ss); return; }
  1685. len = strlen(Content);
  1686. if (len < 1) return;
  1687. if (*ss){
  1688. char* mem=memAcquireSimple(sizeof(char)*(len+1));
  1689. strcpy(mem, Content);
  1690. memFree((*ss)->Ptr);
  1691. (*ss)->Ptr=mem;
  1692. return;
  1693. }
  1694. (*ss) = memAcquireSimple(sizeof(laSafeString));
  1695. (*ss)->Ptr = memAcquireSimple(sizeof(char)*(len+1));
  1696. strcpy((*ss)->Ptr, Content);
  1697. lstAppendItem(&SSC.SafeStrings, *ss);
  1698. }
  1699. void strSafeAppend(laSafeString **ss, char *Content){
  1700. if(!ss || !(*ss) || !Content){ strSafeSet(ss, Content); return; }
  1701. int OrigLen=strlen((*ss)->Ptr), ContentLen=strlen(Content);
  1702. char* mem=memAcquireSimple(sizeof(char)*(OrigLen+ContentLen+1));
  1703. memcpy(mem, (*ss)->Ptr, sizeof(char)*OrigLen);
  1704. memcpy(mem+sizeof(char)*OrigLen, Content, sizeof(char)*ContentLen);
  1705. mem[OrigLen+ContentLen]=0;
  1706. memFree((*ss)->Ptr);
  1707. (*ss)->Ptr=mem;
  1708. }
  1709. void strSafePrint(laSafeString **ss, char *Format, ...){
  1710. char content[1024]; va_list va; va_start(va, Format); vsprintf(content, Format, va); va_end(va);
  1711. strSafeAppend(ss,content);
  1712. }
  1713. void strSafePrintV(laSafeString **ss, char *Format, va_list args){
  1714. char content[1024]; va_list va; vsprintf(content, Format, args);
  1715. strSafeAppend(ss,content);
  1716. }
  1717. void strSafeDump(){
  1718. laSafeString*ss;
  1719. while(ss=lstPopItem(&SSC.SafeStrings)){
  1720. //if(ss->Ptr) printf("[String not freed] \"%s\"\n", ss->Ptr);
  1721. }
  1722. }
  1723. void strBeginEdit(laStringEdit **se, char *FullStr){
  1724. char *p = FullStr;
  1725. char buf[1024];
  1726. laStringEdit *nse = CreateNew(laStringEdit);
  1727. if(*se){ memcpy(nse,*se,sizeof(laStringEdit)); nse->Lines.pFirst=nse->Lines.pLast=0; nse->TotalLines=0; }
  1728. strEndEdit(se, 1);
  1729. nse->_BeginLine = -1; nse->_BeginBefore = -1;
  1730. if (FullStr && FullStr[0]){
  1731. while ((*p)){
  1732. laStringLine *sl = memAcquireSimple(sizeof(laStringLine));
  1733. p += strGetStringTerminateBy(p, '\n', buf);
  1734. strToUnicode(sl->Buf, buf);
  1735. lstAppendItem(&nse->Lines, sl); nse->TotalLines++;
  1736. if(*p){ p+=1; }
  1737. }
  1738. }
  1739. if (!nse->Lines.pFirst){
  1740. laStringLine *sl = memAcquireSimple(sizeof(laStringLine));
  1741. lstAppendItem(&nse->Lines, sl); nse->TotalLines=1;
  1742. }
  1743. laStringLine *sl = strGetCursorLine(nse, 0); int len=strlen(sl->Buf);
  1744. if(len<nse->CursorBefore){ nse->CursorBefore=len; }
  1745. *se=nse;
  1746. }
  1747. char* strGetEditString(laStringEdit *se, int SelectionOnly){
  1748. if(!se) return 0; char* result=0; int next=0, max=0, len=0;
  1749. arrEnsureLength(&result, 0, &max, sizeof(char)); int NextChar=0;
  1750. int Line=0, starti=0, endat=INT_MAX;
  1751. for(laStringLine* sl=se->Lines.pFirst;sl;sl=sl->Item.pNext,Line++){
  1752. starti=0;
  1753. if(SelectionOnly && Line<se->BeginLine){ continue; }
  1754. if(SelectionOnly && Line==se->BeginLine){ starti=se->BeginBefore; }
  1755. int tlen=strlenU(&sl->Buf[starti]); int Extra=sl->Item.pNext?2:1;
  1756. arrEnsureLength(&result, (len+tlen)*4+Extra, &max, sizeof(char));
  1757. if(SelectionOnly && Line==se->EndLine){ endat=NextChar+se->EndBefore-starti; }
  1758. NextChar+=strToUTF8Lim(&result[NextChar], &sl->Buf[starti], endat); len+=tlen;
  1759. if(Extra==2){ result[NextChar]='\n'; NextChar+=1; }
  1760. if(SelectionOnly && Line==se->EndLine){ break; }
  1761. }
  1762. return result;
  1763. }
  1764. char* strEndEdit(laStringEdit **se, int FreeString){
  1765. char *p=0; laStringLine *sl, *NextSl;
  1766. if (!se || !(*se)) return 0;
  1767. p=strGetEditString(*se, 0);
  1768. while (sl=lstPopItem(&(*se)->Lines)){ memFree(sl); }
  1769. FreeMem(*se); *se=0;
  1770. if(FreeString && p){ free(p); p=0; }
  1771. return p;
  1772. }
  1773. void strSetEditViewRange(laStringEdit* se, int Lines, int Cols){
  1774. se->ViewHeight = Lines; se->ViewWidth = Cols;
  1775. }
  1776. void strEnsureCursorVisible(laStringEdit* se){
  1777. if(!se->ViewHeight || !se->ViewWidth || se->CursorLine<0 || se->CursorBefore<0 ){return;}
  1778. if(se->CursorLine>se->ViewHeight+se->ViewStartLine-1){ se->ViewStartLine=se->CursorLine-se->ViewHeight+1; }
  1779. if(se->CursorLine<se->ViewStartLine){ se->ViewStartLine=se->CursorLine; }
  1780. if(se->CursorBefore>se->ViewStartCol+se->ViewWidth-1){ se->ViewStartCol=se->CursorBefore-se->ViewWidth+1; }
  1781. if(se->CursorBefore<se->ViewStartCol){ se->ViewStartCol=se->CursorBefore; }
  1782. }
  1783. void strRemoveLine(laStringEdit *se, laStringLine *sl){
  1784. lstRemoveItem(&se->Lines, sl);
  1785. memFree(sl); se->TotalLines--;
  1786. }
  1787. void strRemoveLineI(laStringEdit *se, int LineIndex){
  1788. int i = 0;
  1789. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1790. while (sl){
  1791. NextSl = sl->Item.pNext;
  1792. if (i == LineIndex){ strRemoveLine(se, sl); break; }
  1793. i++;
  1794. sl = NextSl;
  1795. }
  1796. }
  1797. void strSetCursor(laStringEdit *se, int LineIndex, int BeforeIndex){
  1798. int maxbefore;
  1799. if (!se) return;
  1800. if(LineIndex<0){LineIndex=0;}
  1801. se->CursorLine = LineIndex;
  1802. maxbefore = strlenU(strGetCursorLine(se, &se->CursorLine)->Buf);
  1803. BeforeIndex = BeforeIndex < 0 ? 0 : BeforeIndex > maxbefore ? maxbefore : BeforeIndex;
  1804. se->CursorBefore = BeforeIndex;
  1805. se->BeginLine = -1;
  1806. se->BeginBefore = -1;
  1807. se->EndLine = -1;
  1808. se->EndBefore = -1;
  1809. strEnsureCursorVisible(se);
  1810. }
  1811. void strMoveCursor(laStringEdit *se, int Left, int Select){
  1812. int maxbefore;
  1813. int BeforeIndex;
  1814. int width = 1;
  1815. laStringLine *sl;
  1816. if (!se) return;
  1817. if(Select){ strLazySelect(se); } else { strCancelSelect(se); }
  1818. sl = strGetCursorLine(se, 0);
  1819. maxbefore = strlenU(sl->Buf);
  1820. BeforeIndex = se->CursorBefore - (Left ? 1 : -1);
  1821. if(BeforeIndex<0){
  1822. if(se->CursorLine>0) strSetCursor(se, se->CursorLine-1, INT_MAX);
  1823. }elif(BeforeIndex>maxbefore && se->CursorLine<se->TotalLines-1){
  1824. if(se->CursorLine>0) strSetCursor(se, se->CursorLine+1, 0);
  1825. }else{
  1826. se->CursorBefore = BeforeIndex>=maxbefore?maxbefore:BeforeIndex;
  1827. }
  1828. se->CursorPreferBefore = se->CursorBefore;
  1829. se->BeginLine = -1;
  1830. se->BeginBefore = -1;
  1831. se->EndLine = -1;
  1832. se->EndBefore = -1;
  1833. if(Select){ strEndSelect(se); }
  1834. strEnsureCursorVisible(se);
  1835. }
  1836. void strMoveCursorLine(laStringEdit *se, int Up, int Select){
  1837. int Line, maxbefore, LastIndex=-1;
  1838. laStringLine *sl;
  1839. if (!se) return;
  1840. if(Select){ strLazySelect(se); } else { strCancelSelect(se); }
  1841. Line=se->CursorLine - (Up? 1:-1);
  1842. if(Line<0) {Line=0;}
  1843. se->CursorLine = Line;
  1844. sl = strGetCursorLine(se, &LastIndex);
  1845. if(LastIndex>=0){ se->CursorLine = LastIndex; se->CursorPreferBefore=10000; }
  1846. maxbefore = strlenU(sl->Buf);
  1847. se->CursorBefore = se->CursorPreferBefore;
  1848. if(se->CursorBefore>maxbefore){ se->CursorBefore = maxbefore; }
  1849. if(LastIndex>=0){se->CursorPreferBefore=se->CursorBefore;}
  1850. if(Select){ strEndSelect(se); }
  1851. strEnsureCursorVisible(se);
  1852. }
  1853. int strHasSelection(laStringEdit* se){
  1854. return se->BeginBefore!=se->EndBefore||se->BeginLine!=se->EndLine;
  1855. }
  1856. void strCancelSelect(laStringEdit *se){
  1857. if (!se) return;
  1858. se->_BeginLine = -1;
  1859. se->_BeginBefore = -1;
  1860. se->BeginLine = -1;
  1861. se->EndLine = -1;
  1862. se->BeginBefore = -1;
  1863. se->EndBefore = -1;
  1864. }
  1865. void strLazySelect(laStringEdit *se){
  1866. if (!se || se->_BeginLine>=0) return;
  1867. se->_BeginLine = TNS_MAX2(se->CursorLine,0);
  1868. se->_BeginBefore = se->CursorBefore;
  1869. }
  1870. void strEndSelect(laStringEdit *se){
  1871. if (!se) return;
  1872. se->_EndLine = se->CursorLine;
  1873. se->_EndBefore = se->CursorBefore;
  1874. se->BeginLine = se->_BeginLine;
  1875. se->EndLine = se->_EndLine;
  1876. se->BeginBefore = se->_BeginBefore;
  1877. se->EndBefore = se->_EndBefore;
  1878. if(se->BeginLine>se->EndLine || (se->BeginLine==se->EndLine && se->BeginBefore>se->EndBefore))
  1879. { LA_SWAP(int,se->BeginLine,se->EndLine); LA_SWAP(int,se->BeginBefore,se->EndBefore); }
  1880. }
  1881. void strSelectLineAll(laStringEdit *se){
  1882. if (!se) return;
  1883. laStringLine *sl;
  1884. int len;
  1885. if (se->CursorLine == -1) sl = strGetBeginLine(se);
  1886. else
  1887. sl = strGetCursorLine(se, 0);
  1888. len = strlenU(sl->Buf);
  1889. se->EndBefore = len; se->EndLine=0;
  1890. se->BeginBefore = 0; se->BeginLine=0;
  1891. se->CursorBefore = len;
  1892. se->CursorLine = 0;
  1893. }
  1894. void strDeselectAll(laStringEdit *se){
  1895. if (!se) return;
  1896. laStringLine *sl;
  1897. int len;
  1898. if (se->CursorLine == -1) sl = strGetBeginLine(se);
  1899. else
  1900. sl = strGetCursorLine(se, 0);
  1901. len = strlenU(sl->Buf);
  1902. se->EndBefore = -1;
  1903. se->BeginBefore = -1;
  1904. se->BeginLine = -1;
  1905. se->EndLine = -1;
  1906. se->CursorBefore = len;
  1907. se->CursorLine = -1;
  1908. }
  1909. void strPanFoward(uint32_t *str, int Before, int Offset){
  1910. int len = strlenU(str);
  1911. int i = len + 1;
  1912. for (i; i >= Before; i--){
  1913. str[i + Offset] = str[i];
  1914. }
  1915. }
  1916. void strSquishBackward(uint32_t *str, int Before, int EndBefore){
  1917. int len = strlenU(str);
  1918. int i = Before;
  1919. int Offset = Before - EndBefore;
  1920. if (Before <= 0) return;
  1921. for (i; i <= len; i++){
  1922. str[i - Offset] = str[i];
  1923. }
  1924. }
  1925. void strClearSelection(laStringEdit *se){
  1926. //if (se->EndLine == -1) return;
  1927. if (se->BeginLine != se->EndLine){
  1928. int i = 0; int RemovedLines=0;
  1929. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1930. while (sl){
  1931. NextSl = sl->Item.pNext;
  1932. if (i == se->BeginLine){
  1933. sl->Buf[se->BeginBefore] = U'\0';
  1934. }else if (i > se->BeginLine && i < se->EndLine){
  1935. strRemoveLine(se, sl); RemovedLines++;
  1936. }else if (i == se->EndLine){
  1937. strSquishBackward(sl->Buf, se->EndBefore, 0);
  1938. se->CursorLine = i-RemovedLines;
  1939. se->CursorBefore = 0;
  1940. se->BeginLine = -1;
  1941. se->BeginBefore = -1;
  1942. se->EndLine = -1;
  1943. se->EndBefore = -1;
  1944. strBackspace(se);
  1945. }
  1946. if (i > se->EndLine) break;
  1947. i++;
  1948. sl = NextSl;
  1949. }
  1950. }else{
  1951. int i = 0;
  1952. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1953. while (sl){
  1954. NextSl = sl->Item.pNext;
  1955. if (i == se->EndLine) {
  1956. strSquishBackward(sl->Buf, se->EndBefore, se->BeginBefore);
  1957. se->CursorLine = i;
  1958. se->CursorBefore = se->BeginBefore;
  1959. se->BeginLine = -1;
  1960. se->BeginBefore = -1;
  1961. se->EndLine = -1;
  1962. se->EndBefore = -1;
  1963. break;
  1964. }
  1965. i++; sl = NextSl;
  1966. }
  1967. }
  1968. strEnsureCursorVisible(se);
  1969. }
  1970. laStringLine *strGetCursorLine(laStringEdit *se, int* ReturnIndexIfLast){
  1971. if (!se || se->CursorBefore <= -1) return se->Lines.pFirst;
  1972. int i = 0;
  1973. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1974. while (sl){
  1975. NextSl = sl->Item.pNext;
  1976. if (i == se->CursorLine){
  1977. return sl;
  1978. }
  1979. i++;
  1980. sl = NextSl;
  1981. }
  1982. if(ReturnIndexIfLast){ *ReturnIndexIfLast=i-1;}
  1983. return se->Lines.pLast;
  1984. }
  1985. laStringLine *strGetBeginLine(laStringEdit *se){
  1986. if (!se || se->BeginLine <= -1) return se->Lines.pFirst;
  1987. int i = 0;
  1988. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1989. while (sl){
  1990. NextSl = sl->Item.pNext;
  1991. if (i == se->BeginLine){
  1992. return sl;
  1993. }
  1994. i++;
  1995. sl = NextSl;
  1996. }
  1997. return se->Lines.pFirst;
  1998. }
  1999. void strInsertChar(laStringEdit *se, uint32_t a){
  2000. laStringLine *sl;
  2001. strClearSelection(se);
  2002. sl = strGetCursorLine(se, 0);
  2003. if(a==U'\n'){
  2004. laStringLine* nl=memAcquireSimple(sizeof(laStringLine));
  2005. if(sl->Buf[se->CursorBefore]!=U'\0') strcpyU(nl->Buf, &sl->Buf[se->CursorBefore]);
  2006. sl->Buf[se->CursorBefore]=U'\0';
  2007. se->CursorLine++; se->CursorBefore=0;
  2008. lstInsertItemAfter(&se->Lines, nl, sl); se->TotalLines++;
  2009. }else{
  2010. strPanFoward(sl->Buf, se->CursorBefore, 1);
  2011. sl->Buf[se->CursorBefore] = a;
  2012. se->CursorBefore += 1;
  2013. }
  2014. se->CursorPreferBefore = se->CursorBefore;
  2015. strEnsureCursorVisible(se);
  2016. }
  2017. void strBackspace(laStringEdit *se){
  2018. laStringLine *sl;
  2019. int width = 1;
  2020. if (se->CursorBefore == -1){
  2021. strClearSelection(se);
  2022. }else{
  2023. laStringLine *sl;
  2024. sl = strGetCursorLine(se, 0);
  2025. if (se->CursorBefore > 1 && sl->Buf[se->CursorBefore - 2] < 0) width = 2;
  2026. strSquishBackward(sl->Buf, se->CursorBefore, se->CursorBefore - width);
  2027. se->CursorBefore -= width;
  2028. if (se->CursorBefore <= -1){
  2029. if(sl->Item.pPrev){
  2030. laStringLine* ol=sl->Item.pPrev;
  2031. se->CursorBefore = strlenU(ol->Buf);
  2032. se->CursorLine--;
  2033. strcatU(ol->Buf, sl->Buf);
  2034. strRemoveLine(se, sl);
  2035. }
  2036. else {se->CursorBefore = 0;}
  2037. }
  2038. }
  2039. se->CursorPreferBefore = se->CursorBefore;
  2040. strEnsureCursorVisible(se);
  2041. }
  2042. void strMoveView(laStringEdit *se, int DownLines, int RightCharacters){
  2043. se->ViewStartLine+=DownLines;
  2044. se->ViewStartCol+=RightCharacters;
  2045. if(se->ViewStartLine>=se->TotalLines-1) se->ViewStartLine=se->TotalLines-1;
  2046. if(se->ViewStartLine<0) se->ViewStartLine=0;
  2047. if(se->ViewStartCol<0) se->ViewStartCol=0;
  2048. }
  2049. int laCopyFile(char *to, char *from){
  2050. #ifdef _WIN32
  2051. if(CopyFile(from, to, 0)) return 1; return 0;
  2052. #endif
  2053. #ifdef LA_LINUX
  2054. int fd_to, fd_from; char buf[4096];
  2055. ssize_t nread; int saved_errno;
  2056. fd_from = open(from, O_RDONLY); if (fd_from < 0) return 0;
  2057. fd_to = open(to, O_WRONLY|O_CREAT /* |O_EXCL */, 0666); if (fd_to < 0) goto out_error;
  2058. while (nread=read(fd_from,buf,sizeof(buf)), nread>0) {
  2059. char *out_ptr = buf; ssize_t nwritten;
  2060. do { nwritten = write(fd_to, out_ptr, nread);
  2061. if (nwritten >= 0){ nread -= nwritten; out_ptr += nwritten;}
  2062. else if (errno != EINTR){ goto out_error; }
  2063. }while (nread > 0);
  2064. }
  2065. if (nread == 0){
  2066. if (close(fd_to)<0){ fd_to = -1; goto out_error;}
  2067. close(fd_from);
  2068. return 1;
  2069. }
  2070. out_error:
  2071. saved_errno = errno;
  2072. close(fd_from); if (fd_to >= 0) close(fd_to);
  2073. errno = saved_errno;
  2074. return 0;
  2075. #endif //linux
  2076. }
  2077. int laEnsureDir(const char *dir) {
  2078. #ifdef LA_LINUX
  2079. char tmp[1024]; char *p = NULL; size_t len;
  2080. snprintf(tmp, sizeof(tmp),"%s",dir);
  2081. len = strlen(tmp);
  2082. if (tmp[len - 1] == '/') tmp[len - 1] = 0;
  2083. for (p = tmp + 1; *p; p++){
  2084. if (*p == '/') { *p = 0;
  2085. mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO);
  2086. *p = '/';
  2087. }
  2088. }
  2089. mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO);
  2090. struct stat statbuf;
  2091. if(stat(dir, &statbuf) != 0){ return 0; }
  2092. return S_ISDIR(statbuf.st_mode);
  2093. #endif
  2094. }
  2095. //======================================================[ translation ]
  2096. void transNewLanguage(const char *LanguageID){
  2097. laTranslationNode *tn = memAcquire(sizeof(laTranslationNode));
  2098. strSafeSet(&tn->LanguageName, LanguageID);
  2099. lstAppendItem(&MAIN.Translation.Languages, tn);
  2100. MAIN.Translation.CurrentLanguage = tn;
  2101. }
  2102. void transSetLanguage(const char *LanguageID){
  2103. laTranslationNode *tn;
  2104. if (!LanguageID){
  2105. MAIN.Translation.CurrentLanguage = 0;
  2106. return;
  2107. }
  2108. for (tn = MAIN.Translation.Languages.pFirst; tn; tn = tn->Item.pNext){
  2109. if (!strcmp(SSTR(tn->LanguageName), LanguageID)){
  2110. MAIN.Translation.CurrentLanguage = tn; return;
  2111. }
  2112. }
  2113. transNewLanguage(LanguageID);
  2114. }
  2115. void transDumpMissMatchRecord(const char *filename){
  2116. laTranslationMatch *tm;
  2117. laListHandle *lst;
  2118. int i;
  2119. FILE *f = fopen(filename, "w"); if (!f) return;
  2120. for (i = 0; i < 256; i++){
  2121. lst = &MAIN.Translation.MisMatches.Entries[i];
  2122. for (tm = lst->pFirst; tm; tm = tm->Item.pNext){ if(tm->Target) fprintf(f, "%s | \n", tm->Target); }
  2123. }
  2124. fclose(f);
  2125. }
  2126. int IsThisTranslationMatch(laTranslationMatch *tm, char *p){
  2127. return (tm->Target && (!strcmp(tm->Target, p)));
  2128. }
  2129. void transNewEntry(const char *Target, const char *replacement){
  2130. laTranslationMatch *tm = memAcquireSimple(sizeof(laTranslationMatch));
  2131. tm->Target = Target; tm->Replacement = replacement;
  2132. hsh256InsertItemCSTR(&MAIN.Translation.CurrentLanguage->Matches, tm, Target);
  2133. }
  2134. void transNewMissEntry(const char *Target){
  2135. if (!hsh256FindItemSTR(&MAIN.Translation.MisMatches, IsThisTranslationMatch, Target)){
  2136. laTranslationMatch *tm = memAcquireSimple(sizeof(laTranslationMatch));
  2137. int len=strlen(Target); tm->Target=memAcquireSimple(len*sizeof(char)+1);
  2138. strcpy(tm->Target,Target);
  2139. hsh256InsertItemCSTR(&MAIN.Translation.MisMatches, tm, Target);
  2140. }
  2141. }
  2142. char *transLate(char *Target){
  2143. if (!MAIN.Translation.CurrentLanguage || !MAIN.Translation.EnableTranslation || !Target || !Target[0]) return Target;
  2144. laTranslationMatch *tm = hsh256FindItemSTR(&MAIN.Translation.CurrentLanguage->Matches, IsThisTranslationMatch, Target);
  2145. if (!tm){ transNewMissEntry(Target); return Target; }
  2146. return tm->Replacement;
  2147. }
  2148. void transState(void *UNUSED, int val){
  2149. if (val) MAIN.Translation.EnableTranslation = 1;
  2150. else MAIN.Translation.EnableTranslation = 0;
  2151. laRedrawCurrentWindow();
  2152. }
  2153. void laOpenInternetLink(char *url){
  2154. laSafeString* s=0;
  2155. #ifdef LA_LINUX
  2156. strSafePrint(&s, "xdg-open %s", url);
  2157. #endif
  2158. #ifdef _WIN32
  2159. strSafePrint(&s, "start %s", url);
  2160. #endif
  2161. #ifdef LAGUI_ANDROID
  2162. JNIEnv *env = NULL;
  2163. JavaVM *vm = MAIN.app->activity->vm;
  2164. (*vm)->AttachCurrentThread(vm, &env, NULL);
  2165. jstring urlString = (*env)->NewStringUTF(env, url);
  2166. jclass uriClass = (*env)->FindClass(env, "android/net/Uri");
  2167. jmethodID uriParse = (*env)->GetStaticMethodID(env, uriClass, "parse", "(Ljava/lang/String;)Landroid/net/Uri;");
  2168. jobject uri = (*env)->CallStaticObjectMethod(env, uriClass, uriParse, urlString);
  2169. jclass intentClass = (*env)->FindClass(env, "android/content/Intent");
  2170. jfieldID actionViewId = (*env)->GetStaticFieldID(env, intentClass, "ACTION_VIEW", "Ljava/lang/String;");
  2171. jobject actionView = (*env)->GetStaticObjectField(env, intentClass, actionViewId);
  2172. jmethodID newIntent = (*env)->GetMethodID(env, intentClass, "<init>", "(Ljava/lang/String;Landroid/net/Uri;)V");
  2173. jobject intent = (*env)->AllocObject(env, intentClass);
  2174. (*env)->CallVoidMethod(env, intent, newIntent, actionView, uri);
  2175. jclass activityClass = (*env)->FindClass(env, "android/app/Activity");
  2176. jmethodID startActivity = (*env)->GetMethodID(env, activityClass, "startActivity", "(Landroid/content/Intent;)V");
  2177. (*env)->CallVoidMethod(env, MAIN.app->activity->clazz, startActivity, intent);
  2178. (*vm)->DetachCurrentThread(vm);
  2179. return;
  2180. #endif
  2181. system(s->Ptr);
  2182. strSafeDestroy(&s);
  2183. //these were windows stuff
  2184. //HKEY hkRoot, hSubKey;
  2185. //char ValueName[256]={0};
  2186. //char DataValue[256]={0};
  2187. //u64bit cbValueName = 256;
  2188. //u64bit cbDataValue = 256;
  2189. //char ShellChar[512]={0};
  2190. //DWORD dwType;
  2191. //
  2192. //ShellExecute(0, "open", link, 0, 0, SW_SHOWNORMAL);
  2193. //
  2194. //return;
  2195. }
  2196. #ifdef _WIN32
  2197. void usleep(unsigned int usec){
  2198. HANDLE timer;
  2199. LARGE_INTEGER ft;
  2200. static int init = 0;
  2201. if (init == 0){
  2202. init = 1;
  2203. const HINSTANCE ntdll = LoadLibrary("ntdll.dll");
  2204. if (ntdll != NULL){
  2205. typedef long(NTAPI* pNtQueryTimerResolution)(u64bit* MinimumResolution, u64bit* MaximumResolution, u64bit* CurrentResolution);
  2206. typedef long(NTAPI* pNtSetTimerResolution)(u64bit RequestedResolution, char SetResolution, u64bit* ActualResolution);
  2207. pNtQueryTimerResolution NtQueryTimerResolution = (pNtQueryTimerResolution)GetProcAddress(ntdll, "NtQueryTimerResolution");
  2208. pNtSetTimerResolution NtSetTimerResolution = (pNtSetTimerResolution)GetProcAddress(ntdll, "NtSetTimerResolution");
  2209. if (NtQueryTimerResolution != NULL && NtSetTimerResolution != NULL){
  2210. u64bit minimum, maximum, current;
  2211. NtQueryTimerResolution(&minimum, &maximum, &current);
  2212. NtSetTimerResolution(maximum, (char)1, &current);
  2213. }
  2214. FreeLibrary(ntdll);
  2215. }
  2216. }
  2217. ft.QuadPart = -(10 * (__int64)usec);
  2218. timer = CreateWaitableTimer(NULL, TRUE, NULL);
  2219. SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
  2220. WaitForSingleObject(timer, INFINITE);
  2221. CloseHandle(timer);
  2222. }
  2223. void laSpinInit(SYSLOCK* lock) {
  2224. InitializeCriticalSection(lock);
  2225. }
  2226. void laSpinDestroy(SYSLOCK* lock) {
  2227. DeleteCriticalSection(lock);
  2228. }
  2229. void laSpinLock(SYSLOCK* lock) {
  2230. EnterCriticalSection(lock);
  2231. }
  2232. void laSpinUnlock(SYSLOCK* lock) {
  2233. LeaveCriticalSection(lock);
  2234. }
  2235. #endif
  2236. #ifdef LA_LINUX
  2237. void laSpinInit(SYSLOCK* lock) {
  2238. pthread_spin_init(lock, 0);
  2239. }
  2240. void laSpinDestroy(SYSLOCK* lock) {
  2241. pthread_spin_destroy(lock);
  2242. }
  2243. void laSpinLock(SYSLOCK* lock) {
  2244. pthread_spin_lock(lock);
  2245. }
  2246. void laSpinUnlock(SYSLOCK* lock) {
  2247. pthread_spin_unlock(lock);
  2248. }
  2249. #endif
  2250. #ifdef LAGUI_ANDROID
  2251. void laSpinInit(SYSLOCK* lock) {
  2252. return;
  2253. }
  2254. void laSpinDestroy(SYSLOCK* lock) {
  2255. return;
  2256. }
  2257. void laSpinLock(SYSLOCK* lock) {
  2258. return;
  2259. }
  2260. void laSpinUnlock(SYSLOCK* lock) {
  2261. return;
  2262. }
  2263. #endif
  2264. //======================================= lua utils
  2265. #ifdef LA_WITH_LUAJIT
  2266. static const char *progname = LUA_PROGNAME;
  2267. static int la_luaTraceback(lua_State *L){
  2268. if (!lua_isstring(L, 1)) { /* Non-string error object? Try metamethod. */
  2269. if (lua_isnoneornil(L, 1) ||
  2270. !luaL_callmeta(L, 1, "__tostring") ||
  2271. !lua_isstring(L, -1))
  2272. return 1; /* Return non-string error object. */
  2273. lua_remove(L, 1); /* Replace object by result of __tostring metamethod. */
  2274. }
  2275. luaL_traceback(L, L, lua_tostring(L, 1), 1);
  2276. return 1;
  2277. }
  2278. static void la_luaMessage(const char *msg){
  2279. if (progname) { logPrint("%s: ",progname); } logPrint("%s\n",msg);
  2280. }
  2281. static int la_luaReport(lua_State *L, int status){
  2282. if (status && !lua_isnil(L, -1)) {
  2283. const char *msg = lua_tostring(L, -1);
  2284. if (msg == NULL) msg = "(error object is not a string)";
  2285. la_luaMessage(msg);
  2286. lua_pop(L, 1);
  2287. }
  2288. return status;
  2289. }
  2290. static int la_luaDoCall(lua_State *L, int narg, int clear){
  2291. int status;
  2292. int base = lua_gettop(L) - narg; /* function index */
  2293. lua_pushcfunction(L, la_luaTraceback); /* push la_luaTraceback function */
  2294. lua_insert(L, base); /* put it under chunk and args */
  2295. status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
  2296. lua_remove(L, base); /* remove la_luaTraceback function */
  2297. /* force a complete garbage collection in case of errors */
  2298. if (status != LUA_OK) lua_gc(L, LUA_GCCOLLECT, 0);
  2299. return status;
  2300. }
  2301. static int la_luaIncomplete(lua_State *L, int status){
  2302. if (status == LUA_ERRSYNTAX) {
  2303. size_t lmsg;
  2304. const char *msg = lua_tolstring(L, -1, &lmsg);
  2305. const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
  2306. if (strstr(msg, LUA_QL("<eof>")) == tp) { lua_pop(L, 1); return 1; }
  2307. }
  2308. return 0; /* else... */
  2309. }
  2310. int terLoadLine(char* buf, int firstline){
  2311. lua_State *L=MAIN.L;
  2312. if(!MAIN.TerminalIncomplete){ lua_settop(L, 0); }
  2313. size_t len = strlen(buf); if(len>=512){ buf[512]=0; }
  2314. if(len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
  2315. if(firstline && buf[0] == '=') lua_pushfstring(L, "return %s", buf+1);
  2316. else lua_pushstring(L, buf);
  2317. if(MAIN.TerminalIncomplete){
  2318. lua_pushliteral(L, "\n"); lua_insert(L, -2); lua_concat(L, 3);
  2319. }
  2320. int status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "terLoadLine");
  2321. if(la_luaIncomplete(L,status)){
  2322. MAIN.TerminalIncomplete=1;
  2323. }else{
  2324. MAIN.TerminalIncomplete=0;
  2325. lua_remove(L, 1);
  2326. }
  2327. if(status==LUA_OK && (!MAIN.TerminalIncomplete)){
  2328. status = la_luaDoCall(L, 0, 0);
  2329. la_luaReport(L, status);
  2330. if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */
  2331. lua_getglobal(L, "log");
  2332. lua_insert(L, 1);
  2333. if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
  2334. la_luaMessage(lua_pushfstring(L, "error calling " LUA_QL("log") " (%s)",
  2335. lua_tostring(L, -1)));
  2336. }
  2337. }
  2338. return status;
  2339. }
  2340. static int lalua_Log(lua_State *L) {
  2341. int n = lua_gettop(L); int i;
  2342. lua_getglobal(L, "tostring");
  2343. for (i=1; i<=n; i++) {
  2344. const char *s;
  2345. lua_pushvalue(L, -1); /* tostring function */
  2346. lua_pushvalue(L, i); /* value to print */
  2347. lua_call(L, 1, 1);
  2348. s = lua_tostring(L, -1); /* get result */
  2349. if (s == NULL) return luaL_error(L, LUA_QL("tostring") " must return a string to "LUA_QL("use `log`"));
  2350. if (i>1) logPrint(" ");
  2351. logPrint(s); lua_pop(L, 1); /* pop result */
  2352. }
  2353. logPrint("\n");
  2354. return 0;
  2355. }
  2356. void la_luaLoadLibs(lua_State *L){
  2357. lua_gc(L, LUA_GCSTOP, 0);
  2358. luaL_openlibs(L);
  2359. lua_register(L,"log",lalua_Log);
  2360. if(luaL_loadstring(L, LA_LUA_LIB_COMMON) || lua_pcall(L, 0, 0, 0)){ logPrint(" Error loading lagui lua libs.\n"); };
  2361. if(luaL_loadstring(L, LA_LUA_LIB_AUDIO) || lua_pcall(L, 0, 0, 0)){ logPrint(" Error loading lua libs for audio.\n"); };
  2362. lua_gc(L, LUA_GCRESTART, -1);
  2363. }
  2364. void la_luaPrintStatus(lua_State *L){
  2365. logPrint(LUAJIT_VERSION " -- " LUAJIT_COPYRIGHT ". " LUAJIT_URL "\n");
  2366. int n; const char *s;
  2367. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  2368. lua_getfield(L, -1, "jit"); lua_remove(L, -2); /* Get jit.* module table. */
  2369. lua_getfield(L, -1, "status"); lua_remove(L, -2);
  2370. n = lua_gettop(L);
  2371. lua_call(L, 0, LUA_MULTRET);
  2372. logPrint(lua_toboolean(L, n) ? "JIT: ON" : "JIT: OFF");
  2373. for (n++; (s = lua_tostring(L, n)); n++) { logPrint("%s ",s); }
  2374. logPrint("\n");
  2375. lua_settop(L, 0); /* clear stack */
  2376. }
  2377. void la_luaDumpStack(lua_State *L){
  2378. int top=lua_gettop(L); for (int i=1; i <= top; i++) {
  2379. printf("%d\t%s\t", i, luaL_typename(L,i));
  2380. switch (lua_type(L, i)) {
  2381. case LUA_TNUMBER: printf("%g\n",lua_tonumber(L,i)); break;
  2382. case LUA_TSTRING: printf("%s\n",lua_tostring(L,i)); break;
  2383. case LUA_TBOOLEAN: printf("%s\n", (lua_toboolean(L, i) ? "true" : "false")); break;
  2384. case LUA_TNIL: printf("%s\n", "nil"); break;
  2385. default: printf("%p\n",lua_topointer(L,i)); break;
  2386. }
  2387. }
  2388. }
  2389. #else //luajit
  2390. int terLoadLine(char* buf, int firstline){
  2391. logPrint(buf); return 0;
  2392. }
  2393. #endif //luajit
  2394. #ifdef LAGUI_ANDROID
  2395. void glPointSize(real a){
  2396. return;
  2397. }
  2398. void glDrawBuffer(GLenum mode){
  2399. GLenum b={GL_NONE};
  2400. glDrawBuffers(1,b);
  2401. }
  2402. #endif