diff options
Diffstat (limited to 'src/config.cpp')
-rw-r--r-- | src/config.cpp | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/config.cpp b/src/config.cpp new file mode 100644 index 0000000..60e3345 --- /dev/null +++ b/src/config.cpp @@ -0,0 +1,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(){ +} |