ucsdet.h 14 KB

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