string-util.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #ifndef MUPDF_FITZ_STRING_H
  2. #define MUPDF_FITZ_STRING_H
  3. #include "mupdf/fitz/system.h"
  4. /*
  5. Safe string functions
  6. */
  7. /*
  8. fz_strsep: Given a pointer to a C string (or a pointer to NULL) break
  9. it at the first occurrence of a delimiter char (from a given set).
  10. stringp: Pointer to a C string pointer (or NULL). Updated on exit to
  11. point to the first char of the string after the delimiter that was
  12. found. The string pointed to by stringp will be corrupted by this
  13. call (as the found delimiter will be overwritten by 0).
  14. delim: A C string of acceptable delimiter characters.
  15. Returns a pointer to a C string containing the chars of stringp up
  16. to the first delimiter char (or the end of the string), or NULL.
  17. */
  18. char *fz_strsep(char **stringp, const char *delim);
  19. /*
  20. fz_strlcpy: Copy at most n-1 chars of a string into a destination
  21. buffer with null termination, returning the real length of the
  22. initial string (excluding terminator).
  23. dst: Destination buffer, at least n bytes long.
  24. src: C string (non-NULL).
  25. n: Size of dst buffer in bytes.
  26. Returns the length (excluding terminator) of src.
  27. */
  28. size_t fz_strlcpy(char *dst, const char *src, size_t n);
  29. /*
  30. fz_strlcat: Concatenate 2 strings, with a maximum length.
  31. dst: pointer to first string in a buffer of n bytes.
  32. src: pointer to string to concatenate.
  33. n: Size (in bytes) of buffer that dst is in.
  34. Returns the real length that a concatenated dst + src would have been
  35. (not including terminator).
  36. */
  37. size_t fz_strlcat(char *dst, const char *src, size_t n);
  38. /*
  39. fz_dirname: extract the directory component from a path.
  40. */
  41. void fz_dirname(char *dir, const char *path, size_t dirsize);
  42. /*
  43. fz_urldecode: decode url escapes.
  44. */
  45. char *fz_urldecode(char *url);
  46. /*
  47. fz_format_output_path: create output file name using a template.
  48. If the path contains %[0-9]*d, the first such pattern will be replaced
  49. with the page number. If the template does not contain such a pattern, the page
  50. number will be inserted before the file suffix. If the template does not have
  51. a file suffix, the page number will be added to the end.
  52. */
  53. void fz_format_output_path(fz_context *ctx, char *path, size_t size, const char *fmt, int page);
  54. /*
  55. fz_cleanname: rewrite path to the shortest string that names the same path.
  56. Eliminates multiple and trailing slashes, interprets "." and "..".
  57. Overwrites the string in place.
  58. */
  59. char *fz_cleanname(char *name);
  60. /*
  61. FZ_UTFMAX: Maximum number of bytes in a decoded rune (maximum length returned by fz_chartorune).
  62. */
  63. enum { FZ_UTFMAX = 4 };
  64. /*
  65. fz_chartorune: UTF8 decode a single rune from a sequence of chars.
  66. rune: Pointer to an int to assign the decoded 'rune' to.
  67. str: Pointer to a UTF8 encoded string.
  68. Returns the number of bytes consumed. Does not throw exceptions.
  69. */
  70. int fz_chartorune(int *rune, const char *str);
  71. /*
  72. fz_runetochar: UTF8 encode a rune to a sequence of chars.
  73. str: Pointer to a place to put the UTF8 encoded character.
  74. rune: Pointer to a 'rune'.
  75. Returns the number of bytes the rune took to output. Does not throw
  76. exceptions.
  77. */
  78. int fz_runetochar(char *str, int rune);
  79. /*
  80. fz_runelen: Count how many chars are required to represent a rune.
  81. rune: The rune to encode.
  82. Returns the number of bytes required to represent this run in UTF8.
  83. */
  84. int fz_runelen(int rune);
  85. /*
  86. fz_utflen: Count how many runes the UTF-8 encoded string
  87. consists of.
  88. s: The UTF-8 encoded, NUL-terminated text string.
  89. Returns the number of runes in the string.
  90. */
  91. int fz_utflen(const char *s);
  92. /*
  93. fz_strtod/fz_strtof: Locale-independent decimal to binary
  94. conversion. On overflow return (-)INFINITY and set errno to ERANGE. On
  95. underflow return 0 and set errno to ERANGE. Special inputs (case
  96. insensitive): "NAN", "INF" or "INFINITY".
  97. */
  98. double fz_strtod(const char *s, char **es);
  99. float fz_strtof(const char *s, char **es);
  100. /*
  101. fz_strtof_no_exp: Like fz_strtof, but does not recognize exponent
  102. format. So fz_strtof_no_exp("1.5e20", &tail) will return 1.5 and tail
  103. will point to "e20".
  104. */
  105. float fz_strtof_no_exp(const char *string, char **tailptr);
  106. /*
  107. fz_grisu: Compute decimal integer m, exp such that:
  108. f = m * 10^exp
  109. m is as short as possible without losing exactness
  110. Assumes special cases (0, NaN, +Inf, -Inf) have been handled.
  111. */
  112. int fz_grisu(float f, char *s, int *exp);
  113. /*
  114. Check and parse string into page ranges:
  115. ( ','? ([0-9]+|'N') ( '-' ([0-9]+|N) )? )+
  116. */
  117. int fz_is_page_range(fz_context *ctx, const char *s);
  118. const char *fz_parse_page_range(fz_context *ctx, const char *s, int *a, int *b, int n);
  119. #endif