Advertisement
Guest User

IT"S NOT MINE< IT IS Westie's CODE!

a guest
Apr 10th, 2012
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.58 KB | None | 0 0
  1. /*
  2.     strlib library for string manipulation.
  3.     Created by David Weston
  4.  
  5.     Version: 1.3
  6.     Licence: http://www.typefish.co.uk/licences/
  7. */
  8.  
  9.  
  10. /* Stuff that needs to be included. */
  11. #include <a_samp>
  12.  
  13.  
  14. /* Pretty key definitions */
  15. #if defined _strlib_included
  16.     #endinput
  17. #endif
  18. #define _strlib_included
  19. #define _strlib_sma_string 128
  20. #define _strlib_med_string 256
  21. #define _strlib_big_string 512
  22.  
  23.  
  24. /* Natives for tricking PAWNO. */
  25. /*
  26.     native str_replace(sSearch[], sReplace[], const sSubject[], &iCount = 0)
  27.     native str_ireplace(sSearch[], sReplace[], const sSubject[], &iCount = 0)
  28.     native str_pad(const sSource[], iPadLength, cPadChar = ' ')
  29.     native str_repeat(const sSource[], iMultiplier)
  30.     native str_rot13(const sSource[])
  31.     native strtr(const sSource[], sRemove[], sReplace[])
  32.     native substr(const sSource[], iStart, iLength = sizeof(sSource))
  33.     native trim(const sSource[])
  34.     native rtrim(const sSource[])
  35.     native ltrim(const sSource[])
  36.     native implode(const aPieces[][], const sGlue[] = " ")
  37.     native explode(aExplode[][], sSource[], sDelimiter[] = " ")
  38.     native explodea(aExplode[][], sSource[], sDelimiter[] = " ")
  39.     native str_in_array(sNeedle[], aHaystack[][])
  40. */
  41.  
  42.  
  43. /*
  44.     str_replace:
  45.         Case sensitive string replace
  46.  
  47.     Arguments:
  48.         sSearch[]       String to search for
  49.         sReplace[]      String to replace with
  50.         sSubject[]      Original string
  51.         (op) &iCount    How many times 'sSearch' has been replaced.
  52.  
  53.     Returns:
  54.         Replaced string.
  55. */
  56. stock str_replace(sSearch[], sReplace[], const sSubject[], &iCount = 0)
  57. {
  58.     new
  59.         iLengthTarget = strlen(sSearch),
  60.         iLengthReplace = strlen(sReplace),
  61.         iLengthSource = strlen(sSubject),
  62.         iItterations = (iLengthSource - iLengthTarget) + 1;
  63.  
  64.     new
  65.         sTemp[128],
  66.         sReturn[_strlib_med_string];
  67.  
  68.     strcat(sReturn, sSubject, _strlib_med_string);
  69.     iCount = 0;
  70.  
  71.     for(new iIndex; iIndex < iItterations; ++iIndex)
  72.     {
  73.         strmid(sTemp, sReturn, iIndex, (iIndex + iLengthTarget), (iLengthTarget + 1));
  74.  
  75.         if(!strcmp(sTemp, sSearch, false))
  76.         {
  77.             strdel(sReturn, iIndex, (iIndex + iLengthTarget));
  78.             strins(sReturn, sReplace, iIndex, iLengthReplace);
  79.  
  80.             iIndex += iLengthTarget;
  81.             iCount++;
  82.         }
  83.     }
  84.  
  85.     return sReturn;
  86. }
  87.  
  88.  
  89. /*
  90.     str_ireplace:
  91.         Case insensitive string replace
  92.  
  93.     Arguments:
  94.         sSearch[]       String to search for
  95.         sReplace[]      String to replace with
  96.         sSubject[]      Original string
  97.         (op) &iCount    How many times 'sSearch' has been replaced.
  98.  
  99.     Returns:
  100.         Replaced string.
  101. */
  102. stock str_ireplace(sSearch[], sReplace[], const sSubject[], &iCount = 0)
  103. {
  104.     new
  105.         iLengthTarget = strlen(sSearch),
  106.         iLengthReplace = strlen(sReplace),
  107.         iLengthSource = strlen(sSubject),
  108.         iItterations = (iLengthSource - iLengthTarget) + 1;
  109.  
  110.     new
  111.         sTemp[128],
  112.         sReturn[_strlib_med_string];
  113.  
  114.     strcat(sReturn, sSubject, _strlib_med_string);
  115.     iCount = 0;
  116.  
  117.     for(new iIndex; iIndex < iItterations; ++iIndex)
  118.     {
  119.         strmid(sTemp, sReturn, iIndex, (iIndex + iLengthTarget), (iLengthTarget + 1));
  120.  
  121.         if(!strcmp(sTemp, sSearch, true))
  122.         {
  123.             strdel(sReturn, iIndex, (iIndex + iLengthTarget));
  124.             strins(sReturn, sReplace, iIndex, iLengthReplace);
  125.  
  126.             iIndex += iLengthTarget;
  127.             iCount++;
  128.         }
  129.     }
  130.  
  131.     return sReturn;
  132. }
  133.  
  134.  
  135. /*
  136.     str_pad:
  137.         Pad a string with characters until the defined amount.
  138.  
  139.     Arguments:
  140.         sSource[]         String input to pad.
  141.         iPadLength       Pad to amount. If less than sSource, sSource is returned.
  142.         (op) cPadChar    Character to use as padding.
  143.  
  144.     Returns:
  145.         Padded string.
  146. */
  147. stock str_pad(const sSource[], iPadLength, cPadChar = ' ')
  148. {
  149.     new
  150.         iInputLength = strlen(sSource),
  151.         sReturn[_strlib_med_string];
  152.  
  153.     strcat(sReturn, sSource, _strlib_med_string);
  154.  
  155.     if((iInputLength >= iPadLength) && (iPadLength > 255))
  156.     {
  157.         return sReturn;
  158.     }
  159.  
  160.     while(iInputLength < iPadLength)
  161.     {
  162.         sReturn[iInputLength] = cPadChar;
  163.         ++iInputLength;
  164.     }
  165.  
  166.     return sReturn;
  167. }
  168.  
  169.  
  170. /*
  171.     str_repeat:
  172.         Repeats a string 'iMultiplier' times.
  173.  
  174.     Arguments:
  175.         sSource[]     String to be repeated
  176.         iMultiplier  Amount of times to be repeated
  177.  
  178.     Returns:
  179.         Repeated string.
  180. */
  181. stock str_repeat(const sSource[], iMultiplier)
  182. {
  183.     new
  184.         iInputLength = strlen(sSource),
  185.         iItteration = (iInputLength * iMultiplier),
  186.         iIndex,
  187.         sReturn[_strlib_med_string];
  188.  
  189.     while(iIndex < iItteration)
  190.     {
  191.         strins(sReturn, sSource, iIndex);
  192.         iIndex += iInputLength;
  193.     }
  194.  
  195.     return sReturn;
  196. }
  197.  
  198.  
  199. /*
  200.     str_rot13:
  201.         Rotates alphabetical characters 13 characters along.
  202.  
  203.     Arguments:
  204.         sSource[]     String to be rotated
  205.  
  206.     Returns:
  207.         Rotated string.
  208. */
  209. stock str_rot13(const sSource[])
  210. {
  211.     new
  212.         iInputLength = strlen(sSource),
  213.         sReturn[_strlib_med_string];
  214.  
  215.     for(new iIndex; iIndex < iInputLength; ++iIndex)
  216.     {
  217.         switch(sSource[iIndex])
  218.         {
  219.             case 65..77:
  220.             {
  221.                 sReturn[iIndex] = sSource[iIndex] + 13;
  222.             }
  223.             case 78..90:
  224.             {
  225.                 sReturn[iIndex] = sSource[iIndex] - 13;
  226.             }
  227.             case 97..109:
  228.             {
  229.                 sReturn[iIndex] = sSource[iIndex] + 13;
  230.             }
  231.             case 110..122:
  232.             {
  233.                 sReturn[iIndex] = sSource[iIndex] - 13;
  234.             }
  235.             default:
  236.             {
  237.                 sReturn[iIndex] = sSource[iIndex];
  238.             }
  239.         }
  240.     }
  241.  
  242.     return sReturn;
  243. }
  244.  
  245.  
  246. /*
  247.     strtr:
  248.         Removes 'sRemove' from the string and replaces
  249.         that character from 'sReplace'.
  250.  
  251.     Arguments:
  252.         sSource[]     String to be transformed
  253.         sRemove[]    Characters to be removed (must be in same order as below!)
  254.         sReplace[]   Characters to be replaced (must be in same order as above!)
  255.  
  256.     Returns:
  257.         Trimmed string.
  258. */
  259. stock strtr(const sSource[], sRemove[], sReplace[])
  260. {
  261.     new
  262.         iCurrentChar = 0,
  263.         iInputLength = strlen(sSource),
  264.         iRemoveLength = strlen(sRemove);
  265.  
  266.     new
  267.         iDeleteChar = -1,
  268.         sReturn[_strlib_med_string];
  269.  
  270.     for(new iIndex; iIndex < iInputLength; ++iIndex)
  271.     {
  272.         for(new iChar; iChar < iRemoveLength; ++iChar)
  273.         {
  274.             if(sSource[iIndex] == sRemove[iChar])
  275.             {
  276.                 iDeleteChar = iChar;
  277.                 break;
  278.             }
  279.         }
  280.  
  281.         switch(iDeleteChar)
  282.         {
  283.             case -1:
  284.             {
  285.                 sReturn[iCurrentChar] = sSource[iIndex];
  286.                 iCurrentChar++;
  287.             }
  288.             default:
  289.             {
  290.                 if(strlen(sReplace) > iDeleteChar)
  291.                 {
  292.                     sReturn[iCurrentChar] = sReplace[iDeleteChar];
  293.                     iCurrentChar++;
  294.                 }
  295.                 iDeleteChar = -1;
  296.             }
  297.         }
  298.     }
  299.  
  300.     return sReturn;
  301. }
  302.  
  303.  
  304. /*
  305.     trim:
  306.         Removes whitespace, tabs, and new lines
  307.         from the beginning and end of 'sSource'.
  308.  
  309.     Arguments:
  310.         sSource[]     String to be trimmed
  311.  
  312.     Returns:
  313.         Trimmed string.
  314. */
  315. stock trim(const sSource[])
  316. {
  317.     new
  318.         iBegin,
  319.         iEnd,
  320.         iInputLength = strlen(sSource),
  321.         sReturn[_strlib_med_string];
  322.  
  323.     strcat(sReturn, sSource, _strlib_med_string);
  324.  
  325.     for(iBegin = 0; iBegin < iInputLength; ++iBegin)
  326.     {
  327.         switch(sReturn[iBegin])
  328.         {
  329.             case ' ', '\t', '\r', '\n':
  330.             {
  331.                 continue;
  332.             }
  333.             default:
  334.             {
  335.                 break;
  336.             }
  337.         }
  338.     }
  339.  
  340.     for(iEnd = (iInputLength - 1); iEnd > iBegin; --iEnd)
  341.     {
  342.         switch(sReturn[iEnd])
  343.         {
  344.             case ' ', '\t', '\r', '\n':
  345.             {
  346.                 continue;
  347.             }
  348.             default:
  349.             {
  350.                 break;
  351.             }
  352.         }
  353.     }
  354.  
  355.     strdel(sReturn, (iEnd + 1), iInputLength);
  356.     strdel(sReturn, 0, iBegin);
  357.  
  358.     return sReturn;
  359. }
  360.  
  361.  
  362. /*
  363.     ltrim:
  364.         Removes whitespace, tabs, and new lines
  365.         from the beginning of 'sSource'.
  366.  
  367.     Arguments:
  368.         sSource[]     String to be trimmed
  369.  
  370.     Returns:
  371.         Trimmed string.
  372. */
  373. stock ltrim(const sSource[])
  374. {
  375.     new
  376.         iBegin,
  377.         iInputLength = strlen(sSource),
  378.         sReturn[_strlib_med_string];
  379.  
  380.     strcat(sReturn, sSource, _strlib_med_string);
  381.  
  382.     for(iBegin = 0; iBegin < iInputLength; ++iBegin)
  383.     {
  384.         switch(sReturn[iBegin])
  385.         {
  386.             case ' ', '\t', '\r', '\n':
  387.             {
  388.                 continue;
  389.             }
  390.             default:
  391.             {
  392.                 break;
  393.             }
  394.         }
  395.     }
  396.  
  397.     strdel(sReturn, 0, iBegin);
  398.     return sReturn;
  399. }
  400.  
  401.  
  402. /*
  403.     rtrim:
  404.         Removes whitespace, tabs, and new lines
  405.         from the end of 'sSource'.
  406.  
  407.     Arguments:
  408.         sSource[]     String to be trimmed
  409.  
  410.     Returns:
  411.         Trimmed string.
  412. */
  413. stock rtrim(const sSource[])
  414. {
  415.     new
  416.         iEnd,
  417.         iInputLength = strlen(sSource),
  418.         sReturn[_strlib_med_string];
  419.  
  420.     strcat(sReturn, sSource, _strlib_med_string);
  421.  
  422.     for(iEnd = (iInputLength - 1); iEnd > 0; --iEnd)
  423.     {
  424.         switch(sReturn[iEnd])
  425.         {
  426.             case ' ', '\t', '\r', '\n':
  427.             {
  428.                 continue;
  429.             }
  430.             default:
  431.             {
  432.                 break;
  433.             }
  434.         }
  435.     }
  436.  
  437.     strdel(sReturn, (iEnd + 1), iInputLength);
  438.     return sReturn;
  439. }
  440.  
  441.  
  442. /*
  443.     substr:
  444.         Gets a substring from a string.
  445.  
  446.     Arguments:
  447.         sSource[]     String to be substring'd.
  448.         iStart        Position for the start of substring.
  449.         iLength       Position for the end of substring.
  450.                         (if negative, cells away from end)
  451.  
  452.     Returns:
  453.         Substring.
  454. */
  455. stock substr(const sSource[], iStart, iLength = sizeof sSource)
  456. {
  457.     new
  458.         sReturn[_strlib_med_string];
  459.  
  460.     if(iLength < 0)
  461.     {
  462.         strmid(sReturn, sSource, iStart, strlen(sSource) + iLength);
  463.         return sReturn;
  464.     }
  465.     else
  466.     {
  467.         strmid(sReturn, sSource, iStart, (iStart + iLength));
  468.         return sReturn;
  469.     }
  470. }
  471.  
  472.  
  473. /*
  474.     implode:
  475.         Returns a string where the array has been stuck back together
  476.         again.
  477.  
  478.     Arguments:
  479.         aPieces[][]   The array to glue back together.
  480.         sGlue         The string to use as the glue.
  481.  
  482.     Returns:
  483.         The imploded string.
  484. */
  485. stock implode(const aPieces[][], const sGlue[] = " ", iVertices = sizeof aPieces)
  486. {
  487.     new
  488.         sReturn[_strlib_med_string];
  489.  
  490.     while(iVertices != -1)
  491.     {
  492.         strins(sReturn, aPieces[iVertices], 0);
  493.  
  494.         if(iVertices != 0)
  495.         {
  496.             strins(sReturn, sGlue, 0);
  497.         }
  498.  
  499.         --iVertices;
  500.     }
  501.  
  502.     return sReturn;
  503. }
  504.  
  505.  
  506. /*
  507.     explode:
  508.         Creates an array of values from 'sSource', where only the exact amount of
  509.         values matching sizeof(aExplode) are returned.
  510.  
  511.     Arguments:
  512.         aExplode[][]  The exploded array
  513.         sSource[]     Source string.
  514.         sDelimiter    The string to use as the delimiter.
  515.  
  516.     Returns:
  517.         Returns -1 on failure, otherwise success.
  518. */
  519. stock explode(aExplode[][], const sSource[], const sDelimiter[] = " ", iVertices = sizeof aExplode, iLength = sizeof aExplode[])
  520. {
  521.     new
  522.         iNode,
  523.         iPointer,
  524.         iPrevious = -1,
  525.         iDelimiter = strlen(sDelimiter);
  526.  
  527.     while(iNode < iVertices)
  528.     {
  529.         iPointer = strfind(sSource, sDelimiter, false, iPointer);
  530.  
  531.         if(iPointer == -1)
  532.         {
  533.             strmid(aExplode[iNode], sSource, iPrevious, strlen(sSource), iLength);
  534.             break;
  535.         }
  536.         else
  537.         {
  538.             strmid(aExplode[iNode], sSource, iPrevious, iPointer, iLength);
  539.         }
  540.  
  541.         iPrevious = (iPointer += iDelimiter);
  542.         ++iNode;
  543.     }
  544.  
  545.     return iPrevious;
  546. }
  547.  
  548.  
  549. /*
  550.     explodea:
  551.         Creates an array of values from 'sSource', where any overrun is stored in
  552.         the final node.
  553.  
  554.     Arguments:
  555.         aExplode[][]  The exploded array
  556.         sSource[]     Source string.
  557.         sDelimiter    The string to use as the delimiter.
  558.  
  559.     Returns:
  560.         Returns -1 on failure, otherwise success.
  561. */
  562. stock explodea(aExplode[][], const sSource[], const sDelimiter[] = " ", iVertices = sizeof aExplode, iLength = sizeof aExplode[])
  563. {
  564.     new
  565.         iNode,
  566.         iPointer,
  567.         iPrevious = -1,
  568.         iSource = strlen(sSource),
  569.         iDelimiter = strlen(sDelimiter);
  570.  
  571.     while(iNode < iVertices)
  572.     {
  573.         iPointer = strfind(sSource, sDelimiter, false, iPointer);
  574.  
  575.         strmid(aExplode[iNode], sSource, iPrevious, (iNode == (iVertices - 1) ? iSource : iPointer), iLength);
  576.  
  577.         iPrevious = (iPointer += iDelimiter);
  578.         ++iNode;
  579.     }
  580.  
  581.     return iPrevious;
  582. }
  583.  
  584.  
  585. /*
  586.     str_in_array:
  587.         Checks if a string matches any of the strings in the array.
  588.  
  589.     Arguments:
  590.         sNeedle[]     String that is being matched.
  591.         aHaystack[][] Array with strings to be searched,
  592.  
  593.     Returns:
  594.         Returns true on a match.
  595. */
  596. stock bool:str_in_array(const sNeedle[], const aHaystack[][], const iHaystack = sizeof aHaystack)
  597. {
  598.     new iNode = 0;
  599.  
  600.     while(iNode < iHaystack)
  601.     {
  602.         if(!strcmp(sNeedle, aHaystack[iNode], true))
  603.         {
  604.             return true;
  605.         }
  606.  
  607.         ++iNode;
  608.     }
  609.  
  610.     return false;
  611. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement