00001 /* Copyright (c) 2010, Cedrus Corporation 00002 * All rights reserved. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions are 00006 * met: 00007 * 00008 * Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * 00011 * Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 00015 * Neither the name of Cedrus Corporation nor the names of its 00016 * contributors may be used to endorse or promote products derived from 00017 * this software without specific prior written permission. 00018 * 00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00020 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00021 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00022 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00023 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00025 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00026 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00027 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00028 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00029 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 */ 00031 00032 #ifndef STRING_TOKENIZER_H 00033 #define STRING_TOKENIZER_H 00034 00035 #include <vector> 00036 00037 namespace cedrus 00038 { 00048 template<typename string_type> 00049 std::vector<string_type> tokenize( 00050 const string_type &str, const string_type &delimiters) 00051 { 00052 std::vector<string_type> tokens; 00053 string_type::size_type last_pos = 0, pos = 0; 00054 int count = 0; 00055 00056 if(str.length() < 1) 00057 return tokens; 00058 00059 pos = str.find_first_of(delimiters, last_pos); 00060 while(string_type::npos != pos || string_type::npos != last_pos) 00061 { 00062 // found a token, add it to the vector 00063 tokens.push_back(str.substr(last_pos, pos-last_pos)); 00064 00065 // skip delimiters 00066 last_pos = str.find_first_not_of(delimiters, pos); 00067 00068 if((string_type::npos != pos) && (str.substr(pos, last_pos-pos).length() > 1)) 00069 { 00070 count = str.substr(pos, last_pos-pos).length(); 00071 } 00072 00073 pos = str.find_first_of(delimiters, last_pos); 00074 } 00075 00076 return tokens; 00077 } 00078 } 00079 00080 #endif // STRING_TOKENIZER