string_parser.hpp 2.31 KB
Newer Older
Swann Perarnau's avatar
Swann Perarnau committed
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
#ifndef __STRING_PARSER_HPP__
#define __STRING_PARSER_HPP__

#include <map>
#include <vector>
#include <string>
#include <stdexcept>
#include <typeinfo>
#include <set>

#include "utils.hpp"

using std::map;
using std::string;
using std::vector;


/*
 * The convention is as follows for lists:
 * 	cpus:[1,2...n] values meant to overwrite the corresponding existing list
 * 	+cpus:[1,2...n]  values meant to be added to the corresponding existing list
 * 	-cpus:[1,2...n]  values meant to be removed from the corresponding existing list
 */

class String_parser
{
	private:
		string _raw;
		std::set<string> _keys;
		map<string, bool> _bools;
		map<string, int> _ints;
		map<string, double> _doubles;
		map<string, string> _strings;
		map<string, vector<int> > _int_lists;
		map<string, vector<double> > _double_lists;

		

		void parse_bool(string token);
		void parse_int(string token);
		void parse_double(string token);
		void parse_string(string token);
		void parse_int_list(string token, const string& list_prefix);
		void parse_double_list(string token, const string& list_prefix);
	public:
		String_parser(const string& command);
		~String_parser(){};
		inline void reset_command(const string& command){_raw = command;}

		void parse();
		void get_keys(vector<string>& keys);
		inline bool has_key(const string& key){return _keys.find(key) != _keys.end();}
		inline int get_nb_keys(){return _keys.size();}

		/*The below two methods assume that the keys exist*/
		inline int get_int_list_size(const string& key) {return (int)_int_lists[key].size();} 
		inline int get_double_list_size(const string& key) {return (int)_double_lists[key].size();} 

		bool get_bool(const string& key, bool& value);
		bool get_int(const string& key, int& value);
		bool get_double(const string& key, double& value);
		bool get_string(const string& key, string& value);
		bool get_int_list(const string& key, vector<int>& values);
		bool get_double_list(const string& key, vector<double>& values);

		bool get_added_int_list(const string& key, vector<int>& values); 
		bool get_removed_int_list(const string& key, vector<int>& values); 
		bool get_added_double_list(const string& key, vector<double>& values);
		bool get_removed_double_list(const string& key, vector<double>& values);

		bool has_forbidden_keys(const vector<string> allowed_keys);
};
#endif //__STRING_PARSER_HPP__