X-Git-Url: http://git.vrable.net/?a=blobdiff_plain;f=ref.cc;h=b5c02d605ae4c3ad6f2274c0e98010912b466a37;hb=27cae99a5071a5c9b5a91831ef0b81aa3a3f17f9;hp=ce16cb8914fa649dd40fce3dc0f18a363cee0ac8;hpb=5c10647d667fc4408b73402db9247181a54a57ad;p=cumulus.git diff --git a/ref.cc b/ref.cc index ce16cb8..b5c02d6 100644 --- a/ref.cc +++ b/ref.cc @@ -71,8 +71,74 @@ string ObjectReference::to_string() const /* Parse a string object reference and return a pointer to a new * ObjectReference. The caller is responsible for freeing the object. NULL is * returned if there is an error in the syntax. */ -ObjectReference *ObjectReference::parse(const std::string& s) +ObjectReference *ObjectReference::parse(const std::string& str) { - // TODO: Implement - return NULL; + const char *s = str.c_str(); + const char *t; + + // Segment + t = s; + while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f') || (*t == '-')) + t++; + if (*t != '/') + return NULL; + string segment(s, t - s); + + // Object sequence number + t++; + s = t; + while ((*t >= '0' && *t <= '9') || (*t >= 'a' && *t <= 'f')) + t++; + if (*t != '\0' && *t != '(' && *t != '[') + return NULL; + string object(s, t - s); + + // Checksum + string checksum; + if (*t == '(') { + t++; + s = t; + while (*t != ')' && *t != '\0') + t++; + if (*t != ')') + return NULL; + checksum = string(s, t - s); + t++; + } + + // Range + bool have_range = false; + int64_t range1, range2; + if (*t == '[') { + t++; + s = t; + while (*t >= '0' && *t <= '9') + t++; + if (*t != '+') + return NULL; + + string val(s, t - s); + range1 = atoll(val.c_str()); + + t++; + s = t; + while (*t >= '0' && *t <= '9') + t++; + if (*t != ']') + return NULL; + + val = string(s, t - s); + range2 = atoll(val.c_str()); + + have_range = true; + } + + ObjectReference *ref = new ObjectReference(segment, object); + if (checksum.size() > 0) + ref->set_checksum(checksum); + + if (have_range) + ref->set_range(range1, range2); + + return ref; }