converters.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (C) 2003 Mooffie <mooffie@typo.co.il>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  16. #ifndef BDE_CONVERTERS_H
  17. #define BDE_CONVERTERS_H
  18. #include <config.h>
  19. #include <errno.h>
  20. #ifndef EILSEQ
  21. # define EILSEQ 2000
  22. #endif
  23. #ifdef USE_ICONV
  24. # include <iconv.h>
  25. #endif
  26. #include "types.h"
  27. // A Converter object converts between encodings.
  28. //
  29. // Converter (an abstract class)
  30. // |
  31. // +-- IconvConverter (used when the system has ICONV)
  32. // |
  33. // +-- ISO88598Converter (used when no ICONV presents)
  34. // +-- Latin1Converter ditto
  35. // +-- UTF8Converter ditto
  36. const char *guess_encoding(const char *buf, int len);
  37. class Converter {
  38. protected:
  39. bool ilseq_repr;
  40. public:
  41. // The Converter interface is based on iconv()'s interface.
  42. // if a conversion function fails, it returns -1 and updates errno.
  43. virtual int convert(unichar **dest, char **src, int len) = 0;
  44. virtual int convert(char **dest, unichar **src, int len) = 0;
  45. Converter() {
  46. ilseq_repr = false;
  47. }
  48. virtual ~Converter() { }
  49. void enable_ilseq_repr(bool val = true) {
  50. ilseq_repr = val;
  51. }
  52. };
  53. #ifdef USE_ICONV
  54. class IconvConverter : public Converter {
  55. iconv_t cd;
  56. public:
  57. IconvConverter();
  58. virtual ~IconvConverter();
  59. int set_source_encoding(const char *encoding);
  60. int set_target_encoding(const char *encoding);
  61. virtual int convert(unichar **dest, char **src, int len);
  62. virtual int convert(char **dest, unichar **src, int len);
  63. };
  64. #endif
  65. class ISO88598Converter : public Converter {
  66. public:
  67. virtual int convert(unichar **dest, char **src, int len);
  68. virtual int convert(char **dest, unichar **src, int len);
  69. };
  70. class Latin1Converter : public Converter {
  71. public:
  72. virtual int convert(unichar **dest, char **src, int len);
  73. virtual int convert(char **dest, unichar **src, int len);
  74. };
  75. class UTF8Converter : public Converter {
  76. public:
  77. virtual int convert(unichar **dest, char **src, int len);
  78. virtual int convert(char **dest, unichar **src, int len);
  79. };
  80. class ConverterFactory {
  81. static Converter *get_internal_converter(const char *encoding);
  82. public:
  83. static Converter *get_converter_from(const char *encoding);
  84. static Converter *get_converter_to(const char *encoding);
  85. };
  86. #endif