2
GNOME Commander - A GNOME based file manager
3
Copyright (C) 2001-2006 Marcus Bjurman
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 2 of the License, or
8
(at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
Author: Assaf Gordon <agordon88@gmail.com>
28
#include <libgviewer/libgviewer.h>
29
#include <libgviewer/gvtypes.h>
30
#include <libgviewer/fileops.h>
31
#include <libgviewer/inputmodes.h>
32
#include <libgviewer/datapresentation.h>
34
static offset_type start_line ;
35
static offset_type end_line ;
36
static gchar* filename = NULL;
37
static gchar* encoding = NULL;
38
static PRESENTATION presentation ;
39
static ViewerFileOps *fops = NULL ;
40
static GVInputModesData *imd = NULL ;
41
static GVDataPresentation *dp = NULL ;
42
static guint tab_size ;
43
static guint wrap_limit ;
44
static guint fixed_limit ;
45
static gboolean hexdump;
49
fprintf(stderr,"This program tests the data-presentation module in 'libgviewer'.\n\n" ) ;
51
fprintf(stderr,"Usage: test-datapresentation [-e encoding] [-p presentation] [-w wrap limit]\n");
52
fprintf(stderr,"\t\t[-f fixed limit] [-t tab size] [-x] filename [start_line] [end_line]\n\n" ) ;
54
fprintf(stderr,"\t-e enconding: ASCII, UTF8, CP437, CP1251, etc\n");
55
fprintf(stderr,"\t-p presentation: NOWRAP, WRAP, FIXED.\n");
56
fprintf(stderr,"\t-w wrap limit: max characters per line (in WRAP mode).\n");
57
fprintf(stderr,"\t-f fixed limit: fixed number of characters per line (in FIXED mode).\n");
58
fprintf(stderr,"\t-t tab size: number of space per TAB character.\n");
59
fprintf(stderr,"\t-x hex dump: display a hex dump\n.");
60
fprintf(stderr,"\t\t(hex dump forces FIXED presentation mode.)\n\n");
61
fprintf(stderr,"\tfilename: The file to read. UTF8 output will be sent to STDOUT.\n");
62
fprintf(stderr,"\tstart_line: first line to display (default=0).\n");
63
fprintf(stderr,"\tend_line: last line to display (default=end-of-file).\n");
67
void parse_command_line(int argc, char* argv[])
70
extern int optind, opterr, optopt ;
79
presentation = PRSNT_NO_WRAP ;
80
encoding = g_strdup("ASCII");
82
while ( (c=getopt(argc,argv,"xe:p:f:t:w:")) != -1 ) {
90
encoding = g_strdup(optarg);
94
if (g_ascii_strcasecmp(optarg,"WRAP")==0)
95
presentation = PRSNT_WRAP ;
96
else if (g_ascii_strcasecmp(optarg,"NOWRAP")==0)
97
presentation = PRSNT_NO_WRAP ;
98
else if (g_ascii_strcasecmp(optarg,"FIXED")==0)
99
presentation = PRSNT_BIN_FIXED ;
101
warnx("Invalid presentation mode \"%s\".\n", optarg) ;
107
wrap_limit = atoi(optarg);
109
warnx("Invalid wrap limit \"%s\".\n", optarg);
115
tab_size = atoi(optarg);
117
warnx("Invalid tab size \"%s\".\n", optarg);
123
fixed_limit = atoi(optarg) ;
124
if (fixed_limit<=0) {
125
warnx("Invalid fixed limit \"%s\".\n", optarg);
132
if (hexdump && (g_ascii_strcasecmp(encoding, "UTF8")==0)) {
133
warnx("Can't use HexDump mode with UTF8 encoding. (Hexdump requires each character to be one byte exactly)\n");
138
presentation = PRSNT_BIN_FIXED ;
142
if ( optind == argc ) {
143
warnx("Need file name to work with...\n");
146
filename = g_strdup(argv[optind++]);
148
if ( optind < argc ) {
149
start_line = atol(argv[optind++]) ;
151
if ( optind < argc ) {
152
end_line = atol(argv[optind++]) ;
155
fprintf(stderr,"filename(%s) encoding(%s) presentation(%d) wrap(%d) fixed(%d) tabsize(%d) start(%d) end(%d)\n",
156
filename, encoding, (int)presentation, wrap_limit, fixed_limit, tab_size, (int)start_line, (int)end_line) ;
161
/* Setup the fileops */
162
fops = gv_fileops_new();
163
if (gv_file_open(fops, filename)==-1) {
164
fprintf(stderr,"Failed to open \"%s\"\n", filename) ;
168
/* Setup the input mode translations */
169
imd = gv_input_modes_new();
170
gv_init_input_modes(imd, (get_byte_proc)gv_file_get_byte, fops);
171
gv_set_input_mode(imd,encoding);
173
/* Setup the data presentation mode */
174
dp = gv_data_presentation_new();
175
gv_init_data_presentation(dp, imd, gv_file_get_max_offset(fops));
176
gv_set_data_presentation_mode(dp, presentation);
177
gv_set_wrap_limit(dp, wrap_limit);
178
gv_set_fixed_count(dp, fixed_limit);
179
gv_set_tab_size(dp, tab_size);
190
gv_free_data_presentation(dp);
194
gv_free_input_modes(imd);
202
void display_utf8_char(char_type value)
204
/* value is UTF-8 character, packed into 32bits */
205
printf("%c", GV_FIRST_BYTE(value)) ;
206
if (GV_SECOND_BYTE(value)) {
207
printf("%c", GV_SECOND_BYTE(value)) ;
209
if (GV_THIRD_BYTE(value)) {
210
printf("%c", GV_THIRD_BYTE(value)) ;
212
if (GV_FOURTH_BYTE(value)) {
213
printf("%c", GV_FOURTH_BYTE(value)) ;
219
int display_line(offset_type start_of_line, offset_type end_of_line, gboolean display_control_chars)
221
offset_type current ;
225
printf("line: %d -> %d\n", (int)start_of_line, (int)end_of_line) ;
227
current = start_of_line ;
228
while (current < end_of_line) {
229
/* Read a UTF8 character from the input file.
230
The "inputmode" module is responsible for converting the file into UTF8 */
231
value = gv_input_mode_get_utf8_char(imd, current);
232
if (value==INVALID_CHAR) {
237
/* move to the next character's offset */
238
current = gv_input_get_next_char_offset(imd,current);
240
if (value=='\r' || value=='\n') {
241
if (display_control_chars)
242
value = gv_input_mode_byte_to_utf8(imd,(unsigned char)value);
248
if (display_control_chars)
249
value = gv_input_mode_byte_to_utf8(imd,(unsigned char)value);
252
for(i=0;i<tab_size;i++)
253
display_utf8_char(' ');
258
display_utf8_char(value);
265
int display_hexdump_line(offset_type start_of_line, offset_type end_of_line)
271
printf("%08lx ", (unsigned long)start_of_line ) ;
273
current = start_of_line ;
274
while (current < end_of_line) {
275
byte_value = gv_input_mode_get_raw_byte(imd, current) ;
279
printf("%02x ", (unsigned char)byte_value) ;
282
current = start_of_line ;
283
while (current < end_of_line) {
284
byte_value = gv_input_mode_get_raw_byte(imd, current) ;
287
value = gv_input_mode_byte_to_utf8(imd, (unsigned char)byte_value) ;
288
display_utf8_char(value);
297
int main(int argc, char* argv[])
303
parse_command_line(argc,argv);
310
current = gv_scroll_lines(dp, 0, start_line);
312
offset_type eol_offset ;
314
eol_offset = gv_get_end_of_line_offset(dp, current);
315
if (eol_offset == current)
319
rc =display_hexdump_line(current, eol_offset);
321
rc = display_line(current, eol_offset, (presentation==PRSNT_BIN_FIXED));
326
current = eol_offset ;
329
if (start_line + lines > end_line)