Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Contents of /trunk/src/waysx.c
Parent Directory
|
Revision Log
Revision 181 -
(show annotations)
(download)
(as text)
Sun May 31 12:30:12 2009 UTC (15 years, 9 months ago) by amb
File MIME type: text/x-csrc
File size: 5464 byte(s)
Sun May 31 12:30:12 2009 UTC (15 years, 9 months ago) by amb
File MIME type: text/x-csrc
File size: 5464 byte(s)
Move function from waysx.c to ways.c.
1 | /*************************************** |
2 | $Header: /home/amb/CVS/routino/src/waysx.c,v 1.5 2009-05-31 12:30:12 amb Exp $ |
3 | |
4 | Extended Way data type functions. |
5 | |
6 | Part of the Routino routing software. |
7 | ******************/ /****************** |
8 | This file Copyright 2008,2009 Andrew M. Bishop |
9 | |
10 | This program is free software: you can redistribute it and/or modify |
11 | it under the terms of the GNU Affero General Public License as published by |
12 | the Free Software Foundation, either version 3 of the License, or |
13 | (at your option) any later version. |
14 | |
15 | This program is distributed in the hope that it will be useful, |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public License |
21 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
22 | ***************************************/ |
23 | |
24 | |
25 | #include <assert.h> |
26 | #include <stdlib.h> |
27 | #include <string.h> |
28 | |
29 | #include "functions.h" |
30 | #include "waysx.h" |
31 | |
32 | |
33 | /* Constants */ |
34 | |
35 | /*+ The array size increment for ways - expect ~1,000,000 ways. +*/ |
36 | #define INCREMENT_WAYS 256*1024 |
37 | |
38 | |
39 | /* Functions */ |
40 | |
41 | static int sort_by_name(WayX **a,WayX **b); |
42 | |
43 | |
44 | /*++++++++++++++++++++++++++++++++++++++ |
45 | Allocate a new way list. |
46 | |
47 | WaysX *NewWayList Returns the way list. |
48 | ++++++++++++++++++++++++++++++++++++++*/ |
49 | |
50 | WaysX *NewWayList(void) |
51 | { |
52 | WaysX *waysx; |
53 | |
54 | waysx=(WaysX*)malloc(sizeof(WaysX)); |
55 | |
56 | waysx->sorted=0; |
57 | waysx->alloced=INCREMENT_WAYS; |
58 | waysx->number=0; |
59 | waysx->length=0; |
60 | |
61 | waysx->idata=(WayX*)malloc(waysx->alloced*sizeof(WayX)); |
62 | waysx->ndata=NULL; |
63 | |
64 | return(waysx); |
65 | } |
66 | |
67 | |
68 | /*++++++++++++++++++++++++++++++++++++++ |
69 | Save the way list to a file. |
70 | |
71 | WaysX* waysx The set of ways to save. |
72 | |
73 | const char *filename The name of the file to save. |
74 | ++++++++++++++++++++++++++++++++++++++*/ |
75 | |
76 | void SaveWayList(WaysX* waysx,const char *filename) |
77 | { |
78 | int i; |
79 | int fd; |
80 | Ways *ways=calloc(1,sizeof(Ways)); |
81 | |
82 | assert(waysx->sorted); /* Must be sorted */ |
83 | |
84 | /* Fill in a Ways structure with the offset of the real data in the file after |
85 | the Way structure itself. */ |
86 | |
87 | ways->number=waysx->number; |
88 | ways->data=NULL; |
89 | ways->ways=(void*)sizeof(Ways); |
90 | ways->names=(void*)sizeof(Ways)+ways->number*sizeof(Way); |
91 | |
92 | /* Write out the Ways structure and then the real data. */ |
93 | |
94 | fd=OpenFile(filename); |
95 | |
96 | WriteFile(fd,ways,sizeof(Ways)); |
97 | |
98 | for(i=0;i<waysx->number;i++) |
99 | { |
100 | WriteFile(fd,&waysx->idata[i].way,sizeof(Way)); |
101 | |
102 | if(!((i+1)%10000)) |
103 | { |
104 | printf("\rWriting Ways: Ways=%d",i+1); |
105 | fflush(stdout); |
106 | } |
107 | } |
108 | |
109 | printf("\rWrote Ways: Ways=%d \n",waysx->number); |
110 | fflush(stdout); |
111 | |
112 | WriteFile(fd,waysx->names,waysx->length); |
113 | |
114 | CloseFile(fd); |
115 | |
116 | /* Free the fake Ways */ |
117 | |
118 | free(ways); |
119 | } |
120 | |
121 | |
122 | /*++++++++++++++++++++++++++++++++++++++ |
123 | Append a way to a way list. |
124 | |
125 | Way *AppendWay Returns the newly appended way. |
126 | |
127 | WaysX* waysx The set of ways to process. |
128 | |
129 | const char *name The name or reference of the way. |
130 | ++++++++++++++++++++++++++++++++++++++*/ |
131 | |
132 | Way *AppendWay(WaysX* waysx,const char *name) |
133 | { |
134 | assert(!waysx->sorted); /* Must not be sorted */ |
135 | |
136 | waysx->sorted=0; |
137 | |
138 | /* Check that the array has enough space. */ |
139 | |
140 | if(waysx->number==waysx->alloced) |
141 | { |
142 | waysx->alloced+=INCREMENT_WAYS; |
143 | |
144 | waysx->idata=(WayX*)realloc((void*)waysx->idata,waysx->alloced*sizeof(WayX)); |
145 | } |
146 | |
147 | /* Insert the way */ |
148 | |
149 | waysx->idata[waysx->number].name=strcpy((char*)malloc(strlen(name)+1),name); |
150 | |
151 | memset(&waysx->idata[waysx->number].way,0,sizeof(Way)); |
152 | |
153 | waysx->number++; |
154 | |
155 | return(&waysx->idata[waysx->number-1].way); |
156 | } |
157 | |
158 | |
159 | /*++++++++++++++++++++++++++++++++++++++ |
160 | Sort the list of ways and fix the names. |
161 | |
162 | WaysX* waysx The set of ways to process. |
163 | ++++++++++++++++++++++++++++++++++++++*/ |
164 | |
165 | void SortWayList(WaysX* waysx) |
166 | { |
167 | int i; |
168 | |
169 | assert(!waysx->sorted); /* Must not be sorted */ |
170 | |
171 | waysx->sorted=1; |
172 | |
173 | printf("Sorting Ways"); fflush(stdout); |
174 | |
175 | /* Sort the ways by name */ |
176 | |
177 | waysx->ndata=malloc(waysx->number*sizeof(WayX*)); |
178 | |
179 | for(i=0;i<waysx->number;i++) |
180 | waysx->ndata[i]=&waysx->idata[i]; |
181 | |
182 | qsort(waysx->ndata,waysx->number,sizeof(WayX*),(int (*)(const void*,const void*))sort_by_name); |
183 | |
184 | /* Allocate the new data */ |
185 | |
186 | waysx->names=(char*)malloc(waysx->alloced*sizeof(char)); |
187 | |
188 | /* Setup the offsets for the names in the way array */ |
189 | |
190 | for(i=0;i<waysx->number;i++) |
191 | { |
192 | if(i && !strcmp(waysx->ndata[i]->name,waysx->ndata[i-1]->name)) /* Same name */ |
193 | waysx->ndata[i]->way.name=waysx->ndata[i-1]->way.name; |
194 | else /* Different name */ |
195 | { |
196 | if((waysx->length+strlen(waysx->ndata[i]->name)+1)>=waysx->alloced) |
197 | { |
198 | waysx->alloced+=INCREMENT_WAYS; |
199 | |
200 | waysx->names=(char*)realloc((void*)waysx->names,waysx->alloced*sizeof(char)); |
201 | } |
202 | |
203 | strcpy(&waysx->names[waysx->length],waysx->ndata[i]->name); |
204 | |
205 | waysx->ndata[i]->way.name=waysx->length; |
206 | |
207 | waysx->length+=strlen(waysx->ndata[i]->name)+1; |
208 | } |
209 | } |
210 | |
211 | printf("\rSorted Ways \n"); fflush(stdout); |
212 | } |
213 | |
214 | |
215 | /*++++++++++++++++++++++++++++++++++++++ |
216 | Sort the ways into name order. |
217 | |
218 | int sort_by_name Returns the comparison of the name fields. |
219 | |
220 | Way **a The first Way. |
221 | |
222 | Way **b The second Way. |
223 | ++++++++++++++++++++++++++++++++++++++*/ |
224 | |
225 | static int sort_by_name(WayX **a,WayX **b) |
226 | { |
227 | char *a_name=(*a)->name; |
228 | char *b_name=(*b)->name; |
229 | |
230 | return(strcmp(a_name,b_name)); |
231 | } |
Properties
Name | Value |
---|---|
cvs:description | Extended ways functions. |