Ian Jauslin
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Jauslin <ian.jauslin@roma1.infn.it>2015-06-14 00:52:45 +0000
committerIan Jauslin <ian.jauslin@roma1.infn.it>2015-06-14 00:52:45 +0000
commitaa0f3ae2988d372b190b9bde2e75a6d17e744e93 (patch)
tree14482245c2fca27fcdad3078e97d0871352d52a7 /src/cli_parser.c
Initial commitv1.2
Diffstat (limited to 'src/cli_parser.c')
-rw-r--r--src/cli_parser.c123
1 files changed, 123 insertions, 0 deletions
diff --git a/src/cli_parser.c b/src/cli_parser.c
new file mode 100644
index 0000000..bdca700
--- /dev/null
+++ b/src/cli_parser.c
@@ -0,0 +1,123 @@
+/*
+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 "cli_parser.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "definitions.cpp"
+#include "array.h"
+
+// read a config file and write the output to str_args
+int read_config_file(Str_Array* str_args, const char* file, int read_from_stdin){
+ FILE* infile;
+ char c;
+ Char_Array buffer;
+
+ // allocate memory
+ init_Str_Array(str_args, ARG_COUNT);
+ init_Char_Array(&buffer, STR_SIZE);
+
+ // read from file
+ if(read_from_stdin==0){
+ infile=fopen(file,"r");
+ if(infile==NULL){
+ fprintf(stderr,"error: can't open file %s\n",file);
+ exit(-1);
+ }
+ }
+ else{
+ infile=stdin;
+ }
+
+ while(1){
+ c=fgetc(infile);
+ // add to str_args
+ if(c=='&'){
+ str_array_append_noinit(buffer,str_args);
+ init_Char_Array(&buffer, STR_SIZE);
+ }
+ else if(c==EOF){
+ str_array_append_noinit(buffer,str_args);
+ break;
+ }
+ else{
+ char_array_append(c,&buffer);
+ }
+ }
+ fclose(infile);
+
+ return(0);
+}
+
+
+// get the title from a string argument
+int get_str_arg_title(Char_Array str_arg, Char_Array* out){
+ int j,k;
+
+ init_Char_Array(out,STR_SIZE);
+
+ // find "#!" at beginning of line
+ for(j=0;j<str_arg.length-2;j++){
+ if(str_arg.str[j]=='#' && str_arg.str[j+1]=='!' && (j==0 || str_arg.str[j-1]=='\n')){
+ break;
+ }
+ }
+ // if no title then error
+ if(j>=str_arg.length-2){
+ fprintf(stderr, "error: an entry in the configuration file does not have a title (which should be preceeded by '#!' at the beginning of a line)\n");
+ exit(-1);
+ }
+
+ // get title until end of line
+ for(k=j+2;k<str_arg.length && str_arg.str[k]!='\n'; k++){
+ char_array_append(str_arg.str[k],out);
+ }
+
+ return(0);
+}
+
+// find a string argument with the specified title
+int find_str_arg(char* title, Str_Array str_args){
+ int i,k;
+ char* ptr;
+ Char_Array buffer;
+
+ for(i=0;i<str_args.length;i++){
+ get_str_arg_title(str_args.strs[i], &buffer);
+ // match title
+ for(k=0, ptr=title; k<buffer.length; k++, ptr++){
+ if(buffer.str[k]!=*ptr){
+ break;
+ }
+ }
+ // if match
+ if(k==buffer.length){
+ free_Char_Array(buffer);
+ break;
+ }
+
+ free_Char_Array(buffer);
+ }
+ // if found
+ if(i<str_args.length){
+ return(i);
+ }
+ else{
+ return(-1);
+ }
+}
+