PostGIS  2.2.7dev-r@@SVN_REVISION@@
static char* lwdouble_to_dms ( double  val,
const char *  pos_dir_symbol,
const char *  neg_dir_symbol,
const char *  format 
)
static

Definition at line 56 of file lwprint.c.

References lwalloc(), and lwerror().

Referenced by lwdoubles_to_latlon().

57 {
58  /* 3 numbers, 1 sign or compass dir, and 5 possible strings (degree signs, spaces, misc text, etc) between or around them.*/
59  static int NUM_PIECES = 9;
60  static int WORK_SIZE = 1024;
61  char pieces[NUM_PIECES][WORK_SIZE];
62  int current_piece = 0;
63  int is_negative = 0;
64 
65  double degrees = 0.0;
66  double minutes = 0.0;
67  double seconds = 0.0;
68 
69  int compass_dir_piece = -1;
70 
71  int reading_deg = 0;
72  int deg_digits = 0;
73  int deg_has_decpoint = 0;
74  int deg_dec_digits = 0;
75  int deg_piece = -1;
76 
77  int reading_min = 0;
78  int min_digits = 0;
79  int min_has_decpoint = 0;
80  int min_dec_digits = 0;
81  int min_piece = -1;
82 
83  int reading_sec = 0;
84  int sec_digits = 0;
85  int sec_has_decpoint = 0;
86  int sec_dec_digits = 0;
87  int sec_piece = -1;
88 
89  int format_length = ((NULL == format) ? 0 : strlen(format));
90 
91  char * result;
92 
93  int index, following_byte_index;
94  int multibyte_char_width = 1;
95 
96  /* Initialize the working strs to blank. We may not populate all of them, and
97  * this allows us to concat them all at the end without worrying about how many
98  * we actually needed. */
99  for (index = 0; index < NUM_PIECES; index++)
100  {
101  pieces[index][0] = '\0';
102  }
103 
104  /* If no format is provided, use a default. */
105  if (0 == format_length)
106  {
107  /* C2B0 is UTF-8 for the degree symbol. */
108  format = "D\xC2\xB0""M'S.SSS\"C";
109  format_length = strlen(format);
110  }
111  else if (format_length > WORK_SIZE)
112  {
113  /* Sanity check, we don't want to overwrite an entire piece of work and no one should need a 1K-sized
114  * format string anyway. */
115  lwerror("Bad format, exceeds maximum length (%d).", WORK_SIZE);
116  }
117 
118  for (index = 0; index < format_length; index++)
119  {
120  char next_char = format[index];
121  switch (next_char)
122  {
123  case 'D':
124  if (reading_deg)
125  {
126  /* If we're reading degrees, add another digit. */
127  deg_has_decpoint ? deg_dec_digits++ : deg_digits++;
128  }
129  else
130  {
131  /* If we're not reading degrees, we are now. */
132  current_piece++;
133  deg_piece = current_piece;
134  if (deg_digits > 0)
135  {
136  lwerror("Bad format, cannot include degrees (DD.DDD) more than once.");
137  }
138  reading_deg = 1;
139  reading_min = 0;
140  reading_sec = 0;
141  deg_digits++;
142  }
143  break;
144  case 'M':
145  if (reading_min)
146  {
147  /* If we're reading minutes, add another digit. */
148  min_has_decpoint ? min_dec_digits++ : min_digits++;
149  }
150  else
151  {
152  /* If we're not reading minutes, we are now. */
153  current_piece++;
154  min_piece = current_piece;
155  if (min_digits > 0)
156  {
157  lwerror("Bad format, cannot include minutes (MM.MMM) more than once.");
158  }
159  reading_deg = 0;
160  reading_min = 1;
161  reading_sec = 0;
162  min_digits++;
163  }
164  break;
165  case 'S':
166  if (reading_sec)
167  {
168  /* If we're reading seconds, add another digit. */
169  sec_has_decpoint ? sec_dec_digits++ : sec_digits++;
170  }
171  else
172  {
173  /* If we're not reading seconds, we are now. */
174  current_piece++;
175  sec_piece = current_piece;
176  if (sec_digits > 0)
177  {
178  lwerror("Bad format, cannot include seconds (SS.SSS) more than once.");
179  }
180  reading_deg = 0;
181  reading_min = 0;
182  reading_sec = 1;
183  sec_digits++;
184  }
185  break;
186  case 'C':
187  /* We're done reading anything else we might have been reading. */
188  if (reading_deg || reading_min || reading_sec)
189  {
190  /* We were reading something, that means this is the next piece. */
191  reading_deg = 0;
192  reading_min = 0;
193  reading_sec = 0;
194  }
195  current_piece++;
196 
197  if (compass_dir_piece >= 0)
198  {
199  lwerror("Bad format, cannot include compass dir (C) more than once.");
200  }
201  /* The compass dir is a piece all by itself. */
202  compass_dir_piece = current_piece;
203  current_piece++;
204  break;
205  case '.':
206  /* If we're reading deg, min, or sec, we want a decimal point for it. */
207  if (reading_deg)
208  {
209  deg_has_decpoint = 1;
210  }
211  else if (reading_min)
212  {
213  min_has_decpoint = 1;
214  }
215  else if (reading_sec)
216  {
217  sec_has_decpoint = 1;
218  }
219  else
220  {
221  /* Not reading anything, just pass through the '.' */
222  strncat(pieces[current_piece], &next_char, 1);
223  }
224  break;
225  default:
226  /* Any other char is just passed through unchanged. But it does mean we are done reading D, M, or S.*/
227  if (reading_deg || reading_min || reading_sec)
228  {
229  /* We were reading something, that means this is the next piece. */
230  current_piece++;
231  reading_deg = 0;
232  reading_min = 0;
233  reading_sec = 0;
234  }
235 
236  /* Check if this is a multi-byte UTF-8 character. If so go ahead and read the rest of the bytes as well. */
237  multibyte_char_width = 1;
238  if (next_char & 0x80)
239  {
240  if ((next_char & 0xF8) == 0xF0)
241  {
242  multibyte_char_width += 3;
243  }
244  else if ((next_char & 0xF0) == 0xE0)
245  {
246  multibyte_char_width += 2;
247  }
248  else if ((next_char & 0xE0) == 0xC0)
249  {
250  multibyte_char_width += 1;
251  }
252  else
253  {
254  lwerror("Bad format, invalid high-order byte found first, format string may not be UTF-8.");
255  }
256  }
257  if (multibyte_char_width > 1)
258  {
259  if (index + multibyte_char_width >= format_length)
260  {
261  lwerror("Bad format, UTF-8 character first byte found with insufficient following bytes, format string may not be UTF-8.");
262  }
263  for (following_byte_index = (index + 1); following_byte_index < (index + multibyte_char_width); following_byte_index++)
264  {
265  if ((format[following_byte_index] & 0xC0) != 0x80)
266  {
267  lwerror("Bad format, invalid byte found following leading byte of multibyte character, format string may not be UTF-8.");
268  }
269  }
270  }
271  /* Copy all the character's bytes into the current piece. */
272  strncat(pieces[current_piece], &(format[index]), multibyte_char_width);
273  /* Now increment index past the rest of those bytes. */
274  index += multibyte_char_width - 1;
275  break;
276  }
277  if (current_piece >= NUM_PIECES)
278  {
279  lwerror("Internal error, somehow needed more pieces than it should.");
280  }
281  }
282  if (deg_piece < 0)
283  {
284  lwerror("Bad format, degrees (DD.DDD) must be included.");
285  }
286 
287  /* Divvy the number up into D, DM, or DMS */
288  if (val < 0)
289  {
290  val *= -1;
291  is_negative = 1;
292  }
293  degrees = val;
294  if (min_digits > 0)
295  {
296  degrees = (long)degrees;
297  minutes = (val - degrees) * 60;
298  }
299  if (sec_digits > 0)
300  {
301  if (0 == min_digits)
302  {
303  lwerror("Bad format, cannot include seconds (SS.SSS) without including minutes (MM.MMM).");
304  }
305  minutes = (long)minutes;
306  seconds = (val - (degrees + (minutes / 60))) * 3600;
307  }
308 
309  /* Handle the compass direction. If not using compass dir, display degrees as a positive/negative number. */
310  if (compass_dir_piece >= 0)
311  {
312  strcpy(pieces[compass_dir_piece], is_negative ? neg_dir_symbol : pos_dir_symbol);
313  }
314  else if (is_negative)
315  {
316  degrees *= -1;
317  }
318 
319  /* Format the degrees into their string piece. */
320  if (deg_digits + deg_dec_digits + 2 > WORK_SIZE)
321  {
322  lwerror("Bad format, degrees (DD.DDD) number of digits was greater than our working limit.");
323  }
324  if(deg_piece >= 0)
325  {
326  sprintf(pieces[deg_piece], "%*.*f", deg_digits, deg_dec_digits, degrees);
327  }
328 
329  if (min_piece >= 0)
330  {
331  /* Format the minutes into their string piece. */
332  if (min_digits + min_dec_digits + 2 > WORK_SIZE)
333  {
334  lwerror("Bad format, minutes (MM.MMM) number of digits was greater than our working limit.");
335  }
336  sprintf(pieces[min_piece], "%*.*f", min_digits, min_dec_digits, minutes);
337  }
338  if (sec_piece >= 0)
339  {
340  /* Format the seconds into their string piece. */
341  if (sec_digits + sec_dec_digits + 2 > WORK_SIZE)
342  {
343  lwerror("Bad format, seconds (SS.SSS) number of digits was greater than our working limit.");
344  }
345  sprintf(pieces[sec_piece], "%*.*f", sec_digits, sec_dec_digits, seconds);
346  }
347 
348  /* Allocate space for the result. Leave plenty of room for excess digits, negative sign, etc.*/
349  result = (char*)lwalloc(format_length + WORK_SIZE);
350  /* Append all the pieces together. There may be less than 9, but in that case the rest will be blank. */
351  strcpy(result, pieces[0]);
352  for (index = 1; index < NUM_PIECES; index++)
353  {
354  strcat(result, pieces[index]);
355  }
356 
357  return result;
358 }
void * lwalloc(size_t size)
Definition: lwutil.c:199
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:74

Here is the call graph for this function:

Here is the caller graph for this function: