Branch data Line data Source code
1 : : /** @file brass_version.h
2 : : * @brief BrassVersion class
3 : : */
4 : : /* Copyright (C) 2006,2007,2008,2009 Olly Betts
5 : : *
6 : : * This program is free software; you can redistribute it and/or modify
7 : : * it under the terms of the GNU General Public License as published by
8 : : * the Free Software Foundation; either version 2 of the License, or
9 : : * (at your option) any later version.
10 : : *
11 : : * This program is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : : * GNU General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU General Public License
17 : : * along with this program; if not, write to the Free Software
18 : : * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 : : */
20 : :
21 : : #ifndef OM_HGUARD_BRASS_VERSION_H
22 : : #define OM_HGUARD_BRASS_VERSION_H
23 : :
24 : : #include <cstring>
25 : : #include <string>
26 : :
27 : : #include "common/safeuuid.h"
28 : :
29 : : /** The BrassVersion class manages the "iambrass" file.
30 : : *
31 : : * The "iambrass" file (currently) contains a "magic" string identifying
32 : : * that this is a brass database and a database format version number.
33 : : */
34 : 1187 : class BrassVersion {
35 : : /// The filename of the version file.
36 : : std::string filename;
37 : :
38 : : /** The UUID of this database.
39 : : *
40 : : * This is mutable for older uuid libraries which take non-const uuid_t.
41 : : */
42 : : mutable uuid_t uuid;
43 : :
44 : : public:
45 : 1187 : BrassVersion(const std::string & dbdir) : filename(dbdir) {
46 : 1187 : filename += "/iambrass";
47 : 1187 : }
48 : :
49 : : /** Create the version file. */
50 : : void create();
51 : :
52 : : /** Read the version file and check it's a version we understand.
53 : : *
54 : : * On failure, an exception is thrown.
55 : : */
56 : : void read_and_check();
57 : :
58 : : /// Return pointer to 16 byte UUID.
59 : : const char * get_uuid() const {
60 : : // uuid is unsigned char[].
61 : : return reinterpret_cast<const char *>(uuid);
62 : : }
63 : :
64 : : /// Return UUID in the standard 36 character string format.
65 : 1407 : std::string get_uuid_string() const {
66 : : char buf[37];
67 : 1407 : uuid_unparse_lower(uuid, buf);
68 : 1407 : return std::string(buf, 36);
69 : : }
70 : :
71 : : /// Set the UUID from 16 byte binary value @a data.
72 : : void set_uuid(void * data) {
73 : : std::memcpy(uuid, data, 16);
74 : : }
75 : :
76 : : /** Set the UUID from the standard 36 character string format.
77 : : *
78 : : * @return true if @a s was successfully parsed; false otherwise.
79 : : */
80 : : bool set_uuid_string(const std::string & s) {
81 : : return uuid_parse(s.c_str(), uuid);
82 : : }
83 : : };
84 : :
85 : : #endif
|