File: styles.c

package info (click to toggle)
postgis 3.5.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 69,528 kB
  • sloc: ansic: 162,229; sql: 93,970; xml: 53,139; cpp: 12,646; perl: 5,658; sh: 5,369; makefile: 3,435; python: 1,205; yacc: 447; lex: 151; pascal: 58
file content (215 lines) | stat: -rw-r--r-- 4,875 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**********************************************************************
 *
 * PostGIS - Spatial Types for PostgreSQL
 * http://postgis.net
 *
 * Copyright (C) 2022 Martin Davis
 * 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.
 *
 **********************************************************************/

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

#include "styles.h"


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

	*headRef = NULL;

	if ((pFile = fopen(filename, "r")) == NULL)
	{
		perror ( filename );
		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";
			int lineStartSize = 0;
			int lineEndSize = 0;
			int lineArrowSize = 0;
			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, "lineWidth", 9) == 0)
					{
						lineWidth = atoi(ptr);
						free(ptr);
					}
					else if (strncmp(line, "lineStartSize", 13) == 0)
					{
						lineStartSize = atoi(ptr);
						free(ptr);
					}
					else if (strncmp(line, "lineEndSize", 11) == 0)
					{
						lineEndSize = atoi(ptr);
						free(ptr);
					}
					else if (strncmp(line, "lineArrowSize", 3) == 0)
					{
						lineArrowSize = atoi(ptr);
						free(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, lineStartSize, lineEndSize, lineArrowSize,
				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,
	int lineStartSize, int lineEndSize, int lineArrowSize,
    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->lineStartSize = lineStartSize;
	style->lineEndSize = lineEndSize;
	style->lineArrowSize = lineArrowSize;
	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;
}