File: stringlist.c

package info (click to toggle)
stringlist 0.2.1-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 456 kB
  • ctags: 96
  • sloc: sh: 4,710; ansic: 1,280; makefile: 89
file content (129 lines) | stat: -rw-r--r-- 2,913 bytes parent folder | download
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
#include "StringList.h"

int testForComment(char *line)
{
	EDBUG(8, "testForComment");
	if ((line[0] == '#') || (line[0] == '\n') || (line[0] == 0)) {
		EDBUG_RETURN(1);
	} else {
		EDBUG_RETURN(0);
	}
	EDBUG_RETURN(0);
}

int GetNextLine(char *line, FILE * ConfigFile)
{
	EDBUG(8, "GetNextLine");
	if (!fgets(line, FILEPATH_LEN_MAX, ConfigFile))
		EDBUG_RETURN(0);
	line[strlen(line) - 1] = 0;
	if (testForComment(line)) {
		GetNextLine(line, ConfigFile);
		EDBUG_RETURN(1);
	} else {
		{
			int i = 0;
			char line2[FILEPATH_LEN_MAX];

			line2[0] = 0;

			while (line[i]) {
				if (line[i] == '$') {
					int j = 0;
					char *environment_variable = 0;
					char env_name[FILEPATH_LEN_MAX];

					i++;
					if (line[i] != '$') {
						while (isalnum(line[i]) || line[i] == '_')
							env_name[j++] = line[i++];
						env_name[j] = 0;
						environment_variable = getenv(env_name);
						if (environment_variable)
							strcat(line2, environment_variable);
						else {
							strcat(line2, "$");
							strcat(line2, env_name);
						}
					}
				} else {
					char s[2];

					s[0] = line[i];
					s[1] = 0;
					strcat(line2, s);
					++i;
				}
			}
			strcpy(line, line2);
		}
		EDBUG_RETURN(1);
	}
	EDBUG_RETURN(0);
}

StringType *
 GenerateStringList(char *file)
{
	FILE *definitionFile;
	char line[FILEPATH_LEN_MAX];
	int definition;
	char s[1024];
	StringType *returnList = 0;

	EDBUG(8, "GenerateStringList");
	StringCount = 0;
	if (!file)
		definitionFile = fopen("definitions", "r");
	else
		definitionFile = fopen(file, "r");
	if (!definitionFile) {
		printf("Enlightenment cannot load your string definitions file:\n"
			   "%s\n"
			   "Enlightenment can probably continue to attempt to load\n"
			   "however this error may prove to be fatal.\n"
			   "If you do not require a definitions file, you should\n"
			   "create a blank file to avoid this error message in\n"
			   "the future.\n", file);
	}
	while (GetNextLine(line, definitionFile)) {
		sscanf(line, "%d %4000s", &definition, s);
		if (!returnList)
			returnList = Emalloc(sizeof(StringType));
		else
			returnList = Erealloc(returnList, sizeof(StringType) * (StringCount + 1));
		returnList[StringCount].definition = definition;
		returnList[StringCount].string = Emalloc(sizeof(char) * (strlen(s) + 1));

		returnList[StringCount].string = duplicate(s);
		++StringCount;
	}
	--StringCount;
	fclose(definitionFile);

	EDBUG_RETURN(returnList);
}

void DestroyStringList(StringType * listToKill)
{
	long i = 0;

	EDBUG(4, "DestroyStringList");
	for (i = 0; i <= StringCount; i++) {
		Efree(listToKill[i].string);
	}
	Efree(listToKill);
	EDBUG_RETURN_;

}

int ReturnStringReference(StringType * StringSet, char *actionString)
{
	long i = 0;

	EDBUG(8, "ReturnStringReference");
	for (i = 0; i <= StringCount; i++)
		if (!strcasecmp(StringSet[i].string, actionString))
			EDBUG_RETURN(StringSet[i].definition);
	EDBUG_RETURN(0);
}