/ src / argument_helper_functions.cpp
argument_helper_functions.cpp
 1  /**
 2   * @file argument_helper_functions.cpp
 3   * @author Rene Ceska xceska06 (xceska06@stud.fit.vutbr.cz)
 4   * @date 2023-11-19
 5   */
 6  #include "inc/argument_helper_functions.h"
 7  
 8  argsT parseArguments(int argc, const char **argv) {
 9  
10    // initialize args
11    argsT args;
12    args.err = false;
13    args.dbPath = (char *)malloc(sizeof(char) * 1000);
14    args.port = 389;
15  
16    // main loop for parsing arguments
17    for (int i = 1; i < argc; i++) {
18      if (strcmp(argv[i], "-f") == 0) {
19        // check if there is value for path to file
20        if (i + 1 >= argc) {
21          strcpy(args.dbPath, "");
22          return args;
23        } else {
24          strcpy(args.dbPath, argv[i + 1]);
25        }
26        i++; // skip next argument
27        continue;
28      }
29      if (strcmp(argv[i], "-p") == 0) {
30        // check if there is value for port and if it is in range
31        if (i + 1 >= argc || atoi(argv[i + 1]) <= 0 ||
32            atoi(argv[i + 1]) > 65535) {
33          args.err = true;
34          return args;
35        }
36        args.port = atoi(argv[i + 1]);
37        i++; // skip next argument
38        continue;
39      }
40      // check if there is unknown argument
41      fprintf(stderr, "Unknown argument: %s\n", argv[i]);
42      args.err = true;
43      return args;
44    }
45    return args;
46  }