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, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 **************************************************************************/
19
25#include <wx/datetime.h>
26#include <wx/utils.h>
27
28#include "model/gpx_document.h"
29
30// RFC4122 version 4 compliant random UUIDs generator.
31wxString GpxDocument::GetUUID(void) {
32 wxString str;
33 struct {
34 int time_low;
35 int time_mid;
36 int time_hi_and_version;
37 int clock_seq_hi_and_rsv;
38 int clock_seq_low;
39 int node_hi;
40 int node_low;
41 } uuid;
42
43 uuid.time_low = GetRandomNumber(
44 0, 2147483647); // FIXME: the max should be set to something like
45 // MAXINT32, but it doesn't compile un gcc...
46 uuid.time_mid = GetRandomNumber(0, 65535);
47 uuid.time_hi_and_version = GetRandomNumber(0, 65535);
48 uuid.clock_seq_hi_and_rsv = GetRandomNumber(0, 255);
49 uuid.clock_seq_low = GetRandomNumber(0, 255);
50 uuid.node_hi = GetRandomNumber(0, 65535);
51 uuid.node_low = GetRandomNumber(0, 2147483647);
52
53 /* Set the two most significant bits (bits 6 and 7) of the
54 * clock_seq_hi_and_rsv to zero and one, respectively. */
55 uuid.clock_seq_hi_and_rsv = (uuid.clock_seq_hi_and_rsv & 0x3F) | 0x80;
56
57 /* Set the four most significant bits (bits 12 through 15) of the
58 * time_hi_and_version field to 4 */
59 uuid.time_hi_and_version = (uuid.time_hi_and_version & 0x0fff) | 0x4000;
60
61 str.Printf(_T("%08x-%04x-%04x-%02x%02x-%04x%08x"), uuid.time_low,
62 uuid.time_mid, uuid.time_hi_and_version, uuid.clock_seq_hi_and_rsv,
63 uuid.clock_seq_low, uuid.node_hi, uuid.node_low);
64
65 return str;
66}
67
69 /* Fill with random. Miliseconds hopefully good enough for our usage, reading
70 * /dev/random would be much better on linux and system guid function on
71 * Windows as well */
72 wxDateTime x = wxDateTime::UNow();
73 long seed = x.GetMillisecond();
74 seed *= x.GetTicks();
75 srand(seed);
76}
77
78int GpxDocument::GetRandomNumber(int range_min, int range_max) {
79 long u = (long)wxRound(
80 ((double)rand() / ((double)(RAND_MAX) + 1) * (range_max - range_min)) +
81 range_min);
82 return (int)u;
83}
static wxString GetUUID(void)
Return a unique RFC4122 version 4 compliant GUID string.
static void SeedRandom()
Seed the random generator used by GetUUID().
Class GpxDocument.