-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
206 lines (206 loc) · 4.14 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
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<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include"libcore/library.h"
using namespace lib;
using namespace std;
bool saved=true;
string name;
Library *library;
void getline(string &str,bool loop=true){
auto read=[&str]{
getline(cin,str);
auto first=str.find_first_not_of(" \t\n\v\f\r"),last=str.find_last_not_of(" \t\n\v\f\r");
if(first!=string::npos)
str.erase(0,first);
if(last!=string::npos)
str.erase(last+1);
};
if(loop)
do
read();
while(str.empty());
else
read();
}
void version(){
cout<<"lib4console beta 1.4.1\n"
<<"license: GPL-3.0\n"
<<"GitHub address: https://github.com/lrcno6/lib4console\n";
}
bool main_menu(){
cout<<"lib4console$ ";
string cmd;
getline(cmd,false);
vector<string> argv;
size_t index=0;
while(index<cmd.size()){
auto index2=cmd.find_first_of(" \t\n\v\f\r",index);
if(index2==string::npos)
index2=cmd.size();
if(index2>index)
argv.push_back(cmd.substr(index,index2-index));
index=index2+1;
}
if(argv.empty())
return true;
string &command=argv[0];
if(command=="newid")
cout<<Library::new_id(8)<<endl;
else if(command=="lend"){
string id,name;
if(argv.size()>1)
id=argv[1];
else{
cout<<"the book's ID: ";
do
getline(id);
while(id.empty());
}
if(argv.size()>2)
name=argv[2];
else{
cout<<"borrower: ";
do
getline(name);
while(name.empty());
}
try{
library->lend(id,name);
saved=false;
}
catch(logic_error&){
cerr<<"error: the ID is used\n";
}
}
else if(command=="return"){
string id;
if(argv.size()>1)
id=argv[1];
else{
cout<<"the book's ID: ";
do
getline(id);
while(id.empty());
}
try{
library->Return(id);
saved=false;
}
catch(out_of_range&){
cerr<<"error: the ID is not used\n";
}
}
else if(command=="show"){
if(library->books.empty())
cout<<"no books are borrowed\n";
else
for(auto i:library->sort_by_time())
cout<<i.show();
}
else if(command=="save"){
if(!saved){
if(name.empty()){
if(argv.size()>1)
name=argv[1];
else{
cout<<"enter file name: ";
do
getline(name);
while(name.empty());
}
}
ofstream fout(name);
if(fout.is_open()){
library->save(fout);
saved=true;
}
else
cerr<<"error: the file cannot be written - maybe it is read-only?\n";
}
}
else if(command=="exit"){
if(saved)
return false;
cout<<"Are you sure that you do not need to save? [y/n] :";
string judge;
loop:
getline(judge);
if(judge=="y" || judge=="Y")
return false;
if(judge!="n" && judge!="N")
goto loop;
}
else if(command=="help")
cout<<"newid\tcreate a new ID\n"
<<"lend\tlend a book\n"
<<"return\treturn a book\n"
<<"show\tshow the borrowed books in chronological order\n"
<<"save\n"
<<"exit\n"
<<"help\tget help\n"
<<"version\tget the version information\n";
else if(command=="version")
version();
else
cerr<<"error: command not found\n";
return true;
}
int main(int argc,char **argv){
srand(time(nullptr));
switch(argc){
case 0:
cerr<<"error: How do you do it? You make argc==0!\n";
return 256;
case 1:
cerr<<"warning: a new file\n\n";
library=new Library;
break;
case 2:{
name=argv[1];
if(argv[1][0]=='-'){
if(argv[1][1]=='-'){
if(name=="--help")
cout<<"usage: [./]lib4console [-h | --help] [-v | --version] <file>\n";
else if(name=="--version")
version();
else{
cerr<<"error: invalid option\n";
return 256;
}
}
else{
if(name=="-h")
cout<<"usage: [./]lib4console [-h | --help] [-v | --version] <file>\n";
else if(name=="-v")
version();
else{
cerr<<"error: invalid option\n";
return 256;
}
}
return 0;
}
ifstream fin(argv[1]);
if(!fin.is_open()){
cerr<<"error: "<<argv[1]<<": cannot find the file\n";
return 256;
}
library=new Library(fin);
break;
}
default:
cerr<<"error: too many args\n";
return 256;
}
version();
cout<<"please enter \"help\" for help or \"version\" for version information\n";
while(main_menu());
delete library;
return 0;
}