*/}}

la_util.c 82 KB

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