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
|
/*
Copyright 2015 Ian Jauslin
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "config.h"
#define READMODE_DEFAULT 0
#define READMODE_TIME 1
#define READMODE_SLIDELIST 2
// constructor
Config::Config(int argc,char* argv[]){
// defaults
loadDefaults();
// CLI options
readconf(argc,argv);
}
// default values
void Config::loadDefaults(){
document_path='\0';
presentation_time=15;
slidelist_path='\0';
}
// read CLI options
void Config::readconf(int argc,char* argv[]){
int i;
// mode specifier (e.g. reading duration?)
int mode=READMODE_DEFAULT;
// loop over arguments
for(i=1;i<argc;i++){
// the i=th argument
char* arg=argv[i];
// behavior depends on mode
switch(mode){
// default mode
case READMODE_DEFAULT:
// - means flagging options should follow
if(arg[0]=='-'){
// loop over flags following -
for(arg++;*arg!='\0';arg++){
switch (*arg){
// duration flag
case 't':
//go into duration reading mode (next argument should be the duration)
mode=READMODE_TIME;
break;
// slidelist flag
case 's':
//go into slidelist reading mode
mode=READMODE_SLIDELIST;
}
}
}
else{
// if the argument does not start with -, then it must denote the path to the PDF file
document_path=arg;
}
break;
// read duration mode
case READMODE_TIME:
//check that the argument is an integer
if(check_int(arg)){
sscanf(arg,"%d",&presentation_time);
}
else{
printf("syntax error: received argument '-t %s' when -t must be followed by an integer\n",arg);
}
// back to default mode for next argument
mode=READMODE_DEFAULT;
break;
// read slidelist mode
case READMODE_SLIDELIST:
slidelist_path=arg;
// back to default mode for next argument
mode=READMODE_DEFAULT;
}
}
}
// display configuration options
void Config::showconf(){
printf("document: %s\n",document_path);
printf("presentation duration: %d\n",presentation_time);
printf("slidelist: %s\n",slidelist_path);
}
// check whether a string is an integer
bool Config::check_int(char* string){
// yes?
bool ret=1;
for(;*string!='\0';string++){
// is the character a digit?
ret=ret && isdigit(*string);
}
return(ret);
}
// empty destructor
Config::~Config(){
}
|