-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKxCppRTUtilities.cpp
146 lines (130 loc) · 3.92 KB
/
KxCppRTUtilities.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// ----------------------------------------------------------------------------
// Copyright....: (c) SAP 1999-2021
// Project......:
// Library......:
// File.........: KxCppRTUtilities.cpp
// Author.......:
// Created......: Tue Mar 09 18:00:29 2010
// Description..:
// ----------------------------------------------------------------------------
#include "KxCppRTUtilities.h"
#include <limits>
void KxStringTrim (const char* iWord, size_t iLeft, size_t iLength, const char* iSeparators,
size_t* oLeft, size_t* oLength)
{
size_t lIndex = iLeft;
if (iLength > 0)
{
*(oLength) = 0;
while ((NULL != (void*)strchr (iSeparators, iWord[lIndex])) && (lIndex < iLeft + iLength))
{
lIndex++;
}
*(oLeft) = lIndex;
lIndex = iLeft + iLength - 1;
while ((NULL != (void*)strrchr (iSeparators, iWord[lIndex])) && (lIndex > *(oLeft)))
{
lIndex--;
}
*(oLength) = lIndex - *(oLeft) + 1;
}
else
{ /* iLength is <= 0 */
*(oLeft) = iLeft;
*(oLength) = 0;
}
}
long KxStringTokenSTL (const char* iLine, size_t iLeft, const char* iSeparators, size_t* oLength)
{
size_t lLength = strlen (iLine + iLeft);
if (0 <= lLength)
{
/* I want to stop at the first char in separator list */
*(oLength) = strcspn (iLine + iLeft, iSeparators);
if (*(oLength) < lLength)
return (KXEN_S_OK);
else
return (KXEN_S_FALSE);
}
else
{
return (KXEN_E_INVALIDARG);
}
}
long KxStringSplitNoDupSTL (KxSTL::vector<KxSTL::string>& oWords, KxSTL::string iLine,
const char* iSeparators, const char* iTrimedChars)
{
long lResult = 0;
size_t lLeftBefore = 0;
size_t lTokenLength;
size_t lFirst;
size_t lLength;
if (iLine.empty ()) /* Empty string */
return 0;
lResult = KxStringTokenSTL (iLine.c_str (), lLeftBefore, iSeparators, &lTokenLength);
while (0 <= lResult)
{
KxStringTrim (iLine.c_str (), lLeftBefore, lTokenLength, iTrimedChars, &lFirst, &lLength);
/* While there are tokens in "string" */
if ((lFirst + lLength) < iLine.size ())
{
iLine[lFirst + lLength] = 0;
}
oWords.push_back (iLine.c_str () + lFirst);
/* Prepare to get the next token: */
/* we've found a separator, just skip it */
lLeftBefore += lTokenLength + 1;
if (KXEN_S_FALSE == lResult)
{
lResult = KXEN_E_INVALIDARG;
}
else
{
lResult = KxStringTokenSTL (iLine.c_str (), lLeftBefore, iSeparators, &lTokenLength);
}
}
return lResult;
}
int KxGetStringSTL (FILE* iFile, KxSTL::string& oString)
{
int ch, /* Character read from file */
cnbr; /* Index into returned string */
oString.erase (); /* erase 0 to npos */
if (NULL == iFile)
return 0; /** false */
cnbr = 0; /* Start at the beginning. */
for (;;)
{
#if defined(_WIN32)
ch = getc (iFile);
#else
ch = getc_unlocked (iFile);
#endif
if (ch == '\r') /* Found carriage-return */
ch = '\r'; /* ignore CR */
else
{
if ((ch == '\n') /* Have end of line */
|| (ch == EOF) /* or end of file */
|| (ch == 26)) /* or MS-DOS Ctrl-Z */
{
return (ch == '\n' || cnbr); /* and return TRUE/FALSE */
}
else
{
oString += static_cast<char>(ch); /* Else add char to string */
}
}
}
}
double KxConvertToDouble(const char* iValue, bool& oMissing)
{
char* pEnd;
double value = std::strtod(iValue, &pEnd);
if (pEnd == iValue)
{
oMissing = true;
return std::numeric_limits<double>::quiet_NaN();
}
return value;
}