Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
rlesplit.c
Go to the documentation of this file.
1 /*
2  * rlesplit.c - Split concatenated RLE files into separate files.
3  *
4  * Author: Spencer W. Thomas
5  * Computer Science Dept.
6  * University of Utah
7  * Date: Mon May 4 1987
8  * Copyright (c) 1987, University of Utah
9  */
10 #ifndef lint
11 char rcsid[] = "$Header: /tmp_mnt/n/itn/hendrix/u/spencer/RCS/rlesplit.c,v 3.0.1.2 1992/04/30 14:13:55 spencer Exp spencer $";
12 #endif
13 /*
14 rlesplit() Tag the file.
15 */
16 
17 #include <stdio.h>
18 #include "rle.h"
19 #include "rle_raw.h"
20 
21 /*****************************************************************
22  * TAG( main )
23  *
24  * Usage:
25  * rlesplit [-n number [digits]] [-p prefix] [rlefile]
26  * Inputs:
27  * -n number: If specified, output file numbering will start
28  * with this value (see below). Otherwise, numbering
29  * starts at 1.
30  *
31  * digits: The number of digits to be used in the numeric
32  * portion of the output file names. Defaults to 3.
33  * All numbers will be leading zero filled.
34  *
35  * -p prefix: If specified, output files will be named
36  * "prefix.#.rle". If not specified, and
37  * an rlefile is specified, then output files
38  * will be "rlefileroot.#.rle", where
39  * "rlefileroot" is rlefile with any ".rle" suffix
40  * stripped off. If no arguments are specified,
41  * output files will be "#.rle". In any case, "#"
42  * represents a sequentially increasing number.
43  *
44  * -t: Use TITLE comment as output file name.
45  * Obviously, these should be distinct!
46  *
47  * infile: If specified, input will be read from here,
48  * otherwise, input will be read from stdin.
49  * Outputs:
50  * Writes each rle image in the input stream to an output file
51  * whose name is generated as above.
52  * Assumptions:
53  * Each RLE image in the input stream must be terminated with
54  * an EOF opcode.
55  * Algorithm:
56  * [None]
57  */
58 
59 void
61 int argc;
62 char **argv;
63 {
64  register CONST_DECL char * cp, * slashp;
65  int num = 1, oflag = 0, digits = 3, tflag = 0;
66  int rle_err;
67  CONST_DECL char *infname = NULL, *format = "%s%0*d.rle";
68  static char nullpref[] = "";
69  char *prefix = nullpref;
70  char *title;
71  char filebuf[BUFSIZ];
72  rle_hdr in_hdr, out_hdr;
73 
74  in_hdr = *rle_hdr_init( NULL );
75  out_hdr = *rle_hdr_init( NULL );
76 
77  if ( scanargs( argc, argv,
78  "% n%-number!ddigits%d o%-prefix!s t%- infile%s\n(\
79 \tSplit a multi-image file into separate files.\n\
80 \t-n\tSpecify number assigned to first image, optionally specify\n\
81 \t\tnumber of digits used for numbering (default 3).\n\
82 \t-o\tSpecify output filename prefix. If not specified, input\n\
83 \t\tfile name (without .rle) will be used. Output file names are\n\
84 \t\tof the form prefix.number.rle.\n\
85 \t-t\tUse TITLE (or title) comment as file name. They should be distinct.)",
86  &num, &num, &digits, &oflag, &prefix, &tflag,
87  &infname ) == 0 )
88  exit( 1 );
89 
90  /* Open input file */
91  in_hdr.rle_file = rle_open_f(cmd_name( argv ), infname, "r");
92  rle_names( &in_hdr, cmd_name( argv ), infname, 0 );
93 
94  if ( oflag || infname )
95  {
96  format = "%s.%0*d.rle";
97  if ( !oflag )
98  {
99  /* Strip ".rle" suffix from input file name */
100  /* Avoid strrchr, rindex problem */
101  for ( cp = infname; *cp; cp++ )
102  ; /* find end of name */
103  /* Look for last slash */
104  for ( slashp = cp - 1; *slashp != '/' && slashp > infname; )
105  slashp--;
106  if ( *slashp == '/' )
107  slashp++;
108  /* Look for last dot */
109  while ( *--cp != '.' && cp > infname )
110  ; /* find last . */
111  if ( strcmp( cp, ".rle" ) != 0 )
112  cp = infname + strlen( infname );
113  /* Make null full string buffer */
114  prefix = (char *)calloc( cp - slashp + 1, 1 );
115  RLE_CHECK_ALLOC( in_hdr.cmd, prefix, 0 );
116  /* Copy everything but suffix */
117  strncpy( prefix, infname, cp - slashp );
118  }
119  }
120 
121  while ( (rle_err = rle_get_setup( &in_hdr )) == RLE_SUCCESS )
122  {
123  /* Copy input to output file */
124  if ( tflag &&
125  ( (title=rle_getcom( "TITLE", &in_hdr)) ||
126  (title=rle_getcom( "title", &in_hdr)) ) &&
127  *title != '\0' )
128  sprintf( filebuf, "%s%s", prefix, title );
129  else
130  sprintf( filebuf, format, prefix, digits, num++ );
131 
132  fprintf( stderr, "%s: Creating %s\n", cmd_name( argv ), filebuf );
133  (void)rle_hdr_cp( &in_hdr, &out_hdr );
134  rle_names( &out_hdr, out_hdr.cmd, filebuf, 0 );
135  out_hdr.rle_file = rle_open_f(out_hdr.cmd, filebuf, "w");
136  rle_addhist( argv, &in_hdr, &out_hdr );
137 
138  rle_put_setup( &out_hdr );
139 
140  /* Copy the image to the output file. */
141  rle_cp( &in_hdr, &out_hdr );
142 
143  rle_close_f( out_hdr.rle_file );
144  }
145 
146  if ( rle_err != RLE_EOF && rle_err != RLE_EMPTY )
147  rle_get_error( rle_err, cmd_name( argv ), infname );
148 
149  exit( 0 );
150 }
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
Definition: rle_open_f.c:216
char rcsid[]
Definition: rleswap.c:28
rle_hdr * rle_hdr_cp(rle_hdr *from_hdr, rle_hdr *to_hdr)
Definition: rle_hdr.c:119
void rle_cp(rle_hdr *in_hdr, rle_hdr *the_hdr)
Definition: rle_cp.c:69
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
Definition: rle_hdr.c:48
#define RLE_EMPTY
Definition: rle.h:73
char * cmd_name(char **argv)
Definition: cmd_name.c:31
void rle_close_f(FILE *fd)
Definition: rle_open_f.c:244
void main(int argc, char **argv)
Definition: aliastorle.c:121
int rle_get_setup(rle_hdr *the_hdr)
Definition: rle_getrow.c:74
#define RLE_SUCCESS
Definition: rle.h:70
int rle_get_error(int code, const char *pgmname, const char *fname)
Definition: rle_error.c:76
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
const char * cmd
Definition: rle.h:133
#define RLE_EOF
Definition: rle.h:74
#define CONST_DECL
Definition: rle_config.h:42
void rle_addhist(argv, rle_hdr *in_hdr, rle_hdr *out_hdr)
Definition: rle_addhist.c:54
char * rle_getcom(char *name, rle_hdr *the_hdr) const
Definition: rle_getcom.c:81
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
FILE * rle_file
Definition: rle.h:114
#define RLE_CHECK_ALLOC(pgm, ptr, name)
Definition: rle.h:86