-
Notifications
You must be signed in to change notification settings - Fork 0
/
QCData.cpp
75 lines (60 loc) · 1.5 KB
/
QCData.cpp
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
/*
* QCData.cpp
* CFRaii
*
* Copyright (c) 2009 Richard A. Brown
*
* See license.txt for licensing terms and conditions.
*/
#include "QCData.h"
#define kBufferSize (4096)
BEGIN_QC_NAMESPACE
void QCData::show() const
{
#ifndef NDEBUG
CFShow(data);
#endif
}
bool QCData::writeToFile(CFStringRef const filePath) const
{
QCURL fileURL(filePath, 0); // is not directory
CFWriteStreamRef writeStream(CFWriteStreamCreateWithFile(kCFAllocatorDefault, fileURL));
bool result(CFWriteStreamOpen(writeStream) != 0); // convert from Boolean
if (result)
{
CFIndex bytesWritten( CFWriteStreamWrite(writeStream, GetBytePtr(), length()) );
result = bytesWritten == length();
}
CFWriteStreamClose(writeStream);
Release(writeStream);
return result;
}
// static method
QCData QCData::dataFromFile(CFStringRef const filePath)
{
QCURL fileURL(filePath, false);
return dataFromFile(fileURL);
}
// static method
QCData QCData::dataFromFile(CFURLRef const fileURL)
{
CFReadStreamRef readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault, fileURL);
if (CFReadStreamOpen(readStream) == 0) // treat Boolean
{
// couldn't read file; return empty data
CFRelease(readStream);
return QCData();
}
UInt8 bytes[kBufferSize];
QCData fileData;
CFIndex bytesRead(0);
while ( (bytesRead = CFReadStreamRead(readStream, bytes, kBufferSize)) > 0 )
{
fileData.AppendBytes(bytes, bytesRead);
}
CFReadStreamClose(readStream);
Release(readStream);
return fileData;
}
END_QC_NAMESPACE
#undef kBufferSize