Hash :
c7be82ae
Author :
Date :
2015-06-01T14:14:04
Change behaviour of string_utils, also fix file io. File IO tested manually in a follow-up patch. BUG=angleproject:998 Change-Id: I0bfa778d1787cdedfbf3cd68cd134477e6dcd23c Reviewed-on: https://chromium-review.googlesource.com/274030 Reviewed-by: Geoff Lang <geofflang@chromium.org> Tested-by: Jamie Madill <jmadill@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
//
// Copyright 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// string_utils:
// String helper functions.
//
#include "string_utils.h"
#include <fstream>
#include <sstream>
namespace angle
{
void SplitString(const std::string &input,
char delimiter,
std::vector<std::string> *tokensOut)
{
std::istringstream stream(input);
std::string token;
while (std::getline(stream, token, delimiter))
{
if (!token.empty())
{
tokensOut->push_back(token);
}
}
}
void SplitStringAlongWhitespace(const std::string &input,
std::vector<std::string> *tokensOut)
{
const char *delimiters = " \f\n\r\t\v";
std::istringstream stream(input);
std::string line;
while (std::getline(stream, line))
{
size_t prev = 0, pos;
while ((pos = line.find_first_of(delimiters, prev)) != std::string::npos)
{
if (pos > prev)
tokensOut->push_back(line.substr(prev, pos - prev));
prev = pos + 1;
}
if (prev < line.length())
tokensOut->push_back(line.substr(prev, std::string::npos));
}
}
bool HexStringToUInt(const std::string &input, unsigned int *uintOut)
{
unsigned int offset = 0;
if (input.size() >= 2 && input[0] == '0' && input[1] == 'x')
{
offset = 2u;
}
// Simple validity check
if (input.find_first_not_of("0123456789ABCDEFabcdef", offset) != std::string::npos)
{
return false;
}
std::stringstream inStream(input);
inStream >> std::hex >> *uintOut;
return !inStream.fail();
}
bool ReadFileToString(const std::string &path, std::string *stringOut)
{
std::ifstream inFile(path.c_str());
if (inFile.fail())
{
return false;
}
inFile.seekg(0, std::ios::end);
stringOut->reserve(static_cast<std::string::size_type>(inFile.tellg()));
inFile.seekg(0, std::ios::beg);
stringOut->assign(std::istreambuf_iterator<char>(inFile), std::istreambuf_iterator<char>());
return !inFile.fail();
}
}