Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
Macros | Functions | Variables
rleaddcom.c File Reference
#include "rle.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <string.h>
Include dependency graph for rleaddcom.c:

Go to the source code of this file.

Macros

#define _XOPEN_SOURCE   /* For mkstemp */
 

Functions

void main (int argc, char **argv)
 

Variables

static char temp [] = "rleaddcomXXXXXX"
 
static char buf [4096 +1]
 

Macro Definition Documentation

#define _XOPEN_SOURCE   /* For mkstemp */

Definition at line 30 of file rleaddcom.c.

Function Documentation

void main ( int  argc,
char **  argv 
)

Definition at line 73 of file rleaddcom.c.

76 {
78  char * fname = NULL, * out_fname = NULL;
79  char *i_fname = NULL;
80  FILE *outfile = stdout;
81  char ** comments = NULL;
82  char *my_name = cmd_name( argv );
83  int oflag = 0, iflag = 0, delflag = 0, ncomment = 0;
84  int is_pipe = 0;
85  register int j;
86  int rle_cnt, rle_err;
87 
88  in_hdr = *rle_hdr_init( NULL );
89  out_hdr = *rle_hdr_init( NULL );
90 
91  if ( scanargs( argc, argv, "% d%- i%- o%-outfile!s infile!s comments!*s",
92  &delflag, &iflag, &oflag, &out_fname, &fname,
93  &ncomment, &comments ) == 0 )
94  exit( 1 );
95 
96  if ( iflag )
97  {
98  register char *cp;
99 
100  if ( oflag )
101  i_fname = out_fname;
102  else
103  i_fname = fname;
104 
105  /* Recognize rle_open_f special cases. */
106  if ( strcmp( i_fname, "-" ) == 0 )
107  {
108  fprintf( stderr,
109  "%s: Can't add comments \"in place\" to standard %s.\n",
110  my_name, oflag ? "output" : "input" );
111  exit( 1 );
112  }
113 #ifndef NO_OPEN_PIPES
114  if ( *i_fname == '|' )
115  {
116  fprintf( stderr,
117  "%s: Can't add comments \"in place\" to piped %s.\n",
118  my_name, oflag ? "output" : "input" );
119  exit( 1 );
120  }
121 #endif
122  strcpy( buf, i_fname );
123  if ( (cp = rindex( buf, '/' )) != NULL )
124  {
125  *++cp = 0;
126  strcat( buf, temp );
127  }
128  else
129  strcpy( buf, temp );
130  mkstemp( buf );
131 
132 #ifndef NO_OPEN_PIPES
133  /* Compressed file special case. */
134  cp = i_fname + strlen( i_fname ) - 2;
135  if ( cp > i_fname && *cp == '.' && *(cp + 1) == 'Z' )
136  {
137  strcat( buf, ".Z" );
138  is_pipe = 1;
139  }
140 #endif
141  out_fname = buf;
142  }
143 
144  in_hdr.rle_file = rle_open_f(my_name, fname, "r");
145  rle_names( &in_hdr, my_name, fname, 0 );
146  rle_names( &out_hdr, out_hdr.cmd, out_fname, 0 );
147 
148  /* Read in header */
149  for ( rle_cnt = 0;
150  (rle_err = rle_get_setup( &in_hdr )) == RLE_SUCCESS;
151  rle_cnt++ )
152  {
153  (void)rle_hdr_cp( &in_hdr, &out_hdr );
154  if ( rle_cnt == 0 )
155  outfile = rle_open_f( my_name, out_fname, "w" );
156  out_hdr.rle_file = outfile;
157 
158  /* Copy input to the_hdr struct */
159  for ( j = 0; j < ncomment; j++ )
160  {
161  if ( ! delflag )
162  rle_putcom( comments[j], &out_hdr );
163  else
164  rle_delcom( comments[j], &out_hdr );
165  }
166 
167  /* Start output file */
168  rle_put_setup( &out_hdr );
169 
170  /* Copy rest of input to output */
171  rle_cp( &in_hdr, &out_hdr );
172  }
173  /* Check for an error. EOF or EMPTY is ok if at least one image
174  * has been read. Otherwise, print an error message.
175  */
176  if ( rle_cnt == 0 || (rle_err != RLE_EOF && rle_err != RLE_EMPTY) )
177  {
178  rle_get_error( rle_err, my_name, fname );
179  rle_err = 1;
180  }
181  else
182  rle_err = 0;
183 
184  if ( iflag )
185  {
186  if ( rle_err )
187  fprintf( stderr, "%s: %s not replaced\n",
188  my_name, i_fname );
189  else
190  {
191 #ifndef NO_OPEN_PIPES
192  /* Have to call pclose, else file may not exist yet! */
193  if ( is_pipe )
194  pclose( outfile );
195  else
196 #endif
197  fclose( outfile );
198  if ( rename( buf, i_fname ) < 0 )
199  {
200  fprintf( stderr, "%s: rename failed: ", my_name );
201  perror( "" );
202  unlink( buf ); /* Get rid of temp file. */
203  }
204  }
205  }
206 
207  exit( 0 );
208 }
int scanargs(int argc, char **argv, const char *format,...)
Definition: scanargs.c:94
static rle_hdr in_hdr
Definition: rletogif.c:37
FILE * outfile
Definition: giftorle.c:61
const char * rle_delcom(const char *name, rle_hdr *the_hdr)
#define RLE_EMPTY
Definition: rle.h:73
const char * rle_putcom(const char *value, rle_hdr *the_hdr)
void rle_cp(rle_hdr *in_hdr, rle_hdr *out_hdr)
Definition: rle_cp.c:69
#define rindex
Definition: rle_config.h:97
static char temp[]
Definition: rleaddcom.c:45
#define RLE_SUCCESS
Definition: rle.h:70
char * fname[3]
Definition: show3.c:31
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
const char * cmd
Definition: rle.h:133
int rle_get_setup(rle_hdr *the_hdr)
Definition: rle_getrow.c:74
Definition: rle.h:96
int iflag
Definition: getsun.c:80
int rle_get_error(int code, const char *pgmname, const char *fname)
#define RLE_EOF
Definition: rle.h:74
rle_hdr * rle_hdr_cp(rle_hdr *from_hdr, rle_hdr *to_hdr)
Definition: rle_hdr.c:119
char ** comments
Definition: read98721.c:74
static char buf[4096 +1]
Definition: rleaddcom.c:46
FILE * rle_open_f(const char *prog_name, const char *f_name, const char *mode)
static char * my_name
Definition: gettaac.c:76
void rle_put_setup(rle_hdr *the_hdr)
Definition: rle_putrow.c:453
char * cmd_name(char **argv)
Definition: cmd_name.c:31
int oflag
Definition: painttorle.c:45
FILE * rle_file
Definition: rle.h:114
rle_hdr out_hdr
Definition: dvirle2.c:89
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267

Variable Documentation

char buf[ 4096 +1]
static

Definition at line 46 of file rleaddcom.c.

char temp[] = "rleaddcomXXXXXX"
static

Definition at line 45 of file rleaddcom.c.