File: shptool.c

package info (click to toggle)
lgeneral 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 8,760 kB
  • sloc: ansic: 30,422; sh: 3,574; makefile: 378; sed: 93
file content (170 lines) | stat: -rw-r--r-- 5,157 bytes parent folder | download | duplicates (3)
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
/*
    A command line tool for converting pg-shp into pixmaps.
    Copyright (C) 2005 Leo Savernik  All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:
    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR
    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef _GNU_SOURCE
	#define _GNU_SOURCE
#endif
#include <getopt.h>

#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// #include "list.h"
#include "misc.h"
#include "shp.h"

#ifdef __GNUC__
#  define NORETURN __attribute__ ((noreturn))
#  define PRINTF_STYLE(fmtidx, firstoptidx) __attribute__ ((format(printf,fmtidx,firstoptidx)))
#else
#  define NORETURN
#  define PRINTF_STYLE(x,y)
#endif

#define SHPTOOL_MAJOR 0
#define SHPTOOL_MINOR 1
#define SHPTOOL_PATCHLVL 0

enum Options { OPT_VERSION = 256 };

static void abortf(const char *fmt, ...) NORETURN PRINTF_STYLE(1,2);
static void verbosef(int lvl, const char *fmt, ...) PRINTF_STYLE(2,3);

static void syntax(int argc, char **argv);

static int verbosity;
static int show_help;
static const char *output_file;
static const char *input_file;
int use_def_pal;

// fake externals for shp.c
const char *source_path;
const char *dest_path;

static struct option long_options[] =
{
  {"defpal", 0, 0, 'd'},
  {"help", 0, &show_help, 1},
  {"verbose", 0, 0, 'v'},
  {"version", 0, 0, OPT_VERSION},
  {0, 0, 0, 0}
};

/* aborts with the given error message and exit code 1 */
static void abortf(const char *fmt, ...) {
  va_list ap;
  va_start(ap, fmt);
  vfprintf(stderr, fmt, ap);
  exit(1);
}

/* prints a message if 'level' is at least equal to the current verbosity level */
static void verbosef(int level, const char *fmt, ...) {
  va_list ap;
  if (level > verbosity) return;
  va_start(ap, fmt);
  vfprintf(stderr, fmt, ap);
}

/* handle the command line options */
static void process_cmdline(int argc, char **argv) {
  for (;;) {
    int result = getopt_long(argc, argv, "v", long_options, 0);
    if (result == -1) break;

    switch (result) {
      case 'd': use_def_pal = 1; break;
      case 'v': verbosity++; break;
      case OPT_VERSION: printf("%d.%d.%d\n", SHPTOOL_MAJOR, SHPTOOL_MINOR, SHPTOOL_PATCHLVL); exit(0);
    }
  }

  if (show_help) {
    syntax(argc, argv);
    exit(1);
  }
  
  /* first parameter is input file */
  if (optind >= argc) abortf("Input file missing\n");
  input_file = argv[optind++];
  
  /* second parameter is output file */  
  if (optind >= argc) abortf("Output file missing\n");
  output_file = argv[optind++];
  
  if (optind < argc) abortf("Excess parameters: %s\n", argv[optind]);
  
  verbosef(2, "use_def_pal:\t\t%d\n"
  		"verbosity:\t\t%d\n"
  	, use_def_pal, verbosity);
}

static void syntax(int argc, char **argv) {
  printf("LGeneral Shp-format extraction tool.\n"
         "\n"
         "Syntax: %s [options] infile outfile\n", "shptool");
  printf("\nOptions:\n"
         "-d, --defpal\tUse default palette.\n"
         "-v, --verbose\tIncrease verbosity.\n"
         "    --version\tDisplay version information and exit.\n"
         );
}

static void convert_shp_to_bmp() {
  PG_Shp *shp;
  verbosef(1, "Reading from %s\n", input_file);
  shp = shp_load(input_file);
  if (!shp) abortf("Input file '%s' could not be loaded\n", input_file);
  
  verbosef(1, "Writing to %s\n", output_file);
  if (SDL_SaveBMP(shp->surf, output_file) < 0)
    abortf("Could not write to '%s'\n", output_file);
}

static void init_sdl() {
  verbosef(2, "Initialising SDL...\n");
  verbosef(3, "SDL_Init\n");
  /* SDL required for graphical conversion */
  SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
  verbosef(3, "SDL_SetVideoMode\n");
  SDL_SetVideoMode( 20, 20, 16, SDL_SWSURFACE );
  verbosef(3, "Registering exit-handler\n");
  atexit( SDL_Quit );
}

int main(int argc, char **argv) {

  process_cmdline(argc, argv);
  init_sdl();
  convert_shp_to_bmp();
  return 0;
}

/* kate: tab-indents on; space-indent on; replace-tabs off; indent-width 2; dynamic-word-wrap off; inden(t)-mode cstyle */