rid_hex.3 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. .TH Rid 3 "September 2017" "Version 1.0" "librid_hex API"
  2. .SH NAME
  3. rid_hex \- Rid hexadecimal string library.
  4. .SH SYNOPSIS
  5. .B #include <rid.h>
  6. .br
  7. .B #include <rid_hex.h>
  8. .sp
  9. Link with \fI\-lrid_hex\fP \fI\-lrid\fP and \fI\-lbsd\fP or `pkg-config --libs rid_hex`
  10. .SH DESCRIPTION
  11. A library to convert Rids to hexadecimal strings.
  12. .sp
  13. .SS Defines
  14. .B RID_HEX_LEN
  15. the length of a Rid as a hexadecimal string, it does not include the '\e0'. This would be the same value as calling
  16. .B strlen ()
  17. on a hexadecimal string representing a Rid.
  18. .sp
  19. .SS Types
  20. .B Rid_hex
  21. is a char array big enough for the hexadecimal string representation of a Rid provided by this library. It can hold 32 hexadecimal digits, each two representing one byte, plus a tailing '\e0'. It is defined as:
  22. .sp
  23. typedef char Rid_hex[33];
  24. .sp
  25. .B Rid_hex_path
  26. is similar to
  27. .BR Rid_hex ","
  28. except a '/' is inserted after the first two digits. This is done to provide a relative path for using Rids as file names since some file systems have issues with having many files in a single directory. It is defined as:
  29. .sp
  30. typedef char Rid_hex_path[34];
  31. .SS Functions
  32. .BI "void rid_hex(char *" str ", const Rid " id ");"
  33. .br
  34. .BI "int rid_hex_parse(Rid " id ", const char *" str ");"
  35. .br
  36. .BI "void rid_hex_path(char *" path ", const Rid " id ");"
  37. .sp
  38. .B rid_hex ()
  39. .RI "converts " id " to a hex string stored in " str "."
  40. .sp
  41. .B rid_hex_parse ()
  42. .RI "converts " str " to binary stored in " id "."
  43. .sp
  44. .B rid_hex_path ()
  45. .RI "converts " id " to a path suitable for a file name stored in " path "."
  46. .SH "Return Value"
  47. The function
  48. .B rid_hex_parse ()
  49. returns the number of characters parsed without error. On success this value is
  50. .B RID_HEX_LEN
  51. and on failure is less than
  52. .BR RID_HEX_LEN "."
  53. .SH EXAMPLES
  54. .SS Creating a Rid path in a subdirectory
  55. This can be done by passing a pointer to the last 33 bytes of a string to
  56. .B Rid_hex_path
  57. .RI "for the " path " argument. See the example program below:"
  58. .sp
  59. .nf
  60. #include <rid.h>
  61. #include <stdio.h>
  62. #include <string.h>
  63. #include <rid_hex.h>
  64. /* define the directory we want to generate our Rid paths in */
  65. #define DAT_DIR "dat/"
  66. /* define the number of characters in DAT_DIR minus the '\e0' */
  67. #define DAT_DIR_LEN (sizeof(DAT_DIR) - 1)
  68. /* A type for a string big enough for our path */
  69. typedef char Dir_path[DAT_DIR_LEN + sizeof(Rid_hex_path)];
  70. int
  71. main(int argc, char *argv[])
  72. {
  73. Rid id;
  74. Dir_path dir_path;
  75. /* create a random rid */
  76. rid_set(id, NULL);
  77. /* set the dat/ part of the path */
  78. strcpy(dir_path, DAT_DIR);
  79. /* generate the rest of the path */
  80. rid_hex_path(dir_path + DAT_DIR_LEN, id);
  81. /* Now we can do anything we want with dir_path, it's just a string
  82. * that looks something like "dat/41/7ed28b7db28473685f0c1dbf9f453f"
  83. */
  84. printf("%s\en", dir_path);
  85. return 0;
  86. }
  87. .fi
  88. .SS Converting, Parsing, and Comparing Rids
  89. .nf
  90. #include <rid.h>
  91. #include <stdio.h>
  92. #include <string.h>
  93. #include <rid_hex.h>
  94. int
  95. main(int argc, char *argv[])
  96. {
  97. Rid id1;
  98. Rid id2;
  99. Rid id3;
  100. Rid_hex str1;
  101. Rid_hex str2;
  102. /* set both id1 to a random value */
  103. rid_set(id1, NULL);
  104. /* copy id1 into id2 */
  105. rid_set(id2, id1);
  106. /* convert Rids to strings */
  107. rid_hex(str1, id1);
  108. rid_hex(str2, id2);
  109. /* and they will produce equal strings if they have the same value */
  110. if (!strcmp(str1, str2))
  111. printf("that only makes sense.\en");
  112. /* we can also get a Rid from a string */
  113. if (rid_hex_parse(id3, str1) < RID_HEX_LEN)
  114. printf("not printed since there is no error parsing.\en");
  115. if (!rid_cmp(id3, id1))
  116. printf("this will also be printed!\en");
  117. /* and that's all there is to it! */
  118. return 0;
  119. }
  120. .fi
  121. .SH AUTHOR
  122. Uladox (probably won't hex you)
  123. .SH "AVAILABILITY AND SOURCE"
  124. .B rid_hex
  125. can be found in its repository at https://www.notabug.org/Uladox/rid_hex
  126. .SH "SEE ALSO"
  127. .BR rid (3)
  128. .BR rid_fn85 (3)