1919
2020namespace parser
2121{
22+ // / \brief this is an exception class that gets thrown when there is an error while parsing
23+ class parsing_error : public std ::exception
24+ {
25+ public:
26+ using std::exception::exception;
27+
28+ explicit parsing_error (const std::string& basic_string) : std::exception(basic_string.c_str())
29+ {
30+ }
31+ };
32+
2233 // / \brief this is a data class that holds a trajectory and a function callback
2334 struct parsed_trajectory
2435 {
@@ -82,15 +93,15 @@ namespace parser
8293
8394 // / \brief returns raw file as string
8495 // / \param file_path path to file + file name
85- // / \throw std::exception if file does not exist
96+ // / \throw parsing_error if file does not exist
8697 // / \return raw file as string
8798 inline std::string get_file_raw (const std::string& file_path);
8899
89100 // / \brief loops through file line by line and calls callback function
90101 // / \param file_path path to file + file name
91102 // / \param callback function that gets called for each line
92103 // / \param header_length amount of lines to skip
93- // / \throw std::exception if file does not exist
104+ // / \throw parsing_error if file does not exist
94105 inline void parse_file_by_line (std::string file_path,
95106 const std::function<void (std::string& line)>& callback, int header_length);
96107
@@ -99,7 +110,7 @@ namespace parser
99110 // / \param delimiter delimiter between substrings
100111 // / \param callback function that gets called for each substring for each line
101112 // / \param header_length amount of lines to skip
102- // / \throw std::exception if file does not exist
113+ // / \throw parsing_error if file does not exist
103114 inline void parse_file_with_delimiter (const std::string& file_path, const char delimiter,
104115 const std::function<void (const std::string& substr)>& callback,
105116 int header_length);
@@ -111,8 +122,8 @@ namespace parser
111122 // / \param x_y_delimiter delimiter between x and y
112123 // / \param point_delimiter delimiter between points
113124 // / \param header_length amount of lines to skip
114- // / \throw std::exception if file does not exist
115- // / \throw std::exception if trajectory is empty or has less than 2 points
125+ // / \throw parsing_error if file does not exist
126+ // / \throw parsing_error if trajectory is empty or has less than 2 points
116127 // / \return Trajectory with points from file
117128 inline Trajectory parse_trajectory (std::string file_path, const char x_y_delimiter,
118129 const char point_delimiter, int header_length);
@@ -121,19 +132,19 @@ namespace parser
121132 // / \param file_path path to file + file name
122133 // / \param x_y_delimiter delimiter between x and y
123134 // / \param header_length amount of lines to skip
124- // / \throw std::exception if file does not exist
125- // / \throw std::exception if trajectory is empty or has less than 2 points
135+ // / \throw parsing_error if file does not exist
136+ // / \throw parsing_error if trajectory is empty or has less than 2 points
126137 // / \return Trajectory with points from file
127138 inline Trajectory parse_trajectory (std::string file_path, const char x_y_delimiter, int header_length);
128139
129140 /* *
130141 * \brief finds all trajectories in config file parses them and returns them
131142 * \param config_file_path path to config file
132- * \param delimiter delimiter between each item
143+ * \param delimiter delimiter between each item, N will be used for newline delimiter
133144 * \return list of parsed trajectories
134- * \throw std::exception if file does not exist
135- * \throw std::exception if trajectory function is not found
136- * \throw std::exception if trajectory is empty or has less than 2 points
145+ * \throw parsing_error if file does not exist
146+ * \throw parsing_error if trajectory function is not found
147+ * \throw parsing_error if trajectory is empty or has less than 2 points
137148 */
138149 inline std::vector<::parser::parsed_trajectory> parse_config (std::string config_file_path,
139150 const char delimiter);
@@ -149,7 +160,7 @@ namespace parser
149160
150161 // / \brief returns trajectory function based on name
151162 // / \param name name of the trajectory function
152- // / \throw std::exception if trajectory function is not found
163+ // / \throw parsing_error if trajectory function is not found
153164 // / \return a trajectory function callback
154165 constexpr inline parsed_trajectory::trajectory_function_callback get_trajectory_function (
155166 const std::string_view& name)
@@ -165,7 +176,7 @@ namespace parser
165176 case hash_string (" get_hotspot_fixed_radius" ):
166177 return &Trajectory::get_hotspot_fixed_radius;
167178 default :
168- throw std::exception (" Trajectory function not found" );
179+ throw parsing_error (" Trajectory function not found" );
169180 }
170181 return nullptr ;
171182 }
@@ -175,7 +186,7 @@ namespace parser
175186std::string parser::get_file_raw (const std::string& file_path)
176187{
177188 if (!file_exists (file_path))
178- throw std::exception (" File does not exist" );
189+ throw parsing_error (" File does not exist" );
179190
180191 std::string file_raw;
181192 std::ifstream file (file_path);
@@ -194,7 +205,7 @@ void parser::parse_file_by_line(std::string file_path, const std::function<void(
194205 if (!file_exists (file_path))
195206 {
196207 if (!file_exists (current_path_string + file_path))
197- throw std::exception (" File does not exist" );
208+ throw parsing_error (" File does not exist" );
198209 file_path = current_path_string + file_path;
199210 }
200211
@@ -240,7 +251,7 @@ Trajectory parser::parse_trajectory(std::string file_path, const char x_y_delimi
240251 if (!file_exists (file_path))
241252 {
242253 if (!file_exists (current_path_string + file_path))
243- throw std::exception (" File does not exist" );
254+ throw parsing_error (" File does not exist" );
244255 file_path = current_path_string + file_path;
245256 }
246257
@@ -276,7 +287,7 @@ Trajectory parser::parse_trajectory(std::string file_path, const char x_y_delimi
276287 }, header_length);
277288
278289 if (trajectory_points.empty () || trajectory_points.size () < 2 )
279- throw std::runtime_error (trajectory_points.empty ()
290+ throw parsing_error (trajectory_points.empty ()
280291 ? " Trajectory is empty"
281292 : " Trajectory has less than 2 points" );
282293
@@ -289,7 +300,7 @@ Trajectory parser::parse_trajectory(std::string file_path, const char x_y_delimi
289300 if (!file_exists (file_path))
290301 {
291302 if (!file_exists (current_path_string + file_path))
292- throw std::exception (" File does not exist" );
303+ throw parsing_error (" File does not exist" );
293304 file_path = current_path_string + file_path;
294305 }
295306
@@ -326,7 +337,7 @@ Trajectory parser::parse_trajectory(std::string file_path, const char x_y_delimi
326337 }, header_length);
327338
328339 if (trajectory_points.empty () || trajectory_points.size () < 2 )
329- throw std::runtime_error (trajectory_points.empty ()
340+ throw parsing_error (trajectory_points.empty ()
330341 ? " Trajectory is empty"
331342 : " Trajectory has less than 2 points" );
332343
@@ -363,7 +374,7 @@ inline std::vector<parser::parsed_trajectory> parser::parse_config(std::string c
363374 if (!file_exists (config_file_path))
364375 {
365376 if (!file_exists (current_path_string + config_file_path))
366- throw std::exception (" File does not exist" );
377+ throw parsing_error (" File does not exist" );
367378 config_file_path = current_path_string + config_file_path;
368379 }
369380
@@ -376,26 +387,26 @@ inline std::vector<parser::parsed_trajectory> parser::parse_config(std::string c
376387 const std::vector<std::string> substrings = get_all_substring_by_delimiter (line, delimiter);
377388
378389 if (substrings.size () > 7 || substrings.size () < 6 )
379- throw std::runtime_error (" \" " + line + " \" the arguments don't match, please provide a different input" );
380- const std::string name = substrings[ 0 ] ;
381- const std::string trajectory_file_path = substrings[ 1 ] ;
390+ throw parsing_error (" \" " + line + " \" the arguments don't match, please provide a different input" );
391+ const std::string name = substrings. at ( 0 ) ;
392+ const std::string trajectory_file_path = substrings. at ( 1 ) ;
382393
383- const char x_y_delimiter = substrings[ 2 ] .front ();
384- const char point_delimiter = substrings[ 3 ] .front ();
394+ const char x_y_delimiter = substrings. at ( 2 ) .front ();
395+ const char point_delimiter = substrings. at ( 3 ) .front ();
385396
386- const std::string trajectory_function_name = substrings[ 4 ] ;
397+ const std::string trajectory_function_name = substrings. at ( 4 ) ;
387398
388- const float length = std::stof (substrings[ 5 ] );
399+ const float length = std::stof (substrings. at ( 5 ) );
389400
390401 int header_length = 0 ;
391402 if (substrings.size () > 6 )
392- header_length = std::stoi (substrings[ 6 ] );
403+ header_length = std::stoi (substrings. at ( 6 ) );
393404
394405 const auto trajectory_function = get_trajectory_function (trajectory_function_name);
395406
396407 Trajectory trajectory;
397408
398- if (point_delimiter == ' 0 ' )
409+ if (point_delimiter == ' N ' )
399410 trajectory = parse_trajectory (trajectory_file_path, x_y_delimiter, header_length);
400411 else
401412 trajectory = parse_trajectory (trajectory_file_path, x_y_delimiter, point_delimiter, header_length);
0 commit comments