ucsdet.h 15 KB

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