diff -rN -u catdoc-0.93.4-orig/doc/xls2csv.1.in catdoc-0.93.4/doc/xls2csv.1.in --- catdoc-0.93.4-orig/doc/xls2csv.1.in 2004-09-15 16:34:45 +0300 +++ catdoc-0.93.4/doc/xls2csv.1.in 2004-11-04 17:50:17 +0200 @@ -68,6 +68,10 @@ - specifies date/time format to use for output of all Excel date and time values. If this option is not specified, format, specified in the spreadsheet is used. On POSIX system any format, allowed by +.TP 8 +.BI -p page range +- specifies range of pages to be extracted from Excel file. +usage examples are - (all); 5- ; -6 ; 1,3-8,9 and so on .BR strftime (3) can be used as value of this option. Under MS-DOS .B xls2csv diff -rN -u catdoc-0.93.4-orig/src/intrange.c catdoc-0.93.4/src/intrange.c --- catdoc-0.93.4-orig/src/intrange.c 1970-01-01 03:00:00 +0300 +++ catdoc-0.93.4/src/intrange.c 2004-11-04 18:15:21 +0200 @@ -0,0 +1,204 @@ +/* -*- C -*- + * File: intrange.h + * + * Created: Thu Nov 4 11:20:36 2004 + * + * $Id: newfile-template.el,v 1.1 2004/10/16 18:51:22 igor Exp $ + */ + +#include +#include +#include +#include +#include "intrange.h" + + +#define MAX_NUMBER_LENGTH 6 + + +/* + * count_intrange + * precounts number of intervals + * to allocate memory at once + * + * + */ +int count_intrange (char* argstring) +{ + int count=0; + int subcount=0; + int char_len=0; + char* orig_string=argstring; + while (*argstring) { + if (isdigit(*argstring)) { + char_len++; + if (1==char_len) { + if (subcount==0) count++; + } + if (char_len>MAX_NUMBER_LENGTH) { + fprintf(stderr,"intrange:too long number in %s:\n",orig_string); + return -1; + } + } else { + if (isspace(*argstring)) { + } else if (',' == *argstring) { + subcount=0; + /* next interval */ + } else if ('-' == *argstring) { + if (0==char_len && subcount==0) count++; + subcount++; + if (subcount>1) { + fprintf(stderr,"intrange:too many - in %s:\n",orig_string); + return -1; + } + } else { + fprintf(stderr,"intrange:strange charcater %c in %s:\n", *argstring, orig_string); + return -1; + } + char_len=0; + } + argstring++; + } + if (0==count) { + fprintf(stderr,"intrange:empty string:\n"); + } + return count; +} + +int flash_buffer(char* buffer) +{ + int bufvalue=atoi(buffer); + /* fprintf(stdout,"buffer=%d\n", bufvalue); */ + /* cleaning buffer for new char */ + int i; + for(i=0; i<=MAX_NUMBER_LENGTH+1;buffer[i++]=0); + return bufvalue; +} + +void finish_interval (int* char_len, int* subcount, char* buffer, struct intinterval* pair) { + /* next interval */ + int num=flash_buffer(buffer); + if (0==*subcount) { + (*pair).min=num; + (*pair).max=num; + } else if (*char_len>0) { + /* subcount ==1 */ + (*pair).max=num; + } else { + (*pair).max=INT_MAX; + } + *char_len=0; + *subcount=0; +} + +int fill_intrange (struct intinterval pairs[], char* argstring) +{ + int interval_count=0; + int subcount=0; + int char_len=0; + char* orig_string=argstring; + char buffer[MAX_NUMBER_LENGTH+1]; + while (*argstring) { + if (isdigit(*argstring)) { + buffer[char_len]=*argstring; + char_len++; + if (char_len>MAX_NUMBER_LENGTH) { + fprintf(stderr,"too long number in %s:\n",orig_string); + return -1; + } + } else { + if (isspace(*argstring)) { + } else if (',' == *argstring) { + finish_interval(&char_len, &subcount, buffer, &pairs[interval_count]); + interval_count++; + } else if ('-' == *argstring) { + /* this interval */ + if (subcount==0) { + if (0==char_len) { + pairs[interval_count].min=INT_MIN; + } else { + int num=flash_buffer(buffer); + pairs[interval_count].min=num; + char_len=0; + } + } + subcount++; + if (subcount>1) { + fprintf(stderr,"too many - in %s:\n",orig_string); + return -1; + } + } else { + fprintf(stderr,"strange charcater %c in %s:\n", *argstring, orig_string); + return -1; + } + if (char_len>0) { + /* cleaning buffer for new char */ + int i =flash_buffer(buffer); + fprintf(stderr,"strange cleaning %d\n", i); + } + char_len=0; + } + argstring++; + } + finish_interval(&char_len, &subcount, buffer, &pairs[interval_count]); + return ++interval_count; +} + + +struct intrange create_intrange (char* argstring) +{ + int intrange_presize; + struct intrange irange; + irange.pairs=NULL; + irange.elnum=0; + intrange_presize=count_intrange(argstring); + if (-1==intrange_presize || 0==intrange_presize) { + return irange; + } + irange.pairs=(struct intinterval (*)[] ) calloc(intrange_presize+1, sizeof(struct intinterval)); + if (! irange.pairs) { + fprintf(stderr,"not enough memory for %s:\n",argstring); + return irange; + } + irange.elnum=fill_intrange(*irange.pairs,argstring); + return irange; +} + +void print_intrange (struct intrange range) { + int i; + if (range.pairs) { + for (i=0; i=num) return 1; + } + } + return 0; +} + +int is_empty_intrange(struct intrange range) { + int i; + if (range.pairs==NULL) return 1; + return 0; +} + +void drop_intrange(struct intrange range) { + if (range.pairs) { + free (range.pairs); + } +} + +/* Local Variables: + * mode: C + * coding: cp1251 + * End: + */ diff -rN -u catdoc-0.93.4-orig/src/intrange.h catdoc-0.93.4/src/intrange.h --- catdoc-0.93.4-orig/src/intrange.h 1970-01-01 03:00:00 +0300 +++ catdoc-0.93.4/src/intrange.h 2004-11-04 17:56:46 +0200 @@ -0,0 +1,41 @@ +/* -*- C -*- + * File: intrange.h + * + * Created: Thu Nov 4 11:20:36 2004 + * + * $Id:$ + * + * create_intrange converts strings like '-5,2-7,3,-1,56-' into range of integer intervals stored in struct intrange. + * + * drop_intrange frees its allocated memory + * + * is_in_intrange tests integer to be in range + * + */ + +#ifndef INTRANGE_H +#define INTRANGE_H + +struct intinterval { + int min; + int max; +}; + +struct intrange { + int elnum; + struct intinterval (*pairs)[]; +}; + +struct intrange create_intrange (char* argstring); +void drop_intrange(struct intrange range); +int is_in_intrange(struct intrange range, int num); +int is_empty_intrange(struct intrange range); +void print_intrange (struct intrange range); + +#endif + +/* Local Variables: + * mode: C + * coding: cp1251 + * End: + */ diff -rN -u catdoc-0.93.4-orig/src/Makefile.in catdoc-0.93.4/src/Makefile.in --- catdoc-0.93.4-orig/src/Makefile.in 2003-11-15 14:29:14 +0200 +++ catdoc-0.93.4/src/Makefile.in 2004-11-05 17:01:18 +0200 @@ -67,7 +67,7 @@ OBJ=catdoc.o reader.o writer.o analyze.o rtfread.o $(COMMONOBJ) -OBJXLS=xls2csv.o sheet.o xlsparse.o $(COMMONOBJ) +OBJXLS=xls2csv.o sheet.o xlsparse.o intrange.o $(COMMONOBJ) #.c.o: # $(CC) -c $(CFLAGS) $* diff -rN -u catdoc-0.93.4-orig/src/sheet.c catdoc-0.93.4/src/sheet.c --- catdoc-0.93.4-orig/src/sheet.c 2004-09-15 16:34:45 +0300 +++ catdoc-0.93.4/src/sheet.c 2004-11-04 18:04:48 +0200 @@ -121,9 +121,12 @@ * Prints sheet to stdout. Uses global variable cell_separator */ void print_sheet(void) { + static int sheet_number = 0; int i,j,printed=0; struct rowdescr *row; unsigned char **col; + sheet_number ++; + if (! is_in_intrange(range_of_pages, sheet_number)) return; lastrow--; while (lastrow>0&&!rowptr[lastrow].cells) lastrow--; for(i=0,row=rowptr;i<=lastrow;i++,row++) { diff -rN -u catdoc-0.93.4-orig/src/xls2csv.c catdoc-0.93.4/src/xls2csv.c --- catdoc-0.93.4-orig/src/xls2csv.c 2004-09-15 16:34:45 +0300 +++ catdoc-0.93.4/src/xls2csv.c 2004-11-05 17:03:48 +0200 @@ -20,6 +20,7 @@ #include "catdoc.h" #include "float.h" #include "xls.h" +#include "intrange.h" #ifdef __TURBOC__ #define strcasecmp(a,b) strcmpi(a,b) @@ -27,11 +28,12 @@ extern char *forced_date_format; extern char number_format[]; extern char *sheet_separator; +struct intrange range_of_pages; /************************************************************************/ /* Displays help message */ /************************************************************************/ void help (void) { - printf("Usage:\n xls2csv [-xlV] [-g number] [-f date-format] [-b string] [-s charset] [-d charset] [-c char] [ -q number] files\n"); + printf("Usage:\n xls2csv [-xlV] [-g number] [-f date-format] [-b string] [-s charset] [-d charset] [-c char] [ -q number] [ -p pages] files\n"); } /* Defines unicode chars which should be replaced by strings before UNICODE->target chatset @@ -60,6 +62,7 @@ #endif check_charset(&dest_csname,dest_csname); + range_of_pages=create_intrange("-"); while ((c=getopt(argc,argv,"Vlf:s:d:xq:c:b:g:p:"))!=-1) { switch(c) { @@ -103,6 +106,14 @@ sprintf(number_format,"%%.%dg",digits); } break; + case 'p': + drop_intrange(range_of_pages); + range_of_pages = create_intrange(optarg); + if (is_empty_intrange(range_of_pages)) { + fprintf(stderr,"value of -p option should be comma-separated list of positive integers and pairs like -5,7,9-50,60- \n"); + exit(1); + } + break; case 'V': printf("Catdoc Version %s\n",CATDOC_VERSION); exit(0); default: diff -rN -u catdoc-0.93.4-orig/src/xls.h catdoc-0.93.4/src/xls.h --- catdoc-0.93.4-orig/src/xls.h 2003-11-15 20:21:33 +0200 +++ catdoc-0.93.4/src/xls.h 2004-11-04 18:06:04 +0200 @@ -10,6 +10,7 @@ #include #include +#include "intrange.h" /* types of quoting */ #define QUOTE_NEVER 0 #define QUOTE_SPACES_ONLY 1 @@ -24,6 +25,7 @@ extern struct rowdescr *rowptr; extern int startrow; /* xls2csv-specific configuration */ +extern struct intrange range_of_pages; extern char cell_separator; extern int quote_mode; void print_sheet(void);