File: fileutil.cpp

package info (click to toggle)
povray 1%3A3.7.0.10-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 147,232 kB
  • sloc: cpp: 845,011; ansic: 122,118; sh: 34,204; pascal: 6,420; asm: 3,355; ada: 1,681; makefile: 1,389; cs: 879; awk: 590; perl: 245; xml: 95
file content (297 lines) | stat: -rw-r--r-- 9,788 bytes parent folder | download | duplicates (6)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*******************************************************************************
 * fileutil.cpp
 *
 * This module implements misc utility functions.
 *
 * ---------------------------------------------------------------------------
 * Persistence of Vision Ray Tracer ('POV-Ray') version 3.7.
 * Copyright 1991-2013 Persistence of Vision Raytracer Pty. Ltd.
 *
 * POV-Ray is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * POV-Ray 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ---------------------------------------------------------------------------
 * POV-Ray is based on the popular DKB raytracer version 2.12.
 * DKBTrace was originally written by David K. Buck.
 * DKBTrace Ver 2.0-2.12 were written by David K. Buck & Aaron A. Collins.
 * ---------------------------------------------------------------------------
 * $File: //depot/public/povray/3.x/source/backend/support/fileutil.cpp $
 * $Revision: #1 $
 * $Change: 6069 $
 * $DateTime: 2013/11/06 11:59:40 $
 * $Author: chrisc $
 *******************************************************************************/

// TODO FIXME - fileutil.cpp code is needed by front- abd backend alike, so should be in "base" directory

#include <ctype.h>
#include <stdarg.h>

// frame.h must always be the first POV file included (pulls in platform config)
#include "backend/frame.h"
#include "backend/support/fileutil.h"
#include "backend/parser/parse.h" // TODO FIXME HACK - used for hack below, need to remove [trf]

#include "povrayold.h" // TODO

// this must be the last file included
#include "base/povdebug.h"

namespace pov
{

/*
// not used right now
typedef struct
{
	bool read_local;
	bool read_global;
	bool write_local;
	bool write_global;
} POV_File_Restrictions;

POV_File_Restrictions gPOV_File_Restrictions[POV_File_Count] =
{
	{ false, false, false, false }, // POV_File_Unknown
	{ true,  true,  false, false }, // POV_File_Image_Targa
	{ true,  true,  false, false }, // POV_File_Image_PNG
	{ true,  true,  false, false }, // POV_File_Image_PPM
	{ true,  true,  false, false }, // POV_File_Image_PGM
	{ true,  true,  false, false }, // POV_File_Image_GIF
	{ true,  true,  false, false }, // POV_File_Image_IFF
	{ true,  true,  false, false }, // POV_File_Image_JPEG
	{ true,  true,  false, false }, // POV_File_Image_TIFF
	{ true,  true,  false, false }, // POV_File_Image_System
	{ true,  false, false, false }, // POV_File_Text_POV
	{ true,  false, false, false }, // POV_File_Text_INC
	{ true,  false, false, false }, // POV_File_Text_INI
	{ true,  true,  false, false }, // POV_File_Text_CSV
	{ true,  false, false, false }, // POV_File_Text_Stream
	{ true,  true,  false, false }, // POV_File_Text_User
	{ true,  true,  true,  false }, // POV_File_Data_DF3
	{ true,  true,  true,  true  }, // POV_File_Data_RCA
	{ true,  true,  true,  true  }, // POV_File_Data_LOG
	{ true,  false, true,  false }  // POV_File_Font_TTF
};
*/

#ifndef POV_IS1
	#define POV_IS1 ""
#endif

#ifndef POV_IS2
	#define POV_IS2 ""
#endif

#ifndef POV_IS3
	#define POV_IS3 ""
#endif

#ifndef POV_IS4
	#define POV_IS4 ""
#endif

POV_File_Extensions gPOV_File_Extensions[POV_File_Count] = // TODO FIXME - belongs in backend
{
	{{ "",      "",      "",      ""      }}, // POV_File_Unknown
	{{ ".tga",  ".TGA",  "",      ""      }}, // POV_File_Image_Targa
	{{ ".png",  ".PNG",  "",      ""      }}, // POV_File_Image_PNG
	{{ ".ppm",  ".PPM",  "",      ""      }}, // POV_File_Image_PPM
	{{ ".pgm",  ".PGM",  "",      ""      }}, // POV_File_Image_PGM
	{{ ".gif",  ".GIF",  "",      ""      }}, // POV_File_Image_GIF
	{{ ".iff",  ".IFF",  "",      ""      }}, // POV_File_Image_IFF
	{{ ".jpg",  ".JPG",  ".jpeg", ".JPEG" }}, // POV_File_Image_JPEG
	{{ ".tif",  ".TIF",  ".tiff", ".TIFF" }}, // POV_File_Image_TIFF
	{{ ".bmp",  ".BMP",  "",      ""      }}, // POV_File_Image_BMP
	{{ ".exr",  ".EXR",  "",      ""      }}, // POV_File_Image_EXR
	{{ ".hdr",  ".HDR",  "",      ""      }}, // POV_File_Image_HDR
	{{ POV_IS1, POV_IS2, POV_IS3, POV_IS4 }}, // POV_File_Image_System
	{{ ".pov",  ".POV",  "",      ""      }}, // POV_File_Text_POV
	{{ ".inc",  ".INC",  "",      ""      }}, // POV_File_Text_INC
	{{ ".ini",  ".INI",  "",      ""      }}, // POV_File_Text_INI
	{{ ".csv",  ".CSV",  "",      ""      }}, // POV_File_Text_CSV
	{{ ".txt",  ".TXT",  "",      ""      }}, // POV_File_Text_Stream
	{{ "",      "",      "",      ""      }}, // POV_File_Text_User
	{{ ".df3",  ".DF3",  "",      ""      }}, // POV_File_Data_DF3
	{{ ".rca",  ".RCA",  "",      ""      }}, // POV_File_Data_RCA
	{{ ".log",  ".LOG",  "",      ""      }}, // POV_File_Data_LOG
	{{ ".bak",  ".BAK",  "",      ""      }}, // POV_File_Data_Backup
	{{ ".ttf",  ".TTF",  "",      ""      }}  // POV_File_Font_TTF
};

const int gFile_Type_To_Mask [POV_File_Count] = // TODO FIXME - belongs in backend
{
	NO_FILE,   // POV_File_Unknown
	TGA_FILE,  // POV_File_Image_Targa
	PNG_FILE,  // POV_File_Image_PNG
	PPM_FILE,  // POV_File_Image_PPM
	PGM_FILE,  // POV_File_Image_PGM
	GIF_FILE,  // POV_File_Image_GIF
	IFF_FILE,  // POV_File_Image_IFF
	JPEG_FILE, // POV_File_Image_JPEG
	TIFF_FILE, // POV_File_Image_TIFF
	BMP_FILE,  // POV_File_Image_BMP
	EXR_FILE,  // POV_File_Image_EXR
	HDR_FILE,  // POV_File_Image_HDR
	SYS_FILE,  // POV_File_Image_System
	NO_FILE,   // POV_File_Text_POV
	NO_FILE,   // POV_File_Text_INC
	NO_FILE,   // POV_File_Text_INI
	NO_FILE,   // POV_File_Text_CSV
	NO_FILE,   // POV_File_Text_Stream
	NO_FILE,   // POV_File_Text_User
	NO_FILE,   // POV_File_Data_DF3
	NO_FILE,   // POV_File_Data_RCA
	NO_FILE,   // POV_File_Data_LOG
	NO_FILE,   // POV_File_Data_Backup
	NO_FILE    // POV_File_Font_TTF
};

int InferFileTypeFromExt(const UCS2String& ext) // TODO FIXME - belongs in backend
{
	// TODO FIXME - better compare in the string domain

	string str = UCS2toASCIIString(ext);

	for (int i = 0; i < POV_File_Count; i++)
	{
		// TODO - checking whether extension matches is required at other places, too
		for (int j = 0; j < POV_FILE_EXTENSIONS_PER_TYPE; j ++)
		{
			if ( (strlen(gPOV_File_Extensions[i].ext[j]) > 0) &&
			     (pov_stricmp (gPOV_File_Extensions[i].ext[j], str.c_str()) == 0) )
			{
				return (i);
			}
		}
	}
	return NO_FILE;
}

/*****************************************************************************
*
* FUNCTION
*
*   Locate_File
*
* INPUT
*
* OUTPUT
*
* RETURNS
*
* AUTHOR
*
*   POV-Ray Team
*
* DESCRIPTION
*
*   Find a file in the search path.
*
* CHANGES
*
*   Apr 1996: Don't add trailing FILENAME_SEPARATOR if we are immediately
*             following DRIVE_SEPARATOR because of Amiga probs.  [AED]
*
******************************************************************************/

IStream *Locate_File(Parser *p, shared_ptr<SceneData>& sd, const UCS2String& filename, unsigned int stype, UCS2String& buffer, bool err_flag)
{
	UCS2String fn(filename);
	UCS2String foundfile(sd->FindFile(p->GetPOVMSContext(), fn, stype));

	if(foundfile.empty() == true)
	{
		if(err_flag == true)
			p->PossibleError("Cannot find file '%s', even after trying to append file type extension.", UCS2toASCIIString(fn).c_str());

		return NULL;
	}

	if(fn.find('.') == UCS2String::npos)
	{
		// the passed-in filename didn't have an extension, but a file has been found,
		// which means one of the appended extensions worked. we need to work out which
		// one and append it to the original filename so we can store it in the cache
		// (since it's that name that the cache search routine looks for).
		UCS2String ext = GetFileExtension(Path(foundfile));
		if (ext.size() != 0)
			fn += ext;
	}

	// ReadFile will store both fn and foundfile in the cache for next time round
	IStream *result(sd->ReadFile(p->GetPOVMSContext(), fn, foundfile.c_str(), stype));

	if((result == NULL) && (err_flag == true))
		p->PossibleError("Cannot open file '%s'.", UCS2toASCIIString(foundfile).c_str());

	buffer = foundfile;

	return result;
}
/* TODO FIXME - code above should not be there, this is how it should work but this has bugs [trf]
IStream *Locate_File(Parser *p, shared_ptr<SceneData>& sd, const UCS2String& filename, unsigned int stype, UCS2String& buffer, bool err_flag)
{
	UCS2String foundfile(sd->FindFile(p->GetPOVMSContext(), filename, stype));

	if(foundfile.empty() == true)
	{
		if(err_flag == true)
			p->PossibleError("Cannot find file '%s', even after trying to append file type extension.", UCS2toASCIIString(filename).c_str());

		return NULL;
	}

	IStream *result(sd->ReadFile(p->GetPOVMSContext(), foundfile.c_str(), stype));

	if((result == NULL) && (err_flag == true))
		p->PossibleError("Cannot open file '%s'.", UCS2toASCIIString(foundfile).c_str());

	buffer = foundfile;

	return result;
}
*/
/*****************************************************************************
*
* FUNCTION
*
*   Internal_Font_File
*
* INPUT
*   The numeric identifier of a built-in file
*
* OUTPUT
*
* RETURNS
*   An IStream for usage by the TTF analyser
*
* AUTHOR
*
*   POV-Ray Team
*
* DESCRIPTION
*
*   Provide a compiled font as a Stream for reading
*
* CHANGES
*
*   Creation. Oct 2012 [JG]
*
******************************************************************************/

IMemStream *Internal_Font_File(const int font_id, UCS2String& buffer)
{
	return new IMemStream(font_id); 
}

}