string_parser.cpp 5.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*******************************************************************************
 * Copyright 2019 UChicago Argonne, LLC.
 * (c.f. AUTHORS, LICENSE)
 *
 * This file is part of the Argo containers project.
 * For more info, see https://xgitlab.cels.anl.gov/argo/containers
 *
 * SPDX-License-Identifier: BSD-3-Clause
*******************************************************************************/

Swann Perarnau's avatar
Swann Perarnau committed
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
#include "string_parser.hpp"
#include "utils.hpp"
#include <stdexcept>
#include <cstdlib>
#include "int_list_parser.hpp"

String_parser::String_parser(const string& command)
{
	reset_command(command);
}

/*Does sanity check for list end*/
static void extract_list_str(const string& raw, const string& list_prefix, 
		string& out)
{
	THROW_ON_BAD_INPUT_IF(raw[raw.size()-1] != ']', 
			"Ill-formed list; ']' is missing");
	out = raw.substr(list_prefix.size(), raw.size() - 1 - list_prefix.size());
}

void String_parser::parse()
{
	//--create_container="name:'c0 c1' cpus:<double>[0,2,4,5,10-100,400] mem:[0-3]
	//load_balancing:true cpu_exclusive:true mem_exclusive:false"
	vector<string> tokens;
	split(_raw, ' ', tokens);

	for(int i=0; i<(int)tokens.size(); i++)
	{
		if(str_contains(tokens[i], ":true") || 
				str_contains(tokens[i], ":false"))
			parse_bool(tokens[i]);
		else if(str_contains(tokens[i], ":["))
			parse_int_list(tokens[i], "[");
		else if(str_contains(tokens[i], ":<int>["))
			parse_int_list(tokens[i], "<int>[");
		else if(str_contains(tokens[i], ":<double>["))
			parse_int_list(tokens[i], "<double>[");
		else
			parse_string(tokens[i]);
	}
}

void String_parser::parse_bool(string input)
{
	vector<string> tokens;
	split(input, ':', tokens);
	THROW_ON_BAD_INPUT_IF(
			tokens.size() != 2 || tokens[0] == "" ||
			(tokens[1] != "true" && tokens[1] != "false"),
			"Ill-formed input; invalid boolean key-value pair");
	_bools[tokens[0]] = tokens[1] == "true" ?
		true : false;
	_keys.insert(tokens[0]);
}

void String_parser::parse_int(string input)
{
	vector<string> tokens;
	split(input, ':', tokens);

	THROW_ON_BAD_INPUT_IF(tokens.size() != 2 || tokens[0] == "" ||
			!is_int(tokens[1]),
			"Ill-formed input; bad integer key-value pair");
	_ints[tokens[0]] = atoi(tokens[1].c_str());
	_keys.insert(tokens[0]);
}

void String_parser::parse_double(string input)
{
	vector<string> tokens;
	split(input, ':', tokens);
	THROW_ON_BAD_INPUT_IF(tokens.size() != 2 || 
			tokens[0] == "" ||
			!is_double(tokens[1]),
			"Ill-formed input; bad double key-value pair");
	_doubles[tokens[0]] = atof(tokens[1].c_str());
	_keys.insert(tokens[0]);
}

void String_parser::parse_string(string input)
{
	vector<string> tokens;
	split(input, ':', tokens);
	THROW_ON_BAD_INPUT_IF(tokens.size() != 2,
				"Ill-formed input; bad string key-value pair");
	_strings[tokens[0]] = tokens[1];
	_keys.insert(tokens[0]);
}

void String_parser::parse_int_list(string input, const string& list_prefix)
{
	vector<string> tokens;
	split(input, ':', tokens);
	THROW_ON_BAD_INPUT_IF(tokens.size() != 2,
			"Ill-formed input; bad integer list key-value pair");
	_int_lists[tokens[0]] = vector<int>();
	_keys.insert(tokens[0]);

	string temp;
	extract_list_str(tokens[1], list_prefix, temp);
	Int_list_parser ilp;
	ilp.convert_to_comprehensive(temp, _int_lists[tokens[0]]);
}

void String_parser::parse_double_list(string input, const string& list_prefix)
{
	THROW_NOT_IMPLEMENTED_EXCEPTION();
}


bool String_parser::get_bool(const string& key, bool& value)
{
	if(_bools.find(key) == _bools.end())
		return false;
	value = _bools[key];
	return true;
}

bool String_parser::get_int(const string& key, int& value)
{
	if(_ints.find(key) == _ints.end())
		return false;
	value = _ints[key];
	return true;
}

bool String_parser::get_double(const string& key, double& value)
{
	if(_doubles.find(key) == _doubles.end())
		return false;
	value = _doubles[key];
	return true;
}

bool String_parser::get_string(const string& key, string& value)
{
	if(_strings.find(key) == _strings.end())
		return false;
	value = _strings[key];
	return true;
}

bool String_parser::get_int_list(const string& key, vector<int>& values)
{
	values.clear();
	if(_int_lists.find(key) == _int_lists.end())
		return false;
	values.assign(_int_lists[key].begin(), _int_lists[key].end());
	return true;
}

bool String_parser::get_added_int_list(const string& key, vector<int>& values)
{
	string actual_key = "+" + key;
	return get_int_list(actual_key, values);
}

bool String_parser::get_removed_int_list(const string& key, vector<int>& values)
{
	string actual_key = "-" + key;
	return get_int_list(actual_key, values);
}

bool String_parser::get_double_list(const string& key, vector<double>& values)
{
	values.clear();
	if(_double_lists.find(key) == _double_lists.end())
		return false;
	values.assign(_double_lists[key].begin(), _double_lists[key].end());
	return true;
}

bool String_parser::get_added_double_list(const string& key, vector<double>& values)
{
	string actual_key = "+" + key;
	return get_double_list(actual_key, values);
}

bool String_parser::get_removed_double_list(const string& key, vector<double>& values)
{
	string actual_key = "-" + key;
	return get_double_list(actual_key, values);
}

void String_parser::get_keys(vector<string>& keys)
{
	keys.clear();
	keys.assign(_keys.begin(), _keys.end());
}

bool String_parser::has_forbidden_keys(const vector<string> allowed_keys)
{
	vector<string> temp(_keys.begin(), _keys.end());
	return !is_superset(allowed_keys, temp);
}