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

« back to all changes in this revision

Viewing changes to src/grfcomm.cpp

  • 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
char *lastspritefilename;
 
33
 
 
34
const char *e_openfile = "Error opening %.228s";
 
35
 
 
36
 
 
37
 
 
38
static int getspritefilename(char *filename, const char *basefilename, char *subdirectory, const char *ext, long spriteno)
 
39
{
 
40
        char *fullpath = strdup(basefilename);//_fullpath(NULL, basefilename, 0);
 
41
 
 
42
        char bdrive[5], bdirectory[128], bname[20], bext[6];
 
43
        char sdrive[5], sdirectory[128];
 
44
 
 
45
        fnsplit(subdirectory, sdrive, sdirectory, NULL, NULL);
 
46
        fnsplit(fullpath,     bdrive, bdirectory, bname, bext);
 
47
 
 
48
        if (strlen(sdrive)) {           // drive given, go relative
 
49
                char *sfullpath = strdup(subdirectory);//_fullpath(NULL, subdirectory, 0);
 
50
                fnsplit(sfullpath, sdrive, sdirectory, NULL, NULL);
 
51
                free(sfullpath);
 
52
 
 
53
                strcpy(bdrive, sdrive);
 
54
        }
 
55
 
 
56
        if (strlen(sdirectory)) {
 
57
                if (sdirectory[0] == '\\' || sdirectory[0] == '/')      // absolute path
 
58
                        strcpy(bdirectory, sdirectory);
 
59
                else
 
60
                        strcat(bdirectory, sdirectory);
 
61
        }
 
62
 
 
63
        free(fullpath);
 
64
 
 
65
        if (spriteno >= 0) {
 
66
                sprintf(bname + strlen(bname), "%02ld", spriteno);
 
67
                /*      int baselen = 8 - strlen(bname);
 
68
                char *numpart = bname + (8 - baselen);
 
69
 
 
70
                long max = 1;
 
71
                int i;
 
72
                for (i=0; i<baselen; i++) max*=10;
 
73
 
 
74
                if (spriteno < max) {           // can be expressed with numbers only
 
75
                sprintf(numpart, "%0*d", baselen, spriteno);
 
76
 
 
77
                } else {
 
78
                spriteno -= max;
 
79
                max = 1;
 
80
                for (i=1; i<baselen; i++) max*=36;
 
81
 
 
82
                spriteno += 10*max;
 
83
 
 
84
                if (spriteno >= max*36) {
 
85
                printf("Cannot find a sprite filename!\n");
 
86
                exit(2);
 
87
                }
 
88
 
 
89
                for (i=0; i<baselen; i++) {
 
90
                long digit = spriteno / max;
 
91
                if (digit < 10)
 
92
                *numpart = '0' + digit;
 
93
                else
 
94
                *numpart = 'A' + digit - 10;
 
95
 
 
96
                spriteno -= max * digit;
 
97
                max /= 36;
 
98
                numpart++;
 
99
                }
 
100
                }
 
101
                */
 
102
        }
 
103
 
 
104
        strcpy(bext, ext);
 
105
 
 
106
        fnmerge(filename, bdrive, bdirectory, bname, bext);
 
107
        fnmerge(subdirectory, bdrive, bdirectory, NULL, NULL);
 
108
 
 
109
        return 0;
 
110
}
 
111
 
 
112
char *spritefilename(const char *basefilename, const char *reldirectory, const char *ext, int spriteno, const char *mode, int mustexist)
 
113
{
 
114
        static char filename[128];
 
115
        char directory[128];
 
116
        FILE *sprite = NULL;
 
117
 
 
118
        strcpy(directory, reldirectory);
 
119
        getspritefilename(filename, basefilename, directory, ext, spriteno);
 
120
 
 
121
        size_t dir_len = strlen(directory);
 
122
        if (dir_len != 0 && (directory[dir_len-1] == '\\' || directory[dir_len-1] == '/'))
 
123
                directory[dir_len-1] = 0;       // cut off trailing backslash
 
124
 
 
125
        while (mustexist) {     // actuall mustexist doesn't change, loop is terminated by explicit break
 
126
                sprite = fopen(filename, mode);
 
127
                if (!sprite) {
 
128
                        if (errno == ENOENT) {          // directory doesn't exist
 
129
                                if (strchr(mode, 'w')) {        // but we need to write to a file there
 
130
                                        if (mkdir(directory, 0755)) {   // so try creating it
 
131
                                                fperror("Creating %.228s", directory);
 
132
                                                exit(2);
 
133
                                        }
 
134
                                } else {                        // not writing, so report file missing
 
135
                                        fperror("Cannot read %s", filename);
 
136
                                        exit(2);
 
137
                                }
 
138
                        } else {
 
139
                                fperror(e_openfile, filename);
 
140
                                exit(2);
 
141
                        }
 
142
                } else
 
143
                        break;
 
144
        }
 
145
 
 
146
        if (sprite)
 
147
                fclose(sprite);
 
148
 
 
149
        lastspritefilename = filename;
 
150
 
 
151
        return filename;
 
152
}
 
153
 
 
154
int doopen(const char *grffile, const char *dir, const char *ext, const char *mode,
 
155
                   char **filename, FILE **file, int mustexist)
 
156
{
 
157
        char *fn;
 
158
        FILE *f;
 
159
 
 
160
        fn = spritefilename(grffile, dir, ext, -1, mode, mustexist);
 
161
        if (filename) *filename=strdup(fn);
 
162
 
 
163
        f = fopen(fn, mode);
 
164
 
 
165
        if (!f) {
 
166
                fperror(e_openfile, lastspritefilename);
 
167
                exit(2);
 
168
        }
 
169
 
 
170
        if (file) *file = f;
 
171
        else fclose(f);
 
172
 
 
173
        return 1;
 
174
}
 
175
 
 
176
void cfread(const char *action, void *ptr, size_t size, size_t n, FILE *stream)
 
177
{
 
178
        size_t read = fread(ptr, 1, size * n, stream);
 
179
 
 
180
        if (read != size * n) {
 
181
                fperror("\nError while %s, got %d, wanted %d, at %ld", action, read, size * n,
 
182
                        ftell(stream));
 
183
                exit(2);
 
184
        }
 
185
}
 
186
 
 
187
void cfwrite(const char *action, const void *ptr, size_t size, size_t n, FILE *stream)
 
188
{
 
189
        size_t written = fwrite(ptr, 1, size * n, stream);
 
190
 
 
191
        if (written != size * n) {
 
192
                fperror("\nError while %s, got %d, wanted %d", action, written, size * n);
 
193
                exit(2);
 
194
        }
 
195
}