~ubuntu-branches/ubuntu/hardy/gnome-commander/hardy

« back to all changes in this revision

Viewing changes to tests/gviewer/datapresentation.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2006-06-13 15:39:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060613153948-gvrt3mb2ddk5u62o
Tags: 1.2.0-3
added --disable-scrollkeeper on build

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  GNOME Commander - A GNOME based file manager
 
3
  Copyright (C) 2001-2006 Marcus Bjurman
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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
 
18
 
 
19
   Author: Assaf Gordon  <agordon88@gmail.com>
 
20
*/
 
21
 
 
22
#include <glib.h>
 
23
#include <stdio.h>
 
24
#include <stdlib.h>
 
25
#include <unistd.h>
 
26
#include <err.h>
 
27
 
 
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>
 
33
 
 
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;
 
46
 
 
47
void usage()
 
48
{
 
49
    fprintf(stderr,"This program tests the data-presentation module in 'libgviewer'.\n\n" ) ;
 
50
 
 
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" ) ;
 
53
    
 
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");
 
64
    exit(0);
 
65
}
 
66
 
 
67
void parse_command_line(int argc, char* argv[])
 
68
{
 
69
    extern char* optarg;
 
70
    extern int optind, opterr, optopt ;
 
71
    int c;
 
72
    
 
73
    start_line = 0 ;
 
74
    end_line = -1 ;
 
75
    tab_size = 8 ;
 
76
    wrap_limit = 80 ;
 
77
    fixed_limit = 40 ;
 
78
    hexdump = FALSE ;
 
79
    presentation = PRSNT_NO_WRAP ;
 
80
    encoding = g_strdup("ASCII");
 
81
    
 
82
    while ( (c=getopt(argc,argv,"xe:p:f:t:w:")) != -1 ) {
 
83
        switch(c)
 
84
        {
 
85
        case 'x':
 
86
            hexdump = TRUE ;
 
87
            break;
 
88
        case 'e':
 
89
            g_free(encoding);
 
90
            encoding = g_strdup(optarg);
 
91
            break ;
 
92
        
 
93
        case 'p':
 
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 ;
 
100
            else {
 
101
                warnx("Invalid presentation mode \"%s\".\n", optarg) ;
 
102
                usage();
 
103
            }                
 
104
            break;
 
105
        
 
106
        case 'w':
 
107
            wrap_limit = atoi(optarg);
 
108
            if (wrap_limit<=0) {
 
109
                warnx("Invalid wrap limit \"%s\".\n", optarg);
 
110
                usage();
 
111
            }
 
112
            break ;
 
113
        
 
114
        case 't':
 
115
            tab_size = atoi(optarg);
 
116
            if (tab_size <=0) {
 
117
                warnx("Invalid tab size \"%s\".\n", optarg);
 
118
                usage();
 
119
            }
 
120
            break;
 
121
        
 
122
        case 'f':
 
123
            fixed_limit = atoi(optarg) ;
 
124
            if (fixed_limit<=0) {
 
125
                warnx("Invalid fixed limit \"%s\".\n", optarg);
 
126
                usage();
 
127
            }
 
128
            break ;
 
129
        }
 
130
    }
 
131
    
 
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");
 
134
        exit(0);
 
135
    }
 
136
    
 
137
    if (hexdump) {
 
138
        presentation = PRSNT_BIN_FIXED ;
 
139
        fixed_limit = 16 ;
 
140
    }
 
141
    
 
142
    if ( optind == argc ) {
 
143
        warnx("Need file name to work with...\n");
 
144
        usage();
 
145
    }
 
146
    filename = g_strdup(argv[optind++]);
 
147
    
 
148
    if ( optind < argc ) {
 
149
        start_line = atol(argv[optind++]) ;
 
150
    }
 
151
    if ( optind < argc ) {
 
152
        end_line = atol(argv[optind++]) ;
 
153
    }
 
154
 
 
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) ;
 
157
}
 
158
 
 
159
int init()
 
160
{
 
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) ;
 
165
        return -1;
 
166
    }
 
167
 
 
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);
 
172
    
 
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);
 
180
    
 
181
    return 0;
 
182
}
 
183
 
 
184
void cleanup()
 
185
{
 
186
    g_free(filename);
 
187
    g_free(encoding);
 
188
    
 
189
    if (dp)
 
190
        gv_free_data_presentation(dp);
 
191
    g_free(dp);
 
192
    
 
193
    if (imd)
 
194
        gv_free_input_modes(imd);
 
195
    g_free(imd);
 
196
 
 
197
    if (fops)
 
198
        gv_file_free(fops);
 
199
    g_free(fops) ;
 
200
}
 
201
 
 
202
void display_utf8_char(char_type value)
 
203
{
 
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)) ;
 
208
        
 
209
        if (GV_THIRD_BYTE(value)) {
 
210
            printf("%c", GV_THIRD_BYTE(value)) ;
 
211
            
 
212
            if (GV_FOURTH_BYTE(value)) {
 
213
                printf("%c", GV_FOURTH_BYTE(value)) ;
 
214
            }
 
215
        }
 
216
    }
 
217
}
 
218
 
 
219
int display_line(offset_type start_of_line, offset_type end_of_line, gboolean display_control_chars)
 
220
{
 
221
    offset_type current ;
 
222
    char_type value ;
 
223
    
 
224
#if 0
 
225
    printf("line: %d -> %d\n", (int)start_of_line, (int)end_of_line) ;
 
226
#else
 
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) {
 
233
            printf("\n");
 
234
            return -1;
 
235
        }
 
236
            
 
237
        /* move to the next character's offset */
 
238
        current = gv_input_get_next_char_offset(imd,current);
 
239
 
 
240
        if (value=='\r' || value=='\n') {
 
241
            if (display_control_chars)
 
242
                value = gv_input_mode_byte_to_utf8(imd,(unsigned char)value);
 
243
            else
 
244
                continue ;
 
245
        }
 
246
        
 
247
        if (value=='\t') {
 
248
            if (display_control_chars)
 
249
                value = gv_input_mode_byte_to_utf8(imd,(unsigned char)value);
 
250
            else {
 
251
                int i;
 
252
                for(i=0;i<tab_size;i++)
 
253
                    display_utf8_char(' ');
 
254
                continue ;
 
255
            }
 
256
        }
 
257
        
 
258
        display_utf8_char(value);        
 
259
    }
 
260
    printf("\n");
 
261
#endif
 
262
    return 0;
 
263
}
 
264
 
 
265
int display_hexdump_line(offset_type start_of_line, offset_type end_of_line)
 
266
{
 
267
    int byte_value ;
 
268
    char_type value ;
 
269
    offset_type current;
 
270
    
 
271
    printf("%08lx ", (unsigned long)start_of_line ) ;
 
272
    
 
273
    current = start_of_line ;
 
274
    while (current < end_of_line) {
 
275
        byte_value = gv_input_mode_get_raw_byte(imd, current) ;
 
276
        if (byte_value==-1)
 
277
            break ;
 
278
        current++ ;
 
279
        printf("%02x ", (unsigned char)byte_value) ;
 
280
    }
 
281
    
 
282
    current = start_of_line ;
 
283
    while (current < end_of_line) {
 
284
        byte_value = gv_input_mode_get_raw_byte(imd, current) ;
 
285
        if (byte_value==-1)
 
286
            break ;
 
287
        value = gv_input_mode_byte_to_utf8(imd, (unsigned char)byte_value) ;
 
288
        display_utf8_char(value);
 
289
        current++ ;
 
290
    }
 
291
 
 
292
    printf("\n");
 
293
    
 
294
    return 0 ;
 
295
}
 
296
 
 
297
int main(int argc, char* argv[])
 
298
{
 
299
    offset_type current;
 
300
    int lines ;
 
301
    int rc;
 
302
    
 
303
    parse_command_line(argc,argv);
 
304
    
 
305
    if (init()==-1)
 
306
        goto error;
 
307
    
 
308
    
 
309
    lines = 0 ;
 
310
    current = gv_scroll_lines(dp, 0, start_line);
 
311
    while (1) {
 
312
        offset_type eol_offset ;
 
313
        
 
314
        eol_offset = gv_get_end_of_line_offset(dp, current);
 
315
        if (eol_offset == current)
 
316
            break ;
 
317
    
 
318
        if (hexdump)
 
319
            rc =display_hexdump_line(current, eol_offset);
 
320
        else
 
321
            rc = display_line(current, eol_offset, (presentation==PRSNT_BIN_FIXED));
 
322
        
 
323
        if (rc==-1)
 
324
            break ;
 
325
        
 
326
        current = eol_offset ;
 
327
        
 
328
        lines++ ;
 
329
        if (start_line + lines > end_line)
 
330
            break;
 
331
    }
 
332
        
 
333
error:    
 
334
    cleanup();
 
335
    return 0;
 
336
}