~ubuntu-branches/ubuntu/quantal/ncbi-tools6/quantal

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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* $Id: sortcmd.c,v 6.2 2006/05/11 18:56:29 kans Exp $
* ===========================================================================
*
*                            PUBLIC DOMAIN NOTICE
*               National Center for Biotechnology Information
*
*  This software/database is a "United States Government Work" under the
*  terms of the United States Copyright Act.  It was written as part of
*  the author's official duties as a United States Government employee and
*  thus cannot be copyrighted.  This software/database is freely available
*  to the public for use. The National Library of Medicine and the U.S.
*  Government have not placed any restriction on its use or reproduction.
*
*  Although all reasonable efforts have been taken to ensure the accuracy
*  and reliability of the software and data, the NLM and the U.S.
*  Government do not and cannot warrant the performance or results that
*  may be obtained by using this software or data. The NLM and the U.S.
*  Government disclaim all warranties, express or implied, including
*  warranties of performance, merchantability or fitness for any particular
*  purpose.
*
*  Please cite the author in any work or product based on this material.
*
* ===========================================================================
*
* File Name:  $RCSfile: sortcmd.c,v $
*
* Author:  Sergei Shavirin
*
* Initial Version Creation Date: 03/24/1997
*
* $Revision: 6.2 $
*
* File Description:
*         Main file for sortcmd standalone executable.
*
* $Log: sortcmd.c,v $
* Revision 6.2  2006/05/11 18:56:29  kans
* added line_count argument to SORTFiles
*
* Revision 6.1  1998/06/11 18:18:55  shavirin
* Removed old style function declarations
*
* Revision 6.0  1997/08/25 18:20:42  madden
* Revision changed to 6.0
*
* Revision 1.4  1997/05/09 17:39:58  shavirin
* Added ability to place sorted file on initial file to be
* sorted
*
 * Revision 1.3  1997/03/18  17:27:55  shavirin
 * Fixed handling of stdin
 *
 * Revision 1.2  1997/03/18  16:54:30  shavirin
 * Changed sortcmd to ncbisort in usage string
 *
 * Revision 1.1  1997/03/13  21:53:21  shavirin
 * Initial revision
 *
*
* ==========================================================================
*/

#include <ncbisort.h>

#define UCHAR_LIM (UCHAR_MAX + 1)

static void usage (void);
static void badfieldspec(CharPtr s);
static UcharPtr fold_tolower(void);
static Int4Ptr nonprinting(void);
static Int4Ptr nondictionary(void);

Int2 Main(void)
{
  SORTKeyFieldPtr key = NULL;
  SORTKeyField gkey;
  CharPtr s;
  Int4 i, t, t2;
  Int4 checkonly = 0, mergeonly = 0, nfiles, line_count;
  CharPtr minus = "-", outfile = minus, *files, tmp;
  CharPtr sort_stdin = "stdin";
  FILE *ofp;
  SORTObjectPtr sop;
  SORTKeyFieldPtr keyhead;
  Int4 argc = GetArgc();
  CharPtr PNTR argv = GetArgv();

  if((sop = SORTObjectNew(NULL, '\0', 0,
                          FALSE, FALSE)) == NULL)
    return 1;

  if((keyhead = SORTGetKeyHead(sop)) == NULL)
    return -1;

  gkey.sword = gkey.eword = -1;
  gkey.ignore = NULL;
  gkey.translate = NULL;
  gkey.numeric = gkey.month = gkey.reverse = 0;
  gkey.skipsblanks = gkey.skipeblanks = 0;

  for (i = 1; i < argc; ++i) {
    if (argv[i][0] == '+') {
      if (key) {
        SORTInsertKey(key, keyhead);
      }

      if((key = (SORTKeyFieldPtr)MemNew(sizeof(SORTKeyField))) == NULL)
        return -1;

      key->eword = -1;
      key->ignore = NULL;
      key->translate = NULL;
      key->skipsblanks = key->skipeblanks = 0;
      key->numeric = key->month = key->reverse = 0;
      s = argv[i] + 1;
      if (!IS_DIGIT(*s))
        badfieldspec(argv[i]);
      for (t = 0; IS_DIGIT(*s); ++s)
        t = 10 * t + *s - '0';
      t2 = 0;
      if (*s == '.')
        for (++s; IS_DIGIT(*s); ++s)
          t2 = 10 * t2 + *s - '0';
      if (t2 || t) {
        key->sword = t;
        key->schar = t2;
      } else
        key->sword = -1;
      while (*s) {
        switch (*s) {
        case 'b':
          key->skipsblanks = 1;
          break;
        case 'd':
          key->ignore = nondictionary();
          break;
        case 'f':
          key->translate = fold_tolower();
          break;
        case 'i':
          key->ignore = nonprinting();
          break;
        case 'M':
          key->skipsblanks = key->skipeblanks = key->month = 1;
          break;
        case 'n':
          key->skipsblanks = key->skipeblanks = key->numeric = 1;
          break;
        case 'r':
          key->reverse = 1;
          break;
        default:
          badfieldspec(argv[i]);
          break;
        }
        ++s;
      }
    } else if (argv[i][0] == '-') {
      if (!StringCmp("-", argv[i]))
        break;
      s = argv[i] + 1;
      /* If thios is second part of +x.y -x.y key-structure ? */

      if (IS_DIGIT(*s)) {
        if (! key)
          usage();
        for (t = 0; IS_DIGIT(*s); ++s)
          t = t * 10 + *s - '0';
        t2 = 0;
        if (*s == '.')
          for (++s; IS_DIGIT(*s); ++s)
            t2 = t2 * 10 + *s - '0';
        key->eword = t;
        key->echar = t2;
        while (*s) {

          switch (*s) {
          case 'b':
            key->skipeblanks = 1;
            break;
          case 'd':
            key->ignore = nondictionary();
            break;
          case 'f':
            key->translate = fold_tolower();
            break;
          case 'i':
            key->ignore = nonprinting();
            break;
          case 'M':
            key->skipsblanks = key->skipeblanks = key->month = 1;
            break;
          case 'n':
            key->skipsblanks = key->skipeblanks = key->numeric = 1;
            break;
          case 'r':
            key->reverse = 1;
            break;
          default:
            badfieldspec(argv[i]);
            break;
          }
          ++s;
        }
        SORTInsertKey(key, keyhead);
        key = NULL;
      } else {
        while (*s) {
          switch (*s) {
          case 'b':
            gkey.skipsblanks = gkey.skipeblanks = 1;
            break;
          case 'c':
            checkonly = 1;
            break;
          case 'd':
            gkey.ignore = nondictionary();
            break;
          case 'f':
            gkey.translate = fold_tolower();
            break;
          case 'i':
            gkey.ignore = nonprinting();
            break;
          case 'M':
            gkey.skipsblanks = gkey.skipeblanks = gkey.month = 1;
            break;
          case 'm':
            mergeonly = 1;
            break;
          case 'n':
            gkey.skipsblanks = gkey.skipeblanks = gkey.numeric = 1;
            break;
          case 'o':
            if (s[1])
              outfile = s + 1;
            else {
              if (i == argc - 1) {
                fprintf(stderr, "ncbisort: missing argument to -o\n");
                exit(-1);
              } else
                outfile = argv[++i];
            }
            goto outer;
          case 'r':
            gkey.reverse = 1;
            SORTSetReverse(TRUE, sop);
            break;
          case 'T':
            if (s[1])
              SORTSetPrefix(s + 1, sop);
            else {
              if (i == argc - 1) {
                fprintf(stderr, "ncbisort: missing argument to -T\n");
                exit(-1);
              } else {
                SORTSetPrefix( argv[++i], sop);
              }
            }
            goto outer;
          case 't':
            if (s[1])
              SORTSetTab(*++s, sop);
            else if (i < argc - 1) {
              SORTSetTab(*argv[++i], sop);
              goto outer;
            } else {
              fprintf(stderr, "ncbisort: missing character for -tc\n");
              exit(-1);
            }
            break;
          case 'u':
            SORTSetUnique(TRUE, sop);
            break;
          case 'V':
            fprintf(stderr, "ncbisort, version 1.0a\n");
            break;
          default:
            usage();
            exit(-1);
          }
          ++s;
        }
      }
    } else
      break;
  outer:;
  }

  if (key) {
    SORTInsertKey(key, keyhead);
  }

  /* Inheritance of global options to individual keys. */

  for (key = keyhead->next; key; key = key->next)
    if (!key->ignore && !key->translate && !key->skipsblanks && !key->reverse
	&& !key->skipeblanks && !key->month && !key->numeric) {
      key->ignore = gkey.ignore;
      key->translate = gkey.translate;
      key->skipsblanks = gkey.skipsblanks;
      key->skipeblanks = gkey.skipeblanks;
      key->month = gkey.month;
      key->numeric = gkey.numeric;
      key->reverse = gkey.reverse;
    }

  if (!keyhead->next && (gkey.ignore || gkey.translate || gkey.skipsblanks
			 || gkey.reverse || gkey.skipeblanks
			 || gkey.month || gkey.numeric))
    SORTInsertKey(&gkey, keyhead);

  if (i < argc) {
    nfiles = argc - i;
    files = &argv[i];
  } else {
    nfiles = 1;
    files = &sort_stdin;
  }

  if (checkonly) {
    return (Int2)(SORTCheckOrderS(files, nfiles, sop));
  }

  if (StringCmp(outfile, "-")) {
    for (i = 0; i < nfiles; ++i)
      if (!StringCmp(outfile, files[i]))
        break;
    if (i == nfiles) {
      if((ofp = FileOpen(outfile, "w")) == NULL)
        return -1;
    } else {
      Uchar buf[8192];
      FILE *fp = FileOpen(outfile, "r");
      size_t cc;
      if(fp == NULL)
        return -1;

      tmp = SORTAddTempName(sop);

      if((ofp = FileOpen(tmp, "w")) == NULL)
        return -1;
      while ((cc = FileRead(buf, 1, sizeof(buf), fp)) > 0) {
          if(!FileWrite(buf, 1, cc, ofp))
              return -1;
      }
      
      if (ferror(fp))  {
          fprintf(stderr, "sort: error in fread (%s)\n",
                  strerror(errno));
          return(-1);
      }

      FileClose(ofp);
      FileClose(fp);
      files[i] = tmp;
      if((ofp = FileOpen(outfile, "w")) == NULL)
        return -1;
    }
  } else {
    ofp = stdout;
  }

  if (mergeonly)
    SORTMergeFiles(files, nfiles, ofp, sop);
  else
    SORTFiles(files, nfiles, ofp, sop, &line_count);

  FileClose(ofp);

  return 0;
}

static void usage(void)
{
  fprintf(stderr,
	  "usage: ncbisort [ -cmu ] [ -tc ] [ -o file ] [ -T dir ]\n");
  fprintf(stderr,
	  "                [ -bdfiMnr ] [ +n [ -m ] . . . ] [ files . . . ]\n");
  exit(-1);
}
static void badfieldspec(CharPtr s)
{
  fprintf(stderr, "ncbisort: bad field specification %s\n", s);
  exit(-1);
}

static UcharPtr fold_tolower(void)
{
  UcharPtr buffer;
  Int4 j;

  buffer = (UcharPtr)MemNew(UCHAR_LIM);

  for (j = 0; j < UCHAR_LIM; ++j) {
    if ( IS_UPPER(j) )
      buffer[j] = (Uchar)TO_LOWER(j);
    else
      buffer[j] = (Uchar)j;
  }
  return buffer;
}

static Int4Ptr nonprinting(void)
{
  Int4Ptr buffer;
  Int4 j;

  buffer = (Int4Ptr) MemNew(UCHAR_LIM*sizeof(Int4));
  for (j = 0; j < UCHAR_LIM; ++j) {
    if (!IS_PRINT(j))
      buffer[j] = 1;
  }
  return buffer;
}

static Int4Ptr nondictionary(void)
{
  Int4Ptr buffer;
  Int4 j;

  buffer = (Int4Ptr)MemNew(UCHAR_LIM*sizeof(Int4));

  for (j = 0; j < UCHAR_LIM; ++j) {
    if (!IS_ALPHANUM(j) && !ISBLANK(j))
      buffer[j] = 1;
  }
  return buffer;
}