Added pinned struct.

This commit is contained in:
Jose Marcial Vieira Bisneto 2016-12-30 02:55:25 -03:00
parent 9d194ad484
commit f7a029ade3
2 changed files with 54 additions and 0 deletions

View file

@ -28,9 +28,16 @@
typedef int PinMode; typedef int PinMode;
struct Pinned {
struct Cid *Key;
PinMode Mode;
struct Cid *Via;
};
int ipfs_pin_init (); int ipfs_pin_init ();
// Return pointer to string or NULL if invalid. // Return pointer to string or NULL if invalid.
char *ipfs_pin_mode_to_string (PinMode mode); char *ipfs_pin_mode_to_string (PinMode mode);
// Return array index or -1 if fail. // Return array index or -1 if fail.
PinMode ipfs_string_to_pin_mode (char *str); PinMode ipfs_string_to_pin_mode (char *str);
int ipfs_pin_is_pinned (struct Pinned *p);
#endif // IPFS_PIN_H #endif // IPFS_PIN_H

View file

@ -63,3 +63,50 @@ PinMode ipfs_string_to_pin_mode (char *str)
} }
return -1; // not found. return -1; // not found.
} }
int ipfs_pin_is_pinned (struct Pinned *p)
{
return (p->Mode != NotPinned);
}
char *ipfs_pin_pinned_msg (struct Pinned *p)
{
char *ret, *ptr, *msg;
switch (p->Mode) {
case NotPinned:
msg = "not pinned";
ret = malloc(strlen (msg) + 1);
if (!ret) {
return NULL;
}
strcpy(ret, msg);
break;
case Indirect:
msg = "pinned via ";
ret = malloc(strlen (msg) + p->Via->hash_length + 1);
if (!ret) {
return NULL;
}
ptr = ret;
memcpy(ptr, msg, strlen(msg));
ptr += strlen(msg);
memcpy(ptr, p->Via->hash, p->Via->hash_length);
ptr += p->Via->hash_length;
*ptr = '\0';
break;
default:
ptr = ipfs_pin_mode_to_string (p->Mode);
if (!ptr) {
return NULL;
}
msg = "pinned: ";
ret = malloc(strlen (msg) + strlen (ptr) + 1);
if (!ret) {
return NULL;
}
strcpy(ret, msg);
strcat(ret, ptr);
}
return ret;
}