Check out the latest version of Routino: svn co http://routino.org/svn/trunk routino
Contents of /trunk/src/errorlog.c
Parent Directory
|
Revision Log
Revision 1415 -
(show annotations)
(download)
(as text)
Sat Jun 22 18:52:38 2013 UTC (11 years, 8 months ago) by amb
File MIME type: text/x-csrc
File size: 5563 byte(s)
Sat Jun 22 18:52:38 2013 UTC (11 years, 8 months ago) by amb
File MIME type: text/x-csrc
File size: 5563 byte(s)
Use SlimMapFile() and SlimUnmapFile() [see previous log message] and also use SlimFetch() instead of SeekReadFileUnbuffered() and SlimReplace() instead of SeekWriteFileUnbuffered() to hide the internals of the slim mode.
1 | /*************************************** |
2 | Error log data type functions. |
3 | |
4 | Part of the Routino routing software. |
5 | ******************/ /****************** |
6 | This file Copyright 2013 Andrew M. Bishop |
7 | |
8 | This program is free software: you can redistribute it and/or modify |
9 | it under the terms of the GNU Affero General Public License as published by |
10 | the Free Software Foundation, either version 3 of the License, or |
11 | (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU Affero General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU Affero General Public License |
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | ***************************************/ |
21 | |
22 | |
23 | #include <stdlib.h> |
24 | #include <math.h> |
25 | |
26 | #include "types.h" |
27 | #include "errorlog.h" |
28 | |
29 | #include "files.h" |
30 | |
31 | |
32 | /*++++++++++++++++++++++++++++++++++++++ |
33 | Load in an error log list from a file. |
34 | |
35 | ErrorLogs *LoadErrorLogs Returns the error log list. |
36 | |
37 | const char *filename The name of the file to load. |
38 | ++++++++++++++++++++++++++++++++++++++*/ |
39 | |
40 | ErrorLogs *LoadErrorLogs(const char *filename) |
41 | { |
42 | ErrorLogs *errorlogs; |
43 | |
44 | errorlogs=(ErrorLogs*)malloc(sizeof(ErrorLogs)); |
45 | |
46 | #if !SLIM |
47 | |
48 | errorlogs->data=MapFile(filename); |
49 | |
50 | /* Copy the ErrorLogsFile header structure from the loaded data */ |
51 | |
52 | errorlogs->file=*((ErrorLogsFile*)errorlogs->data); |
53 | |
54 | /* Set the pointers in the ErrorLogs structure. */ |
55 | |
56 | errorlogs->offsets =(index_t* )(errorlogs->data+sizeof(ErrorLogsFile)); |
57 | errorlogs->errorlogs_geo =(ErrorLog*)(errorlogs->data+sizeof(ErrorLogsFile)+(errorlogs->file.latbins*errorlogs->file.lonbins+1)*sizeof(index_t)); |
58 | errorlogs->errorlogs_nongeo=(ErrorLog*)(errorlogs->data+sizeof(ErrorLogsFile)+(errorlogs->file.latbins*errorlogs->file.lonbins+1)*sizeof(index_t)+errorlogs->file.number_geo*sizeof(ErrorLog)); |
59 | errorlogs->strings =(char* )(errorlogs->data+sizeof(ErrorLogsFile)+(errorlogs->file.latbins*errorlogs->file.lonbins+1)*sizeof(index_t)+errorlogs->file.number*sizeof(ErrorLog)); |
60 | |
61 | #else |
62 | |
63 | errorlogs->fd=SlimMapFile(filename); |
64 | |
65 | /* Copy the ErrorLogsFile header structure from the loaded data */ |
66 | |
67 | SlimFetch(errorlogs->fd,&errorlogs->file,sizeof(ErrorLogsFile),0); |
68 | |
69 | errorlogs->offsetsoffset =sizeof(ErrorLogsFile); |
70 | errorlogs->errorlogsoffset_geo =sizeof(ErrorLogsFile)+(errorlogs->file.latbins*errorlogs->file.lonbins+1)*sizeof(index_t); |
71 | errorlogs->errorlogsoffset_nongeo=sizeof(ErrorLogsFile)+(errorlogs->file.latbins*errorlogs->file.lonbins+1)*sizeof(index_t)+errorlogs->file.number_geo*sizeof(ErrorLog); |
72 | errorlogs->stringsoffset =sizeof(ErrorLogsFile)+(errorlogs->file.latbins*errorlogs->file.lonbins+1)*sizeof(index_t)+errorlogs->file.number*sizeof(ErrorLog); |
73 | |
74 | #endif |
75 | |
76 | return(errorlogs); |
77 | } |
78 | |
79 | |
80 | /*++++++++++++++++++++++++++++++++++++++ |
81 | Destroy the node list. |
82 | |
83 | ErrorLogs *errorlogs The node list to destroy. |
84 | ++++++++++++++++++++++++++++++++++++++*/ |
85 | |
86 | void DestroyErrorLogs(ErrorLogs *errorlogs) |
87 | { |
88 | #if !SLIM |
89 | |
90 | errorlogs->data=UnmapFile(errorlogs->data); |
91 | |
92 | #else |
93 | |
94 | errorlogs->fd=SlimUnmapFile(errorlogs->fd); |
95 | |
96 | #endif |
97 | |
98 | free(errorlogs); |
99 | } |
100 | |
101 | |
102 | /*++++++++++++++++++++++++++++++++++++++ |
103 | Get the latitude and longitude associated with an error log. |
104 | |
105 | ErrorLog *errorlogs The set of error logs to use. |
106 | |
107 | index_t index The errorlog index. |
108 | |
109 | ErrorLog *errorlogp A pointer to the error log. |
110 | |
111 | double *latitude Returns the latitude. |
112 | |
113 | double *longitude Returns the logitude. |
114 | ++++++++++++++++++++++++++++++++++++++*/ |
115 | |
116 | void GetErrorLogLatLong(ErrorLogs *errorlogs,index_t index,ErrorLog *errorlogp,double *latitude,double *longitude) |
117 | { |
118 | ll_bin_t latbin,lonbin; |
119 | ll_bin2_t bin=-1; |
120 | ll_bin2_t start,end,mid; |
121 | index_t offset; |
122 | |
123 | /* Binary search - search key nearest match below is required. |
124 | * |
125 | * # <- start | Check mid and move start or end if it doesn't match |
126 | * # | |
127 | * # | A lower bound match is wanted we can set end=mid-1 or |
128 | * # <- mid | start=mid because we know that mid doesn't match. |
129 | * # | |
130 | * # | Eventually either end=start or end=start+1 and one of |
131 | * # <- end | start or end is the wanted one. |
132 | */ |
133 | |
134 | /* Search for offset */ |
135 | |
136 | start=0; |
137 | end=errorlogs->file.lonbins*errorlogs->file.latbins; |
138 | |
139 | do |
140 | { |
141 | mid=(start+end)/2; /* Choose mid point */ |
142 | |
143 | offset=LookupErrorLogOffset(errorlogs,mid); |
144 | |
145 | if(offset<index) /* Mid point is too low for an exact match but could be lower bound */ |
146 | start=mid; |
147 | else if(offset>index) /* Mid point is too high */ |
148 | end=mid?(mid-1):mid; |
149 | else /* Mid point is correct */ |
150 | {bin=mid;break;} |
151 | } |
152 | while((end-start)>1); |
153 | |
154 | if(bin==-1) |
155 | { |
156 | offset=LookupErrorLogOffset(errorlogs,end); |
157 | |
158 | if(offset>index) |
159 | bin=start; |
160 | else |
161 | bin=end; |
162 | } |
163 | |
164 | while(bin<=(errorlogs->file.lonbins*errorlogs->file.latbins) && |
165 | LookupErrorLogOffset(errorlogs,bin)==LookupErrorLogOffset(errorlogs,bin+1)) |
166 | bin++; |
167 | |
168 | latbin=bin%errorlogs->file.latbins; |
169 | lonbin=bin/errorlogs->file.latbins; |
170 | |
171 | /* Return the values */ |
172 | |
173 | if(errorlogp==NULL) |
174 | errorlogp=LookupErrorLog(errorlogs,index,2); |
175 | |
176 | *latitude =latlong_to_radians(bin_to_latlong(errorlogs->file.latzero+latbin)+off_to_latlong(errorlogp->latoffset)); |
177 | *longitude=latlong_to_radians(bin_to_latlong(errorlogs->file.lonzero+lonbin)+off_to_latlong(errorlogp->lonoffset)); |
178 | } |