-
Notifications
You must be signed in to change notification settings - Fork 0
/
QCArray.cpp
79 lines (65 loc) · 1.85 KB
/
QCArray.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
76
77
78
79
/*
* QCArray.cpp
* CFRaii
*
* Copyright (c) 2009-2010 Richard A. Brown
*
* See license.txt for licensing terms and conditions.
*/
#include "QCArray.h"
#include "QCURL.h"
#include "QCString.h"
BEGIN_QC_NAMESPACE
void QCArray1::show() const
{
#ifndef NDEBUG
CFShow(Array());
#endif
}
bool QCArray1::writeToFile(QCString const &filePath, CFPropertyListFormat const format = kCFPropertyListXMLFormat_v1_0) const
{
QCURL fileURL(filePath, 0); // is not directory
CFWriteStreamRef writeStream(CFWriteStreamCreateWithFile(kCFAllocatorDefault, fileURL));
bool result(CFWriteStreamOpen(writeStream) != 0); // convert from Boolean
if (result)
{
// don't care about the error string
result = CFPropertyListWriteToStream(array, writeStream, format, NULL) != 0;
CFWriteStreamClose(writeStream); // we're not ignoring anything -- it returns void
}
Release(writeStream);
return result;
}
// static method
QCArray1 QCArray1::arrayFromFile(QCString const &filePath)
{
QCURL fileURL(filePath, false);
CFPropertyListRef plist(0);
CFStringRef errStr(0);
CFPropertyListFormat plistFormat;
CFReadStreamRef readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault, fileURL);
if (CFReadStreamOpen(readStream) == 0)
{
// couldn't read file; return an empty array
CFRelease(readStream);
return QCArray1();
}
plist = CFPropertyListCreateFromStream(kCFAllocatorDefault
, readStream
, 0 // full file
, kCFPropertyListMutableContainersAndLeaves
, &plistFormat
, &errStr);
CFReadStreamClose(readStream);
CFRelease(readStream);
if (errStr != 0
// if the root of the plist is not an array,
// then we want nothing to do with it.
|| CFGetTypeID(plist) != CFArrayGetTypeID())
{
Release(plist);
return QCArray1();
}
return QCArray1(static_cast<CFArrayRef> ( plist ));
}
END_QC_NAMESPACE