summaryrefslogtreecommitdiff
path: root/source/XIO.cpp
blob: 83c5bfddfb8069d48104084378b5f46bf28f94d8 (plain)
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
// =================================================================================================
// ADOBE SYSTEMS INCORPORATED
// Copyright 2010 Adobe Systems Incorporated
// All Rights Reserved
//
// NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it.
// =================================================================================================

#include "public/include/XMP_Environment.h"	// ! XMP_Environment.h must be the first included header.

#include "public/include/XMP_Const.h"
#include "public/include/XMP_IO.hpp"

#include "source/XIO.hpp"
#include "source/XMP_LibUtils.hpp"
#include "source/UnicodeConversions.hpp"

#if XMP_WinBuild
	#pragma warning ( disable : 4800 )	// forcing value to bool 'true' or 'false' (performance warning)
#endif

// =================================================================================================

static inline void MakeLowerCase ( std::string * str )
{
	for ( size_t i = 0, limit = str->size(); i < limit; ++i ) {
		char ch = (*str)[i];
		if ( ('A' <= ch) && (ch <= 'Z') ) (*str)[i] += 0x20;
	}
}

// =================================================================================================
// XIO::SplitLeafName
// ==================

void XIO::SplitLeafName ( std::string * path, std::string * leafName )
{
	size_t dirPos = path->size();
	// Return if path is empty or just the slash or DOT
	if ( dirPos == 0 || (dirPos == 1 &&
				     ((*path)[dirPos-1] == kDirChar
					      || (*path)[dirPos-1] == '.')
				     )
		     )
	{
		leafName->erase();
		path->erase();
		return;
	}

	// Remove trailing slashes
	--dirPos;
#if XMP_WinBuild
	if ( (*path)[dirPos] == '/' ) (*path)[dirPos] = kDirChar;	// Tolerate both '\' and '/'.
#endif
	if ( (*path)[dirPos] == kDirChar )
	{
		path->erase(dirPos);
	}

	// Search next slash
	for ( --dirPos; dirPos > 0; --dirPos ) {
		#if XMP_WinBuild
			if ( (*path)[dirPos] == '/' ) (*path)[dirPos] = kDirChar;	// Tolerate both '\' and '/'.
		#endif
		if ( (*path)[dirPos] == kDirChar ) break;
	}

	if ( (*path)[dirPos] == kDirChar ) {
		leafName->assign ( &(*path)[dirPos+1] );
		path->erase ( dirPos );
	} else if ( dirPos == 0 ) {
		leafName->erase();
		leafName->swap ( *path );
	}

}	// XIO::SplitLeafName

// =================================================================================================
// XIO::SplitFileExtension
// =======================
//
// ! Must only be called after using SplitLeafName!

void XIO::SplitFileExtension ( std::string * leafName, std::string * fileExt , bool lowercase )
{

	fileExt->erase();

	size_t extPos = leafName->size();
	if ( extPos == 0 ) return;
	
	for ( --extPos; extPos > 0; --extPos ) if ( (*leafName)[extPos] == '.' ) break;

	if ( (*leafName)[extPos] == '.' ) {
		fileExt->assign ( &((*leafName)[extPos+1]) );
		if (lowercase) MakeLowerCase ( fileExt );
		leafName->erase ( extPos );
	}

}	// XIO::SplitFileExtension

// =================================================================================================
// XIO::ReplaceTextFile
// ====================
//
// Replace the contents of a text file in a manner that preserves the meaning of the old content if
// a disk full error occurs. This can mean appended spaces appear in the old content.

void XIO::ReplaceTextFile ( XMP_IO* textFile, const std::string & newContent, bool doSafeUpdate )
{
	XMP_Int64 newContentSize = (XMP_Int64)newContent.size();
	XMP_Enforce ( newContentSize <= (XMP_Int64)0xFFFFFFFFULL );	// Make sure it fits in UInt32 for Write.

	if ( doSafeUpdate ) {
	
		// Safe updates are no problem, the old content is untouched if the temp file write fails.

		XMP_IO* tempFile = textFile->DeriveTemp();
		tempFile->Write ( newContent.data(), (XMP_Uns32)newContentSize );
		textFile->AbsorbTemp();

	} else {
	
		// We're overwriting the existing file. Make sure it is big enough, write the new content,
		// then truncate if necessary.
		
		XMP_Int64 oldContentSize = textFile->Length();
		
		if ( oldContentSize < newContentSize ) {
			size_t spaceCount = (size_t) (newContentSize - oldContentSize);	// AUDIT: newContentSize fits in UInt32.
			std::string spaces;
			spaces.assign ( spaceCount, ' ' );
			textFile->ToEOF();
			textFile->Write ( spaces.data(), (XMP_Uns32)spaceCount );
		}

		XMP_Assert ( newContentSize <= textFile->Length() );
		textFile->Rewind();
		textFile->Write ( newContent.data(), (XMP_Uns32)newContentSize );
		
		if ( oldContentSize > newContentSize ) textFile->Truncate ( newContentSize );
	
	}

}	// XIO::ReplaceTextFile

// =================================================================================================
// XIO::Copy
// =========

void XIO::Copy ( XMP_IO* sourceFile, XMP_IO* destFile, XMP_Int64 length,
				 XMP_AbortProc abortProc /* = 0 */, void* abortArg /* = 0 */ )
{
	const bool checkAbort = (abortProc != 0);
	XMP_Uns8 buffer [64*1024];

	while ( length > 0 ) {

		if ( checkAbort && abortProc(abortArg) ) {
			XMP_Throw ( "XIO::Copy, user abort", kXMPErr_UserAbort );
		}

		XMP_Int32 ioCount = sizeof(buffer);
		if ( length < ioCount ) ioCount = (XMP_Int32)length;

		sourceFile->Read ( buffer, ioCount, XMP_IO::kReadAll );
		destFile->Write ( buffer, ioCount );
		length -= ioCount;

	}

}	// XIO::Copy

// =================================================================================================
// XIO::Move
// =========

// allows to move data within a file (just pass in the same file handle as srcFile and dstFile)
// shadow effects (stumbling over just-written data) are avoided.
//
// * however can also be used to move data between two files *
// (having both option is handy for flexible use in update()/re-write() handler routines)

void XIO::Move ( XMP_IO* srcFile, XMP_Int64 srcOffset,
				 XMP_IO* dstFile, XMP_Int64 dstOffset,
				 XMP_Int64 length, XMP_AbortProc abortProc /* = 0 */, void * abortArg /* = 0 */ )
{
	enum { kBufferLen = 64*1024 };
	XMP_Uns8 buffer [kBufferLen];

	const bool checkAbort = (abortProc != 0);

	if ( srcOffset > dstOffset ) {	// avoiding shadow effects

	// move down -> shift lowest packet first !

		while ( length > 0 ) {

			if ( checkAbort && abortProc(abortArg) ) XMP_Throw ( "XIO::Move - User abort", kXMPErr_UserAbort );
			XMP_Int32 ioCount = kBufferLen;
			if ( length < kBufferLen ) ioCount = (XMP_Int32)length; //smartly avoids 32/64 bit issues

			srcFile->Seek ( srcOffset, kXMP_SeekFromStart );
			srcFile->ReadAll ( buffer, ioCount );
			dstFile->Seek ( dstOffset, kXMP_SeekFromStart );
			dstFile->Write ( buffer, ioCount );
			length -= ioCount;

			srcOffset += ioCount;
			dstOffset += ioCount;

		}

	} else {	// move up -> shift highest packet first

		srcOffset += length; //move to end
		dstOffset += length;

		while ( length > 0 ) {

			if ( checkAbort && abortProc(abortArg) ) XMP_Throw ( "XIO::Move - User abort", kXMPErr_UserAbort );
			XMP_Int32 ioCount = kBufferLen;
			if ( length < kBufferLen ) ioCount = (XMP_Int32)length; //smartly avoids 32/64 bit issues

			srcOffset -= ioCount;
			dstOffset -= ioCount;

			srcFile->Seek ( srcOffset, kXMP_SeekFromStart );
			srcFile->ReadAll ( buffer, ioCount );
			dstFile->Seek ( dstOffset, kXMP_SeekFromStart );
			dstFile->Write ( buffer, ioCount );
			length -= ioCount;

		}

	}

}	// XIO::Move

// =================================================================================================