str_tok.h - Common mIRC identifiers in C++

By NIGathan on Apr 01, 2009

str_tok.h is filled with multiple common mIRC identifiers.

The following identifiers and operators have been added:
[Initial release - April 1, 2009]
$len - len(string text)
$addtok - string addtok(string text, string token, string delim)
$deltok - string deltok(string text, string token, string delim)
$findtok - int findtok(string tokens, string token, int tok, string delim)
$gettok - string gettok(string tokens, int tok, string delim)
$instok - string instok(string text, string token, int tok, string delim)
$istok - int istok(string text, string token, string delim)
$matchtok - string matchtok(string text, string token, int tok, string delim)
$numtok - int numtok(string tokens, string delim)
$puttok - string puttok(string text, string token, int tok, string delim)
$remtok - string remtok(string text, string token, int tok, string delim)
$reptok - string reptok(string text, string token, string stoken, int tok, string delim)
$wildtok - string wildtok(string tokens, string token, int tok, string delim)
isin - int isin(string text, string subtext, int pos = -1)
iswm - int iswm(string text, string subtext)

[Second release (v1.1) - April 2, 2009]
istok() is now a bool, instead of an int
$upper - string upper(string text)
$lower - string lower(string text)
iswm() is now a bool, instead of an int
isalpha - bool stringisalpha(string text)
isalnum - bool stringisalnum(string text)
islower - bool stringislower(string text)
isupper - bool stringisupper(string text)
isnum - bool stringisnum(string text) | bool stringisnum(string text, int lo) | bool stringisnum(string text, int lo, int hi)
isletter - bool isletter(string text) | bool isletter(string text, string subtext)

[Third release (v1.2) - April 11, 2009]
addtok() now doesn't add an extra delimiter char to the beginning if string text is empty.
string appendtok(string text, string token, string delim) - Works just like addtok(), except even though the token already exists, it will be added.
$replace - string strreplace(string text, string find, string replace)
$rand - int randint(int lo, int hi) Keep in mine this does not work with chars.
string randtok(string text, string delim) - Returns a random token.
string randomizetok(string text, string delim) - Returns the tokens in a random order.
$left - string strleft(string text, int n) Allows negative n.
$right - string strright(string text, int n) Allows negative n.
$mid - string strmid(string text, int start, int len)
$str - string strrep(string text, int rep)

[Fourth release (v1.3) - April 15, 2009]
Fixed bug in strright().
$readini - string readini(string loc, string section, string item, int n = 1) - Only new thing here is n, it is optional, but if specified it will allow you to get the nth item in section. (Incase you have two items the same name, you can get the one you want)

len() is fairly useless, unless you want to check the size of an undeclared string, for example: len("how many chars is this?")

isin is a bit different though. I combined $pos and isin, if you take a look at the $pos section of the mIRC help file, youll probably understand. Instead of doing "ah isin blah" you would do "isin(blah,ah)" and same with iswm. The third param in isin() allows it to act exactly like $pos. Example: isin(testicles,t,2) would return 3 because the second instance of 't' is at the 3rd char (in reality its the 4th. Keep in mind 0 is a char place too.) If you specify 0 as the third param, the total number of instances of the second param within the first param is returned. Does not accept negative values.

Other differences between this header and mIRC:
The delimiter is the actual character, not the ascii code of it.
remtok() and reptok() accept negative values.
isletter() will always return false if the first param is more than one character (because two or more characters is not a letter)

Just add this file to the same dir and #include "str_tok.h"

Note: This is NOT compatible with C. C lacks support for STL strings.

Heres an example code showing use of all the functions. Compiled .exe, and source included. (Updated for v1.2)

http://www.mediafire.com/?cmfgyxvg2lj

// str_tok.h written by NIGathan
// Allows functionality of useful mIRC identifiers in C++
// Version 1.3

#ifndef str_tok
#define str_tok

#include <iostream>
#include <string>
#include <locale>
#include <sstream>
#include <fstream>
using namespace std;

int randint(int lo, int hi)
{
    static bool seeded = false;
    if (!seeded) srand(time(NULL)), seeded = true;
    int i = lo + rand() % hi;
    return i;
}

int len(string text)
{
    return text.size();
}

int numtok(string tokens, string delim)
{
    if (tokens.size() == 0) return 0;
    int y = 1;
    for (int x = 0; x < tokens.size(); x++) if (tokens.compare(x,1,delim) == 0) y++;
    return y;
}

string gettok(string tokens, int tok, string delim)
{
     int x = 1, y = 0;
     string ret;
     if (tok < 0) tok = numtok(tokens,delim)+tok+1;
     for (int a = 0; a < tokens.size(); a++)
     {
         if (tokens.compare(a,1,delim) == 0) x++;
         if (x == tok) y = x;
         if (y > 0)
         {
                  if (x > y) break;
                  ret.append(tokens,a,1);
         }
     }
     if (ret.compare(0,1,delim) == 0) return ret.erase(0,1);
     else return ret;
}

int isin(string text, string subtext, int pos = -1)
{
    int a = 0, x, b = 0;
    for (x = 0; x < text.size(); x++)
    {
             if (text.compare(x,1,subtext,0,1) == 0)
             {
                a = 1;
                for (int y = 1; y < subtext.size(); y++)
                {
                    if (a == 0) break;
                    if (text.compare(x+y,1,subtext,y,1) != 0) a = 0;
                }
                if (a == 1) b++;
                if (a == 1 && b >= pos)
                {
                      if (pos == 0) continue;
                      else break;
                }
             }
    }
    if (pos == 0) return b;
    if (b == pos)
    {
            if (a == 1) return x;
            else return -1;
    }
    else if (pos > 0) return -1;
    else return a;
}

bool iswm(string text, string subtext)
{
    int a = -1, b = numtok(subtext,"*"), r, l = 0;
    string str;
    for (int y = 1; y <= b; y++)
    {
        if (l > 0) { str = gettok(gettok(subtext,y-1,"*"),l,"?"); l--; }
        str = gettok(subtext,y,"*");
        if (str.compare(0,1,"?") == 0)
        {
           str.erase(0,1);
           a++;
        }
        else if (isin(str,"?",1) > 0)
        {
             if (str.compare(str.size()-2,1,"?") == 0) a++;
             else l++;
             str = gettok(str,1,"?");
        }
        else l = 0;
        r = 0;
        if (y == 1 && subtext.compare(0,1,"*") != 0 && a == -1)
        {
              for (int s = 0; s < str.size()*-1; s--)
              {
                  if (text.compare(s,1,str,s,1) != 0)
                  {
                     r = 1;
                     break;
                  }
              }
              if (r == 1) break;
        }
        if (str.size() == 0) continue;
        r = 0;
        for (int g = 1; g <= isin(text,str,0); g++)
        {
            if (a < isin(text,str,g))
            {
               a = isin(text,str,g)+str.size();
               r = 2;
               break;
            }
            if (g == isin(text,str,0)) r = 1;
        }
        if (r != 2)
        {
              r = 1;
              break;
        }
    }
    if (r == 1) return false;
    else return true;
}

int findtok(string tokens, string token, int tok, string delim)
{
    int x, a = 0;
    for (x = 1; x <= numtok(tokens,delim); x++)
    {
        if (token.compare(gettok(tokens,x,delim)) == 0) a++;
        if ((a == tok) && (tok > 0)) break;
    }
    if (tok == 0) return a;
    if (a == tok) return x;
    else return 0;
}

string wildtok(string tokens, string token, int tok, string delim)
{
    int a = 0;
    string str, ret;
    for (int x = 1; x <= numtok(tokens,delim); x++)
    {
        str = gettok(tokens,x,delim);
        if (iswm(str,token)) a++;
        if (a == tok) break;
    }
    if (a == tok) return str;
    else return ret;
}

bool istok(string text, string token, string delim)
{
    if (findtok(text,token,1,delim) > 0) return true;
    else return false;
}

string addtok(string text, string token, string delim)
{
    if (!istok(text,token,delim))
    {
        if (text.size() >= 1) text = text + delim + token;
        else text = token;
    }
    return text;
}

string deltok(string text, int tok, string delim)
{
    string ret;
    int a = numtok(text,delim);
    if ((tok > a) || (tok < a*-1) || (tok == 0)) return text;
    if (tok < 0) tok = a+tok+1;
    for (int x = 1; x <= a; x++) if (x != tok) ret = ret + delim + gettok(text,x,delim);
    if (ret.compare(0,1,delim) == 0) return ret.erase(0,1);
    else return ret;
}

string instok(string text, string token, int tok, string delim)
{
    string ret;
    int a = numtok(text,delim);
    if ((tok > a) || (tok < a*-1) || (tok == 0)) return text;
    if (tok < 0) tok = a+tok+1;
    for (int x = 1; x <= a; x++)
    {
        if (x == tok) ret = ret + delim + token + delim + gettok(text,x,delim);
        else ret = ret + delim + gettok(text,x,delim);
    }
    if (ret.compare(0,1,delim) == 0) return ret.erase(0,1);
    else return ret;
}

string matchtok(string text, string token, int tok, string delim)
{
    int a = numtok(text,delim), y = 0, x;
    if ((tok > a) || (tok == 0)) return "";
    for (x = 1; x <= a; x++)
    {
        if (isin(gettok(text,x,delim),token) == 1) y++;
        if (y == tok) break;
    }
    if (y == tok) return gettok(text,x,delim);
    else return "";
}

string puttok(string text, string token, int tok, string delim)
{
    string ret;
    int a = numtok(text,delim);
    if ((tok > a) || (tok == 0) || (tok < a*-1)) return text;
    if (tok < 0) tok = a+tok+1;
    for (int x = 1; x <= a; x++)
    {
        if (x == tok) ret = ret + delim + token;
        else ret = ret + delim + gettok(text,x,delim);
    }
    if (ret.compare(0,1,delim) == 0) return ret.erase(0,1);
    else return ret;
}

string remtok(string text, string token, int tok, string delim)
{
    int a = findtok(text,token,0,delim);
    if ((tok > a) || (tok < a*-1) || (a == 0)) return text;
    if (tok < 0) tok = a+tok+1;
    if ((tok > 0) || (a == 1)) return deltok(text,findtok(text,token,tok > 0 ? tok : 1,delim),delim);
    for (int x = 1; x <= a; x++) text = deltok(text,findtok(text,token,1,delim),delim);
    return text;
}

string reptok(string text, string token, string stoken, int tok, string delim)
{
    int a = findtok(text,token,0,delim);
    if ((tok > a) || (tok < a*-1) || (a == 0)) return text;
    if (tok < 0) tok = a+tok+1;
    if ((tok > 0) || (a == 1)) return puttok(text,stoken,findtok(text,token,tok > 0 ? tok : 1,delim),delim);
    for (int x = 1; x <= a; x++) text = puttok(text,stoken,findtok(text,token,1,delim),delim);
    return text;
}

bool stringisalpha(string text)
{
    for (int x = 0; x < text.size(); x++)
    {
        if (!isalpha(text[x])) return false;
    }
    return true;
}

bool stringisalnum(string text)
{
    for (int x = 0; x < text.size(); x++)
    {
        if (!isalnum(text[x])) return false;
    }
    return true;
}

bool stringislower(string text)
{
    for (int x = 0; x < text.size(); x++)
    {
        if (!islower(text[x]) && isalpha(text[x])) return false;
    }
    return true;
}

bool stringisupper(string text)
{
    for (int x = 0; x < text.size(); x++)
    {
        if (!isupper(text[x]) && isalpha(text[x])) return false;
    }
    return true;
}

bool stringisnum(string text)
{
     if (text.compare(0,1,"-") == 0) text.erase(0,1);
     for (int x = 0; x < text.size(); x++)
     {
         if (!isdigit(text[x])) return false;
     }
     return true;
}

bool stringisnum(string text, int lo)
{
     int num, neg;
     if (text.compare(0,1,"-") == 0) { text.erase(0,1); neg = 1; }
     if (!stringisnum(text)) return false;
     stringstream(text) >> num;
     if (neg == 1) num = num*-1;
     if (lo == num) return true;
     return false;
}

bool stringisnum(string text, int lo, int hi)
{
     int num, neg;
     if (text.compare(0,1,"-") == 0) { text.erase(0,1); neg = 1; }
     if (!stringisnum(text)) return false;
     stringstream(text) >> num;
     if (neg == 1) num = num*-1;
     if (lo <= num && num <= hi) return true;
     return false;
}

bool isletter(string text)
{
     if ((text.size() == 1) && (isalpha(text[0]))) return true;
     return false;
}

bool isletter(string text, string subtext)
{
     if ((isletter(text)) && (isin(subtext,text))) return true;
     return false;
}

string upper(string text)
{
    string ret, t;
    for (int x = 0; x < text.size(); x++)
    {
        if (islower(text[x])) { t = toupper(text[x]); ret.append(t); }
        else { t = text[x]; ret.append(t); }
    }
    return ret;
}

string lower(string text)
{
    string ret, t;
    for (int x = 0; x < text.size(); x++)
    {
        if (isupper(text[x])) { t = tolower(text[x]); ret.append(t); }
        else { t = text[x]; ret.append(t); }
    }
    return ret;
}

string strreplace(string text, string find, string replace)
{
    int flen = find.size(), rlen = replace.size();
    if (!isin(text,find)) return text;
    for (int x = 0; x < text.size(); x++)
    {
        if (text.compare(x,flen,find) == 0)
        {
            text.erase(x,flen);
            text.insert(x,replace);
            x+=rlen-1;
        }
    }
    return text;
}

string appendtok(string text, string token, string delim)
{
    if (text.size() >= 1) text = text + delim + token;
    else text = token;
    return text;
}

string randtok(string text, string delim)
{
    if (numtok(text,delim) <= 1) return text;
    return gettok(text,randint(1,numtok(text,delim)),delim);
}

string randomizetok(string text, string delim)
{
    if (numtok(text,delim) <= 1) return text;
    string ret;
    int x;
    while (numtok(text,delim) > 0)
    {
        x = randint(1,numtok(text,delim));
        ret = appendtok(ret,gettok(text,x,delim),delim);
        text = deltok(text,x,delim);
    }
    return ret;
}

string strleft(string text, int n)
{
    string ret;
    if (n < 0) ret.append(text,text.size()-n,n*-1);
    else ret.append(text,0,n);
    return ret;
}

string strright(string text, int n)
{
    string ret;
    if (n < 0) ret.append(text,0,n*-1);
    else ret.append(text,text.size()-n,n);
    return ret;
}

string strmid(string text, int start, int len)
{
    string ret;
    if (start > text.size()) return ret;
    ret.append(text,start,len);
    return ret;
}

string strrep(string text, int rep)
{
    string ret;
    for (int x = 1; x <= rep; x++) ret.append(text);
    return ret;
}

string readini(string loc, string section, string item, int n = 1)
{
    ifstream file (loc.c_str());
    string line, ret;
    int m = 1, i = 0, c = -1;
    if (!file.is_open()) return "";
    while (!file.eof())
    {
        getline(file,line);
        if ((strleft(line,1) == "[") && (strright(line,1) == "]")) i++;
        if (c == i)
        {
            if (gettok(line,1,"=") == item)
            {
                if (m == n)
                {
                    ret = line.erase(0,isin(line,"=",1)+1);
                    break;
                }
                m++;
            }
        }
        if ((strleft(line,1) == "[") && (strright(line,1) == "]") && (strmid(line,1,line.size()-2) == section)) c = i;
    }
    file.close();
    return ret;
}

#endif

Comments

Sign in to comment.
ulquiorra4   -  Apr 27, 2012

oh nice....

 Respond  
NIGathan   -  Apr 26, 2012

I have updated this a very good bit since my leave here. Maybe I'll get around to posting it.

 Respond  
NIGathan   -  Apr 11, 2009

Another update, check third release (v1.2) section of the description.

Still haven't finished $sorttok, it's not quite done yet, still need to tweak the char sorting and create some symbol sorting method.

In the mean time, here are a few more functions.

New example has been uploaded as well.

 Respond  
NIGathan   -  Apr 02, 2009

Updated to v1.1, a lot has been added, check description for details :D

I also uploaded a new example.

Only thing left on my todo list now is $sorttok

I am also open to any additional ideas

 Respond  
NIGathan   -  Apr 02, 2009

Well I just found a function called strtok() so I may end up naming this header differently.

Also, I will be updating it soon with a sorttok() function as well as adding the isalpha, isalnum, ..etc operators as functions.

 Respond  
NIGathan   -  Apr 01, 2009

Thanks Hawkee :D

 Respond  
Hawkee   -  Apr 01, 2009

Nice work!

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.