2222* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
2323****/
2424
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"
2828
2929using namespace utility ;
3030using namespace concurrency ::streams;
@@ -34,30 +34,30 @@ using namespace concurrency::streams;
3434// / </summary>
3535pplx::task<bool > _do_while_iteration (std::function<pplx::task<bool >(void )> func)
3636{
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);
4343}
4444pplx::task<bool > _do_while_impl (std::function<pplx::task<bool >(void )> func)
4545{
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+ });
5757}
5858pplx::task<void > do_while (std::function<pplx::task<bool >(void )> func)
5959{
60- return _do_while_impl (func).then ([](bool ){});
60+ return _do_while_impl (func).then ([](bool ){});
6161}
6262
6363// / <summary>
@@ -77,21 +77,21 @@ class type_parser<CharType, matched_lines>
7777 basic_istream<CharType> in (buffer);
7878 auto lines = std::make_shared<matched_lines>();
7979 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 ([=]()
9595 {
9696 return matched_lines (std::move (*lines));
9797 });
@@ -103,79 +103,79 @@ class type_parser<CharType, matched_lines>
103103// / </summary>
104104static pplx::task<void > find_matches_in_file (const string_t &fileName, const std::string &searchString, basic_ostream<char > results)
105105{
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+ }
118118
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+ }
135135
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+ });
148148}
149149
150150// / <summary>
151151// / Function to write out results from matched_lines type to file
152152// / </summary>
153153static pplx::task<void > write_matches_to_file (const string_t &fileName, matched_lines results)
154154{
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));
157157
158158 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+ }
167167
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+ });
179179}
180180
181181#ifdef _MS_WINDOWS
@@ -193,26 +193,26 @@ int main(int argc, char *args[])
193193 const std::string searchString = utility::conversions::to_utf8string (args[2 ]);
194194 const string_t outFileName = args[3 ];
195195 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);
199199 find_matches_in_file (inFileName, searchString, outLineResults)
200200
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+ })
207207
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+ })
213213
214- // Wait for everything to complete.
215- .wait ();
214+ // Wait for everything to complete.
215+ .wait ();
216216
217217 return 0 ;
218218}
0 commit comments