summaryrefslogtreecommitdiff
path: root/cpp/src/IceUtil/InputUtil.cpp
blob: 54eaa341889697bc88946cb155f1aee8ffdc6110 (plain)
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
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************

#include <IceUtil/InputUtil.h>
#include <stdlib.h>

using namespace std;

namespace IceUtil
{

Int64
strToInt64(const char* s, char** endptr, int base)
{
#if defined(_WIN32)
    // TODO: WIN32 implementation is missing
#else
    return strtoll(s, endptr, base);
#endif
}

bool
stringToInt64(const string& stringToParse, Int64& result, string::size_type& pos)
{
    string::const_iterator i = stringToParse.begin();
    while(i != stringToParse.end() && isspace(*i))
    {
	++i;
    }
    if(i == stringToParse.end())	// String empty or nothing but whitespace
    {
	result = 0;
	pos = string::npos;
	return false;
    }
    string::const_reverse_iterator j = stringToParse.rbegin();
    while(isspace(*j))
    {
	++j;
    }					// j now points at last non-whitespace char

    string nonWhite(i, j.base());	// nonWhite has leading and trailing whitespace stripped

    errno = 0;
    const char* startp = nonWhite.c_str();
    char* endp;
    result = strtoll(startp, &endp, 0);
    pos = *endp == '\0' ? string::npos : (i - stringToParse.begin()) + (endp - startp);
    return startp != endp && errno != ERANGE && errno != EINVAL;
}

}