File: iconvstream.h

package info (click to toggle)
html2text 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,144 kB
  • sloc: cpp: 5,891; sh: 5,747; yacc: 891; makefile: 84
file content (102 lines) | stat: -rw-r--r-- 2,405 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Copyright 2020-2025 Fabian Groffen <grobian@gentoo.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License in the file COPYING for more details.
 */

#ifndef ICONVSTREAM_H
#define ICONVSTREAM_H 1

#include <fstream>
#include <string>
#include <unistd.h>
#include <iconv.h>

using std::string;

const char endl = '\n';
const char flush = '\0';

class iconvstream {
	public:
		iconvstream():
			open_err(0),
			fd_is(-1),
			fd_os(-1)
		{
		}
		~iconvstream()
		{
			if (find_tokens_c_str_buf != nullptr)
				delete[] find_tokens_c_str_buf;
		}

		void open_is(const char *url, const char *encoding);
		void open_is(const string &url, const char *encoding);
		void close_is(void);
		void open_os(const char *url, const char *encoding);
		void open_os(const string &url, const char *encoding);
		void close_os(void);
		int is_open() const
		{
			return fd_is >= 0;
		}
		int os_open() const
		{
			return fd_os >= 0;
		}
		void close()
		{
			close_is();
			close_os();
		}
		bool os_isatty() const
		{
			return fd_os >= 0 ? (isatty(fd_os) ? true : false) : false;
		}
		const char *open_error_msg() const;
		int get();
		int write(const char *inp, size_t len);
		iconvstream &operator<<(const char *inp);
		iconvstream &operator<<(const string &inp);
		iconvstream &operator<<(char inp);

	private:
		const char* find_tokens(char*, size_t, const char**);
		char *find_tokens_c_str_buf = nullptr;

		const char *open_err;
		const char *encoding;

		int fd_is;
		iconv_t iconv_handle_is = nullptr;
		unsigned char *readbuf = nullptr;
		size_t readbufsze;
		size_t readbuflen;
		size_t readbufpos;
		unsigned char *rutf8buf = nullptr;
		size_t rutf8bufsze;
		size_t rutf8buflen;
		size_t rutf8bufpos;

		int fd_os;
		iconv_t iconv_handle_os = nullptr;
		unsigned char *writebuf = nullptr;
		size_t writebufsze;
		size_t writebuflen;
		size_t writebufpos;
		unsigned char *wutf8buf = nullptr;
		size_t wutf8bufsze;
		size_t wutf8buflen;
		size_t wutf8bufpos;
};

#endif