22
22
* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
23
23
****/
24
24
25
- #include < filestream.h>
26
- #include < containerstream.h>
27
- #include < producerconsumerstream.h>
25
+ #include " cpprest/ filestream.h"
26
+ #include " cpprest/ containerstream.h"
27
+ #include " cpprest/ producerconsumerstream.h"
28
28
29
29
using namespace utility ;
30
30
using namespace concurrency ::streams;
@@ -34,30 +34,30 @@ using namespace concurrency::streams;
34
34
// / </summary>
35
35
pplx::task<bool > _do_while_iteration (std::function<pplx::task<bool >(void )> func)
36
36
{
37
- pplx::task_completion_event<bool > ev;
38
- func ().then ([=](bool guard)
39
- {
40
- ev.set (guard);
41
- });
42
- return pplx::create_task (ev);
37
+ pplx::task_completion_event<bool > ev;
38
+ func ().then ([=](bool guard)
39
+ {
40
+ ev.set (guard);
41
+ });
42
+ return pplx::create_task (ev);
43
43
}
44
44
pplx::task<bool > _do_while_impl (std::function<pplx::task<bool >(void )> func)
45
45
{
46
- return _do_while_iteration (func).then ([=](bool guard) -> pplx::task<bool >
47
- {
48
- if (guard)
49
- {
50
- return ::_do_while_impl (func);
51
- }
52
- else
53
- {
54
- return pplx::task_from_result (false );
55
- }
56
- });
46
+ return _do_while_iteration (func).then ([=](bool guard) -> pplx::task<bool >
47
+ {
48
+ if (guard)
49
+ {
50
+ return ::_do_while_impl (func);
51
+ }
52
+ else
53
+ {
54
+ return pplx::task_from_result (false );
55
+ }
56
+ });
57
57
}
58
58
pplx::task<void > do_while (std::function<pplx::task<bool >(void )> func)
59
59
{
60
- return _do_while_impl (func).then ([](bool ){});
60
+ return _do_while_impl (func).then ([](bool ){});
61
61
}
62
62
63
63
// / <summary>
@@ -77,21 +77,21 @@ class type_parser<CharType, matched_lines>
77
77
basic_istream<CharType> in (buffer);
78
78
auto lines = std::make_shared<matched_lines>();
79
79
return do_while ([=]()
80
- {
81
- container_buffer<std::string> line;
82
- return in.read_line (line).then ([=](const size_t bytesRead)
83
- {
84
- if (bytesRead == 0 && in.is_eof ())
85
- {
86
- return false ;
87
- }
88
- else
89
- {
90
- lines->push_back (std::move (line.collection ()));
91
- return true ;
92
- }
93
- });
94
- }).then ([=]()
80
+ {
81
+ container_buffer<std::string> line;
82
+ return in.read_line (line).then ([=](const size_t bytesRead)
83
+ {
84
+ if (bytesRead == 0 && in.is_eof ())
85
+ {
86
+ return false ;
87
+ }
88
+ else
89
+ {
90
+ lines->push_back (std::move (line.collection ()));
91
+ return true ;
92
+ }
93
+ });
94
+ }).then ([=]()
95
95
{
96
96
return matched_lines (std::move (*lines));
97
97
});
@@ -103,79 +103,79 @@ class type_parser<CharType, matched_lines>
103
103
// / </summary>
104
104
static pplx::task<void > find_matches_in_file (const string_t &fileName, const std::string &searchString, basic_ostream<char > results)
105
105
{
106
- return file_stream<char >::open_istream (fileName).then ([=](basic_istream<char > inFile)
107
- {
108
- auto lineNumber = std::make_shared<int >(1 );
109
- return ::do_while ([=]()
110
- {
111
- container_buffer<std::string> inLine;
112
- return inFile.read_line (inLine).then ([=](size_t bytesRead)
113
- {
114
- if (bytesRead == 0 && inFile.is_eof ())
115
- {
116
- return pplx::task_from_result (false );
117
- }
106
+ return file_stream<char >::open_istream (fileName).then ([=](basic_istream<char > inFile)
107
+ {
108
+ auto lineNumber = std::make_shared<int >(1 );
109
+ return ::do_while ([=]()
110
+ {
111
+ container_buffer<std::string> inLine;
112
+ return inFile.read_line (inLine).then ([=](size_t bytesRead)
113
+ {
114
+ if (bytesRead == 0 && inFile.is_eof ())
115
+ {
116
+ return pplx::task_from_result (false );
117
+ }
118
118
119
- else if (inLine.collection ().find (searchString) != std::string::npos)
120
- {
121
- results.print (" line " );
122
- results.print ((*lineNumber)++);
123
- return results.print (" :" ).then ([=](size_t )
124
- {
125
- container_buffer<std::string> outLine (std::move (inLine.collection ()));
126
- return results.write (outLine, outLine.collection ().size ());
127
- }).then ([=](size_t )
128
- {
129
- return results.print (" \r\n " );
130
- }).then ([=](size_t )
131
- {
132
- return true ;
133
- });
134
- }
119
+ else if (inLine.collection ().find (searchString) != std::string::npos)
120
+ {
121
+ results.print (" line " );
122
+ results.print ((*lineNumber)++);
123
+ return results.print (" :" ).then ([=](size_t )
124
+ {
125
+ container_buffer<std::string> outLine (std::move (inLine.collection ()));
126
+ return results.write (outLine, outLine.collection ().size ());
127
+ }).then ([=](size_t )
128
+ {
129
+ return results.print (" \r\n " );
130
+ }).then ([=](size_t )
131
+ {
132
+ return true ;
133
+ });
134
+ }
135
135
136
- else
137
- {
138
- ++(*lineNumber);
139
- return pplx::task_from_result (true );
140
- }
141
- });
142
- }).then ([=]()
143
- {
144
- // Close the file and results stream.
145
- return inFile.close () && results.close ();
146
- });
147
- });
136
+ else
137
+ {
138
+ ++(*lineNumber);
139
+ return pplx::task_from_result (true );
140
+ }
141
+ });
142
+ }).then ([=]()
143
+ {
144
+ // Close the file and results stream.
145
+ return inFile.close () && results.close ();
146
+ });
147
+ });
148
148
}
149
149
150
150
// / <summary>
151
151
// / Function to write out results from matched_lines type to file
152
152
// / </summary>
153
153
static pplx::task<void > write_matches_to_file (const string_t &fileName, matched_lines results)
154
154
{
155
- // Create a shared pointer to the matched_lines structure to copying repeatedly.
156
- auto sharedResults = std::make_shared<matched_lines>(std::move (results));
155
+ // Create a shared pointer to the matched_lines structure to copying repeatedly.
156
+ auto sharedResults = std::make_shared<matched_lines>(std::move (results));
157
157
158
158
return file_stream<char >::open_ostream (fileName, std::ios::trunc).then ([=](basic_ostream<char > outFile)
159
- {
160
- auto currentIndex = std::make_shared<size_t >(0 );
161
- return ::do_while ([=]()
162
- {
163
- if (*currentIndex >= sharedResults->size ())
164
- {
165
- return pplx::task_from_result (false );
166
- }
159
+ {
160
+ auto currentIndex = std::make_shared<size_t >(0 );
161
+ return ::do_while ([=]()
162
+ {
163
+ if (*currentIndex >= sharedResults->size ())
164
+ {
165
+ return pplx::task_from_result (false );
166
+ }
167
167
168
- container_buffer<std::string> lineData ((*sharedResults)[(*currentIndex)++]);
169
- outFile.write (lineData, lineData.collection ().size ());
170
- return outFile.print (" \r\n " ).then ([](size_t )
171
- {
172
- return true ;
173
- });
174
- }).then ([=]()
175
- {
176
- return outFile.close ();
177
- });
178
- });
168
+ container_buffer<std::string> lineData ((*sharedResults)[(*currentIndex)++]);
169
+ outFile.write (lineData, lineData.collection ().size ());
170
+ return outFile.print (" \r\n " ).then ([](size_t )
171
+ {
172
+ return true ;
173
+ });
174
+ }).then ([=]()
175
+ {
176
+ return outFile.close ();
177
+ });
178
+ });
179
179
}
180
180
181
181
#ifdef _MS_WINDOWS
@@ -193,26 +193,26 @@ int main(int argc, char *args[])
193
193
const std::string searchString = utility::conversions::to_utf8string (args[2 ]);
194
194
const string_t outFileName = args[3 ];
195
195
producer_consumer_buffer<char > lineResultsBuffer;
196
-
197
- // Find all matches in file.
198
- basic_ostream<char > outLineResults (lineResultsBuffer);
196
+
197
+ // Find all matches in file.
198
+ basic_ostream<char > outLineResults (lineResultsBuffer);
199
199
find_matches_in_file (inFileName, searchString, outLineResults)
200
200
201
- // Write matches into custom data structure.
202
- .then ([&]()
203
- {
204
- basic_istream<char > inLineResults (lineResultsBuffer);
205
- return inLineResults.extract <matched_lines>();
206
- })
201
+ // Write matches into custom data structure.
202
+ .then ([&]()
203
+ {
204
+ basic_istream<char > inLineResults (lineResultsBuffer);
205
+ return inLineResults.extract <matched_lines>();
206
+ })
207
207
208
- // Write out stored match data to a new file.
209
- .then ([&](matched_lines lines)
210
- {
211
- return write_matches_to_file (outFileName, std::move (lines));
212
- })
208
+ // Write out stored match data to a new file.
209
+ .then ([&](matched_lines lines)
210
+ {
211
+ return write_matches_to_file (outFileName, std::move (lines));
212
+ })
213
213
214
- // Wait for everything to complete.
215
- .wait ();
214
+ // Wait for everything to complete.
215
+ .wait ();
216
216
217
217
return 0 ;
218
218
}
0 commit comments