Skip to content

Commit

Permalink
eliminated code duplication
Browse files Browse the repository at this point in the history
darcs-hash:20020305072355-f3bf6-33ea832d9f90939cbb61e34dac7a3d249a70e3f8
  • Loading branch information
stevenj committed Mar 5, 2002
1 parent 632d6d8 commit ecd8a0f
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 260 deletions.
18 changes: 9 additions & 9 deletions Makefile.in
Expand Up @@ -17,12 +17,12 @@ distdir = $(package)-$(VERSION)

###########################################################################

OBJ_h5topng = h5topng.o arrayh5.o writepng.o
SRC_h5read = h5read.cc arrayh5.o
OBJ_h5totxt = h5totxt.o arrayh5.o
OBJ_h5tovtk = h5tovtk.o arrayh5.o
OBJ_h5fromtxt = h5fromtxt.o arrayh5.o
OBJ_h5fromh4 = h5fromh4.o arrayh5.o arrayh4.o
OBJ_h5topng = h5topng.o arrayh5.o h5utils.o writepng.o
SRC_h5read = h5read.cc arrayh5.o h5utils.o
OBJ_h5totxt = h5totxt.o arrayh5.o h5utils.o
OBJ_h5tovtk = h5tovtk.o arrayh5.o h5utils.o
OBJ_h5fromtxt = h5fromtxt.o arrayh5.o h5utils.o
OBJ_h5fromh4 = h5fromh4.o arrayh5.o h5utils.o arrayh4.o

OCT_INSTALL_DIR = @OCT_INSTALL_DIR@
H5READ_INSTALL_TARGET = @H5READ_INSTALL_TARGET@
Expand Down Expand Up @@ -59,9 +59,9 @@ h5fromtxt: $(OBJ_h5fromtxt)
h5fromh4: $(OBJ_h5fromh4)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJ_h5fromh4) @H4_LIBS@ $(LIBS) -o $@

h5tov5d: h5tov5d.o
h5tov5d: h5tov5d.o arrayh5.o h5utils.o
$(CC) $(CFLAGS) $(LDFLAGS) h5tov5d.o $(V5D_FILES) \
arrayh5.o $(LIBS) -o $@
arrayh5.o h5utils.o $(LIBS) -o $@

h5tov5d.o: h5tov5d.c
$(CC) -c $(DEFS) $(CPPFLAGS) $(V5D_INCLUDES) $(CFLAGS) $< -o $@
Expand Down Expand Up @@ -166,7 +166,7 @@ dist:
rm -rf $(distdir)

clean:
rm -f h5topng.o h5tov5d.o h5totxt.o h5tovtk.o h5fromtxt.o h5fromh4.o h5read.o arrayh5.o arrayh4.o writepng.o $(TARGETS) core a.out octave-core
rm -f h5topng.o h5tov5d.o h5totxt.o h5tovtk.o h5fromtxt.o h5fromh4.o h5read.o arrayh5.o h5utils.o arrayh4.o writepng.o $(TARGETS) core a.out octave-core

distclean: clean
rm -f config.cache Makefile config.log config.status config.h $(TARGETS)
Expand Down
46 changes: 3 additions & 43 deletions h5fromh4.c
Expand Up @@ -30,6 +30,7 @@
#include "arrayh5.h"
#include "arrayh4.h"
#include "copyright.h"
#include "h5utils.h"

#define CHECK(cond, msg) { if (!(cond)) { fprintf(stderr, "h5fromh4 error: %s\n", msg); exit(EXIT_FAILURE); } }

Expand All @@ -47,35 +48,6 @@ void usage(FILE *f)
);
}

/* given an fname of the form <filename>:<data_name>, return a pointer
to a newly-allocated string containing <filename>, and point data_name
to the position of <data_name> in fname. The user must free() the
<filename> string. */
static char *split_fname(char *fname, char **data_name)
{
int fname_len;
char *colon, *filename;

fname_len = strlen(fname);
colon = strchr(fname, ':');
if (colon) {
int colon_len = strlen(colon);
filename = (char*) malloc(sizeof(char) * (fname_len-colon_len+1));
CHECK(filename, "out of memory");
strncpy(filename, fname, fname_len-colon_len+1);
filename[fname_len-colon_len] = 0;
*data_name = colon + 1;
}
else { /* treat as if ":" were at the end of fname */
filename = (char*) malloc(sizeof(char) * (fname_len + 1));
CHECK(filename, "out of memory");
strcpy(filename, fname);
*data_name = fname + fname_len;
}
return filename;
}


int main(int argc, char **argv)
{
char *dname, *h5_fname = NULL;
Expand Down Expand Up @@ -134,20 +106,8 @@ int main(int argc, char **argv)
char *cur_h5_fname = h5_fname;
arrayh5 a;

if (!cur_h5_fname) {
cur_h5_fname = (char *) malloc(sizeof(char) *
(strlen(h4_fname) + 5));
strcpy(cur_h5_fname, h4_fname);

/* remove ".hdf" from filename: */
if (strlen(cur_h5_fname) >= strlen(".hdf") &&
!strcmp(cur_h5_fname + strlen(cur_h5_fname)-strlen(".hdf"),
".hdf"))
cur_h5_fname[strlen(cur_h5_fname) - strlen(".hdf")] = 0;

/* add ".h5": */
strcat(cur_h5_fname, ".h5");
}
if (!cur_h5_fname)
cur_h5_fname = replace_suffix(h4_fname, ".hdf", ".h5");

/* If we specified -o (to concatenate several HDF4 files into
a single HDF5 file) and if there is more than one filename
Expand Down
29 changes: 1 addition & 28 deletions h5fromtxt.c
Expand Up @@ -29,6 +29,7 @@
#include "config.h"
#include "arrayh5.h"
#include "copyright.h"
#include "h5utils.h"

#define CHECK(cond, msg) { if (!(cond)) { fprintf(stderr, "h5fromtxt error: %s\n", msg); exit(EXIT_FAILURE); } }

Expand All @@ -47,34 +48,6 @@ void usage(FILE *f)
);
}

/* given an fname of the form <filename>:<data_name>, return a pointer
to a newly-allocated string containing <filename>, and point data_name
to the position of <data_name> in fname. The user must free() the
<filename> string. */
static char *split_fname(char *fname, char **data_name)
{
int fname_len;
char *colon, *filename;

fname_len = strlen(fname);
colon = strchr(fname, ':');
if (colon) {
int colon_len = strlen(colon);
filename = (char*) malloc(sizeof(char) * (fname_len-colon_len+1));
CHECK(filename, "out of memory");
strncpy(filename, fname, fname_len-colon_len+1);
filename[fname_len-colon_len] = 0;
*data_name = colon + 1;
}
else { /* treat as if ":" were at the end of fname */
filename = (char*) malloc(sizeof(char) * (fname_len + 1));
CHECK(filename, "out of memory");
strcpy(filename, fname);
*data_name = fname + fname_len;
}
return filename;
}

#define MAX_RANK 10

int main(int argc, char **argv)
Expand Down
45 changes: 3 additions & 42 deletions h5topng.c
Expand Up @@ -32,6 +32,7 @@
#include "arrayh5.h"
#include "copyright.h"
#include "writepng.h"
#include "h5utils.h"

#define CHECK(cond, msg) { if (!(cond)) { fprintf(stderr, "h5topng error: %s\n", msg); exit(EXIT_FAILURE); } }

Expand Down Expand Up @@ -69,34 +70,6 @@ void usage(FILE *f)
);
}

/* given an fname of the form <filename>:<data_name>, return a pointer
to a newly-allocated string containing <filename>, and point data_name
to the position of <data_name> in fname. The user must free() the
<filename> string. */
static char *split_fname(char *fname, char **data_name)
{
int fname_len;
char *colon, *filename;

fname_len = strlen(fname);
colon = strchr(fname, ':');
if (colon) {
int colon_len = strlen(colon);
filename = (char*) malloc(sizeof(char) * (fname_len-colon_len+1));
CHECK(filename, "out of memory");
strncpy(filename, fname, fname_len-colon_len+1);
filename[fname_len-colon_len] = 0;
*data_name = colon + 1;
}
else { /* treat as if ":" were at the end of fname */
filename = (char*) malloc(sizeof(char) * (fname_len + 1));
CHECK(filename, "out of memory");
strcpy(filename, fname);
*data_name = fname + fname_len;
}
return filename;
}

rgba_t gray_colors[2] = { {1,1,1,1}, {0,0,0,1} };
colormap_t gray_cmap = { 2, gray_colors };

Expand Down Expand Up @@ -354,20 +327,8 @@ int main(int argc, char **argv)
CHECK(!contour_fname || arrayh5_conformant(a, contour_data),
"contour data must be conformant with source data");

if (!png_fname) {
png_fname = (char *) malloc(sizeof(char) *
(strlen(h5_fname) + 5));
strcpy(png_fname, h5_fname);

/* remove ".h5" from filename: */
if (strlen(png_fname) >= strlen(".h5") &&
!strcmp(png_fname + strlen(png_fname) - strlen(".h5"),
".h5"))
png_fname[strlen(png_fname) - strlen(".h5")] = 0;

/* add ".png": */
strcat(png_fname, ".png");
}
if (!png_fname)
png_fname = replace_suffix(h5_fname, ".h5", ".png");

{
double a_min, a_max;
Expand Down
29 changes: 1 addition & 28 deletions h5totxt.c
Expand Up @@ -29,6 +29,7 @@
#include "config.h"
#include "arrayh5.h"
#include "copyright.h"
#include "h5utils.h"

#define CHECK(cond, msg) { if (!(cond)) { fprintf(stderr, "h5totxt error: %s\n", msg); exit(EXIT_FAILURE); } }

Expand All @@ -51,34 +52,6 @@ void usage(FILE *f)
);
}

/* given an fname of the form <filename>:<data_name>, return a pointer
to a newly-allocated string containing <filename>, and point data_name
to the position of <data_name> in fname. The user must free() the
<filename> string. */
static char *split_fname(char *fname, char **data_name)
{
int fname_len;
char *colon, *filename;

fname_len = strlen(fname);
colon = strchr(fname, ':');
if (colon) {
int colon_len = strlen(colon);
filename = (char*) malloc(sizeof(char) * (fname_len-colon_len+1));
CHECK(filename, "out of memory");
strncpy(filename, fname, fname_len-colon_len+1);
filename[fname_len-colon_len] = 0;
*data_name = colon + 1;
}
else { /* treat as if ":" were at the end of fname */
filename = (char*) malloc(sizeof(char) * (fname_len + 1));
CHECK(filename, "out of memory");
strcpy(filename, fname);
*data_name = fname + fname_len;
}
return filename;
}

int main(int argc, char **argv)
{
arrayh5 a;
Expand Down
49 changes: 1 addition & 48 deletions h5tov5d.c
Expand Up @@ -27,6 +27,7 @@

#include "config.h"
#include "arrayh5.h"
#include "h5utils.h"

/* Vis5d header files: */
#if defined(HAVE_VIS5Dp_V5D_H)
Expand Down Expand Up @@ -55,54 +56,6 @@ void usage(FILE *f)
);
}

char *replace_suffix(const char *s, const char *old_suff, const char *new_suff)
{
char *new_s;
int s_len, old_suff_len, new_suff_len;

s_len = strlen(s);
old_suff_len = strlen(old_suff);
new_suff_len = strlen(new_suff);

new_s = (char*) malloc(sizeof(char) * (s_len + new_suff_len + 1));
CHECK(new_s, "out of memory");

strcpy(new_s, s);
if (s_len >= old_suff_len && !strcmp(new_s + s_len - old_suff_len,
old_suff))
new_s[s_len - old_suff_len] = 0; /* delete old suffix */
strcat(new_s, new_suff);
return new_s;
}

/* given an fname of the form <filename>:<data_name>, return a pointer
to a newly-allocated string containing <filename>, and point data_name
to the position of <data_name> in fname. The user must free() the
<filename> string. */
static char *split_fname(char *fname, char **data_name)
{
int fname_len;
char *colon, *filename;

fname_len = strlen(fname);
colon = strchr(fname, ':');
if (colon) {
int colon_len = strlen(colon);
filename = (char*) malloc(sizeof(char) * (fname_len-colon_len+1));
CHECK(filename, "out of memory");
strncpy(filename, fname, fname_len-colon_len+1);
filename[fname_len-colon_len] = 0;
*data_name = colon + 1;
}
else { /* treat as if ":" were at the end of fname */
filename = (char*) malloc(sizeof(char) * (fname_len + 1));
CHECK(filename, "out of memory");
strcpy(filename, fname);
*data_name = fname + fname_len;
}
return filename;
}

/* The following routine was adapted from convert/foo2_to_v5d.c from
Vis5D 4.2, which is Copyright (C) 1990-1997 Bill Hibbard, Johan
Kellum, Brian Paul, Dave Santek, and Andre Battaiola, and is
Expand Down

0 comments on commit ecd8a0f

Please sign in to comment.