FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/core/FileSerializer.cpp at master · pearsonca/rstudio · GitHub
pearsonca
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
FileSerializer.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
264 lines (227 loc) · 7.95 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
FileSerializer.cpp
Copy path
File metadata and controls
264 lines (227 loc) · 7.95 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* FileSerializer.cpp
*
* Copyright (C) 2009-19 by RStudio, PBC
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
<
core/FileSerializer.hpp
>
#
include
<
utility
>
#
include
<
iostream
>
#
include
<
sstream
>
#
include
<
algorithm
>
#
include
<
gsl/gsl
>
#
include
<
boost/algorithm/string/trim.hpp
>
#
include
<
boost/algorithm/string/predicate.hpp
>
#
include
<
boost/iostreams/copy.hpp
>
#
include
<
shared_core/FilePath.hpp
>
#
include
<
core/StringUtils.hpp
>
namespace
rstudio
{
namespace
core
{
std::string
stringifyStringPair
(
const
std::pair<std::string,std::string>& pair)
{
return
pair.
first
+
"
=
\"
"
+
string_utils::jsonLiteralEscape
(pair.
second
) +
"
\"
"
;
}
Error
writeStringMapToFile
(
const
core::FilePath& filePath,
const
std::map<std::string,std::string>& map)
{
return
writeCollectionToFile<std::map<std::string,std::string> >(
filePath,
map,
stringifyStringPair) ;
}
ReadCollectionAction
parseStringPair
(
const
std::string& line,
std::pair<
const
std::string,std::string>* pPair)
{
std::string::size_type pos = line.
find
(
"
=
"
) ;
if
( pos != std::string::npos )
{
std::string name = line.
substr
(
0
, pos) ;
boost::algorithm::trim
(name);
std::string value = line.
substr
(pos +
1
) ;
boost::algorithm::trim
(value) ;
if
(value.
length
() >=
2
&& value[
0
] ==
'
"
'
&& value[value.
length
() -
1
] ==
'
"
'
)
{
value =
string_utils::jsonLiteralUnescape
(value);
}
//
HACK: workaround the fact that std::map uses const for the Key
std::string* pFirst =
const_cast
<std::string*>(&(pPair->
first
)) ;
*pFirst = name ;
pPair->
second
= value ;
return
ReadCollectionAddLine ;
}
else
{
return
ReadCollectionIgnoreLine;
}
}
Error
readStringMapFromFile
(
const
core::FilePath& filePath,
std::map<std::string,std::string>* pMap)
{
return
readCollectionFromFile<std::map<std::string,std::string> >(
filePath,
pMap,
parseStringPair) ;
}
std::string
stringifyString
(
const
std::string& str)
{
return
str;
}
Error
writeStringVectorToFile
(
const
core::FilePath& filePath,
const
std::vector<std::string>& vector)
{
return
writeCollectionToFile<std::vector<std::string> >(filePath,
vector,
stringifyString);
}
ReadCollectionAction
parseString
(
const
std::string& line, std::string* pStr)
{
*pStr = line ;
return
ReadCollectionAddLine ;
}
Error
readStringVectorFromFile
(
const
core::FilePath& filePath,
std::vector<std::string>* pVector,
bool
trimAndIgnoreBlankLines)
{
return
readCollectionFromFile<std::vector<std::string> > (
filePath, pVector, parseString, trimAndIgnoreBlankLines);
}
Error
writeStringToFile
(
const
FilePath& filePath,
const
std::string& str,
string_utils::LineEnding lineEnding,
bool
truncate)
{
using
namespace
boost
::system::errc
;
//
open file
std::shared_ptr<std::ostream> pOfs;
Error error = filePath.
openForWrite
(pOfs, truncate);
if
(error)
return
error;
try
{
//
set exception mask (required for proper reporting of errors)
pOfs->
exceptions
(std::ostream::failbit | std::ostream::badbit);
//
copy string to file
std::string normalized = str;
string_utils::convertLineEndings
(&normalized, lineEnding);
std::istringstream
istr
(normalized);
boost::iostreams::copy
(istr, *pOfs);
//
return success
return
Success
();
}
catch
(
const
std::exception& e)
{
Error error =
systemError
(boost::system::errc::io_error,
ERROR_LOCATION
);
error.
addProperty
(
"
what
"
, e.
what
());
error.
addProperty
(
"
path
"
, filePath.
getAbsolutePath
());
return
error;
}
}
Error
readStringFromFile
(
const
FilePath& filePath,
std::string* pStr,
string_utils::LineEnding lineEnding,
int
startLine,
int
endLine,
int
startCharacter,
int
endCharacter)
{
using
namespace
boost
::system::errc
;
//
open file
std::shared_ptr<std::istream> pIfs;
Error error = filePath.
openForRead
(pIfs);
if
(error)
return
error;
try
{
//
if a line region was specified, read that region instead of the
//
entire file.
if
(endLine > startLine)
{
//
set exception mask; note that we can't let failbit create an
//
exception here because reading eof can trigger failbit in our case.
pIfs->
exceptions
(std::istream::badbit);
int
currentLine =
0
;
std::string content;
std::string line;
//
loop over each line in the file. (consider: is there a more
//
performant way to seek past the first N lines?)
while
(++currentLine <= endLine
&& !pIfs->
eof
())
{
std::getline
(*pIfs, line);
if
(currentLine >= startLine)
{
//
compute the portion of the line to be read; if this is the
//
start or end of the region to be read, use the character
//
offsets supplied
int
lineLength = gsl::narrow_cast<
int
>(line.
length
());
content += line.
substr
(
currentLine == startLine ?
std::min
(
std::max
(startCharacter -
1
,
0
),
lineLength) :
0
,
currentLine == endLine ?
std::min
(endCharacter, lineLength) :
lineLength);
if
(currentLine != endLine)
{
content +=
"
\n
"
;
}
}
}
*pStr = content;
}
//
reading the entire file
else
{
//
set exception mask (required for proper reporting of errors)
pIfs->
exceptions
(std::istream::failbit | std::istream::badbit);
//
copy file to string stream
std::ostringstream ostr;
boost::iostreams::copy
(*pIfs, ostr);
*pStr = ostr.
str
();
}
string_utils::convertLineEndings
(pStr, lineEnding);
//
return success
return
Success
();
}
catch
(
const
std::exception& e)
{
Error error =
systemError
(boost::system::errc::io_error,
ERROR_LOCATION
);
error.
addProperty
(
"
what
"
, e.
what
());
error.
addProperty
(
"
path
"
, filePath.
getAbsolutePath
());
return
error;
}
}
bool
stripBOM
(std::string* pStr)
{
if
(
boost::algorithm::starts_with
(*pStr,
"
\xEF\xBB\xBF
"
))
{
pStr->
erase
(
0
,
3
);
return
true
;
}
else
if
(
boost::algorithm::starts_with
(*pStr,
"
\xFF\xFE
"
))
{
pStr->
erase
(
0
,
2
);
return
true
;
}
else
if
(
boost::algorithm::starts_with
(*pStr,
"
\xFE\xFF
"
))
{
pStr->
erase
(
0
,
2
);
return
true
;
}
return
false
;
}
}
//
namespace core
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL