Subversion Repositories Kolibri OS

Rev

Rev 8278 | Rev 8439 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #ifndef INCLUDE_STRING_H
  2. #define INCLUDE_STRING_H
  3.  
  4. #ifndef INCLUDE_MEM_H
  5. #include "../lib/mem.h"
  6. #endif
  7.  
  8. //------------------------------------------------------------------------------
  9. // strspn(dword text1,text2) --- example: strspn("12 year","1234567890") -> return 2
  10. // strpbrk(dword text1,text2) --- example: strpbrk("this test", " ckfi") -> return "is test"
  11. // strcmp( ESI, EDI)
  12. // strlen( EDI)
  13. // utf8_strlen( ESI)
  14. // strcpy( EDI, ESI) --- 0 if ==
  15. // strlcpy(dword text1,text2,signed length)
  16. // strcat( EDI, ESI)
  17. // chrcat(ESI, BL)
  18. // strncat(dword text1,text2,signed length) --- pasting the text of a certain length
  19. // strchr( ESI,BL) --- find first BL
  20. // strrchr( ESI,BL) --- find last BL
  21. // strstr( EBX, EDX)
  22. // itoa(signed long number) --- convert the number as a string
  23. // atoi(dword text) --- convert a string as a number
  24. // strupr( ESI)
  25. // strlwr( ESI) --- Cyrillic symbols may not work
  26. // strttl( EDX)
  27. // strtok( ESI)
  28. // strltrim(dword text) --- removes "blank" characters on the left (\r, \n and space)
  29. // strrtrim(dword text) --- removes "blank" characters on the right (\r, \n and space)
  30. // strtrim(dword text) --- delete "empty" characters (\ r \ n and space) on both sides
  31. // chrnum(dword searchin, char symbol)
  32. // strcpyb(dword searchin, copyin, startstr, endstr) --- copy string between strings
  33. // strnumb(dword searchin, startstr, endstr) --- get number between strings
  34. // strdup(dword text) --- allocation under the text
  35. //------------------------------------------------------------------------------
  36.  
  37. /*
  38. inline fastcall signed int strcmp( ESI, EDI)
  39. {
  40.     loop()
  41.     {
  42.         IF (DSBYTE[ESI]<DSBYTE[EDI]) RETURN -1;
  43.         IF (DSBYTE[ESI]>DSBYTE[EDI]) RETURN 1;
  44.         IF (DSBYTE[ESI]=='\0') RETURN 0;
  45.         ESI++;
  46.         EDI++;
  47.     }
  48. }
  49. */
  50.  
  51.  
  52.  
  53. inline int strspn(dword text1,text2)
  54. {
  55.         dword beg;
  56.         char s1,s2;
  57.         int ret;
  58.         ret = 0;
  59.         beg = text2;
  60.         do {
  61.                 s1 = ESBYTE[text1];
  62.                 text2 = beg;
  63.                 do {
  64.                         s2 = ESBYTE[text2];
  65.                         if(s1==s2)
  66.                         {
  67.                                 if(!s2)break;
  68.                                 $inc ret
  69.                                 break;
  70.                         }
  71.                         else $inc text2
  72.                 } while(s2);
  73.                 $inc text1
  74.         } while(s1);
  75.         return ret;
  76. }
  77.  
  78. inline dword strpbrk(dword text1,text2)
  79. {
  80.         char s,ss;
  81.         dword beg;
  82.         beg = text2;
  83.         do {
  84.                 s = ESBYTE[text1];
  85.                 text2 = beg;
  86.                 do {
  87.                         ss = ESBYTE[text2];
  88.                         if(ss==s) return text1;
  89.                         $inc text2
  90.                 } while(ss);
  91.                 $inc text1
  92.         } while(s);
  93.         return text1;
  94. }
  95.  
  96. inline fastcall signed int strncmp( ESI, EDI, ECX)
  97. {
  98.   asm {
  99.     MOV EBX, EDI
  100.     XOR EAX, EAX
  101.     MOV EDX, ECX
  102.     OR ECX, ECX
  103.     JE L1
  104.     REPNE SCASB
  105.     SUB EDX, ECX
  106.     MOV ECX, EDX
  107.     MOV EDI, EBX
  108.     XOR EBX, EBX
  109.     REPE CMPSB
  110.     MOV AL, DSBYTE[ ESI-1]
  111.     MOV BL, DSBYTE[ EDI-1]
  112.     SUB EAX, EBX
  113. L1:
  114.   }
  115. }
  116.  
  117. /*
  118. signed int strncmp(dword s1, s2, signed n)
  119. unsigned char _s1,_s2;
  120. {
  121.         if (n == 0)
  122.                 return 0;
  123.         do {
  124.                 _s1 = DSBYTE[s1];
  125.                 _s2 = DSBYTE[s2];
  126.                 if (_s1 != _s2)
  127.                 {
  128.                         $dec s2
  129.                         return _s1 - _s2;
  130.                 }
  131.                 $inc s2
  132.                 if (_s1 == 0)
  133.                         break;
  134.                 $inc s1
  135.                 $dec n
  136.         } while (n);
  137.         return 0;
  138. }
  139. */
  140.  
  141. /*
  142. inline signed int strncmp(dword text1,text2,len)
  143. {
  144.        
  145.         loop()
  146.         {
  147.                 if(DSBYTE[text1]!=DSBYTE[text2])return text1-text2;
  148.                 $dec len
  149.                 if(!len)return 0;
  150.         }
  151. }
  152. */
  153. inline fastcall unsigned int strlen( EDI)
  154. {
  155.         $xor eax, eax
  156.         $mov ecx, -1
  157.         $REPNE $SCASB
  158.         EAX-=2+ECX;
  159. }
  160.  
  161. inline dword strnlen(dword str, dword maxlen)
  162. {
  163.         dword cp;
  164.         for (cp = str; (maxlen != 0) && (DSBYTE[cp] != '\0'); cp++, maxlen--);
  165.         return cp - str;
  166. }
  167.  
  168. inline fastcall unsigned int utf8_strlen( ESI)
  169. {
  170.  $xor  ecx, ecx
  171.   _loop:
  172.  $lodsb
  173.  $test  al, al
  174.  $jz  _done
  175.  $and al, 0xc0
  176.  $cmp al, 0x80
  177.  $jz  _loop
  178.  $inc ecx
  179.  $jmp _loop
  180.  
  181.   _done:
  182.  return ECX;
  183. }
  184.  
  185. inline signed int strcmp(dword text1, text2)
  186. {
  187.         char s1,s2;
  188.         dword p1,p2;
  189.         p1 = text1;
  190.         p2 = text2;
  191.         loop()
  192.         {
  193.                 s1 = DSBYTE[text1];
  194.                 s2 = DSBYTE[text2];
  195.                 if(s1==s2)
  196.                 {
  197.                         if(s1==0) return 0;
  198.                 }
  199.                 else {
  200.                         return s1-s2;
  201.                 }
  202.                 $inc text1
  203.                 $inc text2
  204.         }
  205.         return 0;
  206. }
  207.  
  208.  
  209. inline fastcall bool streq(ESI, EDI)
  210. {
  211.         loop()
  212.         {
  213.                 if(DSBYTE[ESI]==DSBYTE[EDI])
  214.                 {
  215.                         if(DSBYTE[ESI]==0) return true;
  216.                 }
  217.                 else {
  218.                         return false;
  219.                 }
  220.                 ESI++;
  221.                 EDI++;
  222.         }
  223. }
  224.  
  225. inline fastcall bool streqrp(ESI, EDI) //streq right part
  226. {
  227.         if(DSBYTE[ESI]==0) || (DSBYTE[EDI]==0) return false;
  228.         loop()
  229.         {
  230.                 if(DSBYTE[EDI]==0) return true;
  231.                 if(DSBYTE[ESI]!=DSBYTE[EDI]) return false;
  232.                 ESI++;
  233.                 EDI++;
  234.         }
  235. }
  236.  
  237. inline fastcall void strcpy( EDI, ESI)
  238. {
  239.     $cld
  240. L2:
  241.     $lodsb
  242.     $stosb
  243.     $test al,al
  244.     $jnz L2
  245. }
  246.  
  247. inline fastcall int strlcpy(dword ESI, EDI, EBX)
  248. {
  249.     if (EBX<0) return -1;
  250.     EDX=0;
  251.     do {
  252.         DSBYTE[ESI]=DSBYTE[EDI];
  253.         ESI++;
  254.         EDI++;
  255.         EDX++;
  256.         if (EDX==EBX) { DSBYTE[ESI]='\0'; return -1;}
  257.     } while(DSBYTE[EDI-1]!='\0');
  258.     return 0;
  259. }
  260.  
  261. :void strncpy(dword dst, src, len)
  262. {
  263.         while (len) && (ESBYTE[src])
  264.         {
  265.                 ESBYTE[dst] = ESBYTE[src];
  266.                 dst++;
  267.                 src++;
  268.                 len--;
  269.         }
  270.         ESBYTE[dst]=0;
  271. }
  272.  
  273. /*
  274. inline fastcall void strtrim( ESI)
  275. {
  276.     EDI = ESI;
  277.     do{
  278.         AL=DSBYTE[EDI];
  279.         if (AL != '\32') && (AL != '\13') && (AL != '\10')
  280.         {
  281.             DSBYTE[ESI]=AL;
  282.             $inc ESI
  283.         }
  284.          $inc EDI
  285.     }while(AL!=0);
  286.     DSBYTE[ESI] = '\0';
  287. }
  288. */
  289.  
  290. inline byte __isWhite(int s){ if (s==13)||(s==32)||(s==10)||(s==9) return true; return false; }
  291. inline void strltrim(dword text){
  292.         int s;
  293.         dword back_text;
  294.         back_text = text;
  295.         s = ESBYTE[text];
  296.         while(__isWhite(s))
  297.         {
  298.                 $inc text
  299.                 s = ESBYTE[text];
  300.         }
  301.         loop()
  302.         {
  303.                 ESBYTE[back_text] = s;
  304.                 $inc back_text
  305.                 if(!s) break;
  306.                 $inc text
  307.                 s = ESBYTE[text];
  308.         };
  309. }
  310.  
  311. inline void strrtrim(dword text)
  312. {
  313.         int s;
  314.         dword p;
  315.         do {
  316.                 s = ESBYTE[text];
  317.                 if(__isWhite(s))
  318.                 {
  319.                         p = text;
  320.                         while(__isWhite(s))
  321.                         {
  322.                                 $inc text;
  323.                                 s = ESBYTE[text];
  324.                         }
  325.                 }
  326.                 else $inc text
  327.         } while(s);
  328.         $dec text
  329.         s = ESBYTE[text];
  330.         if(__isWhite(s)) ESBYTE[p] = 0;
  331. }
  332.  
  333. inline void strtrim(dword text){
  334.         int s;
  335.         dword p,back_text;
  336.         back_text = text;
  337.         s = ESBYTE[text];
  338.         while(__isWhite(s))
  339.         {
  340.                 $inc text
  341.                 s = ESBYTE[text];
  342.         }
  343.         do {
  344.                 s = ESBYTE[text];
  345.                 if(__isWhite(s))
  346.                 {
  347.                         p = back_text;
  348.                         while(__isWhite(s))
  349.                         {
  350.                                 ESBYTE[back_text] = s;
  351.                                 $inc back_text
  352.                                 $inc text;
  353.                                 s = ESBYTE[text];
  354.                         }
  355.                 }
  356.                 else {
  357.                         ESBYTE[back_text] = s;
  358.                         $inc back_text
  359.                         $inc text
  360.                 }
  361.         } while(s);
  362.         $dec text
  363.         s = ESBYTE[text];
  364.         if(__isWhite(s)) ESBYTE[p] = 0;
  365. }
  366.  
  367. inline fastcall void strcat( EDI, ESI)
  368. {
  369.   asm {
  370.     mov ebx, edi
  371.     xor ecx, ecx
  372.     xor eax, eax
  373.     dec ecx
  374.     repne scasb
  375.     dec edi
  376.     mov edx, edi
  377.     mov edi, esi
  378.     xor ecx, ecx
  379.     xor eax, eax
  380.     dec ecx
  381.     repne scasb
  382.     xor ecx, 0ffffffffh
  383.     mov edi, edx
  384.     mov edx, ecx
  385.     mov eax, edi
  386.     shr ecx, 2
  387.     rep movsd
  388.     mov ecx, edx
  389.     and ecx, 3
  390.     rep movsb
  391.     mov eax, ebx
  392.     }
  393. }
  394.  
  395. :void strncat(dword dst, src, len)
  396. {
  397.         while (ESBYTE[dst]) && (len) {
  398.                 dst++;
  399.                 len--;
  400.         }
  401.         while (ESBYTE[src]) && (len>1) {
  402.                 ESBYTE[dst] = ESBYTE[src];
  403.                 dst++;
  404.                 src++;
  405.                 len--;
  406.         }
  407.         ESBYTE[dst] = 0;
  408. }
  409.  
  410. :void chrncat(dword dst, unsigned char s, dword len)
  411. {
  412.         while (ESBYTE[dst]) && (len) {
  413.                 dst++;
  414.                 len--;
  415.         }
  416.         if (len>1) {
  417.                 ESBYTE[dst] = s;
  418.                 ESBYTE[dst+1] = 0;
  419.         }
  420. }
  421.  
  422. inline fastcall void chrcat(ESI, DI)
  423. {
  424.     while (ESBYTE[ESI]) ESI++;
  425.     ESBYTE[ESI] = DI;
  426.     ESI++;
  427.     ESBYTE[ESI] = 0;
  428. }
  429.  
  430. inline dword strchr(dword shb;char s)
  431. {
  432.         char ss;
  433.         loop()
  434.         {
  435.                 ss = DSBYTE[shb];
  436.                 if(!ss)return 0;
  437.                 if(ss==s)return shb;
  438.                 shb++;
  439.         }
  440. }
  441.  
  442. inline fastcall signed int strrchr( ESI,BL)
  443. {
  444.     int jj=0, last=0;
  445.     do{
  446.         jj++;
  447.         $lodsb
  448.         IF(AL==BL) last=jj;
  449.     } while(AL!=0);
  450.     return last;
  451. }
  452.  
  453.  
  454. inline fastcall unsigned int chrnum( ESI, BL)
  455. {
  456.     int num = 0;
  457.     while(DSBYTE[ESI])
  458.     {
  459.         if (DSBYTE[ESI] == BL) num++;
  460.         ESI++;
  461.     }
  462.     return num;
  463. }
  464.  
  465.  
  466. inline fastcall signed int strstr( EBX, EDX)
  467. {
  468.   asm {
  469.     MOV EDI, EDX
  470.     XOR ECX, ECX
  471.     XOR EAX, EAX
  472.     DEC ECX
  473.     REPNE SCASB
  474.     NOT ECX
  475.     DEC ECX
  476.     JE LS2
  477.     MOV ESI, ECX
  478.     XOR ECX, ECX
  479.     MOV EDI, EBX
  480.     DEC ECX
  481.     REPNE SCASB
  482.     NOT ECX
  483.     SUB ECX, ESI
  484.     JBE LS2
  485.     MOV EDI, EBX
  486.     LEA EBX, DSDWORD[ ESI-1]
  487. LS1: MOV ESI, EDX
  488.     LODSB
  489.     REPNE SCASB
  490.     JNE LS2
  491.     MOV EAX, ECX
  492.     PUSH EDI
  493.     MOV ECX, EBX
  494.     REPE CMPSB
  495.     POP EDI
  496.     MOV ECX, EAX
  497.     JNE LS1
  498.     LEA EAX, DSDWORD[ EDI-1]
  499.     JMP SHORT LS3
  500. LS2: XOR EAX, EAX
  501. LS3:
  502.   }
  503. }
  504.  
  505. inline int strnum(dword haystack, needle)
  506. {
  507.         int count = 0;
  508.         int needle_len = strlen(needle);
  509.         loop() {
  510.                 if (! haystack = strstr(haystack, needle)) break;
  511.                 haystack+=needle_len;
  512.                 count++;
  513.         }
  514.         return count;
  515. }
  516.  
  517. inline int strinum(dword haystack, needle)
  518. {
  519.         int count = 0;
  520.         int needle_len = strlen(needle);
  521.         loop() {
  522.                 if (! haystack = strstri(haystack, needle)) break;
  523.                 haystack+=needle_len;
  524.                 count++;
  525.         }
  526.         return count;
  527. }
  528.  
  529. inline signed int strcmpi(dword cmp1, cmp2)
  530. {
  531.     char si, ue;
  532.  
  533.     loop()
  534.     {
  535.         si = DSBYTE[cmp1];
  536.         ue = DSBYTE[cmp2];
  537.         if (si>='A') && (si<='Z') si +=32;
  538.         if (ue>='A') && (ue<='Z') ue +=32;
  539.         if (si != ue) return si-ue;
  540.         cmp1++;
  541.         cmp2++;
  542.         if ((DSBYTE[cmp1]=='\0') && (DSBYTE[cmp2]=='\0')) return 0;
  543.         if (DSBYTE[cmp1]=='\0') return -1;
  544.         if (DSBYTE[cmp2]=='\0') return 1;
  545.     }
  546. }
  547.  
  548. inline dword strstri(dword searchin, usestr_s)
  549. {
  550.     dword usestr_e = usestr_s;
  551.     char si, ue;
  552.  
  553.     while(DSBYTE[searchin])
  554.     {
  555.         si = DSBYTE[searchin];
  556.         ue = DSBYTE[usestr_e];
  557.         if (si>='A') && (si<='Z') si +=32;
  558.         if (ue>='A') && (ue<='Z') ue +=32;
  559.         if (si == ue) usestr_e++; else usestr_e = usestr_s;
  560.         searchin++;
  561.         if (DSBYTE[usestr_e]=='\0') return searchin;
  562.     }
  563.     return 0;
  564. }
  565.  
  566.  
  567. inline unsigned int strcpyb(dword search_in, copyin, startstr, endstr)
  568. {
  569.     dword startp, endp;
  570.     dword copyin_start_off = copyin;
  571.     if (startstr==0) startp = search_in; else startp = strstr(search_in, startstr) + strlen(startstr);
  572.     if (! endp = strstri(startp, endstr)) endp = startp+strlen(search_in);
  573.     //if (startp==endp) return 0;
  574.     do
  575.     {
  576.         DSBYTE[copyin] = DSBYTE[startp];
  577.         copyin++;
  578.         startp++;
  579.     }
  580.     while (startp<endp);
  581.     DSBYTE[copyin] = '\0';
  582.     return copyin_start_off;
  583. }
  584.  
  585.  
  586. /*void strcat(char *to, char *from)
  587. {
  588.     while(*to) to++;
  589.     while(*from)
  590.     {
  591.         *to = *from;
  592.         to++;
  593.         from++;
  594.     }
  595.     *to = '\0';
  596. }*/
  597.  
  598.  
  599. inline fastcall dword atoi( EDI)
  600. {
  601.     $push ebx
  602.     $push esi
  603.     ESI=EDI;
  604.     while (DSBYTE[ESI]==' ') ESI++;
  605.     if (DSBYTE[ESI]=='-') ESI++;
  606.     EAX=0;
  607.     while (DSBYTE[ESI]>='0') && (DSBYTE[ESI]<='9')
  608.     {
  609.         $xor ebx, ebx
  610.         EBX = DSBYTE[ESI]-'0';
  611.         EAX *= 10;
  612.         EAX += EBX;
  613.         ESI++;
  614.     }
  615.     IF (DSBYTE[EDI]=='-') -EAX;
  616.     $pop esi
  617.     $pop ebx
  618. }
  619.  
  620.  
  621.  
  622. inline fastcall strupr( ESI)
  623. {
  624.     do{
  625.         AL=DSBYTE[ESI];
  626.         IF(AL>='a')IF(AL<='z')DSBYTE[ESI]=AL&0x5f;
  627.         IF (AL>=160) && (AL<=175) DSBYTE[ESI] = AL - 32;    //à-ï
  628.         IF (AL>=224) && (AL<=239) DSBYTE[ESI] = AL - 80;    //à-ï
  629.          ESI++;
  630.     }while(AL!=0);
  631. }
  632.  
  633. inline fastcall strlwr( ESI)
  634. {
  635.     do{
  636.         $LODSB
  637.         IF(AL>='A')&&(AL<='Z'){
  638.             AL+=0x20;
  639.             DSBYTE[ESI-1]=AL;
  640.             CONTINUE;
  641.         }
  642.     }while(AL!=0);
  643. }
  644.  
  645. inline fastcall strttl( EDX)
  646. {
  647.     AL=DSBYTE[EDX];
  648.     IF(AL>='a')&&(AL<='z')DSBYTE[EDX]=AL&0x5f;
  649.     IF (AL>=160) && (AL<=175) DSBYTE[EDX] = AL - 32;    //à-ï
  650.     IF (AL>=224) && (AL<=239) DSBYTE[EDX] = AL - 80;    //à-ï
  651.     do{
  652.         EDX++;
  653.         AL=DSBYTE[EDX];
  654.         IF(AL>='A')&&(AL<='Z'){DSBYTE[EDX]=AL|0x20; CONTINUE;}
  655.         IF(AL>='€')&&(AL<='')DSBYTE[EDX]=AL|0x20; // -¯
  656.         IF (AL>=144) && (AL<=159) DSBYTE[EDX] = AL + 80;    //à-ï
  657.     }while(AL!=0);
  658. }
  659.  
  660. /*
  661. dword itoa( ESI)
  662. {
  663.     unsigned char buffer[11];
  664.     $pusha
  665.     EDI = #buffer;
  666.     ECX = 10;
  667.     if (ESI < 0)
  668.     {
  669.          $mov     al, '-'
  670.          $stosb
  671.          $neg     esi
  672.     }
  673.  
  674.     $mov     eax, esi
  675.     $push    -'0'
  676. F2:
  677.     $xor     edx, edx
  678.     $div     ecx
  679.     $push    edx
  680.     $test    eax, eax
  681.     $jnz     F2
  682. F3:
  683.     $pop     eax
  684.     $add     al, '0'
  685.     $stosb
  686.     $jnz     F3
  687.    
  688.     $mov     al, '\0'
  689.     $stosb
  690.  
  691.     $popa
  692.     return #buffer;
  693. }
  694. */
  695. :unsigned char BUF_ITOA[11];
  696. inline dword itoa(signed long number)
  697. {
  698.         dword ret,p;
  699.         byte cmd;
  700.         long mask,tmp;
  701.         mask = 1000000000;
  702.         cmd = true;
  703.         p = #BUF_ITOA;
  704.         if(!number){
  705.                 ESBYTE[p] = '0';
  706.                 ESBYTE[p+1] = 0;
  707.                 return p;
  708.         }
  709.         ret = p;
  710.         if(number<0)
  711.         {
  712.                 $neg number
  713.                 ESBYTE[p] = '-';
  714.                 $inc p
  715.         }
  716.         while(mask)
  717.         {
  718.                 tmp = number / mask;
  719.                 tmp = tmp%10;
  720.                
  721.                 if(cmd){
  722.                         if(tmp){
  723.                                 ESBYTE[p] = tmp + '0';
  724.                                 $inc p
  725.                                 cmd = false;
  726.                         }
  727.                 }
  728.                 else {
  729.                         ESBYTE[p] = tmp + '0';
  730.                         $inc p
  731.                 }
  732.                 mask /= 10;
  733.         }
  734.         ESBYTE[p] = 0;
  735.         return ret;
  736. }
  737.  
  738. inline fastcall itoa_(signed int EDI, ESI)
  739. {
  740.     $pusha
  741.     EBX = EDI;
  742.     ECX = 10;
  743.     if (ESI > 90073741824)
  744.     {
  745.          $mov     al, '-'
  746.          $stosb
  747.          $neg     esi
  748.     }
  749.  
  750.     $mov     eax, esi
  751.     $push    -'0'
  752. F2:
  753.     $xor     edx, edx
  754.     $div     ecx
  755.     $push    edx
  756.     $test    eax, eax
  757.     $jnz     F2
  758. F3:
  759.     $pop     eax
  760.     $add     al, '0'
  761.     $stosb
  762.     $jnz     F3
  763.    
  764.     $mov     al, '\0'
  765.     $stosb
  766.  
  767.     $popa
  768.     return EBX;
  769. }
  770.  
  771. inline dword memchr(dword s,int c,signed len)
  772. {
  773.         if(!len) return NULL;
  774.         do {
  775.                 if(DSBYTE[s] == c) return s;
  776.                 $inc s
  777.                 $dec len
  778.         } while(len);
  779.         return NULL;
  780. }
  781.  
  782. inline dword strdup(dword text)
  783. {
  784.     dword l = strlen(text);
  785.     dword ret = malloc(l+1);
  786.         if(!ret) return NULL;
  787.     strlcpy(ret,text,l);
  788.     return ret;
  789. }
  790.  
  791. inline dword strndup(dword str, signed maxlen)
  792. {
  793.         dword copy,len;
  794.  
  795.         len = strnlen(str, maxlen);
  796.         copy = malloc(len + 1);
  797.         if (copy != NULL)
  798.         {
  799.                 strlcpy(copy, str, len);
  800.                 DSBYTE[len+copy] = '\0';
  801.         }
  802.         return copy;
  803. }
  804.  
  805. inline dword hexdec(dword text)
  806. {
  807.         char s;
  808.         dword ret,l;
  809.         ret = 0;
  810.         s = DSBYTE[text];
  811.         while(s)
  812.         {      
  813.                 ret <<= 4;
  814.                 if(s>='A')&&(s<='F')ret |= s-'A'+10;
  815.                 else if(s>='a')&&(s<='f')ret |= s-'a'+10;
  816.                 else if(s>='0')&&(s<='9')ret |= s-'0';
  817.                 text++;
  818.                 s = DSBYTE[text];
  819.         }
  820.         return ret;
  821. }
  822.  
  823. inline signed csshexdec(dword text)
  824. {
  825.         char s;
  826.         dword ret,l;
  827.         byte tmp;
  828.         l = strlen(text);
  829.         ret = 0;
  830.         s = DSBYTE[text];
  831.         tmp = 0;
  832.         if(l==6) while(s)
  833.         {      
  834.                 ret <<= 4;
  835.                 if(s>='A')&&(s<='F')ret |= s-'A'+10;
  836.                 else if(s>='a')&&(s<='f')ret |= s-'a'+10;
  837.                 else if(s>='0')&&(s<='9')ret |= s-'0';
  838.                 text++;
  839.                 s = DSBYTE[text];
  840.         }
  841.         else if(l==3) while(s)
  842.         {      
  843.                 ret |= tmp;
  844.                 ret <<= 4;
  845.                 ret |= tmp;
  846.                 ret <<= 4;
  847.                 if(s>='A')&&(s<='F')tmp = s-'A'+10;
  848.                 else if(s>='a')&&(s<='f')tmp = s-'a'+10;
  849.                 else if(s>='0')&&(s<='9')tmp = s-'0';
  850.                 text++;
  851.                 s = DSBYTE[text];
  852.         }
  853.         return ret;
  854. }
  855.  
  856. void miniprintf(dword dst, format, insert_line)
  857. {
  858.         dword in_pos = strchr(format, '%');
  859.         if (ESBYTE[in_pos+1] == 's') {
  860.                 strlcpy(dst, format, in_pos - format);
  861.                 strcat(dst, insert_line);
  862.                 strcat(dst, in_pos+2);
  863.         }
  864. }
  865.  
  866. inline cdecl int sprintf(dword buf, format,...)
  867. {
  868.         #define END_ARGS 0xFF00FF //ARGS FUNCTION
  869.         byte s;
  870.         char X[10];
  871.         dword ret, tmp, l;
  872.         dword arg = #format;
  873.         ret = buf;
  874.         s = DSBYTE[format];
  875.         while(s){
  876.                 if(s=='%'){
  877.                         arg+=4;
  878.                         tmp = DSDWORD[arg];
  879.                         if(tmp==END_ARGS)goto END_FUNC_SPRINTF;
  880.                         $inc format
  881.                         s = DSBYTE[format];
  882.                         if(!s)goto END_FUNC_SPRINTF;
  883.                         switch(s)
  884.                         {
  885.                                 case 's':
  886.                                         l = tmp;
  887.                                         s = DSBYTE[tmp];
  888.                                         while(s)
  889.                                         {
  890.                                                 DSBYTE[buf] = s;
  891.                                                 $inc tmp
  892.                                                 $inc buf
  893.                                                 s = DSBYTE[tmp];
  894.                                         }
  895.                                 break;
  896.                                 case 'c':
  897.                                         DSBYTE[buf] = tmp;
  898.                                         $inc buf
  899.                                 break;
  900.                                 case 'u': //if(tmp<0)return ret;
  901.                                 case 'd':
  902.                                 case 'i':
  903.                                         tmp = itoa(tmp);
  904.                                         if(!DSBYTE[tmp])goto END_FUNC_SPRINTF;
  905.                                         l = strlen(tmp);
  906.                                         strlcpy(buf,tmp,l);
  907.                                         buf += l;
  908.                                 break;
  909.                                 case 'a':
  910.                                 case 'A':
  911.                                         strlcpy(buf,"0x00000000",10);
  912.                                         buf+=10;
  913.                                         l=buf;
  914.                                         while(tmp)
  915.                                         {
  916.                                                 $dec buf
  917.                                                 s=tmp&0xF;
  918.                                                 if(s>9)DSBYTE[buf]='A'+s-10;
  919.                                                 else DSBYTE[buf]='0'+s;
  920.                                                 tmp>>=4;
  921.                                         }
  922.                                         buf=l;
  923.                                 break;
  924.                                 case 'p':
  925.                                         tmp = itoa(#tmp);
  926.                                         if(!DSBYTE[tmp])goto END_FUNC_SPRINTF;
  927.                                         l = strlen(tmp);
  928.                                         strlcpy(buf,tmp,l);
  929.                                         buf += l;
  930.                                 break;
  931.                                 case '%':
  932.                                         DSBYTE[buf] = '%';
  933.                                         $inc buf
  934.                                 break;
  935.                                 default:
  936.                                 goto END_FUNC_SPRINTF;
  937.                         }
  938.                 }
  939.                 else {
  940.                         DSBYTE[buf] = s;
  941.                         $inc buf
  942.                 }
  943.                 $inc format
  944.                 s = DSBYTE[format];
  945.         }
  946.         END_FUNC_SPRINTF:
  947.         DSBYTE[buf] = 0;
  948.         return ret;
  949. }
  950.  
  951. inline signed strcoll(dword text1,text2)
  952. {
  953.         char s,ss;
  954.         loop()
  955.         {
  956.                 s = DSBYTE[text2];
  957.                 ss=strchr(text1,s);
  958.                 if(ss)return ss;
  959.                 text2++;
  960.         }
  961.         return 0;
  962. }
  963.  
  964. // void * memset( ptr, value, num );
  965. // fills the memory with a dword
  966. // example: memset(str,'-', sizeof(str));
  967. inline void MEMSETD(EDI,ECX,EAX)
  968. {
  969.         $REP
  970.         $STOSD
  971. }
  972.  
  973. :replace_char(dword in_str, char from_char, to_char, int length) {
  974.         dword max = in_str + length;
  975.         while (in_str < max) {
  976.                 if (ESBYTE[in_str] == from_char) ESBYTE[in_str] = to_char;
  977.                 in_str++;
  978.         }
  979. }
  980.  
  981. #endif