/* Copyright 2015-2022 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 #include #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){ 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