~ubuntu-branches/ubuntu/trusty/grfcodec/trusty

« back to all changes in this revision

Viewing changes to grfcomm.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthijs Kooijman
  • Date: 2010-08-23 14:45:52 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100823144552-4qggmf9izixsw8li
Tags: 1.0.0+debian1-1
* [30caa6a] Repackaged upstream source, to remove a duplicate file (which
  the lenny version of tar --keep-old-files doesn't like.
* [331d12b] Update watch file to upstream's new versioning scheme.
* [28b6b51] Mangle the +debian suffix in the watch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
        Functions and classes common to all GRF programs
3
 
        Copyright (C) 2000-2003 by Josef Drexler
4
 
        Distributed under the GNU General Public License
5
 
*/
6
 
 
7
 
#include <stdlib.h>
8
 
#include <stdio.h>
9
 
//#include <dir.h>
10
 
#include <string.h>
11
 
#include <errno.h>
12
 
#include <sys/stat.h>
13
 
#ifdef MINGW
14
 
#       include <io.h>
15
 
#       define mkdir(a,b) mkdir(a)
16
 
#elif defined(_MSC_VER)
17
 
#       include <direct.h>
18
 
#       define mkdir(a,b) mkdir(a)
19
 
#endif
20
 
 
21
 
 
22
 
#include "error.h"
23
 
//#include "sprites.h"
24
 
 
25
 
#if defined(WIN32) || defined(GCC32) || defined(GCC64)
26
 
#       include "path.h"
27
 
#endif
28
 
 
29
 
 
30
 
#include "grfcomm.h"
31
 
 
32
 
extern char* usagetext;
33
 
char *lastspritefilename;
34
 
 
35
 
const char *e_openfile = "Error opening %.228s";
36
 
 
37
 
 
38
 
void usage(const char *extratext)
39
 
{
40
 
        printf(usagetext, extratext);
41
 
 
42
 
        exit(1);
43
 
}
44
 
 
45
 
 
46
 
static int getspritefilename(char *filename, const char *basefilename, char *subdirectory, const char *ext, long spriteno)
47
 
{
48
 
        char *fullpath = strdup(basefilename);//_fullpath(NULL, basefilename, 0);
49
 
 
50
 
        char bdrive[5], bdirectory[128], bname[20], bext[6];
51
 
        char sdrive[5], sdirectory[128];
52
 
 
53
 
        fnsplit(subdirectory, sdrive, sdirectory, NULL, NULL);
54
 
        fnsplit(fullpath,     bdrive, bdirectory, bname, bext);
55
 
 
56
 
        if (strlen(sdrive)) {           // drive given, go relative
57
 
                char *sfullpath = strdup(subdirectory);//_fullpath(NULL, subdirectory, 0);
58
 
                fnsplit(sfullpath, sdrive, sdirectory, NULL, NULL);
59
 
                free(sfullpath);
60
 
 
61
 
                strcpy(bdrive, sdrive);
62
 
        }
63
 
 
64
 
        if (strlen(sdirectory)) {
65
 
                if (sdirectory[0] == '\\' || sdirectory[0] == '/')      // absolute path
66
 
                        strcpy(bdirectory, sdirectory);
67
 
                else
68
 
                        strcat(bdirectory, sdirectory);
69
 
        }
70
 
 
71
 
        free(fullpath);
72
 
 
73
 
        if (spriteno >= 0) {
74
 
                sprintf(bname + strlen(bname), "%02ld", spriteno);
75
 
                /*      int baselen = 8 - strlen(bname);
76
 
                char *numpart = bname + (8 - baselen);
77
 
 
78
 
                long max = 1;
79
 
                int i;
80
 
                for (i=0; i<baselen; i++) max*=10;
81
 
 
82
 
                if (spriteno < max) {           // can be expressed with numbers only
83
 
                sprintf(numpart, "%0*d", baselen, spriteno);
84
 
 
85
 
                } else {
86
 
                spriteno -= max;
87
 
                max = 1;
88
 
                for (i=1; i<baselen; i++) max*=36;
89
 
 
90
 
                spriteno += 10*max;
91
 
 
92
 
                if (spriteno >= max*36) {
93
 
                printf("Cannot find a sprite filename!\n");
94
 
                exit(2);
95
 
                }
96
 
 
97
 
                for (i=0; i<baselen; i++) {
98
 
                long digit = spriteno / max;
99
 
                if (digit < 10)
100
 
                *numpart = '0' + digit;
101
 
                else
102
 
                *numpart = 'A' + digit - 10;
103
 
 
104
 
                spriteno -= max * digit;
105
 
                max /= 36;
106
 
                numpart++;
107
 
                }
108
 
                }
109
 
                */
110
 
        }
111
 
 
112
 
        strcpy(bext, ext);
113
 
 
114
 
        fnmerge(filename, bdrive, bdirectory, bname, bext);
115
 
        fnmerge(subdirectory, bdrive, bdirectory, NULL, NULL);
116
 
 
117
 
        return 0;
118
 
}
119
 
 
120
 
char *spritefilename(const char *basefilename, const char *reldirectory, const char *ext, int spriteno, const char *mode, int mustexist)
121
 
{
122
 
        static char filename[128];
123
 
        char directory[128];
124
 
        FILE *sprite = NULL;
125
 
 
126
 
        strcpy(directory, reldirectory);
127
 
        getspritefilename(filename, basefilename, directory, ext, spriteno);
128
 
 
129
 
        if (directory[strlen(directory)-1] == '\\' || directory[strlen(directory)-1] == '/')
130
 
                directory[strlen(directory)-1] = 0;     // cut off trailing backslash
131
 
 
132
 
        while (mustexist) {     // actuall mustexist doesn't change, loop is terminated by explicit break
133
 
                sprite = fopen(filename, mode);
134
 
                if (!sprite) {
135
 
                        if (errno == ENOENT) {          // directory doesn't exist
136
 
                                if (strchr(mode, 'w')) {        // but we need to write to a file there
137
 
                                        if (mkdir(directory, 0755)) {   // so try creating it
138
 
                                                fperror("Creating %.228s", directory);
139
 
                                                exit(2);
140
 
                                        }
141
 
                                } else {                        // not writing, so report file missing
142
 
                                        fperror("Cannot read %s", filename);
143
 
                                        exit(2);
144
 
                                }
145
 
                        } else {
146
 
                                fperror(e_openfile, filename);
147
 
                                exit(2);
148
 
                        }
149
 
                } else
150
 
                        break;
151
 
        }
152
 
 
153
 
        if (sprite)
154
 
                fclose(sprite);
155
 
 
156
 
        lastspritefilename = filename;
157
 
 
158
 
        return filename;
159
 
}
160
 
 
161
 
int doopen(const char *grffile, const char *dir, const char *ext, const char *mode,
162
 
                   char **filename, FILE **file, int mustexist)
163
 
{
164
 
        char *fn;
165
 
        FILE *f;
166
 
 
167
 
        fn = spritefilename(grffile, dir, ext, -1, mode, mustexist);
168
 
        if (filename) *filename=strdup(fn);
169
 
 
170
 
        f = fopen(fn, mode);
171
 
 
172
 
        if (!f) {
173
 
                fperror(e_openfile, lastspritefilename);
174
 
                exit(2);
175
 
        }
176
 
 
177
 
        if (file) *file = f;
178
 
        else fclose(f);
179
 
 
180
 
        return 1;
181
 
}
182