A few minor adjustments to the ObjectReference interface.
[cumulus.git] / ref.cc
1 /* LBS: An LFS-inspired filesystem backup system
2  * Copyright (C) 2007  Michael Vrable
3  *
4  * Backups are structured as a collection of objects, which may refer to other
5  * objects.  Object references are used to name other objects or parts of them.
6  * This file defines the class for representing object references and the
7  * textual representation of these references. */
8
9 #include <assert.h>
10 #include <stdio.h>
11 #include <uuid/uuid.h>
12
13 #include <string>
14
15 #include "ref.h"
16
17 using std::string;
18
19 /* Generate a new UUID, and return the text representation of it.  This is
20  * suitable for generating the name for a new segment. */
21 string generate_uuid()
22 {
23     uint8_t uuid[16];
24     char buf[40];
25
26     uuid_generate(uuid);
27     uuid_unparse_lower(uuid, buf);
28     return string(buf);
29 }
30
31
32 ObjectReference::ObjectReference(const std::string& segment, int sequence)
33     : segment(segment)
34 {
35     char seq_buf[64];
36     sprintf(seq_buf, "%08x", sequence);
37     object = seq_buf;
38
39     clear_checksum();
40     clear_range();
41 }
42
43 ObjectReference::ObjectReference(const std::string& segment,
44                                  const std::string& sequence)
45     : segment(segment), object(sequence)
46 {
47     clear_checksum();
48     clear_range();
49 }
50
51 string ObjectReference::to_string() const
52 {
53     string result = segment + "/" + object;
54
55     if (checksum_valid)
56         result += "(" + checksum + ")";
57
58     if (range_valid) {
59         char buf[64];
60         sprintf(buf, "[%zu+%zu]", range_start, range_length);
61         result += buf;
62     }
63
64     return result;
65 }
66
67 /* Parse a string object reference and return a pointer to a new
68  * ObjectReference.  The caller is responsible for freeing the object.  NULL is
69  * returned if there is an error in the syntax. */
70 ObjectReference *ObjectReference::parse(const std::string& s)
71 {
72     // TODO: Implement
73     return NULL;
74 }