Ian Jauslin
summaryrefslogtreecommitdiff
blob: 6012e8a9b2e7a9790276254c79e8dd116df2901f (plain)
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
/*
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 <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);
  }
}

// find a string argument with the specified title
// namespace support
int find_str_arg_ns(char* title, Char_Array namespace, Str_Array str_args){
  Char_Array buffer;
  int ret;

  // append namespace to title
  char_array_cpy(namespace,&buffer);
  char_array_append(':',&buffer);
  char_array_append_str(title,&buffer);

  // check whether the namespace entry exists
  ret=find_str_arg(char_array_to_str_noinit(&buffer), str_args);
  free_Char_Array(buffer);
  // if not, use global entry
  if(ret==-1){
    return(find_str_arg(title, str_args));
  }
  return(ret);
}