Improved path/path.c code readability.

This commit is contained in:
Jose Marcial Vieira Bisneto 2016-11-17 18:14:19 -03:00
parent 8ed4f62526
commit 1f10b99939

View file

@ -6,11 +6,12 @@
// FromCid safely converts a cid.Cid type to a Path type // FromCid safely converts a cid.Cid type to a Path type
char* PathFromCid (struct Cid *c) char* PathFromCid (struct Cid *c)
{ {
const char prefix[] = "/ipfs/";
char *rpath, *cidstr = CidString(c); char *rpath, *cidstr = CidString(c);
rpath = malloc(strlen(cidstr) + 7); rpath = malloc(sizeof(prefix) + strlen(cidstr));
if (!rpath) return NULL; if (!rpath) return NULL;
strcpy(rpath, "/ipfs/"); strcpy(rpath, prefix);
strcat(rpath, cidstr); strcat(rpath, cidstr);
return rpath; return rpath;
} }
@ -31,7 +32,7 @@ char** Segments (char *p)
rbuf = malloc(strlen(p) + 1); rbuf = malloc(strlen(p) + 1);
if (!rbuf) return NULL; if (!rbuf) return NULL;
rsegs = malloc(sizeof(char*) * (slash_count + 2)); // slashs splits plus NULL pointer termination rsegs = calloc(sizeof(char*), slash_count + 2); // slashs splits plus NULL pointer termination
if (!rsegs) { if (!rsegs) {
free(rbuf); free(rbuf);
return NULL; return NULL;
@ -48,8 +49,9 @@ int SegmentsLength (char **s)
{ {
int r = 0; int r = 0;
if (!s) return 0; if (s) {
while (s[r]) r++; while (s[r]) r++;
}
return r; return r;
} }
@ -84,7 +86,7 @@ int PopLastSegment (char *p, char **str)
{ {
if (IsJustAKey(p)) return 0; if (IsJustAKey(p)) return 0;
*str = strrchr(p, '/'); *str = strrchr(p, '/');
if (!*str) return -1; // error if (!*str) return ErrBadPath; // error
**str = '\0'; **str = '\0';
*str++; *str++;
return 0; return 0;
@ -115,7 +117,8 @@ char *PathFromSegments(char *prefix, char **seg)
int ParseCidToPath (char *dst, char *txt) int ParseCidToPath (char *dst, char *txt)
{ {
char *c, *r; struct Cid *c;
char *r;
if (!txt || txt[0] == '\0') return ErrNoComponents; if (!txt || txt[0] == '\0') return ErrNoComponents;
@ -126,7 +129,6 @@ int ParseCidToPath (char *dst, char *txt)
} }
r = PathFromCid(c); r = PathFromCid(c);
free(c);
if (!r) { if (!r) {
return ErrCidDecode; return ErrCidDecode;
@ -140,6 +142,8 @@ int ParsePath (char *dst, char *txt)
{ {
int err, i; int err, i;
char *c; char *c;
const char prefix[] = "/ipfs/";
const int plen = strlen(prefix);
if (!txt || txt[0] == '\0') return ErrNoComponents; if (!txt || txt[0] == '\0') return ErrNoComponents;
@ -147,9 +151,9 @@ int ParsePath (char *dst, char *txt)
if (*txt == '/') { if (*txt == '/') {
txt++; txt++;
} }
err = ParseCidToPath (dst+6, txt); err = ParseCidToPath (dst+plen, txt);
if (err == 0) { if (err == 0) { // only change dst if ParseCidToPath returned success.
memcpy (dst, "/ipfs/", 6); memcpy (dst, prefix, plen); // use memcpy to don't copy null terminator.
return 0; return 0;
} }
return err; return err;
@ -159,7 +163,7 @@ int ParsePath (char *dst, char *txt)
for (i = 0 ; (c = strchr(c, '/')) ; i++) c++; for (i = 0 ; (c = strchr(c, '/')) ; i++) c++;
if (i < 3) return ErrBadPath; if (i < 3) return ErrBadPath;
if (strcmp (txt, "/ipfs/") == 0) { if (strcmp (txt, prefix) == 0) {
char buf[strlen(txt+5)]; char buf[strlen(txt+5)];
strcpy (buf, txt+6); // copy to temp buffer. strcpy (buf, txt+6); // copy to temp buffer.
c = strchr(buf, '/'); c = strchr(buf, '/');