textwolf  0.2
charset_interface.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Patrick P. Frey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
11 #ifndef __TEXTWOLF_CHARSET_INTERFACE_HPP__
12 #define __TEXTWOLF_CHARSET_INTERFACE_HPP__
13 #include <cstddef>
15 
16 namespace textwolf {
19 namespace charset {
20 
23 struct Encoder
24 {
29  static bool encode( UChar chr, char* bufptr, std::size_t bufsize)
30  {
31  static const char* HEX = "0123456789abcdef";
32  StaticBuffer buf( bufptr, bufsize);
33  char bb[ 32];
34  unsigned int ii=0;
35  while (chr > 0)
36  {
37  bb[ii++] = HEX[ chr & 0xf];
38  chr /= 16;
39  }
40  buf.push_back( '&');
41  buf.push_back( '#');
42  buf.push_back( 'x');
43  while (ii)
44  {
45  buf.push_back( bb[ --ii]);
46  }
47  buf.push_back( ';');
48  buf.push_back( '\0');
49  return !buf.overflow();
50  }
51 };
52 
55 struct Interface
56 {
58  enum {MaxChar=0xFF};
59 
66  template <class Iterator>
67  static void skip( char* buf, unsigned int& bufpos, Iterator& itr);
68 
76  template <class Iterator>
77  static signed char asciichar( char* buf, unsigned int& bufpos, Iterator& itr);
78 
85  template <class Iterator>
86  static void fetchbytes( char* buf, unsigned int& bufpos, Iterator& itr);
87 
95  template <class Iterator>
96  UChar value( char* buf, unsigned int& bufpos, Iterator& itr) const;
97 
102  template <class Buffer_>
103  void print( UChar chr, Buffer_& buf) const;
104 
107  static bool is_equal( const Interface&, const Interface&)
108  {
109  return true;
110  }
111 };
112 
115 struct ByteOrder
116 {
117  enum
118  {
119  LE=0, //< little endian
120  BE=1 //< big endian
121  };
122 };
123 
124 }//namespace
125 }//namespace
126 #endif
127 
Fixed size buffer fulfilling the requirement of a back insertion sequence needed for textwolf output...
void print(UChar chr, Buffer_ &buf) const
Prints a unicode character to a buffer.
static bool encode(UChar chr, char *bufptr, std::size_t bufsize)
Write the character 'chr' in encoded form as nul-terminated string to a buffer.
Definition: charset_interface.hpp:29
Definition: charset_interface.hpp:119
Simple back insertion sequence for storing the outputs of textwolf in a contant size buffer...
Definition: staticbuffer.hpp:24
Definition: charset_interface.hpp:120
Collection of functions for encode/decode XML character entities.
Definition: charset_interface.hpp:23
uint32_t UChar
Unicode character type.
Definition: char.hpp:37
static void fetchbytes(char *buf, unsigned int &bufpos, Iterator &itr)
Fetches the bytes of the current character into a buffer.
This interface has to be implemented for a character set encoding.
Definition: charset_interface.hpp:55
static signed char asciichar(char *buf, unsigned int &bufpos, Iterator &itr)
Fetches the ascii char representation of the current character.
bool overflow() const
check for array bounds write
Definition: staticbuffer.hpp:141
Definition: charset_interface.hpp:58
UChar value(char *buf, unsigned int &bufpos, Iterator &itr) const
Fetches the unicode character representation of the current character.
Order of bytes for wide char character sets.
Definition: charset_interface.hpp:115
void push_back(char ch)
Append one character.
Definition: staticbuffer.hpp:71
static void skip(char *buf, unsigned int &bufpos, Iterator &itr)
Skip to start of the next character.
static bool is_equal(const Interface &, const Interface &)
Evaluate if two character set encodings of the same type are equal in all properties (code page...
Definition: charset_interface.hpp:107