File: styles.c

package info (click to toggle)
postgis 2.3.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 58,660 kB
  • ctags: 10,181
  • sloc: ansic: 132,858; sql: 131,148; xml: 46,460; sh: 4,832; perl: 4,476; makefile: 2,749; python: 1,198; yacc: 442; lex: 131
file content (190 lines) | stat: -rw-r--r-- 4,202 bytes parent folder | download | duplicates (5)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**********************************************************************
 *
 * PostGIS - Spatial Types for PostgreSQL
 * http://postgis.net
 * Copyright 2008 Kevin Neufeld
 *
 * This is free software; you can redistribute and/or modify it under
 * the terms of the GNU General Public Licence. See the COPYING file.
 *
 * TODO: fix segfault bug caused by a referenced style that doesn't exist in
 *          the .conf file
 **********************************************************************/

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

#include "styles.h"


void
getStyles( LAYERSTYLE **headRef )
{
	char line [128];
	FILE* pFile;
	char *getResults;

	*headRef = NULL;

	if ((pFile = fopen("styles.conf", "r")) == NULL)
	{
		perror ( "styles.conf: No such file or directory" );
		return;
	}

	getResults = fgets ( line, sizeof line, pFile );
	while ( getResults != NULL )
	{

		// process defined styles
		while ( (getResults != NULL) && strncmp(line, "[Style]", 7) == 0)
		{
			char *styleName = "DefaultStyle";
			int pointSize = 5;
			char *pointColor = "Grey";
			int lineWidth = 5;
			char *lineColor = "Grey";
			char *polygonFillColor = "Grey";
			char *polygonStrokeColor = "Grey";
			int polygonStrokeWidth = 0;

			getResults = fgets ( line, sizeof line, pFile );
			while ( (getResults != NULL) && (strncmp(line, "[Style]", 7) != 0) )
			{
				char *ptr;

				// loop over all lines until [Style] is reached again
				if ( (*line != '#') && (ptr = strchr(line, '=')) )
				{
					ptr = trim((++ptr));

					if (strncmp(line, "styleName", 9) == 0)
						styleName = ptr;
					else if (strncmp(line, "pointSize", 9) == 0)
					{
						pointSize = atoi(ptr);
						free(ptr);
					}
					else if (strncmp(line, "pointColor", 10) == 0)
						pointColor = ptr;
					else if (strncmp(line, "lineWidth", 9) == 0)
					{
						lineWidth = atoi(ptr);
						free(ptr);
					}
					else if (strncmp(line, "lineColor", 9) == 0)
						lineColor = ptr;
					else if (strncmp(line, "polygonFillColor", 16) == 0)
						polygonFillColor = ptr;
					else if (strncmp(line, "polygonStrokeColor", 18) == 0)
						polygonStrokeColor = ptr;
					else if (strncmp(line, "polygonStrokeWidth", 18) == 0)
					{
						polygonStrokeWidth = atoi(ptr);
						free(ptr);
					}

				}
				getResults = fgets ( line, sizeof line, pFile );
			}

			addStyle(headRef, styleName, pointSize, pointColor, lineWidth, lineColor, polygonFillColor, polygonStrokeColor, polygonStrokeWidth);
		}

		getResults = fgets ( line, sizeof line, pFile );
	}

	fclose( pFile );
}


void
freeStyles( LAYERSTYLE **headRef )
{
	LAYERSTYLE *curr = *headRef;
	LAYERSTYLE *next;

	while (curr != NULL)
	{
		next = curr->next;
		free(curr->styleName);
		free(curr->pointColor);
		free(curr->lineColor);
		free(curr->polygonFillColor);
		free(curr->polygonStrokeColor);
		free(curr);
		curr = next;
	}

	*headRef = NULL;
}


void
addStyle(
    LAYERSTYLE **headRef,
    char* styleName,
    int pointSize, char* pointColor,
    int lineWidth, char* lineColor,
    char* polygonFillColor, char* polygonStrokeColor, int polygonStrokeWidth)
{
	LAYERSTYLE *style = malloc( sizeof(LAYERSTYLE) );

	style->styleName = styleName;
	style->pointSize = pointSize;
	style->pointColor = pointColor;
	style->lineWidth = lineWidth;
	style->lineColor = lineColor;
	style->polygonFillColor = polygonFillColor;
	style->polygonStrokeColor = polygonStrokeColor;
	style->polygonStrokeWidth = polygonStrokeWidth;
	style->next = *headRef;
	*headRef = style;
}


int
length( LAYERSTYLE *head )
{
	int count = 0;
	LAYERSTYLE *curr = head;

	while (curr != NULL)
	{
		count++;
		curr = curr->next;
	}

	return (count);
}


LAYERSTYLE*
getStyle( LAYERSTYLE *headRef, char* styleName )
{
	LAYERSTYLE *curr = headRef;

	while (curr != NULL && (strcmp(curr->styleName, styleName) != 0))
		curr = curr->next;

	return (curr);
}


char*
trim(char* str)
{
	int len;
	char* result;
	char* start = str;
	char* end = strchr(start, '\0');
	while (start<end && isspace(*start)) start++;
	while (start<end && isspace(*(end-1))) end--;
	len = end-start;
	result = malloc( len+1 );
	strncpy(result, start, len);
	result[len] = '\0';
	return result;
}