-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
110 lines (92 loc) · 3.11 KB
/
main.cpp
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
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
/*
This program replaces the placeholders ${namespace:key} in the string with the value from the variables files
Input: <keyword> [parameters]
1. language [country_code]
2. replace "[String to process]" [preserveUnmatched=false]
3. quit
output: string with placeholders replaced
*/
#include<iostream>
#include<string>
#include "replacer.h"
using namespace std;
//Error code description
#define INVALID_REPLACE_FORMAT "Please enter valid replace format : replace \"[String to process]\" [true/false] "
#define INVALID_KEYWORD "Please enter valid keyword: language/replace/quit"
#define INVALID_LANGUAGE_FORMAT "Please enter the country code: language [country_code]"
void trim(string& s);
int main()
{
string command, keyword, parameters;
replacer rep;
size_t index;
cout<<"Please enter any of the below command"<<endl;
cout<<"\t-language [country_code]\n\t-replace \"[String to process]\" [preserveUnmatched=false]\n\t-quit"<<endl;
while (1)
{
getline(cin, command);
trim(command); //reads the command from STDIN
index = command.find(" ");
keyword = command.substr(0, index); //extracts the keyword
if (keyword == "quit")
{
return 0;
}
else if (keyword == "replace")
{
size_t index1 = command.find_first_of('"',index+1);
if(index1 == string::npos || command[command.find_first_not_of(" \t",index)] != '"')
{
cerr<<INVALID_REPLACE_FORMAT<<endl;
continue;
}
size_t index2 = command.find_last_of('"');
if(index2 == index1)
{
cerr<<INVALID_REPLACE_FORMAT<<endl;
continue;
}
//defaults preserveUnmatched parameter to false if not provided
if(index2 == command.length()-1)
{
rep.preserveUnmatched = "false";
}
else
{
rep.preserveUnmatched = command.substr(index2+2);
if(rep.preserveUnmatched != "false" && rep.preserveUnmatched != "true")
{
cerr<<INVALID_REPLACE_FORMAT<<endl;
continue;
}
}
//extracts the string to be parsed
parameters = command.substr(index1+1,index2-index1-1);
rep.multipleReplace(parameters);
}
else if (keyword == "language")
{
//if country code is not present
if(command.length() == keyword.length())
{
cerr<<INVALID_LANGUAGE_FORMAT<<endl;
continue;
}
rep.language = command.substr(index+1);
}
else
{
cerr<<INVALID_KEYWORD<<endl;
}
}
return 0;
}
//removes the leading and trailing whitespaces from the string
void trim(string& s)
{
size_t p = s.find_first_not_of(" \t");
s.erase(0, p);
p = s.find_last_not_of(" \t");
if (string::npos != p)
s.erase(p+1);
}