Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

StringTool.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  */
00009 #include "StringTool.h"
00010 
00011 #include <sstream>
00012 
00013 //-----------------------------------------------------------------
00014 /**
00015  * Convert string to number.
00016  * @param strNum string
00017  * @param ok true on sucess
00018  * @return number or zero
00019  */
00020     long
00021 StringTool::readInt(const char *strNum, bool *ok)
00022 {
00023     long result = 0;
00024     *ok = false;
00025 
00026     if (strNum != NULL) {
00027         char *endptr;
00028         result = strtol(strNum, &endptr, 0);
00029         if (strNum[0] != '\0' && endptr[0] == '\0') {
00030             *ok = true;
00031         }
00032     }
00033 
00034     if (!ok) {
00035         result = 0;
00036     }
00037 
00038     return result;
00039 }
00040 //-----------------------------------------------------------------
00041 /**
00042  * Convert long to string
00043  * @return string value
00044  */
00045     std::string
00046 StringTool::toString(long value)
00047 {
00048     std::ostringstream buffer;
00049     buffer << value;
00050     return buffer.str();
00051 }
00052 //-----------------------------------------------------------------
00053     bool
00054 StringTool::startsWith(const std::string &str,
00055         const std::string &prefix)
00056 {
00057     return prefix == str.substr(0, prefix.size());
00058 }
00059 //-----------------------------------------------------------------
00060 /**
00061  * Replace one substring with another
00062  * @param buffer string to change
00063  * @param pattern what replace
00064  * @param newstring what to place
00065  */
00066     void
00067 StringTool::replace(std::string &buffer,
00068         const std::string &pattern, const std::string &newstring)
00069 {
00070     std::string::size_type pos = buffer.find(pattern);
00071     while (pos != std::string::npos) {
00072         buffer.replace(pos, pattern.size(), newstring);
00073         pos = buffer.find(pattern, pos + newstring.size());
00074     }
00075 }
00076 //-----------------------------------------------------------------
00077 /**
00078  * Split string.
00079  * @return splited segments without separator
00080  */
00081     StringTool::t_args
00082 StringTool::split(const std::string &str, char separator)
00083 {
00084     std::string remain = str;
00085     t_args args;
00086 
00087     std::string::size_type pos = remain.find(separator);
00088     while (pos != std::string::npos) {
00089         args.push_back(remain.substr(0, pos));
00090         remain.erase(0, pos + 1);
00091 
00092         pos = remain.find(separator);
00093     }
00094     if (remain.size() > 0) {
00095         args.push_back(remain);
00096     }
00097 
00098     return args;
00099 }
00100 

Generated on Wed Jun 1 09:54:31 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2