1b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// Copyright (c) 2010, Google Inc. 2b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// All rights reserved. 3b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// 4b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// Redistribution and use in source and binary forms, with or without 5b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// modification, are permitted provided that the following conditions are 6b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// met: 7b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// 8b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// * Redistributions of source code must retain the above copyright 9b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// notice, this list of conditions and the following disclaimer. 10b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// * Redistributions in binary form must reproduce the above 11b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// copyright notice, this list of conditions and the following disclaimer 12b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// in the documentation and/or other materials provided with the 13b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// distribution. 14b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// * Neither the name of Google Inc. nor the names of its 15b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// contributors may be used to endorse or promote products derived from 16b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// this software without specific prior written permission. 17b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// 18b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek 30b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek#include <string.h> 31b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek 32b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek#include <string> 33b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek#include <vector> 34b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek 354e518a4357a2d1c379d4a91df6d4e153ee791101ivan.penkov@gmail.com#include "common/using_std_string.h" 364e518a4357a2d1c379d4a91df6d4e153ee791101ivan.penkov@gmail.com 37b223627d81c083a64f2ccecf2651a18111421280ted.mielczareknamespace google_breakpad { 38b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek 39f2e56b1fde88a521971cb6e74d53ce6ac04aa186ted.mielczarek@gmail.com#ifdef _WIN32 40f2e56b1fde88a521971cb6e74d53ce6ac04aa186ted.mielczarek@gmail.com#define strtok_r strtok_s 41f2e56b1fde88a521971cb6e74d53ce6ac04aa186ted.mielczarek@gmail.com#endif 42f2e56b1fde88a521971cb6e74d53ce6ac04aa186ted.mielczarek@gmail.com 43b223627d81c083a64f2ccecf2651a18111421280ted.mielczarekusing std::vector; 44b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek 45b223627d81c083a64f2ccecf2651a18111421280ted.mielczarekbool Tokenize(char *line, 46b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek const char *separators, 47b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek int max_tokens, 48b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek vector<char*> *tokens) { 49b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek tokens->clear(); 50b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek tokens->reserve(max_tokens); 51b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek 52b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek int remaining = max_tokens; 53b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek 54b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek // Split tokens on the separator character. 55b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek // strip them out before exhausting max_tokens. 56b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek char *save_ptr; 57b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek char *token = strtok_r(line, separators, &save_ptr); 58b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek while (token && --remaining > 0) { 59b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek tokens->push_back(token); 60b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek if (remaining > 1) 61b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek token = strtok_r(NULL, separators, &save_ptr); 62b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek } 63b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek 64b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek // If there's anything left, just add it as a single token. 65057144e9090a96f12457f91284b709efb413d8cbmark@chromium.org if (remaining == 0 && (token = strtok_r(NULL, "\r\n", &save_ptr))) { 66057144e9090a96f12457f91284b709efb413d8cbmark@chromium.org tokens->push_back(token); 67b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek } 68b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek 69b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek return tokens->size() == static_cast<unsigned int>(max_tokens); 70b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek} 71b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek 72b223627d81c083a64f2ccecf2651a18111421280ted.mielczarekvoid StringToVector(const string &str, vector<char> &vec) { 73d95350e97a1edc7757f668c35b045408512db5baSiyangXie@gmail.com vec.resize(str.length() + 1); 74b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek std::copy(str.begin(), str.end(), 75b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek vec.begin()); 76b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek vec[str.length()] = '\0'; 77b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek} 78b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek 79b223627d81c083a64f2ccecf2651a18111421280ted.mielczarek} // namespace google_breakpad 80