File: cdbsplit.c

package info (click to toggle)
cookietool 2.5-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 328 kB
  • sloc: ansic: 1,179; makefile: 38
file content (366 lines) | stat: -rw-r--r-- 11,972 bytes parent folder | download | duplicates (3)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
    cookietool is (c) 1995-2001 by Wilhelm Noeker (wnoeker@t-online.de)

    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 for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA
    
 */

  
/*========================================================================*\
 |  File: cdbsplit.c                                   Date: 25 Oct 1997  |
 *------------------------------------------------------------------------*
 |    Split parts off your cookie database, by keyword, by line length,   |
 |       by number of lines, or as groups of "similar" cookies.           |
 | Expected file format is plain text with a "%%" line ending each cookie.|
 |                     See help() for usage notes.                        |
 |                                                                        |
\*========================================================================*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "strstuff.h"
#include "cookio.h"

char version[] = "$VER: cdbsplit 2.5 (18.03.2001)";


#define FBUFSIZE 16384          /* we'll use larger file buffers */

int l_min = 1, l_max = 0, w_min = 1, w_max = 0;
long n_min = 1, n_max = 0;
int matchlen = 0;
int c_fmt = DEF_FMT;
#define NTARGET 10              /* max. number of keywords to search for */
char *rtarget[NTARGET];         /* required keywords */
char *otarget[NTARGET];         /* optional keywords */
int required = 0, optional = 0;


/*
 * print a help text and nag about illegal parameter <s>
 */
static void help( char *s )
    {
    if( s )
        printf( "illegal option '%s'\n", s );
    printf( "usage:  cdbsplit [options] <cookiefile> <newfile>\n" );
    printf( "where options are:\n" );
    printf( " -k<keywd> / -K<keywd>  search for optional / required keyword\n" );
    printf( " -l<lines> / -L<lines>  range for number of lines in a cookie\n" );
    printf( " -w<width> / -W<width>  range for cookie line width\n" );
    printf( " -n<no.>   / -N<no.>    range of cookie numbers\n" );
    printf( " -m<m>     extract groups of cookies with <m> matching chars\n" );
    printf( " -f[0-3]   cookie format: words, lines, %%, %%%% (default: %d)\n", DEF_FMT );
    printf( " -d[0-3]   how fussy about word delimiters? (default: 2)\n" );
    printf( " -c        case sensitive comparisons\n" );
    printf( " -x        extract only, do not modify the input file\n" );
    printf( " -a        append to an existing output file\n" );
    }



/*
 * Write a cookie to one of two files. Returns the number of cookies
 * written to the hit-file (which of course is either 1 or 0).
 */
static int dispatch( char *cookie, int good, FILE *fp2, FILE *fp3 )
    {
    int success = 1;

    if( good )
        success = write_cookie( cookie, fp2, c_fmt );
    else if( fp3 != NULL )
        success = write_cookie( cookie, fp3, c_fmt );
    if( !success )
        {
        printf( "\nfile error, aborted !!!\n" );
        exit( 20 );
        }
    return good ? 1 : 0;
    }


/*
 * Scatter contents of <fp1> across <fp2> (hitfile) and <fp3> (dumpfile),
 * return value is the number of cookies in the dumpfile
 */
static long filter_cookies( FILE *fp1, FILE *fp2, FILE *fp3 )
    {
static char cbak[ CBUFSIZE ];   /* backup of the last cookie, to find groups */
    char *cptr;
    long count = 0, hits = 0, lines, width;
    int ok = 0, ok2, i;
    time_t checkpt;

    strcpy( cbak, "" );
    time( &checkpt );
    while( (cptr = read_cookie( fp1, c_fmt, NULL, &lines, &width )) != NULL )
        {
        count++;
        /* perform the checks: */
        if( matchlen )        /* "similar cookies" mode */
            {
            ok2 = ok;
            ok = (str_ncmp( cbak, cptr, matchlen ) == 0);
            if( *cbak )       /* skip the first loop */
                hits += dispatch( cbak, ok || ok2, fp2, fp3 );
            strcpy( cbak, cptr );
            }
        else                  /* standard mode */
            {
            ok = (lines >= l_min && width >= w_min && count >= n_min);
            if( l_max )
                ok = ok && ( lines <= l_max );
            if( w_max )
                ok = ok && ( width <= w_max );
            if( n_max )
                ok = ok && ( count <= n_max );
            if( ok )          /* string checks still necessary? */
                {
                if( required == 0 && optional > 0 )
                    ok = 0;
                for( i = 0; i < required; i++ )
                    ok = ok && (str_str( cptr, rtarget[i] ) != NULL);
                for( i = 0; i < optional; i++ )
                    ok = ok || (str_str( cptr, otarget[i] ) != NULL);
              }
            hits += dispatch( cptr, ok, fp2, fp3 );
            }
        if( difftime( time( NULL ), checkpt ) >= 1 )
            {
            time( &checkpt );
            printf( "%ld hits/%ld misses.\r", hits, count - hits );
            fflush( stdout );
            }
        }
    if( matchlen )              /* one cookie still pending in this mode */
        hits += dispatch( cbak, ok, fp2, fp3 );
    printf( "Done, %ld hits out of %ld\n", hits, count );
    return (count - hits);
    }



static void confirm_options(void)
/* tell the user what his options will do */
    {
    int flags, i;

    if( matchlen )
        printf( "  searching for groups of cookies with "
                "%d matching characters.\n", matchlen );
    else
        {
        flags = 0xf;            /* what restrictions do apply? */
        if( optional + required > 0 )
            {
            printf( "  search string%s: ", (optional + required > 1) ? "s" : "");
            if( required > 1 && optional > 0 )
                printf("(");
            if( required > 0 )
                {
                printf( "\"%s\"", rtarget[0] );
                for( i = 1; i < required; i++ )
                    printf( " && \"%s\"", rtarget[i] );
                }
            if( required > 1 && optional > 0 )
                printf(")");
            if( required > 0 && optional > 0 )
                printf(" || ");
            if( optional > 0 )
                {
                printf( "\"%s\"", otarget[0] );
                for( i = 1; i < optional; i++ )
                    printf( " || \"%s\"", otarget[i] );
                }
            printf( "\n" );
            }
        else
            flags ^= 1;
        if( l_max )
            printf( "  looking for cookies %d - %d lines long.\n", l_min, l_max );
        else if( l_min > 1 )
            printf( "  looking for cookies at least %d lines long.\n", l_min );
        else
            flags ^= 2;
        if( w_max )
            printf( "  looking for cookies %d - %d columns wide.\n", w_min, w_max );
        else if( w_min > 1 )
            printf( "  looking for cookies at least %d columns wide.\n", w_min );
        else
            flags ^= 4;
        if( n_max )
            printf( "  considering cookies #%ld - #%ld only.\n", n_min, n_max);
        else if( n_min > 1 )
            printf( "  starting at cookie #%ld.\n", n_min );
        else
            flags ^= 8;
        if( flags == 0 )
            printf( "  no restrictions.\n" );
        }
    }


int main( int argc, char *argv[] )
    {
    char *s;
    const char *name1 = NULL, *name2 = NULL, *name3 = "cdb_temp_kickme";
    int append = 0, move = 1;
    int case_sense = 0, bordermode = 2;
    long dumped;
    FILE *infile, *hitfile, *dumpfile;

    if( argc < 3 )
        {
        help( NULL );
        return 5;
        }
    while( --argc )
        {
        s = *++argv;
        if( *s != '-' )
            {
            if( name1 == NULL )
                name1 = s;
            else
                name2 = s;
            }
        else
            {
            s++;
            switch( *s++ )
                {
                case 'k':
                    if( optional < NTARGET )
                        otarget[ optional++ ] = s;
                    break;
                case 'K':
                    if( required < NTARGET )
                        rtarget[ required++ ] = s;
                    break;
                case 'm':
                    matchlen = atoi( s );
                    break;
                case 'l':
                    l_min = atoi( s );
                    break;
                case 'L':
                    l_max = atoi( s );
                    break;
                case 'w':
                    w_min = atoi( s );
                    break;
                case 'W':
                    w_max = atoi( s );
                    break;
                case 'n':
                    n_min = atol( s );
                    break;
                case 'N':
                    n_max = atol( s );
                    break;
                case 'a':
                    append = 1;
                    break;
                case 'x':
                    move = 0;
                    break;
                case 'c':
                    case_sense = 1;
                    break;
                case 'd':
                    if( isdigit( *s ) )
                        bordermode = atoi( s );
                    else
                        goto bad_parm;
                    break;
                case 'f':
                    if( isdigit( *s ) )
                        c_fmt = atoi( s );
                    else
                        goto bad_parm;
                    break;
                default:
                    goto bad_parm;
                }
            }
        }
    str_setup( bordermode, case_sense ); /* !!! */
    if( name1 == NULL || name2 == NULL )
        {
        help( NULL );
        return 5;
        }
    if( !(infile = fopen( name1, "r" ) ) )
        {
        printf( "Can't open '%s' for input!\n", name1 );
        return 10;
        }
    setvbuf( infile, NULL, _IOFBF, FBUFSIZE );
    if( !append && (hitfile = fopen( name2, "r" )) )
        {
        printf( "Error: '%s' exists!  Use -a to append.\n", name2 );
        return 10;
        }
    if( !(hitfile = fopen( name2, "a" )) )
        {
        printf( "Can't open '%s' for output!\n", name2 );
        return 10;
        }
    setvbuf( hitfile, NULL, _IOFBF, FBUFSIZE );
    if( move )
        {
        if( !(dumpfile = fopen( name3, "w" ) ) )
            {
            printf( "Can't open '%s' for output!\n", name3 );
            return 10;
            }
        setvbuf( dumpfile, NULL, _IOFBF, FBUFSIZE );
        }
    else
        dumpfile = NULL;
    printf( "CdbSplit " );
    print_strstat();
    printf( "File format: " );
    print_fmtinfo( c_fmt );
    printf( "%s from '%s' to '%s',\n", move ? "Moving" : "Copying", name1, name2 );
    confirm_options();
    /* OK, here we go: */
    dumped = filter_cookies( infile, hitfile, dumpfile );
    fclose( infile );
    fclose( hitfile );
    if( move )
        {
        fclose( dumpfile );
        if( remove( name1 ) != 0 || rename( name3, name1 ) != 0 )
            {
            printf( "Couldn't overwrite the input file!  Your cookies are in '%s'.\n", name3 );
            return 5;
            }
        if( dumped == 0 )
            {
            remove( name1 );
            printf( "'%s' is now empty and has been deleted.\n", name1 );
            }
        }
    return 0;
bad_parm:
    help( argv[ 0 ] );
    return 5;
    }