ucsdet.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. **********************************************************************
  5. * Copyright (C) 2005-2013, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. * file name: ucsdet.h
  9. * encoding: UTF-8
  10. * indentation:4
  11. *
  12. * created on: 2005Aug04
  13. * created by: Andy Heninger
  14. *
  15. * ICU Character Set Detection, API for C
  16. *
  17. * Draft version 18 Oct 2005
  18. *
  19. */
  20. #ifndef __UCSDET_H
  21. #define __UCSDET_H
  22. #include "unicode/utypes.h"
  23. #if !UCONFIG_NO_CONVERSION
  24. #include "unicode/uenum.h"
  25. #if U_SHOW_CPLUSPLUS_API
  26. #include "unicode/localpointer.h"
  27. #endif // U_SHOW_CPLUSPLUS_API
  28. /**
  29. * \file
  30. * \brief C API: Charset Detection API
  31. *
  32. * This API provides a facility for detecting the
  33. * charset or encoding of character data in an unknown text format.
  34. * The input data can be from an array of bytes.
  35. * <p>
  36. * Character set detection is at best an imprecise operation. The detection
  37. * process will attempt to identify the charset that best matches the characteristics
  38. * of the byte data, but the process is partly statistical in nature, and
  39. * the results can not be guaranteed to always be correct.
  40. * <p>
  41. * For best accuracy in charset detection, the input data should be primarily
  42. * in a single language, and a minimum of a few hundred bytes worth of plain text
  43. * in the language are needed. The detection process will attempt to
  44. * ignore html or xml style markup that could otherwise obscure the content.
  45. * <p>
  46. * An alternative to the ICU Charset Detector is the
  47. * Compact Encoding Detector, https://github.com/google/compact_enc_det.
  48. * It often gives more accurate results, especially with short input samples.
  49. */
  50. struct UCharsetDetector;
  51. /**
  52. * Structure representing a charset detector
  53. * @stable ICU 3.6
  54. */
  55. typedef struct UCharsetDetector UCharsetDetector;
  56. struct UCharsetMatch;
  57. /**
  58. * Opaque structure representing a match that was identified
  59. * from a charset detection operation.
  60. * @stable ICU 3.6
  61. */
  62. typedef struct UCharsetMatch UCharsetMatch;
  63. /**
  64. * Open a charset detector.
  65. *
  66. * @param status Any error conditions occurring during the open
  67. * operation are reported back in this variable.
  68. * @return the newly opened charset detector.
  69. * @stable ICU 3.6
  70. */
  71. U_CAPI UCharsetDetector * U_EXPORT2
  72. ucsdet_open(UErrorCode *status);
  73. /**
  74. * Close a charset detector. All storage and any other resources
  75. * owned by this charset detector will be released. Failure to
  76. * close a charset detector when finished with it can result in
  77. * memory leaks in the application.
  78. *
  79. * @param ucsd The charset detector to be closed.
  80. * @stable ICU 3.6
  81. */
  82. U_CAPI void U_EXPORT2
  83. ucsdet_close(UCharsetDetector *ucsd);
  84. #if U_SHOW_CPLUSPLUS_API
  85. U_NAMESPACE_BEGIN
  86. /**
  87. * \class LocalUCharsetDetectorPointer
  88. * "Smart pointer" class, closes a UCharsetDetector via ucsdet_close().
  89. * For most methods see the LocalPointerBase base class.
  90. *
  91. * @see LocalPointerBase
  92. * @see LocalPointer
  93. * @stable ICU 4.4
  94. */
  95. U_DEFINE_LOCAL_OPEN_POINTER(LocalUCharsetDetectorPointer, UCharsetDetector, ucsdet_close);
  96. U_NAMESPACE_END
  97. #endif
  98. /**
  99. * Set the input byte data whose charset is to detected.
  100. *
  101. * Ownership of the input text byte array remains with the caller.
  102. * The input string must not be altered or deleted until the charset
  103. * detector is either closed or reset to refer to different input text.
  104. *
  105. * @param ucsd the charset detector to be used.
  106. * @param textIn the input text of unknown encoding. .
  107. * @param len the length of the input text, or -1 if the text
  108. * is NUL terminated.
  109. * @param status any error conditions are reported back in this variable.
  110. *
  111. * @stable ICU 3.6
  112. */
  113. U_CAPI void U_EXPORT2
  114. ucsdet_setText(UCharsetDetector *ucsd, const char *textIn, int32_t len, UErrorCode *status);
  115. /** Set the declared encoding for charset detection.
  116. * The declared encoding of an input text is an encoding obtained
  117. * by the user from an http header or xml declaration or similar source that
  118. * can be provided as an additional hint to the charset detector.
  119. *
  120. * How and whether the declared encoding will be used during the
  121. * detection process is TBD.
  122. *
  123. * @param ucsd the charset detector to be used.
  124. * @param encoding an encoding for the current data obtained from
  125. * a header or declaration or other source outside
  126. * of the byte data itself.
  127. * @param length the length of the encoding name, or -1 if the name string
  128. * is NUL terminated.
  129. * @param status any error conditions are reported back in this variable.
  130. *
  131. * @stable ICU 3.6
  132. */
  133. U_CAPI void U_EXPORT2
  134. ucsdet_setDeclaredEncoding(UCharsetDetector *ucsd, const char *encoding, int32_t length, UErrorCode *status);
  135. /**
  136. * Return the charset that best matches the supplied input data.
  137. *
  138. * Note though, that because the detection
  139. * only looks at the start of the input data,
  140. * there is a possibility that the returned charset will fail to handle
  141. * the full set of input data.
  142. * <p>
  143. * The returned UCharsetMatch object is owned by the UCharsetDetector.
  144. * It will remain valid until the detector input is reset, or until
  145. * the detector is closed.
  146. * <p>
  147. * The function will fail if
  148. * <ul>
  149. * <li>no charset appears to match the data.</li>
  150. * <li>no input text has been provided</li>
  151. * </ul>
  152. *
  153. * @param ucsd the charset detector to be used.
  154. * @param status any error conditions are reported back in this variable.
  155. * @return a UCharsetMatch representing the best matching charset,
  156. * or NULL if no charset matches the byte data.
  157. *
  158. * @stable ICU 3.6
  159. */
  160. U_CAPI const UCharsetMatch * U_EXPORT2
  161. ucsdet_detect(UCharsetDetector *ucsd, UErrorCode *status);
  162. /**
  163. * Find all charset matches that appear to be consistent with the input,
  164. * returning an array of results. The results are ordered with the
  165. * best quality match first.
  166. *
  167. * Because the detection only looks at a limited amount of the
  168. * input byte data, some of the returned charsets may fail to handle
  169. * the all of input data.
  170. * <p>
  171. * The returned UCharsetMatch objects are owned by the UCharsetDetector.
  172. * They will remain valid until the detector is closed or modified
  173. *
  174. * <p>
  175. * Return an error if
  176. * <ul>
  177. * <li>no charsets appear to match the input data.</li>
  178. * <li>no input text has been provided</li>
  179. * </ul>
  180. *
  181. * @param ucsd the charset detector to be used.
  182. * @param matchesFound pointer to a variable that will be set to the
  183. * number of charsets identified that are consistent with
  184. * the input data. Output only.
  185. * @param status any error conditions are reported back in this variable.
  186. * @return A pointer to an array of pointers to UCharSetMatch objects.
  187. * This array, and the UCharSetMatch instances to which it refers,
  188. * are owned by the UCharsetDetector, and will remain valid until
  189. * the detector is closed or modified.
  190. * @stable ICU 3.6
  191. */
  192. U_CAPI const UCharsetMatch ** U_EXPORT2
  193. ucsdet_detectAll(UCharsetDetector *ucsd, int32_t *matchesFound, UErrorCode *status);
  194. /**
  195. * Get the name of the charset represented by a UCharsetMatch.
  196. *
  197. * The storage for the returned name string is owned by the
  198. * UCharsetMatch, and will remain valid while the UCharsetMatch
  199. * is valid.
  200. *
  201. * The name returned is suitable for use with the ICU conversion APIs.
  202. *
  203. * @param ucsm The charset match object.
  204. * @param status Any error conditions are reported back in this variable.
  205. * @return The name of the matching charset.
  206. *
  207. * @stable ICU 3.6
  208. */
  209. U_CAPI const char * U_EXPORT2
  210. ucsdet_getName(const UCharsetMatch *ucsm, UErrorCode *status);
  211. /**
  212. * Get a confidence number for the quality of the match of the byte
  213. * data with the charset. Confidence numbers range from zero to 100,
  214. * with 100 representing complete confidence and zero representing
  215. * no confidence.
  216. *
  217. * The confidence values are somewhat arbitrary. They define an
  218. * an ordering within the results for any single detection operation
  219. * but are not generally comparable between the results for different input.
  220. *
  221. * A confidence value of ten does have a general meaning - it is used
  222. * for charsets that can represent the input data, but for which there
  223. * is no other indication that suggests that the charset is the correct one.
  224. * Pure 7 bit ASCII data, for example, is compatible with a
  225. * great many charsets, most of which will appear as possible matches
  226. * with a confidence of 10.
  227. *
  228. * @param ucsm The charset match object.
  229. * @param status Any error conditions are reported back in this variable.
  230. * @return A confidence number for the charset match.
  231. *
  232. * @stable ICU 3.6
  233. */
  234. U_CAPI int32_t U_EXPORT2
  235. ucsdet_getConfidence(const UCharsetMatch *ucsm, UErrorCode *status);
  236. /**
  237. * Get the RFC 3066 code for the language of the input data.
  238. *
  239. * The Charset Detection service is intended primarily for detecting
  240. * charsets, not language. For some, but not all, charsets, a language is
  241. * identified as a byproduct of the detection process, and that is what
  242. * is returned by this function.
  243. *
  244. * CAUTION:
  245. * 1. Language information is not available for input data encoded in
  246. * all charsets. In particular, no language is identified
  247. * for UTF-8 input data.
  248. *
  249. * 2. Closely related languages may sometimes be confused.
  250. *
  251. * If more accurate language detection is required, a linguistic
  252. * analysis package should be used.
  253. *
  254. * The storage for the returned name string is owned by the
  255. * UCharsetMatch, and will remain valid while the UCharsetMatch
  256. * is valid.
  257. *
  258. * @param ucsm The charset match object.
  259. * @param status Any error conditions are reported back in this variable.
  260. * @return The RFC 3066 code for the language of the input data, or
  261. * an empty string if the language could not be determined.
  262. *
  263. * @stable ICU 3.6
  264. */
  265. U_CAPI const char * U_EXPORT2
  266. ucsdet_getLanguage(const UCharsetMatch *ucsm, UErrorCode *status);
  267. /**
  268. * Get the entire input text as a UChar string, placing it into
  269. * a caller-supplied buffer. A terminating
  270. * NUL character will be appended to the buffer if space is available.
  271. *
  272. * The number of UChars in the output string, not including the terminating
  273. * NUL, is returned.
  274. *
  275. * If the supplied buffer is smaller than required to hold the output,
  276. * the contents of the buffer are undefined. The full output string length
  277. * (in UChars) is returned as always, and can be used to allocate a buffer
  278. * of the correct size.
  279. *
  280. *
  281. * @param ucsm The charset match object.
  282. * @param buf A UChar buffer to be filled with the converted text data.
  283. * @param cap The capacity of the buffer in UChars.
  284. * @param status Any error conditions are reported back in this variable.
  285. * @return The number of UChars in the output string.
  286. *
  287. * @stable ICU 3.6
  288. */
  289. U_CAPI int32_t U_EXPORT2
  290. ucsdet_getUChars(const UCharsetMatch *ucsm,
  291. UChar *buf, int32_t cap, UErrorCode *status);
  292. /**
  293. * Get an iterator over the set of all detectable charsets -
  294. * over the charsets that are known to the charset detection
  295. * service.
  296. *
  297. * The returned UEnumeration provides access to the names of
  298. * the charsets.
  299. *
  300. * <p>
  301. * The state of the Charset detector that is passed in does not
  302. * affect the result of this function, but requiring a valid, open
  303. * charset detector as a parameter insures that the charset detection
  304. * service has been safely initialized and that the required detection
  305. * data is available.
  306. *
  307. * <p>
  308. * <b>Note:</b> Multiple different charset encodings in a same family may use
  309. * a single shared name in this implementation. For example, this method returns
  310. * an array including "ISO-8859-1" (ISO Latin 1), but not including "windows-1252"
  311. * (Windows Latin 1). However, actual detection result could be "windows-1252"
  312. * when the input data matches Latin 1 code points with any points only available
  313. * in "windows-1252".
  314. *
  315. * @param ucsd a Charset detector.
  316. * @param status Any error conditions are reported back in this variable.
  317. * @return an iterator providing access to the detectable charset names.
  318. * @stable ICU 3.6
  319. */
  320. U_CAPI UEnumeration * U_EXPORT2
  321. ucsdet_getAllDetectableCharsets(const UCharsetDetector *ucsd, UErrorCode *status);
  322. /**
  323. * Test whether input filtering is enabled for this charset detector.
  324. * Input filtering removes text that appears to be HTML or xml
  325. * markup from the input before applying the code page detection
  326. * heuristics.
  327. *
  328. * @param ucsd The charset detector to check.
  329. * @return true if filtering is enabled.
  330. * @stable ICU 3.6
  331. */
  332. U_CAPI UBool U_EXPORT2
  333. ucsdet_isInputFilterEnabled(const UCharsetDetector *ucsd);
  334. /**
  335. * Enable filtering of input text. If filtering is enabled,
  336. * text within angle brackets ("<" and ">") will be removed
  337. * before detection, which will remove most HTML or xml markup.
  338. *
  339. * @param ucsd the charset detector to be modified.
  340. * @param filter <code>true</code> to enable input text filtering.
  341. * @return The previous setting.
  342. *
  343. * @stable ICU 3.6
  344. */
  345. U_CAPI UBool U_EXPORT2
  346. ucsdet_enableInputFilter(UCharsetDetector *ucsd, UBool filter);
  347. #ifndef U_HIDE_INTERNAL_API
  348. /**
  349. * Get an iterator over the set of detectable charsets -
  350. * over the charsets that are enabled by the specified charset detector.
  351. *
  352. * The returned UEnumeration provides access to the names of
  353. * the charsets.
  354. *
  355. * @param ucsd a Charset detector.
  356. * @param status Any error conditions are reported back in this variable.
  357. * @return an iterator providing access to the detectable charset names by
  358. * the specified charset detector.
  359. * @internal
  360. */
  361. U_CAPI UEnumeration * U_EXPORT2
  362. ucsdet_getDetectableCharsets(const UCharsetDetector *ucsd, UErrorCode *status);
  363. /**
  364. * Enable or disable individual charset encoding.
  365. * A name of charset encoding must be included in the names returned by
  366. * {@link #ucsdet_getAllDetectableCharsets()}.
  367. *
  368. * @param ucsd a Charset detector.
  369. * @param encoding encoding the name of charset encoding.
  370. * @param enabled <code>true</code> to enable, or <code>false</code> to disable the
  371. * charset encoding.
  372. * @param status receives the return status. When the name of charset encoding
  373. * is not supported, U_ILLEGAL_ARGUMENT_ERROR is set.
  374. * @internal
  375. */
  376. U_CAPI void U_EXPORT2
  377. ucsdet_setDetectableCharset(UCharsetDetector *ucsd, const char *encoding, UBool enabled, UErrorCode *status);
  378. #endif /* U_HIDE_INTERNAL_API */
  379. #endif
  380. #endif /* __UCSDET_H */