You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

462 lines
13 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /** @file stringop.c
  2. ** @brief String operations - Definition
  3. ** @author Andrea Vedaldi
  4. **/
  5. /*
  6. Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
  7. All rights reserved.
  8. This file is part of the VLFeat library and is made available under
  9. the terms of the BSD license (see the COPYING file).
  10. */
  11. /**
  12. @file stringop.h
  13. @brief String operations
  14. @author Andrea Vedaldi
  15. @tableofcontents
  16. @ref stringop.h implements basic string operations. All functions that
  17. write to strings use range checking, which makes them safer than some
  18. standard POSIX equivalent (see @ref vl-stringop-err).
  19. @section vl-stringop-enumeration Enumerations
  20. @ref stringop.h defines a simple enumeration data type. This is given
  21. by an array of enumeration members, represented by
  22. instances of the ::VlEnumerator strucutre, each storing a
  23. name-value pair. The enumeration must end by a member whose
  24. name is set to @c NULL.
  25. Use ::vl_enumeration_get and ::vl_enumeration_get_casei
  26. to retrieve an enumeration member by name.
  27. @section vl-stringop-file-protocols File protocols
  28. @ref stringop.h defines a few file "protocols" and helps parsing them
  29. from URL-like formatted strings. The supported protocols are:
  30. <table>
  31. <caption>File protocols</caption>
  32. <tr><td>Protocol</td><td>Code</td><td>URL prefix</td></tr>
  33. <tr><td>ASCII</td><td>::VL_PROT_ASCII</td><td><code>ascii://</code></td></tr>
  34. <tr><td>BINARY</td><td>::VL_PROT_BINARY</td><td><code>binary://</code></td></tr>
  35. </table>
  36. @section vl-stringop-err Detecting overflow
  37. @ref stringop.h functions that write a string to a character buffer take
  38. both the buffer and its size @c n as input. If @c n is not large
  39. enough, the output may be truncated but it is always a null terminated
  40. string (provided that @c n &gt;= 1). Such functions also return the
  41. length of the string that would have been written @c r (which does not
  42. include the terminating null character) had the buffer been large
  43. enough. Hence an <em>overflow</em> can be detected by testing if @c r
  44. &gt;= @c n, @c r can be used to re-allocate a buffer large enough to
  45. contain the result, and the operation can be repeated.
  46. **/
  47. #include "stringop.h"
  48. #include <string.h>
  49. #include <ctype.h>
  50. /** ------------------------------------------------------------------
  51. ** @brief Extract the protocol prefix from a string
  52. ** @param string string.
  53. ** @param protocol protocol code (output).
  54. ** @return pointer to the first character after the protocol prefix.
  55. **
  56. ** The function extracts the prefix of the string @a string
  57. ** terminated by the first occurrence of the @c :// substring (if
  58. ** any). It then matches the suffix terminated by @c :// to the
  59. ** supported @ref vl-stringop-file-protocols protocols. If @c protocol is not
  60. ** @c NULL, the corresponding protocol code is written to @a protocol
  61. **
  62. ** The function writes to @a protocol the value ::VL_PROT_NONE if no
  63. ** suffix is detected and ::VL_PROT_UNKNOWN if there is a suffix but
  64. ** it cannot be matched to any of the supported protocols.
  65. **/
  66. VL_EXPORT char *
  67. vl_string_parse_protocol (char const *string, int *protocol)
  68. {
  69. char const * cpt ;
  70. int dummy ;
  71. /* handle the case prot = 0 */
  72. if (protocol == 0)
  73. protocol = &dummy ;
  74. /* look for :// */
  75. cpt = strstr(string, "://") ;
  76. if (cpt == 0) {
  77. *protocol = VL_PROT_NONE ;
  78. cpt = string ;
  79. }
  80. else {
  81. if (strncmp(string, "ascii", cpt - string) == 0) {
  82. *protocol = VL_PROT_ASCII ;
  83. }
  84. else if (strncmp(string, "bin", cpt - string) == 0) {
  85. *protocol = VL_PROT_BINARY ;
  86. }
  87. else {
  88. *protocol = VL_PROT_UNKNOWN ;
  89. }
  90. cpt += 3 ;
  91. }
  92. return (char*) cpt ;
  93. }
  94. /** ------------------------------------------------------------------
  95. ** @brief Get protocol name
  96. ** @param protocol protocol code.
  97. ** @return pointer protocol name string.
  98. **
  99. ** The function returns a pointer to a string containing the name of
  100. ** the protocol @a protocol (see the @a vl-file-protocols protocols
  101. ** list). If the protocol is unknown the function returns the empty
  102. ** string.
  103. **/
  104. VL_EXPORT char const *
  105. vl_string_protocol_name (int protocol)
  106. {
  107. switch (protocol) {
  108. case VL_PROT_ASCII:
  109. return "ascii" ;
  110. case VL_PROT_BINARY:
  111. return "bin" ;
  112. case VL_PROT_NONE :
  113. return "" ;
  114. default:
  115. return 0 ;
  116. }
  117. }
  118. /** ------------------------------------------------------------------
  119. ** @brief Extract base of file name
  120. ** @param destination destination buffer.
  121. ** @param destinationSize size of destination buffer.
  122. ** @param source input string.
  123. ** @param maxNumStrippedExtensions maximum number of extensions to strip.
  124. ** @return length of the destination string.
  125. **
  126. ** The function removes the leading path and up to @c
  127. ** maxNumStrippedExtensions trailing extensions from the string @a
  128. ** source and writes the result to the buffer @a destination.
  129. **
  130. ** The leading path is the longest suffix that ends with either the
  131. ** @c \ or @c / characters. An extension is a string starting with
  132. ** the <code>.</code> character not containing it. For instance, the string @c
  133. ** file.png contains the extension <code>.png</code> and the string @c
  134. ** file.tar.gz contains two extensions (<code>.tar</code> and @c <code>.gz</code>).
  135. **
  136. ** @sa @ref vl-stringop-err.
  137. **/
  138. VL_EXPORT vl_size
  139. vl_string_basename (char * destination,
  140. vl_size destinationSize,
  141. char const * source,
  142. vl_size maxNumStrippedExtensions)
  143. {
  144. char c ;
  145. vl_uindex k = 0, beg, end ;
  146. /* find beginning */
  147. beg = 0 ;
  148. for (k = 0 ; (c = source[k]) ; ++ k) {
  149. if (c == '\\' || c == '/') beg = k + 1 ;
  150. }
  151. /* find ending */
  152. end = strlen (source) ;
  153. for (k = end ; k > beg ; --k) {
  154. if (source[k - 1] == '.' && maxNumStrippedExtensions > 0) {
  155. -- maxNumStrippedExtensions ;
  156. end = k - 1 ;
  157. }
  158. }
  159. return vl_string_copy_sub (destination, destinationSize,
  160. source + beg, source + end) ;
  161. }
  162. /** ------------------------------------------------------------------
  163. ** @brief Replace wildcard characters by a string
  164. ** @param destination output buffer.
  165. ** @param destinationSize size of the output buffer.
  166. ** @param source input string.
  167. ** @param wildcardChar wildcard character.
  168. ** @param escapeChar escape character.
  169. ** @param replacement replacement string.
  170. **
  171. ** The function replaces the occurrence of the specified wildcard
  172. ** character @a wildcardChar by the string @a replacement. The result
  173. ** is written to the buffer @a destination of size @a
  174. ** destinationSize.
  175. **
  176. ** Wildcard characters may be escaped by preceding them by the @a esc
  177. ** character. More in general, anything following an occurrence of @a
  178. ** esc character is copied verbatim. To disable the escape characters
  179. ** simply set @a esc to 0.
  180. **
  181. ** @return length of the result.
  182. ** @sa @ref vl-stringop-err.
  183. **/
  184. VL_EXPORT vl_size
  185. vl_string_replace_wildcard (char * destination,
  186. vl_size destinationSize,
  187. char const * source,
  188. char wildcardChar,
  189. char escapeChar,
  190. char const * replacement)
  191. {
  192. char c ;
  193. vl_uindex k = 0 ;
  194. vl_bool escape = 0 ;
  195. while ((c = *source++)) {
  196. /* enter escape mode ? */
  197. if (! escape && c == escapeChar) {
  198. escape = 1 ;
  199. continue ;
  200. }
  201. /* wildcard or regular? */
  202. if (! escape && c == wildcardChar) {
  203. char const * repl = replacement ;
  204. while ((c = *repl++)) {
  205. if (destination && k + 1 < destinationSize) {
  206. destination[k] = c ;
  207. }
  208. ++ k ;
  209. }
  210. }
  211. /* regular character */
  212. else {
  213. if (destination && k + 1 < destinationSize) {
  214. destination[k] = c ;
  215. }
  216. ++ k ;
  217. }
  218. escape = 0 ;
  219. }
  220. /* add trailing 0 */
  221. if (destinationSize > 0) {
  222. destination[VL_MIN(k, destinationSize - 1)] = 0 ;
  223. }
  224. return k ;
  225. }
  226. /** ------------------------------------------------------------------
  227. ** @brief Copy string
  228. ** @param destination output buffer.
  229. ** @param destinationSize size of the output buffer.
  230. ** @param source string to copy.
  231. ** @return length of the source string.
  232. **
  233. ** The function copies the string @a source to the buffer @a
  234. ** destination of size @a destinationSize.
  235. **
  236. ** @sa @ref vl-stringop-err.
  237. **/
  238. VL_EXPORT vl_size
  239. vl_string_copy (char * destination, vl_size destinationSize,
  240. char const * source)
  241. {
  242. char c ;
  243. vl_uindex k = 0 ;
  244. while ((c = *source++)) {
  245. if (destination && k + 1 < destinationSize) {
  246. destination[k] = c ;
  247. }
  248. ++ k ;
  249. }
  250. /* finalize */
  251. if (destinationSize > 0) {
  252. destination[VL_MIN(k, destinationSize - 1)] = 0 ;
  253. }
  254. return k ;
  255. }
  256. /** ------------------------------------------------------------------
  257. ** @brief Copy substring
  258. ** @param destination output buffer.
  259. ** @param destinationSize size of output buffer.
  260. ** @param beginning start of the substring.
  261. ** @param end end of the substring.
  262. ** @return length of the destination string.
  263. **
  264. ** The function copies the substring from at @a beginning to @a end
  265. ** (not included) to the buffer @a destination of size @a
  266. ** destinationSize. If, however, the null character is found before
  267. ** @a end, the substring terminates there.
  268. **
  269. ** @sa @ref vl-stringop-err.
  270. **/
  271. VL_EXPORT vl_size
  272. vl_string_copy_sub (char * destination,
  273. vl_size destinationSize,
  274. char const * beginning,
  275. char const * end)
  276. {
  277. char c ;
  278. vl_uindex k = 0 ;
  279. while (beginning < end && (c = *beginning++)) {
  280. if (destination && k + 1 < destinationSize) {
  281. destination[k] = c ;
  282. }
  283. ++ k ;
  284. }
  285. /* finalize */
  286. if (destinationSize > 0) {
  287. destination[VL_MIN(k, destinationSize - 1)] = 0 ;
  288. }
  289. return k ;
  290. }
  291. /** ------------------------------------------------------------------
  292. ** @brief Search character in reversed order
  293. ** @param beginning pointer to the substring beginning.
  294. ** @param end pointer to the substring end.
  295. ** @param c character to search for.
  296. ** @return pointer to last occurrence of @a c, or 0 if none.
  297. **
  298. ** The function searches for the last occurrence of the character @a c
  299. ** in the substring from @a beg to @a end (the latter not being included).
  300. **/
  301. VL_EXPORT char *
  302. vl_string_find_char_rev (char const *beginning, char const* end, char c)
  303. {
  304. while (end -- != beginning) {
  305. if (*end == c) {
  306. return (char*) end ;
  307. }
  308. }
  309. return 0 ;
  310. }
  311. /** ------------------------------------------------------------------
  312. ** @brief Calculate string length
  313. ** @param string string.
  314. ** @return string length.
  315. **/
  316. VL_EXPORT vl_size
  317. vl_string_length (char const *string)
  318. {
  319. vl_uindex i ;
  320. for (i = 0 ; string[i] ; ++i) ;
  321. return i ;
  322. }
  323. /** ------------------------------------------------------------------
  324. ** @brief Compare strings case-insensitive
  325. ** @param string1 fisrt string.
  326. ** @param string2 second string.
  327. ** @return an integer =,<,> 0 if @c string1 =,<,> @c string2
  328. **/
  329. VL_EXPORT int
  330. vl_string_casei_cmp (const char * string1, const char * string2)
  331. {
  332. while (tolower((char unsigned)*string1) ==
  333. tolower((char unsigned)*string2))
  334. {
  335. if (*string1 == 0) {
  336. return 0 ;
  337. }
  338. string1 ++ ;
  339. string2 ++ ;
  340. }
  341. return
  342. (int)tolower((char unsigned)*string1) -
  343. (int)tolower((char unsigned)*string2) ;
  344. }
  345. /* -------------------------------------------------------------------
  346. * VlEnumeration
  347. * ---------------------------------------------------------------- */
  348. /** @brief Get a member of an enumeration by name
  349. ** @param enumeration array of ::VlEnumerator objects.
  350. ** @param name the name of the desired member.
  351. ** @return enumerator matching @a name.
  352. **
  353. ** If @a name is not found in the enumeration, then the value
  354. ** @c NULL is returned.
  355. **
  356. ** @sa vl-stringop-enumeration
  357. **/
  358. VL_EXPORT VlEnumerator *
  359. vl_enumeration_get (VlEnumerator const *enumeration, char const *name)
  360. {
  361. assert(enumeration) ;
  362. while (enumeration->name) {
  363. if (strcmp(name, enumeration->name) == 0) return (VlEnumerator*)enumeration ;
  364. enumeration ++ ;
  365. }
  366. return NULL ;
  367. }
  368. /** @brief Get a member of an enumeration by name (case insensitive)
  369. ** @param enumeration array of ::VlEnumerator objects.
  370. ** @param name the name of the desired member.
  371. ** @return enumerator matching @a name.
  372. **
  373. ** If @a name is not found in the enumeration, then the value
  374. ** @c NULL is returned. @a string is matched case insensitive.
  375. **
  376. ** @sa vl-stringop-enumeration
  377. **/
  378. VL_EXPORT VlEnumerator *
  379. vl_enumeration_get_casei (VlEnumerator const *enumeration, char const *name)
  380. {
  381. assert(enumeration) ;
  382. while (enumeration->name) {
  383. if (vl_string_casei_cmp(name, enumeration->name) == 0) return (VlEnumerator*)enumeration ;
  384. enumeration ++ ;
  385. }
  386. return NULL ;
  387. }
  388. /** @brief Get a member of an enumeration by value
  389. ** @param enumeration array of ::VlEnumerator objects.
  390. ** @param value value of the desired member.
  391. ** @return enumerator matching @a value.
  392. **
  393. ** If @a value is not found in the enumeration, then the value
  394. ** @c NULL is returned.
  395. **
  396. ** @sa vl-stringop-enumeration
  397. **/
  398. VL_EXPORT VlEnumerator *
  399. vl_enumeration_get_by_value (VlEnumerator const *enumeration, vl_index value)
  400. {
  401. assert(enumeration) ;
  402. while (enumeration->name) {
  403. if (enumeration->value == value) return (VlEnumerator*)enumeration ;
  404. enumeration ++ ;
  405. }
  406. return NULL ;
  407. }