GitLab will be upgraded on 30 Jan 2023 from 2.00 pm (AEDT) to 3.00 pm (AEDT). During the update, GitLab and Mattermost services will not be available. If you have any concerns with this, please talk to us at N110 (b) CSIT building.

dacapooptions.c 3.01 KB
Newer Older
john's avatar
john committed
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
/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdarg.h>
*/

#include "dacapooptions.h"

#ifndef FALSE
#define FALSE 0
#endif

/*
	public static final String CLASSES_INITIALIZATION = "clinit";
	public static final String METHOD_CALLS           = "method_calls";
	public static final String LOG_START              = "log_start";
	public static final String LOG_STOP               = "log_stop";
	public static final String RUNTIME                = "runtime";
*/

struct option_s {
    struct option_s*  next;
    const char*       option;
    int               length;
    const char*       argument;
    int               argLength;
};

struct option_s*    optionList = NULL;
struct option_s*    optionTail = NULL;

static struct option_s* findOption(const char* option) {
	struct option_s* check = optionList;

	while (check!=NULL) {
	    if (strncmp(check->option,option,check->length)==0 &&
	        (int)strlen(option)==check->length) {
    		return check;
	    }
	    check = check->next;
	}
	return NULL;
}

47
_Bool isSelected(const char* option, char** argument) {
john's avatar
john committed
48
49
	struct option_s* opt = findOption(option);

50
51
52
53
54
	if (opt != NULL && argument != NULL) {
		if (*argument==NULL)
			*argument = (char*)malloc(opt->argLength+1);
		if (0 < opt->argLength) strncpy(*argument,opt->argument,opt->argLength);
		(*argument)[opt->argLength]='\0';
john's avatar
john committed
55
	}
56

john's avatar
john committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
	return opt!=NULL;
}

_Bool hasArgument(const char* option) {
	struct option_s* opt = findOption(option);

	return opt!=NULL && opt->argument!=NULL && opt->argLength>0;
}

void reportOptionsList() {
	char tmp[10240];

	struct option_s* opt = optionList;

    fprintf(stderr,"reportOptionsList\n");
	while (opt!=NULL) {
	    strncpy(tmp,opt->option,opt->length);
	    tmp[opt->length] = '\0';
		if (opt->argument!=NULL) {
		    tmp[opt->length]=':';
john's avatar
john committed
77
			strncpy(tmp+opt->length+1,opt->argument,opt->argLength);
john's avatar
john committed
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
			tmp[opt->length+1+opt->argLength]='\0';
		}
		fprintf(stderr,"  %s\n",tmp);
		opt = opt->next;
	}
}

void makeOptionList(const char* options) {
	if (options != NULL) {
        int start = 0;
		while (options[start]!='\0') {
		    int end = start+(start==0?0:1);

		    while (options[end]!=','&&options[end]!='\0'&&options[end]!='=') end++;

		    int arg = end;

		    if (options[arg]=='=')
		    	while(options[arg]!=','&&options[arg]!='\0') arg++;

		    if (0<(end-start)) {
		        struct option_s *tmp = (struct option_s*)malloc(sizeof(struct option_s));
		        tmp->next = NULL;
		        tmp->option = options+start;
		        tmp->length = end-start;

		        if (arg==end) {
		        	tmp->argument  = NULL;
		        	tmp->argLength = 0;
		        } else {
		        	tmp->argument  = options+end+1;
		        	tmp->argLength = arg-end-1;
		        }

		        if (optionList==NULL) {
		        	optionList = optionTail = tmp;
		        } else {
		            optionTail->next = tmp;
		            optionTail = tmp;
		        }
		    }

		    start = arg + (options[arg]=='\0'?0:1);
		}
	}

john's avatar
john committed
124
125
	if (isSelected(OPT_REPORT_OPTIONS, NULL))
		reportOptionsList();
john's avatar
john committed
126
127
}