StringUtils.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #include "debug/Log.h"
00019 #include "StringUtils.h"
00020 
00021 namespace oasys {
00022 
00023 int
00024 tokenize(const std::string& str,
00025          const std::string& sep,
00026          std::vector<std::string>* tokens)
00027 {
00028     size_t start, end;
00029 
00030     tokens->clear();
00031 
00032     start = str.find_first_not_of(sep);
00033     if (start == std::string::npos || start == str.length()) {
00034         return 0; // nothing to do
00035     }
00036     
00037     while (1) {
00038         end = str.find_first_of(sep, start);
00039         if (end == std::string::npos) {
00040             end = str.length();
00041         }
00042         
00043         tokens->push_back(str.substr(start, end - start));
00044         
00045         if (end == str.length()) {
00046             break; // all done
00047         }
00048         
00049         start = str.find_first_not_of(sep, end);
00050         if (start == std::string::npos) {
00051             break; // all done
00052         }
00053     }
00054 
00055     return tokens->size();
00056 }
00057 
00058 void
00059 StringSet::dump(const char* log) const
00060 {
00061     logf(log, LOG_DEBUG, "dumping string set...");
00062     for (iterator i = begin(); i != end(); ++i) {
00063         logf(log, LOG_DEBUG, "\t%s", i->c_str());
00064     }
00065 }
00066 
00067 void
00068 StringHashSet::dump(const char* log) const
00069 {
00070     logf(log, LOG_DEBUG, "dumping string set...");
00071     for (iterator i = begin(); i != end(); ++i) {
00072         logf(log, LOG_DEBUG, "\t%s", i->c_str());
00073     }
00074 }
00075 
00076 //----------------------------------------------------------------------------
00077 const char*
00078 bool_to_str(bool b)
00079 {
00080     if (b) 
00081     {
00082         return "true";
00083     }
00084     
00085     return "false";
00086 }
00087 
00088 //----------------------------------------------------------------------------
00089 const char*
00090 str_if(bool b, const char* true_str, const char* false_str)
00091 {
00092     if (b) 
00093     {
00094         return true_str;
00095     }
00096     else
00097     {
00098         return false_str;
00099     }
00100 }
00101 
00102 } // namespace oasys

Generated on Thu Jun 7 16:56:52 2007 for DTN Reference Implementation by  doxygen 1.5.1