*/}}

la_util.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720
  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. return strToUTF8Lim(target,src,INT_MAX);
  48. }
  49. int strlenU(uint32_t* str){ int i=0; while(str[i]!=0) i++; return i; }
  50. void strcpyU(uint32_t* target, uint32_t* const source ){ int i=0; while(source[i]!=0){ target[i]=source[i]; i++; } target[i]=0; }
  51. void 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. return 1;
  397. }
  398. void arrFree(void** head, int* max){ free(*head); *head=0; *max=0; }
  399. void lstPushSingle(void **Head, laListSingle *Item){
  400. Item->pNext = *Head;
  401. *Head = Item;
  402. }
  403. void *lstPopSingle(void **Head, laListSingle *Item){
  404. *Head = ((laListSingle *)(*Head))->pNext;
  405. Item->pNext = 0;
  406. return *Head;
  407. }
  408. int lstCountElements(laListHandle* Handle){
  409. int count=0; if(!Handle) return 0;
  410. for(laListItem* i=Handle->pFirst;i;i=i->pNext){count++;}
  411. return count;
  412. }
  413. void lstAppendItem(laListHandle* Handle, void* Item){
  414. laListItem* li = Item;
  415. li->pNext = li->pPrev = 0;
  416. if (!Handle->pFirst) Handle->pFirst = Item;
  417. if (Handle->pLast) ((laListItem*)Handle->pLast)->pNext = li;
  418. li->pPrev = Handle->pLast;
  419. li->pNext = 0;
  420. Handle->pLast = li;
  421. };
  422. void lstPushItem(laListHandle* Handle, void* Item){
  423. laListItem* li = Item;
  424. li->pNext = li->pPrev = 0;
  425. if (!Handle->pLast) Handle->pLast = Item;
  426. li->pNext = Handle->pFirst;
  427. if (Handle->pFirst) ((laListItem*)Handle->pFirst)->pPrev = Item;
  428. Handle->pFirst = li;
  429. };
  430. void* lstPopItem(laListHandle* Handle){
  431. laListItem* popitem;
  432. laListItem* next;
  433. if (!Handle->pFirst) return 0;
  434. popitem = Handle->pFirst;
  435. next = ((laListItem*)Handle->pFirst)->pNext;
  436. if (!next){
  437. Handle->pFirst = 0;
  438. Handle->pLast = 0;
  439. }else{
  440. Handle->pFirst = next;
  441. if (next) next->pPrev = 0;
  442. };
  443. popitem->pNext=popitem->pPrev=0;
  444. return popitem;
  445. };
  446. int lstHaveItemInList(laListHandle *Handle){
  447. if (Handle->pFirst) return 1;
  448. return 0;
  449. };
  450. void lstAppendItem2(laListHandle *Handle, void *Item){
  451. laListItem2 *li = Item;
  452. li->pNext = li->pPrev = 0;
  453. if (!Handle->pFirst) Handle->pFirst = Item;
  454. if (Handle->pLast) ((laListItem2 *)Handle->pLast)->pNext = li;
  455. li->pPrev = Handle->pLast;
  456. li->pNext = 0;
  457. Handle->pLast = li;
  458. };
  459. void lstPushItem2(laListHandle *Handle, void *Item){
  460. laListItem2 *li = Item;
  461. li->pNext = li->pPrev = 0;
  462. if (!Handle->pLast) Handle->pLast = Item;
  463. li->pNext = Handle->pFirst;
  464. if (Handle->pFirst) ((laListItem2 *)Handle->pFirst)->pPrev = Item;
  465. Handle->pFirst = li;
  466. };
  467. void *lstPopItem2(laListHandle *Handle){
  468. void *popitem;
  469. laListItem2 *next;
  470. if (!Handle->pFirst) return 0;
  471. popitem = Handle->pFirst;
  472. next = ((laListItem2 *)Handle->pFirst)->pNext;
  473. if (!next){
  474. Handle->pFirst = 0;
  475. Handle->pLast = 0;
  476. }else{
  477. Handle->pFirst = next;
  478. if (next) next->pPrev = 0;
  479. };
  480. return popitem;
  481. };
  482. void lstAppendItem3(laListHandle *Handle, void *Item){
  483. laListItem3 *li = Item;
  484. li->pNext = li->pPrev = 0;
  485. if (!Handle->pFirst) Handle->pFirst = Item;
  486. if (Handle->pLast) ((laListItem3 *)Handle->pLast)->pNext = li;
  487. li->pPrev = Handle->pLast;
  488. li->pNext = 0;
  489. Handle->pLast = li;
  490. };
  491. void lstPushItem3(laListHandle *Handle, void *Item){
  492. laListItem3 *li = Item;
  493. li->pNext = li->pPrev = 0;
  494. if (!Handle->pLast) Handle->pLast = Item;
  495. li->pNext = Handle->pFirst;
  496. if (Handle->pFirst) ((laListItem3 *)Handle->pFirst)->pPrev = Item;
  497. Handle->pFirst = li;
  498. };
  499. void *lstPopItem3(laListHandle *Handle){
  500. void *popitem;
  501. laListItem3 *next;
  502. if (!Handle->pFirst) return 0;
  503. popitem = Handle->pFirst;
  504. next = ((laListItem3 *)Handle->pFirst)->pNext;
  505. if (!next){
  506. Handle->pFirst = 0;
  507. Handle->pLast = 0;
  508. }else{
  509. Handle->pFirst = next;
  510. if (next) next->pPrev = 0;
  511. };
  512. return popitem;
  513. };
  514. void *lstGetTop(laListHandle *Handle){
  515. return Handle->pFirst;
  516. };
  517. int lstRemoveItem(laListHandle* Handle, laListItem* li) {
  518. if (!li->pPrev && Handle->pFirst != li) return 0;
  519. if (!li->pPrev) Handle->pFirst = li->pNext;
  520. else ((laListItem*)li->pPrev)->pNext = li->pNext;
  521. if (!li->pNext) Handle->pLast = li->pPrev;
  522. else ((laListItem*)li->pNext)->pPrev = li->pPrev;
  523. li->pNext = li->pPrev = 0;
  524. return 1;
  525. }
  526. int lstRemoveItem2(laListHandle *Handle, laListItem2 *li){
  527. if (!li->pPrev) Handle->pFirst = li->pNext;
  528. else ((laListItem2 *)li->pPrev)->pNext = li->pNext;
  529. if (!li->pNext) Handle->pLast = li->pPrev;
  530. else ((laListItem2 *)li->pNext)->pPrev = li->pPrev;
  531. li->pNext = li->pPrev = 0;
  532. return 1;
  533. };
  534. int lstRemoveItem3(laListHandle *Handle, laListItem2 *li){
  535. if (!li->pPrev) Handle->pFirst = li->pNext;
  536. else ((laListItem3 *)li->pPrev)->pNext = li->pNext;
  537. if (!li->pNext) Handle->pLast = li->pPrev;
  538. else ((laListItem3 *)li->pNext)->pPrev = li->pPrev;
  539. li->pNext = li->pPrev = 0;
  540. return 1;
  541. };
  542. int lstRemoveSegment(laListHandle *Handle, laListItem *Begin, laListItem *End){
  543. if (!Begin->pPrev) Handle->pFirst = End->pNext;
  544. else ((laListItem *)Begin->pPrev)->pNext = End->pNext;
  545. if (!End->pNext) Handle->pLast = Begin->pPrev;
  546. else ((laListItem *)End->pNext)->pPrev = Begin->pPrev;
  547. End->pNext = Begin->pPrev = 0;
  548. return 1;
  549. };
  550. void lstInsertItemBefore(laListHandle *Handle, laListItem *toIns, laListItem *pivot){
  551. if (!pivot){
  552. lstPushItem(Handle, toIns);
  553. return;
  554. }
  555. if (pivot->pPrev){
  556. ((laListItem *)pivot->pPrev)->pNext = toIns;
  557. toIns->pPrev = pivot->pPrev;
  558. }else{
  559. Handle->pFirst = toIns;
  560. }
  561. toIns->pNext = pivot;
  562. pivot->pPrev = toIns;
  563. };
  564. void lstInsertItemAfter(laListHandle *Handle, laListItem *toIns, laListItem *pivot){
  565. if (!pivot){
  566. lstAppendItem(Handle, toIns);
  567. return;
  568. }
  569. if (pivot->pNext){
  570. ((laListItem *)pivot->pNext)->pPrev = toIns;
  571. toIns->pNext = pivot->pNext;
  572. }else{
  573. Handle->pLast = toIns;
  574. }
  575. toIns->pPrev = pivot;
  576. pivot->pNext = toIns;
  577. }
  578. void lstInsertSegmentBefore(laListHandle *Handle, laListItem *Begin, laListItem *End, laListItem *pivot){
  579. if (pivot->pPrev){
  580. ((laListItem *)pivot->pPrev)->pNext = Begin;
  581. Begin->pPrev = pivot->pPrev;
  582. }else{
  583. Handle->pFirst = Begin;
  584. }
  585. End->pNext = pivot;
  586. pivot->pPrev = End;
  587. };
  588. void lstInsertSegmentAfter(laListHandle *Handle, laListItem *Begin, laListItem *End, laListItem *pivot){
  589. if (pivot->pNext){
  590. ((laListItem *)pivot->pNext)->pPrev = End;
  591. End->pNext = pivot->pNext;
  592. }else{
  593. Handle->pLast = End;
  594. }
  595. Begin->pPrev = pivot;
  596. pivot->pNext = Begin;
  597. }
  598. void *lstAppendPointerOnly(laListHandle *h, void *p){
  599. laListItemPointer *lip;
  600. if (!h) return 0;
  601. lip = CreateNew(laListItemPointer);
  602. lip->p = p;
  603. lstAppendItem(h, lip);
  604. return lip;
  605. }
  606. void *lstAppendPointerSizedOnly(laListHandle *h, void *p, int size){
  607. laListItemPointer *lip;
  608. if (!h) return 0;
  609. lip = calloc(1, size);
  610. lip->p = p;
  611. lstAppendItem(h, lip);
  612. return lip;
  613. }
  614. void *lstPushPointerOnly(laListHandle *h, void *p){
  615. laListItemPointer *lip = 0;
  616. if (!h) return 0;
  617. lip = CreateNew(laListItemPointer);
  618. lip->p = p;
  619. lstPushItem(h, lip);
  620. return lip;
  621. }
  622. void *lstPushPointerSizedOnly(laListHandle *h, void *p, int size){
  623. laListItemPointer *lip = 0;
  624. if (!h) return 0;
  625. lip = calloc(1, size);
  626. lip->p = p;
  627. lstPushItem(h, lip);
  628. return lip;
  629. }
  630. void lstReverse(laListHandle* h){
  631. laListHandle l={0}; void* i; while(i=lstPopItem(h)){ lstPushItem(&l,i); }
  632. memcpy(h,&l,sizeof(laListHandle));
  633. }
  634. int lstHasPointer(laListHandle* h, void *p){
  635. laListItemPointer *i; for (i = h->pFirst; i; i = i->pNext){
  636. if (i->p == p){return 1;}
  637. } return 0;
  638. }
  639. void *lstAppendPointer(laListHandle *h, void *p){
  640. laListItemPointer *lip;
  641. if (!h) return 0;
  642. lip = memAcquireSimple(sizeof(laListItemPointer));
  643. lip->p = p;
  644. lstAppendItem(h, lip);
  645. return lip;
  646. }
  647. void *lstAppendPointerSized(laListHandle *h, void *p, int size){
  648. laListItemPointer *lip;
  649. if (!h) return 0;
  650. lip = memAcquireSimple(size);
  651. lip->p = p;
  652. lstAppendItem(h, lip);
  653. return lip;
  654. }
  655. void *lstPushPointer(laListHandle *h, void *p){
  656. laListItemPointer *lip = 0;
  657. if (!h) return 0;
  658. lip = memAcquireSimple(sizeof(laListItemPointer));
  659. lip->p = p;
  660. lstPushItem(h, lip);
  661. return lip;
  662. }
  663. void *lstPushPointerSized(laListHandle *h, void *p, int size){
  664. laListItemPointer *lip = 0;
  665. if (!h) return 0;
  666. lip = memAcquireSimple(size);
  667. lip->p = p;
  668. lstPushItem(h, lip);
  669. return lip;
  670. }
  671. void *lstAppendPointerStatic(laListHandle *h, laStaticMemoryPool *smp, void *p){
  672. laListItemPointer *lip;
  673. if (!h) return 0;
  674. lip = memStaticAcquire(smp, sizeof(laListItemPointer));
  675. lip->p = p;
  676. lstAppendItem(h, lip);
  677. return lip;
  678. }
  679. void *lstAppendPointerStaticSized(laListHandle *h, laStaticMemoryPool *smp, void *p, int size){
  680. laListItemPointer *lip;
  681. if (!h) return 0;
  682. lip = memStaticAcquire(smp, size);
  683. lip->p = p;
  684. lstAppendItem(h, lip);
  685. return lip;
  686. }
  687. void *lstPushPointerStatic(laListHandle *h, laStaticMemoryPool *smp, void *p){
  688. laListItemPointer *lip = 0;
  689. if (!h) return 0;
  690. lip = memStaticAcquire(smp, sizeof(laListItemPointer));
  691. lip->p = p;
  692. lstPushItem(h, lip);
  693. return lip;
  694. }
  695. void *lstPushPointerStaticSized(laListHandle *h, laStaticMemoryPool *smp, void *p, int size){
  696. laListItemPointer *lip = 0;
  697. if (!h) return 0;
  698. lip = memStaticAcquire(smp, size);
  699. lip->p = p;
  700. lstPushItem(h, lip);
  701. return lip;
  702. }
  703. void *lstPopPointerOnly(laListHandle *h){
  704. laListItemPointer *lip;
  705. void *rev = 0;
  706. if (!h) return 0;
  707. lip = lstPopItem(h);
  708. rev = lip ? lip->p : 0;
  709. FreeMem(lip);
  710. return rev;
  711. }
  712. void lstRemovePointerItemOnly(laListHandle *h, laListItemPointer *lip){
  713. lstRemoveItem(h, lip);
  714. FreeMem(lip);
  715. }
  716. void lstRemovePointerOnly(laListHandle *h, void *p){
  717. laListItemPointer *i;
  718. for (i = h->pFirst; i; i = i->pNext){
  719. if (i->p == p){
  720. lstRemovePointerItem(h, i);
  721. break;
  722. }
  723. }
  724. }
  725. void lstClearPointerOnly(laListHandle *h){
  726. laListItemPointer *i;
  727. while (h && h->pFirst){
  728. lstPopPointer(h);
  729. }
  730. }
  731. void lstGeneratePointerListOnly(laListHandle *from1, laListHandle *from2, laListHandle *to){
  732. laListItemPointer *lip = from2 ? from2->pLast : 0;
  733. while (lip){
  734. lstPushPointer(to, lip->p);
  735. lip = lip->pPrev;
  736. }
  737. lip = from1 ? from1->pLast : 0;
  738. while (lip){
  739. lstPushPointer(to, lip->p);
  740. lip = lip->pPrev;
  741. }
  742. }
  743. void *lstPopPointer(laListHandle *h){
  744. laListItemPointer *lip;
  745. void *rev = 0;
  746. if (!h) return 0;
  747. lip = lstPopItem(h);
  748. rev = lip ? lip->p : 0;
  749. memFree(lip);
  750. return rev;
  751. }
  752. void lstRemovePointerItem(laListHandle *h, laListItemPointer *lip){
  753. lstRemoveItem(h, lip);
  754. memFree(lip);
  755. }
  756. void lstRemovePointer(laListHandle *h, void *p){
  757. laListItemPointer *i;
  758. for (i = h->pFirst; i; i = i->pNext){
  759. if (i->p == p){ lstRemovePointerItem(h, i); break; }
  760. }
  761. }
  762. void lstRemovePointerLeave(laListHandle *h, void *p){
  763. laListItemPointer *i;
  764. for (i = h->pFirst; i; i = i->pNext){
  765. if (i->p == p){ lstRemoveItem(h, i); memLeave(i); break; }
  766. }
  767. }
  768. void lstClearPointer(laListHandle *h){
  769. laListItemPointer *i;
  770. while (h && h->pFirst){
  771. lstPopPointer(h);
  772. }
  773. }
  774. void lstGeneratePointerList(laListHandle *from1, laListHandle *from2, laListHandle *to){
  775. laListItemPointer *lip = from1 ? from1->pLast : 0;
  776. while (lip){
  777. lstPushPointer(to, lip->p);
  778. lip = lip->pPrev;
  779. }
  780. lip = from2 ? from2->pLast : 0;
  781. while (lip){
  782. lstPushPointer(to, lip->p);
  783. lip = lip->pPrev;
  784. }
  785. }
  786. void *lstAppendPointerStaticPool(laStaticMemoryPool *mph, laListHandle *h, void *p){
  787. laListItemPointer *lip;
  788. if (!h) return 0;
  789. lip = memStaticAcquire(mph, sizeof(laListItemPointer));
  790. lip->p = p;
  791. lstAppendItem(h, lip);
  792. return lip;
  793. }
  794. void *lstPopPointerLeave(laListHandle *h){
  795. laListItemPointer *lip;
  796. void *rev = 0;
  797. if (!h) return 0;
  798. lip = lstPopItem(h); memLeave(lip);
  799. rev = lip ? lip->p : 0;
  800. return rev;
  801. }
  802. void lstRemovePointerItemNoFree(laListHandle *h, laListItemPointer *lip){
  803. lstRemoveItem(h, lip);
  804. }
  805. void lstCopyHandle(laListHandle *target, laListHandle *src){
  806. target->pFirst = src->pFirst;
  807. target->pLast = src->pLast;
  808. };
  809. void lstClearHandle(laListHandle *h){
  810. h->pFirst = 0;
  811. h->pLast = 0;
  812. }
  813. void lstClearPrevNext(laListItem *li){
  814. li->pNext = 0;
  815. li->pPrev = 0;
  816. }
  817. void lstMoveUp(laListHandle *h, laListItem *li){
  818. void *pprev = li->pPrev ? ((laListItem *)li->pPrev)->pPrev : 0;
  819. if (!h || !li) return;
  820. if (li == h->pFirst) return;
  821. else{
  822. if (li == h->pLast) h->pLast = li->pPrev;
  823. ((laListItem *)li->pPrev)->pNext = li->pNext;
  824. ((laListItem *)li->pPrev)->pPrev = li;
  825. if (li->pNext) ((laListItem *)li->pNext)->pPrev = li->pPrev;
  826. li->pNext = li->pPrev;
  827. li->pPrev = pprev;
  828. if (pprev) ((laListItem *)pprev)->pNext = li;
  829. }
  830. if (!li->pPrev) h->pFirst = li;
  831. }
  832. void lstMoveDown(laListHandle *h, laListItem *li){
  833. void *ppnext = li->pNext ? ((laListItem *)li->pNext)->pNext : 0;
  834. if (!h || !li) return;
  835. if (li == h->pLast) return;
  836. else{
  837. if (li == h->pFirst) h->pFirst = li->pNext;
  838. ((laListItem *)li->pNext)->pPrev = li->pPrev;
  839. ((laListItem *)li->pNext)->pNext = li;
  840. if (li->pPrev) ((laListItem *)li->pPrev)->pNext = li->pNext;
  841. li->pPrev = li->pNext;
  842. li->pNext = ppnext;
  843. if (ppnext) ((laListItem *)ppnext)->pPrev = li;
  844. }
  845. if (!li->pNext) h->pLast = li;
  846. }
  847. void lstForAllItemsDo(laListDoFunc func, laListHandle *hList){
  848. laListItem *it = hList->pFirst;
  849. for (; it; it = it->pNext){
  850. func(it);
  851. }
  852. };
  853. void lstForAllItemsDoLNRR(laListNonRecursiveDoFunc func, laListHandle *hList){
  854. laListItem *it = hList->pFirst;
  855. for (; it; it = it->pNext){
  856. func(0, it, 0);
  857. }
  858. };
  859. void lstForAllItemsDo_DirectFree(laListDoFunc func, laListHandle *hList){
  860. laListItem *it;
  861. while (it = lstPopItem(hList)){
  862. if (func) func(it);
  863. FreeMem(it);
  864. }
  865. };
  866. void lstForAllItemsDo_arg_ptr(laListDoFuncArgp func, laListHandle *hList, void *arg){
  867. laListItem *it = hList->pFirst;
  868. for (; it; it = it->pNext){
  869. func(it, arg);
  870. };
  871. };
  872. void lstForAllItemsDo_NonRecursive_Root(laListHandle *FirstHandle, laListNonRecursiveDoFunc func, int bFreeItem, void *custom_data, laListCustomDataRemover remover){
  873. laListItem *li = 0, *NextLi;
  874. laListNonRecursiveRoot root = {0};
  875. laListNonRecursiveItem *nrItem = CreateNew(laListNonRecursiveItem);
  876. nrItem->bFreeList = bFreeItem;
  877. nrItem->func = func;
  878. nrItem->CustomData = custom_data;
  879. nrItem->remover = remover;
  880. lstCopyHandle(&nrItem->handle, FirstHandle);
  881. lstAppendItem(&root.NSItems, nrItem);
  882. while (lstHaveItemInList(&root.NSItems)){
  883. nrItem = lstPopItem(&root.NSItems);
  884. for (li = nrItem->handle.pFirst; li /*!=nrItem->handle.pLast*/; li = NextLi){
  885. if (nrItem->func) nrItem->func(&root, li, custom_data);
  886. NextLi = li->pNext;
  887. if (nrItem->bFreeList){
  888. laListItem *fli = li;
  889. FreeMem(fli);
  890. }
  891. if (li == nrItem->handle.pLast) break;
  892. }
  893. if (nrItem->remover) nrItem->remover(nrItem->CustomData);
  894. FreeMem(nrItem);
  895. }
  896. };
  897. void lstAddNonRecursiveListHandle(laListNonRecursiveRoot *root, laListHandle *newHandle, laListNonRecursiveDoFunc nrFunc, int bFreeList, void *custom_data, laListCustomDataRemover remover){
  898. laListNonRecursiveItem *nrItem = CreateNew(laListNonRecursiveItem);
  899. nrItem->bFreeList = bFreeList;
  900. nrItem->func = nrFunc;
  901. nrItem->CustomData = custom_data;
  902. nrItem->remover = remover;
  903. lstCopyHandle(&nrItem->handle, newHandle);
  904. lstAppendItem(&root->NSItems, nrItem);
  905. };
  906. void lstCopy_NonRecursive_Root(laListHandle *FromHandle, laListHandle *ToHandle, int SizeEachNode, laListNonRecursiveCopyFunc func, void *custom_data, laListCustomDataRemover remover){
  907. laListItem *li = 0, *tli = 0;
  908. laListNonRecursiveRoot root = {0};
  909. laListNonRecursiveItem *nrItem = CreateNew(laListNonRecursiveItem);
  910. laListItem *NextLi;
  911. nrItem->CopyFunc = func;
  912. lstCopyHandle(&nrItem->handle, FromHandle);
  913. nrItem->ToHandle = ToHandle; //Pointer
  914. lstClearHandle(ToHandle);
  915. nrItem->CustomData = custom_data;
  916. nrItem->remover = remover;
  917. nrItem->SizeEachNode = SizeEachNode;
  918. lstAppendItem(&root.NSItems, nrItem);
  919. while (lstHaveItemInList(&root.NSItems)){
  920. nrItem = lstPopItem(&root.NSItems);
  921. if (nrItem->CopyFunc){
  922. for (li = nrItem->handle.pFirst; li; li = li->pNext){
  923. tli = CreateNew_Size(nrItem->SizeEachNode);
  924. nrItem->CopyFunc(&root, li, tli, nrItem->CustomData);
  925. lstClearPrevNext(tli);
  926. lstAppendItem(nrItem->ToHandle, tli);
  927. }
  928. if (nrItem->remover) nrItem->remover(nrItem->CustomData);
  929. }else if (nrItem->func){
  930. for (li = nrItem->handle.pFirst; li /*!=nrItem->handle.pLast*/; li = NextLi){
  931. if (nrItem->func) nrItem->func(&root, li, custom_data);
  932. NextLi = li->pNext;
  933. if (nrItem->bFreeList){
  934. laListItem *fli = li;
  935. FreeMem(fli);
  936. }
  937. if (li == nrItem->handle.pLast) break;
  938. }
  939. if (nrItem->remover) nrItem->remover(nrItem->CustomData);
  940. }
  941. FreeMem(nrItem);
  942. }
  943. };
  944. void lstAddNonRecursiveListCopier(laListNonRecursiveRoot *root, laListHandle *oldHandle, laListHandle *newHandle, int sizeEach, laListNonRecursiveCopyFunc nrCpyFunc, void *custom_data, laListCustomDataRemover remover){
  945. laListNonRecursiveItem *nrItem = CreateNew(laListNonRecursiveItem);
  946. nrItem->CopyFunc = nrCpyFunc;
  947. lstCopyHandle(&nrItem->handle, oldHandle);
  948. nrItem->ToHandle = newHandle;
  949. nrItem->CustomData = custom_data;
  950. nrItem->remover = remover;
  951. nrItem->SizeEachNode = sizeEach;
  952. lstAppendItem(&root->NSItems, nrItem);
  953. };
  954. void *lstFindItem(const void *CmpData, laCompareFunc func, laListHandle *hList){
  955. laListItem *it;
  956. if (!CmpData || !hList) return 0;
  957. it = hList->pFirst;
  958. for (; it; it = it->pNext){
  959. if (func(it, CmpData)) return it;
  960. };
  961. return 0;
  962. };
  963. void lstCombineLists(laListHandle *dest, laListHandle *src){
  964. if ((!dest) || (!src)) return;
  965. if ((!dest->pFirst) && (!dest->pLast)){
  966. dest->pFirst = src->pFirst;
  967. dest->pLast = src->pLast;
  968. }else{
  969. if (src->pLast){
  970. ((laListItem *)src->pFirst)->pPrev = dest->pLast;
  971. ((laListItem *)dest->pLast)->pNext = src->pFirst;
  972. dest->pLast = src->pLast;
  973. }
  974. }
  975. src->pFirst = 0;
  976. src->pLast = 0;
  977. }
  978. void lstDestroyList(laListHandle *hlst){
  979. laListItem *li, *nextli;
  980. for (li = hlst->pFirst; li; li = nextli){
  981. nextli = li->pNext;
  982. memFree(li);
  983. }
  984. }
  985. void lstDestroyListA(laListHandle *hlst){
  986. laListItem *li, *nextli;
  987. for (li = hlst->pFirst; li; li = nextli){
  988. nextli = li->pNext;
  989. FreeMem(li);
  990. }
  991. }
  992. void lstDestroyList_User(laListHandle *hlst, laListDoFunc func){
  993. laListItem *it = hlst->pFirst;
  994. for (; it; it = it->pNext){
  995. func(it);
  996. FreeMem(it);
  997. }
  998. };
  999. void lstCopyList(laListHandle *hOldlst, laListHandle *hNewList, int SizeEachNode, laCopyListFunc func){
  1000. laListItem *li, *nextli, *newli;
  1001. for (li = hOldlst->pFirst; li; li = nextli){
  1002. newli = (laListItem *)CreateNew_Size(SizeEachNode);
  1003. func(li, newli);
  1004. lstAppendItem(hNewList, newli);
  1005. nextli = li->pNext;
  1006. }
  1007. }
  1008. void *lstReMatch(laListHandle *SearchHandle, laListHandle *CurrentHandle, void *ItemToFind){
  1009. laListItem *sl = 0, *rl = 0;
  1010. if (!SearchHandle || !CurrentHandle || !ItemToFind) return 0;
  1011. sl = SearchHandle->pFirst;
  1012. rl = CurrentHandle->pFirst;
  1013. while (sl && rl){
  1014. if (ItemToFind == sl){
  1015. return rl;
  1016. }else{
  1017. sl = sl->pNext;
  1018. rl = rl->pNext;
  1019. }
  1020. }
  1021. return 0;
  1022. }
  1023. //void* lstReMatchEx(laListHandle* SearchHandle, laListHandle* CurrentHandle, void* ItemToFind, MatcherFunc func){
  1024. // laListItem* sl = 0, *rl = 0;
  1025. //
  1026. // if (!SearchHandle || !CurrentHandle || !ItemToFind) return 0;
  1027. //
  1028. // sl = SearchHandle->pFirst; rl = CurrentHandle->pFirst;
  1029. //
  1030. // while (sl && rl){
  1031. // if (func(ItemToFind, sl)){
  1032. // return rl;
  1033. // }
  1034. // else{
  1035. // sl = sl->pNext;
  1036. // rl = rl->pNext;
  1037. // }
  1038. // }
  1039. // return 0;
  1040. //}
  1041. void lstAddElement(laListHandle *hlst, void *ext){
  1042. laElementListItem *eli = CreateNew(laElementListItem);
  1043. eli->Ext = ext;
  1044. lstAppendItem(hlst, eli);
  1045. }
  1046. void lstDestroyElementList(laListHandle *hlst){
  1047. laElementListItem *eli, *NextEli;
  1048. for (eli = hlst->pFirst; eli; eli = NextEli){
  1049. lstRemoveItem(hlst, eli);
  1050. NextEli = eli->Item.pNext;
  1051. FreeMem(eli);
  1052. }
  1053. }
  1054. uint16_t BKDRHash16bit(char* str){
  1055. unsigned int seed = 131, hash = 0;
  1056. while (*str) { hash = hash * seed + (*str++); }
  1057. return (hash & 0xFFFF);
  1058. }
  1059. void hsh65536Init(laHash65536** h){
  1060. if(!h) return; *h=calloc(1,sizeof(laHash65536));
  1061. }
  1062. void hshFree(laHash65536** h){
  1063. if(!h || !*h) return; free(*h); *h=0;
  1064. }
  1065. laListHandle* hsh65536DoHashLongPtr(laHash65536* hash, u64bit buckle) {
  1066. return &hash->Entries[(unsigned short)(buckle*13)];
  1067. }
  1068. laListHandle* hsh65536DoHashNUID(laHash65536* hash, char * NUID) {
  1069. u64bit Hash;
  1070. return &hash->Entries[BKDRHash16bit(NUID)];
  1071. }
  1072. unsigned char hsh256DoHashSTR(char *buckle){
  1073. int i, len = 0;
  1074. unsigned char rev = 0;
  1075. if (buckle) len = strlen(buckle);
  1076. for (i = 0; i < len; i++){
  1077. rev = rev * 31 + (unsigned char)buckle[i];
  1078. }
  1079. return (unsigned char)rev;
  1080. }
  1081. void hsh256InsertItemCSTR(laHash256 *hash, laListItem *li, char *buckle){
  1082. unsigned char a = hsh256DoHashSTR(buckle);
  1083. lstAppendItem(&hash->Entries[a], li);
  1084. };
  1085. void hsh256InsertItem(laHash256 *hash, laListItem *li, char buckle){
  1086. lstAppendItem(&hash->Entries[(unsigned char)buckle], li);
  1087. };
  1088. void hsh65536InsertItem(laHash65536 *hash, laListItem *li, long buckle){
  1089. lstAppendItem(&hash->Entries[(unsigned short)((buckle >> 10))], li);
  1090. //hsh256InsertItem(&hash->HashHandles[(char)((buckle >> 8) / 8)], li, (char)(buckle/8));
  1091. //printf("%d %d\n", (char)(buckle >> 5), (char)(buckle >> 6));
  1092. };
  1093. laListItem *hsh256FindItemSTR(laHash256 *hash, laCompareFunc func, char *buckle){
  1094. unsigned char hsh;
  1095. hsh = hsh256DoHashSTR(buckle);
  1096. //if(hash->Entries[hsh].pFirst == hash->Entries[hsh].pLast)
  1097. // return hash->Entries[hsh].pFirst;
  1098. laListItem* item=lstFindItem(buckle, func, &hash->Entries[hsh]);
  1099. return item;
  1100. }
  1101. //================================================================ [mem]
  1102. void* memGetHead(void* UserMem, int* HyperLevel){
  1103. laMemoryPoolPart **mpp = (laMemoryPoolPart**)(((char*)UserMem)-sizeof(void*));
  1104. if(!(*mpp)) return 0;
  1105. laMemoryPool* mp = (*mpp)->PoolRoot;
  1106. if(HyperLevel) *HyperLevel= mp->Hyperlevel;
  1107. if(mp->Hyperlevel==2) return ((char*)UserMem)-sizeof(laMemNodeHyper);
  1108. if(mp->Hyperlevel==1) return ((char*)UserMem)-sizeof(laMemNode);
  1109. if(mp->Hyperlevel==0) return ((char*)UserMem)-sizeof(laMemNode0);
  1110. return 0;
  1111. }
  1112. laListHandle* memGetUserList(void* UserMem){
  1113. int level; void* head=memGetHead(UserMem, &level);
  1114. if(level==2) return &((laMemNodeHyper*)head)->Users;
  1115. if(level==1) return &((laMemNode*)head)->Users;
  1116. return 0;
  1117. }
  1118. laMemoryPool *memInitPool(int NodeSize, int HyperLevel){
  1119. if (!NodeSize) return 0;
  1120. laMemoryPool *mph = calloc(1, sizeof(laMemoryPool));
  1121. mph->NodeSize = NodeSize;
  1122. mph->NextCount = 1;
  1123. mph->Hyperlevel = HyperLevel;
  1124. u8bit Buckle = NodeSize;
  1125. lstAppendItem(&MAIN.GlobalMemPool.Entries[Buckle], mph);
  1126. return mph;
  1127. }
  1128. laMemoryPoolPart *memNewPoolPart(laMemoryPool *mph){
  1129. if (!mph->NodeSize) return 0;
  1130. int MemNodeSize=(mph->Hyperlevel==0)?sizeof(laMemNode0):((mph->Hyperlevel==1)?sizeof(laMemNode):sizeof(laMemNodeHyper));
  1131. int PoolRefOffset=MemNodeSize-sizeof(void*);
  1132. int RealNodeSize = mph->NodeSize + MemNodeSize;
  1133. int NodeCount = mph->NextCount;
  1134. int TotalSize = sizeof(laMemoryPoolPart) + NodeCount * RealNodeSize;
  1135. laMemoryPoolPart *mp = calloc(1, TotalSize);
  1136. void *BeginMem = ((BYTE *)mp) + sizeof(laMemoryPoolPart);
  1137. mp->PoolRoot = mph;
  1138. mp->FreeMemoryNodes.pFirst = mp->FreeMemoryNodes.pLast = 0;
  1139. for (int i = 0; i < NodeCount; i++){
  1140. void* mpn = ((BYTE *)BeginMem) + RealNodeSize * i;
  1141. void** ref = ((BYTE *)mpn) + PoolRefOffset;
  1142. (*ref)=mp;
  1143. lstAppendItem(&mp->FreeMemoryNodes, mpn);
  1144. }
  1145. lstPushItem(&mph->Pools, mp);
  1146. return mp;
  1147. }
  1148. void *memAcquireH(laMemoryPool *Handle){
  1149. laMemoryPoolPart *mp = Handle->Pools.pFirst;
  1150. laMemNode *mpn;
  1151. if (!mp || !mp->FreeMemoryNodes.pFirst){
  1152. mp = memNewPoolPart(Handle);
  1153. }
  1154. if (!mp) return 0;
  1155. mpn = mp->FreeMemoryNodes.pFirst;
  1156. lstRemoveItem(&mp->FreeMemoryNodes, mpn);
  1157. mp->UsedCount++;
  1158. //lstAppendItem(&mp->MemoryNodes, mpn);
  1159. return mpn;
  1160. }
  1161. void *memAcquire_(int Size, int Hyper){
  1162. laMemoryPool *mp;
  1163. u8bit Buckle = Size;
  1164. laSpinLock(&MAIN.MemLock);
  1165. mp = MAIN.GlobalMemPool.Entries[Buckle].pFirst;
  1166. while (mp && (mp->NodeSize != Size || mp->Hyperlevel!=Hyper))
  1167. mp = mp->Item.pNext;
  1168. if (!mp) mp = memInitPool(Size, Hyper);
  1169. void* ret=memAcquireH(mp);
  1170. laSpinUnlock(&MAIN.MemLock);
  1171. return ret;
  1172. }
  1173. void *memAcquireSimple(int Size){
  1174. void *mpn = memAcquire_(Size, 0);
  1175. return ((char*)mpn)+sizeof(laMemNode0);
  1176. }
  1177. void *memAcquire(int Size){
  1178. laMemNode *mpn = memAcquire_(Size, 1);
  1179. void* mem = ((char*)mpn)+sizeof(laMemNode);
  1180. return mem;
  1181. }
  1182. void *memAcquireHyperNoAppend(int Size){
  1183. laMemNodeHyper *mpn = memAcquire_(Size, 2);
  1184. void* mem = ((char*)mpn)+sizeof(laMemNodeHyper);
  1185. memMakeHyperData(mpn);
  1186. return mem;
  1187. }
  1188. void *memAcquireHyper(int Size){
  1189. laMemNodeHyper *mpn = memAcquire_(Size, 2);
  1190. void* mem = ((char*)mpn)+sizeof(laMemNodeHyper);
  1191. memMakeHyperData(mpn);
  1192. laListHandle* l=hsh65536DoHashNUID(&MAIN.DBInst2,mpn->NUID.String);
  1193. lstAppendItem(l,mpn);
  1194. return mem;
  1195. }
  1196. void memFree(void *Data){
  1197. if (!Data) return;
  1198. int level; void* head = memGetHead(Data, &level);
  1199. laMemoryPoolPart *mp;
  1200. if(level==2) { mp = ((laMemNodeHyper*)head)->InPool; laDataBlockNoLongerExists(Data,&((laMemNodeHyper*)head)->Users);
  1201. laListHandle* l=hsh65536DoHashNUID(&MAIN.DBInst2,((laMemNodeHyper*)head)->NUID.String); lstRemoveItem(l,head);}
  1202. if(level==1) { mp = ((laMemNode*)head)->InPool; laDataBlockNoLongerExists(Data,&((laMemNode*)head)->Users); }
  1203. if(level==0) { mp = ((laMemNode0*)head)->InPool; }
  1204. laMemoryPool *mph = mp->PoolRoot;
  1205. laSpinLock(&MAIN.MemLock);
  1206. //lstRemoveItem(&mp->MemoryNodes, head);
  1207. mp->UsedCount--;
  1208. void* head_except_item = ((char*)head)+sizeof(laListItem);
  1209. //memset(head_except_item, 0, ((level==2)?sizeof(laMemNodeHyper):((level==1)?sizeof(laMemNode):sizeof(laMemNode0)))+mph->NodeSize-sizeof(laListItem));
  1210. lstAppendItem(&mp->FreeMemoryNodes, head);
  1211. memset(Data, 0, mph->NodeSize);
  1212. MAIN.ByteCount -= mph->NodeSize;
  1213. if (!mp->UsedCount){
  1214. lstRemoveItem(&mph->Pools, mp);
  1215. FreeMem(mp);
  1216. }
  1217. laSpinUnlock(&MAIN.MemLock);
  1218. //if (!mph->Pools.pFirst) {
  1219. // mph->CountPerPool = 0;
  1220. // mph->NodeSize = 0;
  1221. //}
  1222. }
  1223. void memDestroyPool(laMemoryPool *mph){
  1224. laMemoryPool *mp;
  1225. while ((mp = lstPopItem(&mph->Pools))){
  1226. FreeMem(mp);
  1227. }
  1228. FreeMem(mph);
  1229. }
  1230. // Leave memory in an temporary place and if when push difference these are still not acquired, free them.
  1231. void memLeave(void *Data){
  1232. laListHandle* l=hsh65536DoHashLongPtr(MAIN.DBInstMemLeft,Data); lstAppendPointer(l,Data);
  1233. }
  1234. void memTake(void *Data){
  1235. laListHandle* l=hsh65536DoHashLongPtr(MAIN.DBInstMemLeft,Data); lstRemovePointer(l,Data);
  1236. }
  1237. void memFreeRemainingLeftNodes(){
  1238. laListHandle* l; void* m;
  1239. for(int i=0;i<65536;i++){
  1240. l=&MAIN.DBInstMemLeft->Entries[i]; while(m=lstPopPointer(l)){ memFree(m);
  1241. #ifdef DEBUG
  1242. printf("left freed %x\n",m);
  1243. #endif
  1244. }
  1245. }
  1246. }
  1247. void memNoLonger(){
  1248. for(int i=0;i<256;i++){
  1249. laMemoryPool* mp; while(mp=lstPopItem(&MAIN.GlobalMemPool.Entries[i])){ memDestroyPool(mp); }
  1250. }
  1251. }
  1252. laStaticMemoryPoolNode *memNewStaticPool(laStaticMemoryPool *smp){
  1253. laStaticMemoryPoolNode *smpn = calloc(1, LA_MEMORY_POOL_128MB);
  1254. smpn->UsedByte = sizeof(laStaticMemoryPoolNode);
  1255. lstPushItem(&smp->Pools, smpn);
  1256. return smpn;
  1257. }
  1258. void *memStaticAcquire(laStaticMemoryPool *smp, int size){
  1259. laStaticMemoryPoolNode *smpn = smp->Pools.pFirst;
  1260. void *ret;
  1261. if (!smpn || (smpn->UsedByte + size) > LA_MEMORY_POOL_128MB) smpn = memNewStaticPool(smp);
  1262. ret = ((BYTE *)smpn) + smpn->UsedByte;
  1263. smpn->UsedByte += size;
  1264. return ret;
  1265. }
  1266. void *memStaticAcquireThread(laStaticMemoryPool *smp, int size){
  1267. laStaticMemoryPoolNode *smpn = smp->Pools.pFirst;
  1268. void *ret;
  1269. //laSpinLock(&smp->csMem);
  1270. if (!smpn || (smpn->UsedByte + size) > LA_MEMORY_POOL_128MB) smpn = memNewStaticPool(smp);
  1271. ret = ((BYTE *)smpn) + smpn->UsedByte;
  1272. smpn->UsedByte += size;
  1273. //laSpinUnlock(&smp->csMem);
  1274. return ret;
  1275. }
  1276. void *memStaticDestroy(laStaticMemoryPool *smp){
  1277. laStaticMemoryPoolNode *smpn;
  1278. void *ret;
  1279. while (smpn = lstPopItem(&smp->Pools)){
  1280. FreeMem(smpn);
  1281. }
  1282. smp->EachSize = 0;
  1283. return ret;
  1284. }
  1285. void la_ReferencedBlockDeleted(void* This, laItemUserLinker* iul){
  1286. void** user=iul->Pointer.p; if(*user==This){ (*user)=0; } laStopUsingDataBlock(iul->Additional, 0, This); // <<< should always remove.
  1287. }
  1288. void la_ReferrerDeleted(void* This, laItemUserLinker* iul){
  1289. void* instance=iul->Pointer.p; if(instance!=This){ laStopUsingDataBlock(instance, 0, This); }
  1290. }
  1291. void memAssignRef(void* This, void** ptr, void* instance){
  1292. laItemUserLinker* iul;
  1293. if((!This)||(!ptr)) return;
  1294. if(instance){
  1295. laItemUserLinker*iul=laUseDataBlock(instance, 0, 0, ptr, la_ReferencedBlockDeleted, 0); if(iul){iul->Additional=This;}
  1296. laUseDataBlock(This, 0, 0, instance, la_ReferrerDeleted, 0);
  1297. }else{
  1298. laStopUsingDataBlock((*ptr), 0, This);
  1299. laStopUsingDataBlock(This, 0, (*ptr));
  1300. }
  1301. (*ptr)=instance;
  1302. }
  1303. void memAssignRefSafe(laSubProp* sp, void* This, void** ptr, void* instance){
  1304. laPropContainer* pc=sp?la_EnsureSubTarget(sp,instance):0;
  1305. if(pc&&!pc->OtherAlloc) memAssignRef(This,ptr,instance);
  1306. else (*ptr)=instance;
  1307. }
  1308. //=======================================================================[str]
  1309. char *strGetNextString(char **pivot, char *NextMark){
  1310. int lenth = 0;
  1311. char *countP = *pivot;
  1312. char *result = 0;
  1313. int FloatArg = 0;
  1314. int i,advance;
  1315. if (**pivot == U'\0') return 0;
  1316. if (*NextMark == U'~') FloatArg = 1;
  1317. // container@identifier=window container#window contianer%
  1318. int UC=1;
  1319. while (!lenth){
  1320. for (countP; *countP != U'.' && *(*pivot) != U'\0' && UC && *countP && *countP != U'@' && *countP != U'=' && *countP != U'#' && *countP != U'$';){
  1321. if((*countP)=='\\'){
  1322. countP++; lenth++; }
  1323. UC = laToUnicode(countP, &advance);
  1324. lenth+=advance;
  1325. countP+=advance;
  1326. }
  1327. if (lenth || (*countP) == 0) break;
  1328. (*pivot)++; countP++;
  1329. }
  1330. *NextMark = (*pivot)[lenth];
  1331. if (!(*NextMark)) *NextMark = U'.';
  1332. if (lenth){
  1333. result = CreateNewBuffer(char, lenth + 1);
  1334. int pi=0; for (i = 0; i < lenth; i++){
  1335. if((*pivot)[i]=='\\'){ continue; }
  1336. result[pi] = (*pivot)[i]; pi++;
  1337. }
  1338. result[pi] = U'\0';
  1339. if ((*pivot)[lenth] == U'\0') *pivot = &((*pivot)[lenth]);
  1340. else
  1341. (*pivot) += lenth + 1;
  1342. return result;
  1343. }else{
  1344. return 0;
  1345. }
  1346. };
  1347. int strGetStringTerminateBy(char *content, char terminator, char *Out){
  1348. int Ofst = 0;
  1349. int Skip = 0;
  1350. int i = 0, advance;
  1351. if ((!content) || (*content == U'\0')) return 0;
  1352. int UC;
  1353. for (Ofst; content[Ofst] != terminator && content[Ofst] != U'\0'; ){
  1354. UC = laToUnicode(&content[Ofst], &advance);
  1355. for(int a=0;a<advance;a++){
  1356. Out[i] = content[Ofst];
  1357. Ofst++; i++;
  1358. }
  1359. }
  1360. Out[i] = 0;
  1361. return i;
  1362. };
  1363. char *strGetNewStringTerminateBy_PivotOver(char *content, char terminator, char **NewPivot, int IgnoreSpace){
  1364. int Ofst = 0;
  1365. int Skip = 0;
  1366. int i = 0;
  1367. char *NewString;
  1368. if (!content || *content == U'\0') return 0;
  1369. if (IgnoreSpace) for (i; content[i] == U' '; i++) ;
  1370. int UC,advance;
  1371. for (Ofst; content[Ofst] != terminator && content[Ofst] != U'\0';){
  1372. UC = laToUnicode(&content[Ofst], &advance);
  1373. Ofst+=advance;
  1374. }
  1375. NewString = CreateNewBuffer(char, Ofst + 1 - i);
  1376. memcpy(NewString, &content[i], sizeof(char) * (Ofst - i));
  1377. NewString[Ofst - i] = U'\0';
  1378. *NewPivot = content[Ofst]?&content[Ofst + 1]:&content[Ofst];
  1379. return NewString;
  1380. };
  1381. int strHeadOfStringMatch(char *Str, char *SubStr){
  1382. int len = strlen(SubStr);
  1383. int i = 0;
  1384. for (i; i < len; i++){
  1385. if (Str[i] != SubStr[i]) return 0;
  1386. }
  1387. return 1;
  1388. }
  1389. int strSkipSegmet(char **pivot, char *content){
  1390. if (!pivot || !(*pivot) || !(*(*pivot)) || !content) return 0;
  1391. if (strHeadOfStringMatch(*pivot, content)){
  1392. (*pivot) += strlen(content);
  1393. return 1;
  1394. }
  1395. return 0;
  1396. }
  1397. char *strGetLastSegment(char *Content, char Seperator){
  1398. char *p = Content;
  1399. char *pn = Content;
  1400. while (1){
  1401. while (*pn != Seperator){
  1402. if (!(*pn)) return p;
  1403. pn++;
  1404. }
  1405. pn++;
  1406. p = pn;
  1407. }
  1408. }
  1409. void strDiscardLastSegmentSeperateBy(char *Content, char Seperator){
  1410. char *p = Content;
  1411. char *pn = Content;
  1412. while (1){
  1413. while (*pn != Seperator){
  1414. if (!(*pn)){
  1415. *p = 0;
  1416. return;
  1417. }
  1418. pn++;
  1419. }
  1420. p = pn;
  1421. pn++;
  1422. }
  1423. }
  1424. void strDiscardSameBeginningSeperatedBy(char *s1, char *s2, char **Result1, char **Result2, char Seperator){
  1425. int i = 0;
  1426. int p = 0;
  1427. while (s1[i] == s2[i]){
  1428. i++;
  1429. if (s1[i] == Seperator) p = i;
  1430. if (!s1[i]){
  1431. p = i;
  1432. break;
  1433. }
  1434. if (!s2[i]){
  1435. p = i;
  1436. break;
  1437. }
  1438. }
  1439. *Result1 = &s1[p];
  1440. *Result2 = &s2[p];
  1441. }
  1442. int strCountSegmentSeperateBy(char *Content, char Seperator){
  1443. char *p = Content;
  1444. char *pn = Content;
  1445. int c = Content[0] ? (Content[0] == Seperator ? 0 : 1) : 0;
  1446. while (1){
  1447. while (*pn != Seperator){
  1448. if (!(*pn)){
  1449. if ((*p) == Seperator) c--;
  1450. return c;
  1451. }
  1452. p = pn;
  1453. pn++;
  1454. }
  1455. c++;
  1456. pn++;
  1457. }
  1458. return c;
  1459. }
  1460. void strMakeDifferentName(char *Target){
  1461. char *p = strGetLastSegment(Target, '.');
  1462. int Temp;
  1463. if (!sscanf(p, "%d", &Temp)){
  1464. int l = strlen(p);
  1465. if (p[l - 1] != U'.') strcat(p, ".");
  1466. strPrintIntAfter(Target, 0, 001);
  1467. }else{
  1468. sprintf(p, "%d", Temp + 1);
  1469. };
  1470. }
  1471. void strReplaceCharacter(char *Str, char Find, char Replace){
  1472. char *p = Str;
  1473. if (!p) return;
  1474. while (*p){
  1475. if (*p == Find) *p = Replace;
  1476. p++;
  1477. }
  1478. }
  1479. void strToUpper(char *Str){
  1480. char *p = Str; if (!p) return;
  1481. while (*p){
  1482. if (*p >= 'a' && *p <= 'z') *p += 'A' - 'a';
  1483. p++;
  1484. }
  1485. }
  1486. void strToLower(char *Str){
  1487. char *p = Str; if (!p) return;
  1488. while (*p){
  1489. if (*p >= 'A' && *p <= 'Z') *p -= 'A' - 'a';
  1490. p++;
  1491. }
  1492. }
  1493. int tolowerGuarded(int a) {
  1494. if (a >= 'A' && a <= 'Z') a -= 'A' - 'a';
  1495. return a;
  1496. }
  1497. laStringSplitor *strSplitPath(const char *path,char terminator){
  1498. laStringPart *sp;
  1499. laStringSplitor *ss;
  1500. char *pivot = path;
  1501. char *temp_result;
  1502. char Type = terminator?terminator:'.';
  1503. char NextType = '.';
  1504. if (!path || !path[0]) return 0;
  1505. ss = memAcquireSimple(sizeof(laStringSplitor));
  1506. while (temp_result = strGetNextString(&pivot, &NextType)){
  1507. if (*temp_result != U'\0'){
  1508. sp = memAcquireSimple(sizeof(laStringPart));
  1509. sp->Content = temp_result;
  1510. lstAppendItem(&ss->parts, sp);
  1511. ss->NumberParts += 1;
  1512. if (NextType == U'$') sp->Type = U'$';
  1513. else
  1514. sp->Type = Type;
  1515. if (sp->Type == U'='){
  1516. if (sp->Content[0] >= U'0' && sp->Content[0] <= 9){
  1517. sscanf(sp->Content, "%d", &sp->IntValue);
  1518. }
  1519. }
  1520. if (NextType == U'$') NextType = U'.';
  1521. Type = NextType;
  1522. }
  1523. }
  1524. if (ss->NumberParts == 0){
  1525. strDestroyStringSplitor(&ss);
  1526. return 0;
  1527. }
  1528. return ss;
  1529. };
  1530. void DF_ClearStingParts(laStringPart *sp){
  1531. FreeMem(sp->Content);
  1532. };
  1533. int strDestroyStringSplitor(laStringSplitor **ss){
  1534. if (!(*ss)) return 0;
  1535. lstForAllItemsDo(DF_ClearStingParts, &(*ss)->parts);
  1536. lstDestroyList(&(*ss)->parts);
  1537. memFree(*ss);
  1538. *ss = 0;
  1539. return 1;
  1540. }
  1541. char * strSub(const char *input, const char *substring, const char *replace){
  1542. int number_of_matches = 0;
  1543. size_t substring_size = strlen(substring), replace_size = strlen(replace), buffer_size;
  1544. char *buffer, *bp, *ip;
  1545. if (substring_size){
  1546. ip = strstr(input, substring);
  1547. while (ip != NULL){
  1548. number_of_matches++;
  1549. ip = strstr(ip+substring_size, substring);
  1550. }
  1551. }
  1552. else number_of_matches = strlen (input) + 1;
  1553. buffer_size = strlen(input) + number_of_matches*(replace_size - substring_size) + 1;
  1554. if ((buffer = ((char *) malloc(buffer_size))) == NULL){
  1555. return NULL;
  1556. }
  1557. bp = buffer;
  1558. ip = strstr(input, substring);
  1559. while ((ip != NULL) && (*input != '\0')){
  1560. if (ip == input){
  1561. memcpy (bp, replace, replace_size+1);
  1562. bp += replace_size;
  1563. if (substring_size)input += substring_size;
  1564. else*(bp++) = *(input++);
  1565. ip = strstr(input, substring);
  1566. }
  1567. else while (input != ip) *(bp++) = *(input++);
  1568. }
  1569. if (substring_size)strcpy (bp, input);
  1570. else memcpy (bp, replace, replace_size+1);
  1571. return buffer;
  1572. }
  1573. char buff[128]={0};
  1574. int strMakeInstructions(laStringSplitor **result, const char *content){
  1575. laStringPart *sp;
  1576. laStringSplitor *ss = *result;
  1577. char *pivot = content;
  1578. unsigned char *temp_result;
  1579. if (!content || !content[0]) return 0;
  1580. if (!ss) ss = *result = memAcquireSimple(sizeof(laStringSplitor));
  1581. while (temp_result = strGetNewStringTerminateBy_PivotOver(pivot, '=', &pivot, 0)){
  1582. if (*temp_result != U'\0'){
  1583. sp = memAcquireSimple(sizeof(laStringPart));
  1584. sp->Content = temp_result;
  1585. lstAppendItem(&ss->parts, sp);
  1586. ss->NumberParts += 1;
  1587. }
  1588. temp_result = strGetNewStringTerminateBy_PivotOver(pivot, ';', &pivot, 0);
  1589. if (!temp_result) break;
  1590. if (*temp_result != U'\0'){
  1591. sp = memAcquireSimple(sizeof(laStringPart));
  1592. sp->Content = temp_result;
  1593. lstAppendItem(&ss->parts, sp);
  1594. ss->NumberParts += 1;
  1595. if (temp_result[0] >= U'0' && temp_result[0] <= U'9' || temp_result[0]>=128){
  1596. sscanf(temp_result, "%d", &sp->IntValue);
  1597. sscanf(temp_result, "%lf", &sp->FloatValue);
  1598. }
  1599. }
  1600. }
  1601. if (ss->NumberParts == 0){
  1602. strDestroyStringSplitor(&ss);
  1603. return 0;
  1604. }
  1605. return 1;
  1606. }
  1607. laStringPart *strGetArgument(laStringSplitor *ss, const char *content){
  1608. laStringPart *sp;
  1609. if (!ss) return 0;
  1610. for (sp = ss->parts.pFirst; sp; sp = sp->Item.pNext ? ((laListItem *)sp->Item.pNext)->pNext : 0){
  1611. if (strSame(content, sp->Content)) return sp->Item.pNext;
  1612. }
  1613. return 0;
  1614. }
  1615. char *strGetArgumentString(laStringSplitor *ss, const char *content){
  1616. laStringPart *sp;
  1617. if (!ss) return 0;
  1618. for (sp = ss->parts.pFirst; sp; sp = sp->Item.pNext ? ((laListItem *)sp->Item.pNext)->pNext : 0){
  1619. if (strSame(content, sp->Content)) return sp->Item.pNext ? ((laStringPart *)sp->Item.pNext)->Content : 0;
  1620. }
  1621. return 0;
  1622. }
  1623. int strArgumentMatch(laStringSplitor *ss, const char *id, const char *value){
  1624. laStringPart *sp;
  1625. if (!ss) return 0;
  1626. for (sp = ss->parts.pFirst; sp; sp = sp->Item.pNext ? ((laListItem *)sp->Item.pNext)->pNext : 0){
  1627. if (strSame(id, sp->Content)) return (strSame(((laStringPart *)sp->Item.pNext)->Content, value));
  1628. }
  1629. return 0;
  1630. }
  1631. int strGetIntSimple(const char *content){
  1632. int a;
  1633. sscanf(content, "%d", &a);
  1634. return a;
  1635. }
  1636. real strGetFloatSimple(const char *content){
  1637. real a;
  1638. sscanf(content, "%lf", &a);
  1639. return a;
  1640. }
  1641. void strConvInt_CString(int src, char *dest, int lenth){
  1642. sprintf(dest, "%d", src);
  1643. };
  1644. void strConvFloat_CString(real src, char *dest, int lenth){
  1645. sprintf(dest, "%lf", src);
  1646. };
  1647. void strCopyFull(char *dest, const char *src){
  1648. if (src && dest) strcpy(dest, src);
  1649. }
  1650. void strCopySized(char *dest, int LenthLim,const char *src){
  1651. if (src && dest) strcpy(dest, src);
  1652. }
  1653. void strPrintFloatAfter(char *dest, int LenthLim, int bits, real data){
  1654. char temp[64]={0};
  1655. sprintf(temp, "%.*lf", bits, data);
  1656. strcat(dest, temp);
  1657. }
  1658. void strPrintIntAfter(char *dest, int LenthLim, int data){
  1659. char temp[64]={0};
  1660. sprintf(&temp[0], "%d", data);
  1661. strcat(dest, temp);
  1662. }
  1663. void strEscapePath(char* OutCanBeSame, char* path){
  1664. char t[256]={0}; int ti=0;
  1665. for(int i=0;path[i];i++,ti++){
  1666. if(path[i]=='.'){ t[ti]='\\'; ti++; }
  1667. t[ti]=path[i];
  1668. }
  1669. strcpy(OutCanBeSame,t);
  1670. }
  1671. int strSame(const char *src, const char *dest){
  1672. return (src && dest && !strcmp(src, dest));
  1673. }
  1674. void strSafeDestroy(laSafeString **ss){
  1675. if (!*ss) return; lstRemoveItem(&SSC.SafeStrings, *ss);
  1676. if((*ss)->Ptr) memFree((*ss)->Ptr);
  1677. memFree(*ss); *ss=0;
  1678. }
  1679. void strSafeSet(laSafeString **ss, const char *Content){
  1680. int len;
  1681. if (!Content||!Content[0]){ strSafeDestroy(ss); return; }
  1682. len = strlen(Content);
  1683. if (len < 1) return;
  1684. if (*ss){
  1685. char* mem=memAcquireSimple(sizeof(char)*(len+1));
  1686. strcpy(mem, Content);
  1687. memFree((*ss)->Ptr);
  1688. (*ss)->Ptr=mem;
  1689. return;
  1690. }
  1691. (*ss) = memAcquireSimple(sizeof(laSafeString));
  1692. (*ss)->Ptr = memAcquireSimple(sizeof(char)*(len+1));
  1693. strcpy((*ss)->Ptr, Content);
  1694. lstAppendItem(&SSC.SafeStrings, *ss);
  1695. }
  1696. void strSafeAppend(laSafeString **ss, const char *Content){
  1697. if(!ss || !(*ss) || !Content){ strSafeSet(ss, Content); return; }
  1698. int OrigLen=strlen((*ss)->Ptr), ContentLen=strlen(Content);
  1699. char* mem=memAcquireSimple(sizeof(char)*(OrigLen+ContentLen+1));
  1700. memcpy(mem, (*ss)->Ptr, sizeof(char)*OrigLen);
  1701. memcpy(mem+sizeof(char)*OrigLen, Content, sizeof(char)*ContentLen);
  1702. mem[OrigLen+ContentLen]=0;
  1703. memFree((*ss)->Ptr);
  1704. (*ss)->Ptr=mem;
  1705. }
  1706. void strSafePrint(laSafeString **ss, const char *Format, ...){
  1707. char content[1024]; va_list va; va_start(va, Format); vsprintf(content, Format, va); va_end(va);
  1708. strSafeAppend(ss,content);
  1709. }
  1710. void strSafePrintV(laSafeString **ss, const char *Format, va_list args){
  1711. char content[1024]; va_list va; vsprintf(content, Format, args);
  1712. strSafeAppend(ss,content);
  1713. }
  1714. void strSafeDump(){
  1715. laSafeString*ss;
  1716. while(ss=lstPopItem(&SSC.SafeStrings)){
  1717. //if(ss->Ptr) printf("[String not freed] \"%s\"\n", ss->Ptr);
  1718. }
  1719. }
  1720. void strBeginEdit(laStringEdit **se, char *FullStr){
  1721. char *p = FullStr;
  1722. char buf[1024];
  1723. laStringEdit *nse = CreateNew(laStringEdit);
  1724. if(*se){ memcpy(nse,*se,sizeof(laStringEdit)); nse->Lines.pFirst=nse->Lines.pLast=0; nse->TotalLines=0; }
  1725. strEndEdit(se, 1);
  1726. nse->_BeginLine = -1; nse->_BeginBefore = -1;
  1727. if (FullStr && FullStr[0]){
  1728. while ((*p)){
  1729. laStringLine *sl = memAcquireSimple(sizeof(laStringLine));
  1730. p += strGetStringTerminateBy(p, '\n', buf);
  1731. strToUnicode(sl->Buf, buf);
  1732. lstAppendItem(&nse->Lines, sl); nse->TotalLines++;
  1733. if(*p){ p+=1; }
  1734. }
  1735. }
  1736. if (!nse->Lines.pFirst){
  1737. laStringLine *sl = memAcquireSimple(sizeof(laStringLine));
  1738. lstAppendItem(&nse->Lines, sl); nse->TotalLines=1;
  1739. }
  1740. laStringLine *sl = strGetCursorLine(nse, 0); int len=strlen(sl->Buf);
  1741. if(len<nse->CursorBefore){ nse->CursorBefore=len; }
  1742. *se=nse;
  1743. }
  1744. char* strGetEditString(laStringEdit *se, int SelectionOnly){
  1745. if(!se) return 0; char* result=0; int next=0, max=0, len=0;
  1746. arrEnsureLength(&result, 0, &max, sizeof(char)); int NextChar=0;
  1747. int Line=0, starti=0, endat=INT_MAX;
  1748. for(laStringLine* sl=se->Lines.pFirst;sl;sl=sl->Item.pNext,Line++){
  1749. starti=0;
  1750. if(SelectionOnly && Line<se->BeginLine){ continue; }
  1751. if(SelectionOnly && Line==se->BeginLine){ starti=se->BeginBefore; }
  1752. int tlen=strlenU(&sl->Buf[starti]); int Extra=sl->Item.pNext?2:1;
  1753. arrEnsureLength(&result, (len+tlen)*4+Extra, &max, sizeof(char));
  1754. if(SelectionOnly && Line==se->EndLine){ endat=NextChar+se->EndBefore-starti; }
  1755. NextChar+=strToUTF8Lim(&result[NextChar], &sl->Buf[starti], endat); len+=tlen;
  1756. if(Extra==2){ result[NextChar]='\n'; NextChar+=1; }
  1757. if(SelectionOnly && Line==se->EndLine){ break; }
  1758. }
  1759. return result;
  1760. }
  1761. char* strEndEdit(laStringEdit **se, int FreeString){
  1762. char *p=0; laStringLine *sl, *NextSl;
  1763. if (!se || !(*se)) return 0;
  1764. p=strGetEditString(*se, 0);
  1765. while (sl=lstPopItem(&(*se)->Lines)){ memFree(sl); }
  1766. FreeMem(*se); *se=0;
  1767. if(FreeString && p){ free(p); p=0; }
  1768. return p;
  1769. }
  1770. void strSetEditViewRange(laStringEdit* se, int Lines, int Cols){
  1771. se->ViewHeight = Lines; se->ViewWidth = Cols;
  1772. }
  1773. void strEnsureCursorVisible(laStringEdit* se){
  1774. if(!se->ViewHeight || !se->ViewWidth || se->CursorLine<0 || se->CursorBefore<0 ){return;}
  1775. if(se->CursorLine>se->ViewHeight+se->ViewStartLine-1){ se->ViewStartLine=se->CursorLine-se->ViewHeight+1; }
  1776. if(se->CursorLine<se->ViewStartLine){ se->ViewStartLine=se->CursorLine; }
  1777. if(se->CursorBefore>se->ViewStartCol+se->ViewWidth-1){ se->ViewStartCol=se->CursorBefore-se->ViewWidth+1; }
  1778. if(se->CursorBefore<se->ViewStartCol){ se->ViewStartCol=se->CursorBefore; }
  1779. }
  1780. void strRemoveLine(laStringEdit *se, laStringLine *sl){
  1781. lstRemoveItem(&se->Lines, sl);
  1782. memFree(sl); se->TotalLines--;
  1783. }
  1784. void strRemoveLineI(laStringEdit *se, int LineIndex){
  1785. int i = 0;
  1786. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1787. while (sl){
  1788. NextSl = sl->Item.pNext;
  1789. if (i == LineIndex){ strRemoveLine(se, sl); break; }
  1790. i++;
  1791. sl = NextSl;
  1792. }
  1793. }
  1794. void strSetCursor(laStringEdit *se, int LineIndex, int BeforeIndex){
  1795. int maxbefore;
  1796. if (!se) return;
  1797. if(LineIndex<0){LineIndex=0;}
  1798. se->CursorLine = LineIndex;
  1799. maxbefore = strlenU(strGetCursorLine(se, &se->CursorLine)->Buf);
  1800. BeforeIndex = BeforeIndex < 0 ? 0 : BeforeIndex > maxbefore ? maxbefore : BeforeIndex;
  1801. se->CursorBefore = BeforeIndex;
  1802. se->BeginLine = -1;
  1803. se->BeginBefore = -1;
  1804. se->EndLine = -1;
  1805. se->EndBefore = -1;
  1806. strEnsureCursorVisible(se);
  1807. }
  1808. void strMoveCursor(laStringEdit *se, int Left, int Select){
  1809. int maxbefore;
  1810. int BeforeIndex;
  1811. int width = 1;
  1812. laStringLine *sl;
  1813. if (!se) return;
  1814. if(Select){ strLazySelect(se); } else { strCancelSelect(se); }
  1815. sl = strGetCursorLine(se, 0);
  1816. maxbefore = strlenU(sl->Buf);
  1817. BeforeIndex = se->CursorBefore - (Left ? 1 : -1);
  1818. if(BeforeIndex<0){
  1819. if(se->CursorLine>0) strSetCursor(se, se->CursorLine-1, INT_MAX);
  1820. }elif(BeforeIndex>maxbefore && se->CursorLine<se->TotalLines-1){
  1821. if(se->CursorLine>0) strSetCursor(se, se->CursorLine+1, 0);
  1822. }else{
  1823. se->CursorBefore = BeforeIndex>=maxbefore?maxbefore:BeforeIndex;
  1824. }
  1825. se->CursorPreferBefore = se->CursorBefore;
  1826. se->BeginLine = -1;
  1827. se->BeginBefore = -1;
  1828. se->EndLine = -1;
  1829. se->EndBefore = -1;
  1830. if(Select){ strEndSelect(se); }
  1831. strEnsureCursorVisible(se);
  1832. }
  1833. void strMoveCursorLine(laStringEdit *se, int Up, int Select){
  1834. int Line, maxbefore, LastIndex=-1;
  1835. laStringLine *sl;
  1836. if (!se) return;
  1837. if(Select){ strLazySelect(se); } else { strCancelSelect(se); }
  1838. Line=se->CursorLine - (Up? 1:-1);
  1839. if(Line<0) {Line=0;}
  1840. se->CursorLine = Line;
  1841. sl = strGetCursorLine(se, &LastIndex);
  1842. if(LastIndex>=0){ se->CursorLine = LastIndex; se->CursorPreferBefore=10000; }
  1843. maxbefore = strlenU(sl->Buf);
  1844. se->CursorBefore = se->CursorPreferBefore;
  1845. if(se->CursorBefore>maxbefore){ se->CursorBefore = maxbefore; }
  1846. if(LastIndex>=0){se->CursorPreferBefore=se->CursorBefore;}
  1847. if(Select){ strEndSelect(se); }
  1848. strEnsureCursorVisible(se);
  1849. }
  1850. int strHasSelection(laStringEdit* se){
  1851. return se->BeginBefore!=se->EndBefore||se->BeginLine!=se->EndLine;
  1852. }
  1853. void strCancelSelect(laStringEdit *se){
  1854. if (!se) return;
  1855. se->_BeginLine = -1;
  1856. se->_BeginBefore = -1;
  1857. se->BeginLine = -1;
  1858. se->EndLine = -1;
  1859. se->BeginBefore = -1;
  1860. se->EndBefore = -1;
  1861. }
  1862. void strLazySelect(laStringEdit *se){
  1863. if (!se || se->_BeginLine>=0) return;
  1864. se->_BeginLine = TNS_MAX2(se->CursorLine,0);
  1865. se->_BeginBefore = se->CursorBefore;
  1866. }
  1867. void strEndSelect(laStringEdit *se){
  1868. if (!se) return;
  1869. se->_EndLine = se->CursorLine;
  1870. se->_EndBefore = se->CursorBefore;
  1871. se->BeginLine = se->_BeginLine;
  1872. se->EndLine = se->_EndLine;
  1873. se->BeginBefore = se->_BeginBefore;
  1874. se->EndBefore = se->_EndBefore;
  1875. if(se->BeginLine>se->EndLine || (se->BeginLine==se->EndLine && se->BeginBefore>se->EndBefore))
  1876. { LA_SWAP(int,se->BeginLine,se->EndLine); LA_SWAP(int,se->BeginBefore,se->EndBefore); }
  1877. }
  1878. void strSelectLineAll(laStringEdit *se){
  1879. if (!se) return;
  1880. laStringLine *sl;
  1881. int len;
  1882. if (se->CursorLine == -1) sl = strGetBeginLine(se);
  1883. else
  1884. sl = strGetCursorLine(se, 0);
  1885. len = strlenU(sl->Buf);
  1886. se->EndBefore = len; se->EndLine=0;
  1887. se->BeginBefore = 0; se->BeginLine=0;
  1888. se->CursorBefore = len;
  1889. se->CursorLine = 0;
  1890. }
  1891. void strDeselectAll(laStringEdit *se){
  1892. if (!se) return;
  1893. laStringLine *sl;
  1894. int len;
  1895. if (se->CursorLine == -1) sl = strGetBeginLine(se);
  1896. else
  1897. sl = strGetCursorLine(se, 0);
  1898. len = strlenU(sl->Buf);
  1899. se->EndBefore = -1;
  1900. se->BeginBefore = -1;
  1901. se->BeginLine = -1;
  1902. se->EndLine = -1;
  1903. se->CursorBefore = len;
  1904. se->CursorLine = -1;
  1905. }
  1906. void strPanFoward(uint32_t *str, int Before, int Offset){
  1907. int len = strlenU(str);
  1908. int i = len + 1;
  1909. for (i; i >= Before; i--){
  1910. str[i + Offset] = str[i];
  1911. }
  1912. }
  1913. void strSquishBackward(uint32_t *str, int Before, int EndBefore){
  1914. int len = strlenU(str);
  1915. int i = Before;
  1916. int Offset = Before - EndBefore;
  1917. if (Before <= 0) return;
  1918. for (i; i <= len; i++){
  1919. str[i - Offset] = str[i];
  1920. }
  1921. }
  1922. void strClearSelection(laStringEdit *se){
  1923. //if (se->EndLine == -1) return;
  1924. if (se->BeginLine != se->EndLine){
  1925. int i = 0; int RemovedLines=0;
  1926. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1927. while (sl){
  1928. NextSl = sl->Item.pNext;
  1929. if (i == se->BeginLine){
  1930. sl->Buf[se->BeginBefore] = U'\0';
  1931. }else if (i > se->BeginLine && i < se->EndLine){
  1932. strRemoveLine(se, sl); RemovedLines++;
  1933. }else if (i == se->EndLine){
  1934. strSquishBackward(sl->Buf, se->EndBefore, 0);
  1935. se->CursorLine = i-RemovedLines;
  1936. se->CursorBefore = 0;
  1937. se->BeginLine = -1;
  1938. se->BeginBefore = -1;
  1939. se->EndLine = -1;
  1940. se->EndBefore = -1;
  1941. strBackspace(se);
  1942. }
  1943. if (i > se->EndLine) break;
  1944. i++;
  1945. sl = NextSl;
  1946. }
  1947. }else{
  1948. int i = 0;
  1949. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1950. while (sl){
  1951. NextSl = sl->Item.pNext;
  1952. if (i == se->EndLine) {
  1953. strSquishBackward(sl->Buf, se->EndBefore, se->BeginBefore);
  1954. se->CursorLine = i;
  1955. se->CursorBefore = se->BeginBefore;
  1956. se->BeginLine = -1;
  1957. se->BeginBefore = -1;
  1958. se->EndLine = -1;
  1959. se->EndBefore = -1;
  1960. break;
  1961. }
  1962. i++; sl = NextSl;
  1963. }
  1964. }
  1965. strEnsureCursorVisible(se);
  1966. }
  1967. laStringLine *strGetCursorLine(laStringEdit *se, int* ReturnIndexIfLast){
  1968. if (!se || se->CursorBefore <= -1) return se->Lines.pFirst;
  1969. int i = 0;
  1970. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1971. while (sl){
  1972. NextSl = sl->Item.pNext;
  1973. if (i == se->CursorLine){
  1974. return sl;
  1975. }
  1976. i++;
  1977. sl = NextSl;
  1978. }
  1979. if(ReturnIndexIfLast){ *ReturnIndexIfLast=i-1;}
  1980. return se->Lines.pLast;
  1981. }
  1982. laStringLine *strGetBeginLine(laStringEdit *se){
  1983. if (!se || se->BeginLine <= -1) return se->Lines.pFirst;
  1984. int i = 0;
  1985. laStringLine *sl = se->Lines.pFirst, *NextSl;
  1986. while (sl){
  1987. NextSl = sl->Item.pNext;
  1988. if (i == se->BeginLine){
  1989. return sl;
  1990. }
  1991. i++;
  1992. sl = NextSl;
  1993. }
  1994. return se->Lines.pFirst;
  1995. }
  1996. void strInsertChar(laStringEdit *se, uint32_t a){
  1997. laStringLine *sl;
  1998. strClearSelection(se);
  1999. sl = strGetCursorLine(se, 0);
  2000. if(a==U'\n'){
  2001. laStringLine* nl=memAcquireSimple(sizeof(laStringLine));
  2002. if(sl->Buf[se->CursorBefore]!=U'\0') strcpyU(nl->Buf, &sl->Buf[se->CursorBefore]);
  2003. sl->Buf[se->CursorBefore]=U'\0';
  2004. se->CursorLine++; se->CursorBefore=0;
  2005. lstInsertItemAfter(&se->Lines, nl, sl); se->TotalLines++;
  2006. }else{
  2007. strPanFoward(sl->Buf, se->CursorBefore, 1);
  2008. sl->Buf[se->CursorBefore] = a;
  2009. se->CursorBefore += 1;
  2010. }
  2011. se->CursorPreferBefore = se->CursorBefore;
  2012. strEnsureCursorVisible(se);
  2013. }
  2014. void strBackspace(laStringEdit *se){
  2015. laStringLine *sl;
  2016. int width = 1;
  2017. if (se->CursorBefore == -1){
  2018. strClearSelection(se);
  2019. }else{
  2020. laStringLine *sl;
  2021. sl = strGetCursorLine(se, 0);
  2022. if (se->CursorBefore > 1 && sl->Buf[se->CursorBefore - 2] < 0) width = 2;
  2023. strSquishBackward(sl->Buf, se->CursorBefore, se->CursorBefore - width);
  2024. se->CursorBefore -= width;
  2025. if (se->CursorBefore <= -1){
  2026. if(sl->Item.pPrev){
  2027. laStringLine* ol=sl->Item.pPrev;
  2028. se->CursorBefore = strlenU(ol->Buf);
  2029. se->CursorLine--;
  2030. strcatU(ol->Buf, sl->Buf);
  2031. strRemoveLine(se, sl);
  2032. }
  2033. else {se->CursorBefore = 0;}
  2034. }
  2035. }
  2036. se->CursorPreferBefore = se->CursorBefore;
  2037. strEnsureCursorVisible(se);
  2038. }
  2039. void strMoveView(laStringEdit *se, int DownLines, int RightCharacters){
  2040. se->ViewStartLine+=DownLines;
  2041. se->ViewStartCol+=RightCharacters;
  2042. if(se->ViewStartLine>=se->TotalLines-1) se->ViewStartLine=se->TotalLines-1;
  2043. if(se->ViewStartLine<0) se->ViewStartLine=0;
  2044. if(se->ViewStartCol<0) se->ViewStartCol=0;
  2045. }
  2046. int laCopyFile(char *to, char *from){
  2047. #ifdef _WIN32
  2048. if(CopyFile(from, to, 0)) return 1; return 0;
  2049. #endif
  2050. #ifdef LA_LINUX
  2051. int fd_to, fd_from; char buf[4096];
  2052. ssize_t nread; int saved_errno;
  2053. fd_from = open(from, O_RDONLY); if (fd_from < 0) return 0;
  2054. fd_to = open(to, O_WRONLY|O_CREAT /* |O_EXCL */, 0666); if (fd_to < 0) goto out_error;
  2055. while (nread=read(fd_from,buf,sizeof(buf)), nread>0) {
  2056. char *out_ptr = buf; ssize_t nwritten;
  2057. do { nwritten = write(fd_to, out_ptr, nread);
  2058. if (nwritten >= 0){ nread -= nwritten; out_ptr += nwritten;}
  2059. else if (errno != EINTR){ goto out_error; }
  2060. }while (nread > 0);
  2061. }
  2062. if (nread == 0){
  2063. if (close(fd_to)<0){ fd_to = -1; goto out_error;}
  2064. close(fd_from);
  2065. return 1;
  2066. }
  2067. out_error:
  2068. saved_errno = errno;
  2069. close(fd_from); if (fd_to >= 0) close(fd_to);
  2070. errno = saved_errno;
  2071. return 0;
  2072. #endif //linux
  2073. }
  2074. int laEnsureDir(const char *dir) {
  2075. #ifdef LA_LINUX
  2076. char tmp[1024]; char *p = NULL; size_t len;
  2077. snprintf(tmp, sizeof(tmp),"%s",dir);
  2078. len = strlen(tmp);
  2079. if (tmp[len - 1] == '/') tmp[len - 1] = 0;
  2080. for (p = tmp + 1; *p; p++){
  2081. if (*p == '/') { *p = 0;
  2082. mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO);
  2083. *p = '/';
  2084. }
  2085. }
  2086. mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO);
  2087. struct stat statbuf;
  2088. if(stat(dir, &statbuf) != 0){ return 0; }
  2089. return S_ISDIR(statbuf.st_mode);
  2090. #endif
  2091. }
  2092. //======================================================[ translation ]
  2093. void transNewLanguage(const char *LanguageID){
  2094. laTranslationNode *tn = memAcquire(sizeof(laTranslationNode));
  2095. strSafeSet(&tn->LanguageName, LanguageID);
  2096. lstAppendItem(&MAIN.Translation.Languages, tn);
  2097. MAIN.Translation.CurrentLanguage = tn;
  2098. }
  2099. void transSetLanguage(const char *LanguageID){
  2100. laTranslationNode *tn;
  2101. if (!LanguageID){
  2102. MAIN.Translation.CurrentLanguage = 0;
  2103. return;
  2104. }
  2105. for (tn = MAIN.Translation.Languages.pFirst; tn; tn = tn->Item.pNext){
  2106. if (!strcmp(SSTR(tn->LanguageName), LanguageID)){
  2107. MAIN.Translation.CurrentLanguage = tn; return;
  2108. }
  2109. }
  2110. transNewLanguage(LanguageID);
  2111. }
  2112. void transDumpMissMatchRecord(const char *filename){
  2113. laTranslationMatch *tm;
  2114. laListHandle *lst;
  2115. int i;
  2116. FILE *f = fopen(filename, "w"); if (!f) return;
  2117. for (i = 0; i < 256; i++){
  2118. lst = &MAIN.Translation.MisMatches.Entries[i];
  2119. for (tm = lst->pFirst; tm; tm = tm->Item.pNext){ if(tm->Target) fprintf(f, "%s | \n", tm->Target); }
  2120. }
  2121. fclose(f);
  2122. }
  2123. int IsThisTranslationMatch(laTranslationMatch *tm, char *p){
  2124. return (tm->Target && (!strcmp(tm->Target, p)));
  2125. }
  2126. void transNewEntry(const char *Target, const char *replacement){
  2127. laTranslationMatch *tm = memAcquireSimple(sizeof(laTranslationMatch));
  2128. tm->Target = Target; tm->Replacement = replacement;
  2129. hsh256InsertItemCSTR(&MAIN.Translation.CurrentLanguage->Matches, tm, Target);
  2130. }
  2131. void transNewMissEntry(const char *Target){
  2132. if (!hsh256FindItemSTR(&MAIN.Translation.MisMatches, IsThisTranslationMatch, Target)){
  2133. laTranslationMatch *tm = memAcquireSimple(sizeof(laTranslationMatch));
  2134. int len=strlen(Target); tm->Target=memAcquireSimple(len*sizeof(char)+1);
  2135. strcpy(tm->Target,Target);
  2136. hsh256InsertItemCSTR(&MAIN.Translation.MisMatches, tm, Target);
  2137. }
  2138. }
  2139. char *transLate(const char *Target){
  2140. if (!MAIN.Translation.CurrentLanguage || !MAIN.Translation.EnableTranslation || !Target || !Target[0]) return Target;
  2141. laTranslationMatch *tm = hsh256FindItemSTR(&MAIN.Translation.CurrentLanguage->Matches, IsThisTranslationMatch, Target);
  2142. if (!tm){ transNewMissEntry(Target); return Target; }
  2143. return tm->Replacement;
  2144. }
  2145. void transState(void *UNUSED, int val){
  2146. if (val) MAIN.Translation.EnableTranslation = 1;
  2147. else MAIN.Translation.EnableTranslation = 0;
  2148. laRedrawCurrentWindow();
  2149. }
  2150. void laOpenInternetLink(char *url){
  2151. laSafeString* s=0;
  2152. #ifdef LA_LINUX
  2153. strSafePrint(&s, "xdg-open %s", url);
  2154. #endif
  2155. #ifdef _WIN32
  2156. strSafePrint(&s, "start %s", url);
  2157. #endif
  2158. #ifdef LAGUI_ANDROID
  2159. JNIEnv *env = NULL;
  2160. JavaVM *vm = MAIN.app->activity->vm;
  2161. (*vm)->AttachCurrentThread(vm, &env, NULL);
  2162. jstring urlString = (*env)->NewStringUTF(env, url);
  2163. jclass uriClass = (*env)->FindClass(env, "android/net/Uri");
  2164. jmethodID uriParse = (*env)->GetStaticMethodID(env, uriClass, "parse", "(Ljava/lang/String;)Landroid/net/Uri;");
  2165. jobject uri = (*env)->CallStaticObjectMethod(env, uriClass, uriParse, urlString);
  2166. jclass intentClass = (*env)->FindClass(env, "android/content/Intent");
  2167. jfieldID actionViewId = (*env)->GetStaticFieldID(env, intentClass, "ACTION_VIEW", "Ljava/lang/String;");
  2168. jobject actionView = (*env)->GetStaticObjectField(env, intentClass, actionViewId);
  2169. jmethodID newIntent = (*env)->GetMethodID(env, intentClass, "<init>", "(Ljava/lang/String;Landroid/net/Uri;)V");
  2170. jobject intent = (*env)->AllocObject(env, intentClass);
  2171. (*env)->CallVoidMethod(env, intent, newIntent, actionView, uri);
  2172. jclass activityClass = (*env)->FindClass(env, "android/app/Activity");
  2173. jmethodID startActivity = (*env)->GetMethodID(env, activityClass, "startActivity", "(Landroid/content/Intent;)V");
  2174. (*env)->CallVoidMethod(env, MAIN.app->activity->clazz, startActivity, intent);
  2175. (*vm)->DetachCurrentThread(vm);
  2176. return;
  2177. #endif
  2178. system(s->Ptr);
  2179. strSafeDestroy(&s);
  2180. //these were windows stuff
  2181. //HKEY hkRoot, hSubKey;
  2182. //char ValueName[256]={0};
  2183. //char DataValue[256]={0};
  2184. //u64bit cbValueName = 256;
  2185. //u64bit cbDataValue = 256;
  2186. //char ShellChar[512]={0};
  2187. //DWORD dwType;
  2188. //
  2189. //ShellExecute(0, "open", link, 0, 0, SW_SHOWNORMAL);
  2190. //
  2191. //return;
  2192. }
  2193. #ifdef _WIN32
  2194. void usleep(unsigned int usec){
  2195. HANDLE timer;
  2196. LARGE_INTEGER ft;
  2197. static int init = 0;
  2198. if (init == 0){
  2199. init = 1;
  2200. const HINSTANCE ntdll = LoadLibrary("ntdll.dll");
  2201. if (ntdll != NULL){
  2202. typedef long(NTAPI* pNtQueryTimerResolution)(u64bit* MinimumResolution, u64bit* MaximumResolution, u64bit* CurrentResolution);
  2203. typedef long(NTAPI* pNtSetTimerResolution)(u64bit RequestedResolution, char SetResolution, u64bit* ActualResolution);
  2204. pNtQueryTimerResolution NtQueryTimerResolution = (pNtQueryTimerResolution)GetProcAddress(ntdll, "NtQueryTimerResolution");
  2205. pNtSetTimerResolution NtSetTimerResolution = (pNtSetTimerResolution)GetProcAddress(ntdll, "NtSetTimerResolution");
  2206. if (NtQueryTimerResolution != NULL && NtSetTimerResolution != NULL){
  2207. u64bit minimum, maximum, current;
  2208. NtQueryTimerResolution(&minimum, &maximum, &current);
  2209. NtSetTimerResolution(maximum, (char)1, &current);
  2210. }
  2211. FreeLibrary(ntdll);
  2212. }
  2213. }
  2214. ft.QuadPart = -(10 * (__int64)usec);
  2215. timer = CreateWaitableTimer(NULL, TRUE, NULL);
  2216. SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
  2217. WaitForSingleObject(timer, INFINITE);
  2218. CloseHandle(timer);
  2219. }
  2220. void laSpinInit(SYSLOCK* lock) {
  2221. InitializeCriticalSection(lock);
  2222. }
  2223. void laSpinDestroy(SYSLOCK* lock) {
  2224. DeleteCriticalSection(lock);
  2225. }
  2226. void laSpinLock(SYSLOCK* lock) {
  2227. EnterCriticalSection(lock);
  2228. }
  2229. void laSpinUnlock(SYSLOCK* lock) {
  2230. LeaveCriticalSection(lock);
  2231. }
  2232. #endif
  2233. #ifdef LA_LINUX
  2234. void laSpinInit(SYSLOCK* lock) {
  2235. pthread_spin_init(lock, 0);
  2236. }
  2237. void laSpinDestroy(SYSLOCK* lock) {
  2238. pthread_spin_destroy(lock);
  2239. }
  2240. void laSpinLock(SYSLOCK* lock) {
  2241. pthread_spin_lock(lock);
  2242. }
  2243. void laSpinUnlock(SYSLOCK* lock) {
  2244. pthread_spin_unlock(lock);
  2245. }
  2246. #endif
  2247. #ifdef LAGUI_ANDROID
  2248. void laSpinInit(SYSLOCK* lock) {
  2249. return;
  2250. }
  2251. void laSpinDestroy(SYSLOCK* lock) {
  2252. return;
  2253. }
  2254. void laSpinLock(SYSLOCK* lock) {
  2255. return;
  2256. }
  2257. void laSpinUnlock(SYSLOCK* lock) {
  2258. return;
  2259. }
  2260. #endif
  2261. //======================================= lua utils
  2262. #ifdef LA_WITH_LUAJIT
  2263. static const char *progname = LUA_PROGNAME;
  2264. static int la_luaTraceback(lua_State *L){
  2265. if (!lua_isstring(L, 1)) { /* Non-string error object? Try metamethod. */
  2266. if (lua_isnoneornil(L, 1) ||
  2267. !luaL_callmeta(L, 1, "__tostring") ||
  2268. !lua_isstring(L, -1))
  2269. return 1; /* Return non-string error object. */
  2270. lua_remove(L, 1); /* Replace object by result of __tostring metamethod. */
  2271. }
  2272. luaL_traceback(L, L, lua_tostring(L, 1), 1);
  2273. return 1;
  2274. }
  2275. static void la_luaMessage(const char *msg){
  2276. if (progname) { logPrint("%s: ",progname); } logPrint("%s\n",msg);
  2277. }
  2278. static int la_luaReport(lua_State *L, int status){
  2279. if (status && !lua_isnil(L, -1)) {
  2280. const char *msg = lua_tostring(L, -1);
  2281. if (msg == NULL) msg = "(error object is not a string)";
  2282. la_luaMessage(msg);
  2283. lua_pop(L, 1);
  2284. }
  2285. return status;
  2286. }
  2287. static int la_luaDoCall(lua_State *L, int narg, int clear){
  2288. int status;
  2289. int base = lua_gettop(L) - narg; /* function index */
  2290. lua_pushcfunction(L, la_luaTraceback); /* push la_luaTraceback function */
  2291. lua_insert(L, base); /* put it under chunk and args */
  2292. status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
  2293. lua_remove(L, base); /* remove la_luaTraceback function */
  2294. /* force a complete garbage collection in case of errors */
  2295. if (status != LUA_OK) lua_gc(L, LUA_GCCOLLECT, 0);
  2296. return status;
  2297. }
  2298. static int la_luaIncomplete(lua_State *L, int status){
  2299. if (status == LUA_ERRSYNTAX) {
  2300. size_t lmsg;
  2301. const char *msg = lua_tolstring(L, -1, &lmsg);
  2302. const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
  2303. if (strstr(msg, LUA_QL("<eof>")) == tp) { lua_pop(L, 1); return 1; }
  2304. }
  2305. return 0; /* else... */
  2306. }
  2307. int terLoadLine(char* buf, int firstline){
  2308. lua_State *L=MAIN.L;
  2309. if(!MAIN.TerminalIncomplete){ lua_settop(L, 0); }
  2310. size_t len = strlen(buf); if(len>=512){ buf[512]=0; }
  2311. if(len > 0 && buf[len-1] == '\n') buf[len-1] = '\0';
  2312. if(firstline && buf[0] == '=') lua_pushfstring(L, "return %s", buf+1);
  2313. else lua_pushstring(L, buf);
  2314. if(MAIN.TerminalIncomplete){
  2315. lua_pushliteral(L, "\n"); lua_insert(L, -2); lua_concat(L, 3);
  2316. }
  2317. int status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "terLoadLine");
  2318. if(la_luaIncomplete(L,status)){
  2319. MAIN.TerminalIncomplete=1;
  2320. }else{
  2321. MAIN.TerminalIncomplete=0;
  2322. lua_remove(L, 1);
  2323. }
  2324. if(status==LUA_OK && (!MAIN.TerminalIncomplete)){
  2325. status = la_luaDoCall(L, 0, 0);
  2326. la_luaReport(L, status);
  2327. if (status == LUA_OK && lua_gettop(L) > 0) { /* any result to print? */
  2328. lua_getglobal(L, "log");
  2329. lua_insert(L, 1);
  2330. if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
  2331. la_luaMessage(lua_pushfstring(L, "error calling " LUA_QL("log") " (%s)",
  2332. lua_tostring(L, -1)));
  2333. }
  2334. }
  2335. return status;
  2336. }
  2337. static int lalua_Log(lua_State *L) {
  2338. int n = lua_gettop(L); int i;
  2339. lua_getglobal(L, "tostring");
  2340. for (i=1; i<=n; i++) {
  2341. const char *s;
  2342. lua_pushvalue(L, -1); /* tostring function */
  2343. lua_pushvalue(L, i); /* value to print */
  2344. lua_call(L, 1, 1);
  2345. s = lua_tostring(L, -1); /* get result */
  2346. if (s == NULL) return luaL_error(L, LUA_QL("tostring") " must return a string to "LUA_QL("use `log`"));
  2347. if (i>1) logPrint(" ");
  2348. logPrint(s); lua_pop(L, 1); /* pop result */
  2349. }
  2350. logPrint("\n");
  2351. return 0;
  2352. }
  2353. void la_luaLoadLibs(lua_State *L){
  2354. lua_gc(L, LUA_GCSTOP, 0);
  2355. luaL_openlibs(L);
  2356. lua_register(L,"log",lalua_Log);
  2357. if(luaL_loadstring(L, LA_LUA_LIB_COMMON) || lua_pcall(L, 0, 0, 0)){ logPrint(" Error loading lagui lua libs.\n"); };
  2358. if(luaL_loadstring(L, LA_LUA_LIB_AUDIO) || lua_pcall(L, 0, 0, 0)){ logPrint(" Error loading lua libs for audio.\n"); };
  2359. lua_gc(L, LUA_GCRESTART, -1);
  2360. }
  2361. void la_luaPrintStatus(lua_State *L){
  2362. logPrint(LUAJIT_VERSION " -- " LUAJIT_COPYRIGHT ". " LUAJIT_URL "\n");
  2363. int n; const char *s;
  2364. lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
  2365. lua_getfield(L, -1, "jit"); lua_remove(L, -2); /* Get jit.* module table. */
  2366. lua_getfield(L, -1, "status"); lua_remove(L, -2);
  2367. n = lua_gettop(L);
  2368. lua_call(L, 0, LUA_MULTRET);
  2369. logPrint(lua_toboolean(L, n) ? "JIT: ON" : "JIT: OFF");
  2370. for (n++; (s = lua_tostring(L, n)); n++) { logPrint("%s ",s); }
  2371. logPrint("\n");
  2372. lua_settop(L, 0); /* clear stack */
  2373. }
  2374. void la_luaDumpStack(lua_State *L){
  2375. int top=lua_gettop(L); for (int i=1; i <= top; i++) {
  2376. printf("%d\t%s\t", i, luaL_typename(L,i));
  2377. switch (lua_type(L, i)) {
  2378. case LUA_TNUMBER: printf("%g\n",lua_tonumber(L,i)); break;
  2379. case LUA_TSTRING: printf("%s\n",lua_tostring(L,i)); break;
  2380. case LUA_TBOOLEAN: printf("%s\n", (lua_toboolean(L, i) ? "true" : "false")); break;
  2381. case LUA_TNIL: printf("%s\n", "nil"); break;
  2382. default: printf("%p\n",lua_topointer(L,i)); break;
  2383. }
  2384. }
  2385. }
  2386. #else //luajit
  2387. int terLoadLine(char* buf, int firstline){
  2388. logPrint(buf); return 0;
  2389. }
  2390. #endif //luajit
  2391. #ifdef LAGUI_ANDROID
  2392. void glPointSize(real a){
  2393. return;
  2394. }
  2395. void glDrawBuffer(GLenum mode){
  2396. GLenum b={GL_NONE};
  2397. glDrawBuffers(1,b);
  2398. }
  2399. #endif