OpenCPN Partial API docs
Loading...
Searching...
No Matches
gpx_document.cpp
Go to the documentation of this file.
1/**************************************************************************
2 * Copyright (C) 2010 by David S. Register *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
24#include <wx/datetime.h>
25#include <wx/utils.h>
26
27#include "model/gpx_document.h"
28
29// RFC4122 version 4 compliant random UUIDs generator.
31 wxString str;
32 struct {
33 int time_low;
34 int time_mid;
35 int time_hi_and_version;
36 int clock_seq_hi_and_rsv;
37 int clock_seq_low;
38 int node_hi;
39 int node_low;
40 } uuid;
41
42 uuid.time_low = GetRandomNumber(
43 0, 2147483647); // FIXME: the max should be set to something like
44 // MAXINT32, but it doesn't compile un gcc...
45 uuid.time_mid = GetRandomNumber(0, 65535);
46 uuid.time_hi_and_version = GetRandomNumber(0, 65535);
47 uuid.clock_seq_hi_and_rsv = GetRandomNumber(0, 255);
48 uuid.clock_seq_low = GetRandomNumber(0, 255);
49 uuid.node_hi = GetRandomNumber(0, 65535);
50 uuid.node_low = GetRandomNumber(0, 2147483647);
51
52 /* Set the two most significant bits (bits 6 and 7) of the
53 * clock_seq_hi_and_rsv to zero and one, respectively. */
54 uuid.clock_seq_hi_and_rsv = (uuid.clock_seq_hi_and_rsv & 0x3F) | 0x80;
55
56 /* Set the four most significant bits (bits 12 through 15) of the
57 * time_hi_and_version field to 4 */
58 uuid.time_hi_and_version = (uuid.time_hi_and_version & 0x0fff) | 0x4000;
59
60 str.Printf("%08x-%04x-%04x-%02x%02x-%04x%08x", uuid.time_low, uuid.time_mid,
61 uuid.time_hi_and_version, uuid.clock_seq_hi_and_rsv,
62 uuid.clock_seq_low, uuid.node_hi, uuid.node_low);
63
64 return str;
65}
66
68 /* Fill with random. Miliseconds hopefully good enough for our usage, reading
69 * /dev/random would be much better on linux and system guid function on
70 * Windows as well */
71 wxDateTime x = wxDateTime::UNow();
72 long seed = x.GetMillisecond();
73 seed *= x.GetTicks();
74 srand(seed);
75}
76
77int GpxDocument::GetRandomNumber(int range_min, int range_max) {
78 long u = (long)wxRound(
79 ((double)rand() / ((double)(RAND_MAX) + 1) * (range_max - range_min)) +
80 range_min);
81 return (int)u;
82}
static wxString GetUUID(void)
Return a unique RFC4122 version 4 compliant GUID string.
static void SeedRandom()
Seed the random generator used by GetUUID().
GPX files UUID support.