summaryrefslogtreecommitdiff
path: root/src/client/configFile.c
blob: 162658ba4c67094f01efb4d3135c57d83741adea (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
144
145
146

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "main.h"
#include "configFile.h"

int isYesOrNO(char *s)
{
	if (s[0] == 'Y' || s[0] == 'y')
		return 1;
	return 0;
}

char *getYesOrNo(int n)
{
	return (n != 0 ? "YES" : "NO");
}

int getValue(char *line, char *env, char *val, int len)
{
	char *offset_env;
	char *offset_val_begin;
	char *offset_val_end;
	char clone_env[STR_SIZE];
	int val_len;

	strcpy(clone_env, env);

	offset_env = strstr(line, clone_env);

	if (offset_env == NULL)
		return -1;

	offset_val_begin = strchr(offset_env, '"');

	if (offset_val_begin == NULL)
		return -1;

	offset_val_end = strchr(offset_val_begin + 1, '"');

	if (offset_val_end == NULL)
		return -1;

	val_len = (int) (offset_val_end - (offset_val_begin + 1));

	if (val_len > len - 1)
		val_len = len - 1;

	memset(val, 0, len);
	memcpy(val, offset_val_begin + 1, val_len);

	return 0;
}

char *setValue(char *line, char *env, char *val)
{
	char *offset_env;
	char *offset_val_begin;
	char *offset_val_end;
	char clone_env[STR_SIZE];
	char clone[STR_SIZE];

	//strcpy(clone_env, " ");
	strcpy(clone_env, env);

	offset_env = strstr(line, clone_env);

	if (offset_env == NULL)
		return NULL;

	offset_val_begin = strchr(offset_env, '"');

	if (offset_val_begin == NULL)
		return NULL;

	offset_val_end = strchr(offset_val_begin + 1, '"');

	if (offset_val_end == NULL)
		return NULL;

	memset(clone, 0, STR_SIZE);
	strncpy(clone, line, (int) ((offset_val_begin - line) + 1));
	strcat(clone, val);
	strcat(clone, offset_val_end);

	return strdup(clone);
}

int getValueInConfigFile(textFile_t * textFile, char *env, char *val, int len)
{
	int i;
	char *line;

	for (i = 0; i < textFile->text->count; i++) {
		line = (char *) (textFile->text->list[i]);

		//printf("env = %s line = %s\n", env, line);

		if (strncmp(line, env, strlen(env)) == 0) {
			return getValue(line, env, val, len);
		}
	}

	return -1;
}

int setValueInConfigFile(textFile_t * textFile, char *env, char *val)
{
	int i;
	char *line;
	char *ret;

	for (i = 0; i < textFile->text->count; i++) {
		line = (char *) (textFile->text->list[i]);

		if (strncmp(line, env, strlen(env)) == 0) {
			ret = setValue(line, env, val);

			if (ret != NULL) {
				list_del_item(textFile->text, i, free);
				list_ins(textFile->text, i, ret);

				return 0;
			}
		}
	}

	return -1;
}

void loadValueFromConfigFile(textFile_t * textFile, char *env, char *val, int len, char *butVal)
{
	strcpy(val, butVal);

	if (getValueInConfigFile(textFile, env, val, len) != 0) {
		char line[STR_SIZE];

		sprintf(line, "%s=\"%s\"", env, butVal);
		list_add(textFile->text, strdup(line));

		DEBUG_MSG(_("ADDING: \"%s\" into config file.\n"), line);
	}
}