File: imgvinfo.c

package info (click to toggle)
imgvtopgm 1.2-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 636 kB
  • ctags: 268
  • sloc: ansic: 1,557; makefile: 100; sh: 95
file content (203 lines) | stat: -rw-r--r-- 5,133 bytes parent folder | download
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
/*
 * imgvinfo.c
 *	Pilot Image Viewer header dumper.
 *
 * Copyright (C) 1997 Eric A. Howe
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *   Authors:  Eric A. Howe (mu@trends.net)
 */
#include "slop.h"
MU_ID("$Mu: imgvtopgm/imgvinfo.c 1.19 1998/03/27 06:20:52 $")

#include <time.h>
#include <stdlib.h>

#include "ipdb.h"

/*
 * These tend to live in different places on different systems so I'll
 * just do it by hand and assume you have a proper libc.
 */
extern	int	optind;
extern	char	*optarg;
extern	int	getopt(int, char *const *, const char *);

/*
 * "Globals".  I would just use plain global variables but I have a
 * pathalogical problem with writable globals.
 */
typedef struct {
	char	*timefmt;
	int	utc;
	IPDB	*pdb;
} SLOP;

/*
 * Default time format (this is ISO8601 standard, see strftime(3) for
 * the available codes).
 */
#define	TIMEFMT	"%Y-%m-%d %H:%M:%S"

static int
version(const char *me)
{
	printf("%s %s\n", me, VERSION);
	return EXIT_SUCCESS;
}

#define	OPTS	"hvt:ul"
static char *usefmt    = "%s [-hv] [-t fmt] [-{u|l}] [in [...]]\n";
static char *explain[] = {
	"\tDump the header information for a Pilot Image Viewer pdb file.",
	"\tThis program was originally written to test some .pdb header",
	"\tparsing code but it may be a usefull little tool anyway.",
	"",
	"\t-h      Display this usage message and exit.",
	"\t-v      Display the version number and exit.",
	"\t-t fmt  Specify the time display format (see strftime(3)).",
	"\t-u      Display times in UTC.",
	"\t-l      Display times in the local timezone (default).",
	"",
	"\tThe default time format is \"" TIMEFMT "\" (ISO8601 standard).",
	"\tA file name of \"-\" means to read the standard input.",
	NULL
};
static int
usage(const char *me, int ret)
{
	char	**s;

	printf(usefmt, me);
	for(s = &explain[0]; *s != NULL; ++s)
		printf("%s\n", *s);
	return ret;
}

static SLOP *
readit(char *name, SLOP *s)
{
	FILE	*fp  = NULL;
	IPDB	*pdb = NULL;
	int	status;

	if(name == NULL || strcmp(name, "-") == 0) {
		fp = stdin;
	}
	else if((fp = fopen(name, "r")) == NULL) {
		pm_message("cannot open %s: %s", name, strerror(errno));
		goto TheEnd;
	}
	if((pdb = ipdb_alloc(NULL)) == NULL) {
		pm_message("%s: %s", name, ipdb_err(ENOMEM));
		goto TheEnd;
	}
	if((status = ipdb_read(pdb, fp)) != 0) {
		pm_message("%s: %s", name, ipdb_err(status));
		pdb = ipdb_free(pdb);
		goto TheEnd;
	}

TheEnd:
	if(fp != NULL && fp != stdin)
		fclose(fp);
	s->pdb = pdb;
	return s;
}

static char *
ptime(SLOP *s, time_t t)
{
static	char	buf[1024];
	struct tm *(*tf)(const time_t *) = s->utc ? gmtime : localtime;

	strftime(buf, sizeof(buf) - 1, s->timefmt, tf(&t));
	return buf;
}

static char *
typestr(IPDB *pdb)
{
	char	*s;
	switch(ipdb_type(pdb)) {
	case IMG_GRAY:	s = "grayscale";	break;
	case IMG_MONO:	s = "monochrome";	break;
	default:	s = "unknown";		break;
	}
	return s;
}

#define compstr(pdb)	(ipdb_compressed(pdb) ? "compressed" : "uncompressed")

static void
dumpit(SLOP *s, char *file)
{
	IPDB	*pdb    = s->pdb;
	char	*note   = NULL;

	if(pdb == NULL)
		return;

	printf("file:\t\t%s\n", file == NULL ? "-" : file);
	printf("name:\t\t%s (%s)\n", ipdb_iname(pdb), ipdb_pname(pdb));
	printf("created:\t%s\n", ptime(s, ipdb_ctime(pdb)));
	printf("last modified:\t%s\n", ptime(s, ipdb_mtime(pdb)));
	printf("last backed up:\t%s\n", ptime(s, ipdb_btime(pdb)));
	printf("version:\t%d (%s)\n", ipdb_version(pdb), compstr(pdb));
	printf("type:\t\t%d (%s)\n", ipdb_type(pdb), typestr(pdb));
	printf("last position:\t(%d, %d)\n", ipdb_xlast(pdb), ipdb_ylast(pdb));
	printf("anchor:\t\t(%d, %d)\n", ipdb_xanchor(pdb), ipdb_yanchor(pdb));
	printf("size:\t\t%dx%d\n", ipdb_width(pdb), ipdb_height(pdb));

	if((note = ipdb_text(pdb)) != NULL)
		printf("Note:\n%s\n", note);
	printf("\n");

	s->pdb = ipdb_free(pdb);
}

int
main(int argc, char **argv)
{
	char	*me;
	int	o;
	SLOP	s;

	memset((void *)&s, '\0', sizeof(s));
	if((me = strrchr(*argv, '/')) == NULL)
		me = *argv;
	else
		++me;
	s.timefmt = TIMEFMT;
	s.utc     = FALSE;
	pgm_init(&argc, argv);
	while((o = getopt(argc, argv, OPTS)) != EOF) {
		switch(o) {
		case 'h':	exit(usage(me, EXIT_SUCCESS));		break;
		case 'v':	exit(version(me));			break;
		case 't':	s.timefmt = optarg;			break;
		case 'u':	s.utc     = TRUE;			break;
		case 'l':	s.utc     = FALSE;			break;
		default:	exit(usage(me, EXIT_FAILURE));		break;
		}
	}

	do {
		dumpit(readit(argv[optind], &s), argv[optind]);
	} while(argv[++optind] != NULL);

	return EXIT_SUCCESS;
}