#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "cfManager.h"
#ifdef NOTNOW
__private_extern__
CFArrayRef
configRead(const char *path)
{
int fd;
struct stat statBuf;
CFMutableDataRef data;
CFStringRef str;
CFArrayRef config = NULL;
fd = open(path, O_RDONLY, 0644);
if (fd < 0) {
goto done;
}
if (fstat(fd, &statBuf) < 0) {
goto done;
}
if ((statBuf.st_mode & S_IFMT) != S_IFREG) {
goto done;
}
data = CFDataCreateMutable(NULL, statBuf.st_size);
CFDataSetLength(data, statBuf.st_size);
if(read(fd, (void *)CFDataGetMutableBytePtr(data), statBuf.st_size) != statBuf.st_size) {
CFRelease(data);
goto done;
}
str = CFStringCreateFromExternalRepresentation(NULL, data, kCFStringEncodingMacRoman);
CFRelease(data);
config = CFStringCreateArrayBySeparatingStrings(NULL, str, CFSTR("\n"));
CFRelease(str);
done:
if (fd >= 0) {
close(fd);
}
if (config == NULL) {
CFMutableArrayRef emptyConfig;
emptyConfig = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
CFArrayAppendValue(emptyConfig, CFSTR(""));
config = (CFArrayRef)emptyConfig;
}
return config;
}
#endif
__private_extern__
void
configWrite(const char *path, CFArrayRef config)
{
CFStringRef str;
CFDataRef data;
int fd = -1;
int len;
str = CFStringCreateByCombiningStrings(NULL, config, CFSTR("\n"));
data = CFStringCreateExternalRepresentation(NULL, str, kCFStringEncodingMacRoman, '.');
CFRelease(str);
fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644);
if (fd < 0) {
goto done;
}
len = CFDataGetLength(data);
if (len) {
(void)write(fd, CFDataGetBytePtr(data), len);
}
done:
if (fd >= 0)
close(fd);
CFRelease(data);
return;
}
#ifdef NOTNOW
__private_extern__
void
configSet(CFMutableArrayRef config, CFStringRef key, CFStringRef value)
{
CFMutableStringRef pref;
CFIndex configLen = CFArrayGetCount(config);
CFIndex i;
CFIndex n;
CFMutableStringRef newValue;
CFRange range;
CFStringRef specialChars = CFSTR(" \"'$!|\\<>`{}[]");
boolean_t mustQuote = FALSE;
pref = CFStringCreateMutableCopy(NULL, 0, key);
CFStringAppend(pref, CFSTR("="));
newValue = CFStringCreateMutableCopy(NULL, 0, value);
n = CFStringGetLength(specialChars);
for (i = 0; i < n; i++) {
CFStringRef specialChar;
specialChar = CFStringCreateWithSubstring(NULL, specialChars, CFRangeMake(i, 1));
range = CFStringFind(newValue, specialChar, 0);
CFRelease(specialChar);
if (range.location != kCFNotFound) {
mustQuote = TRUE;
break;
}
}
if (mustQuote) {
CFStringRef charsToQuote = CFSTR("\\\"$`");
n = CFStringGetLength(charsToQuote);
for (i = 0; i < n; i++) {
CFStringRef quoteChar;
CFArrayRef matches;
quoteChar = CFStringCreateWithSubstring(NULL, charsToQuote, CFRangeMake(i, 1));
range = CFRangeMake(0, CFStringGetLength(newValue));
matches = CFStringCreateArrayWithFindResults(NULL,
newValue,
quoteChar,
range,
0);
CFRelease(quoteChar);
if (matches) {
CFIndex j = CFArrayGetCount(matches);
while (--j >= 0) {
const CFRange *range;
range = CFArrayGetValueAtIndex(matches, j);
CFStringInsert(newValue, range->location, CFSTR("\\"));
}
CFRelease(matches);
}
}
CFStringInsert(newValue, 0, CFSTR("\""));
CFStringAppend(newValue, CFSTR("\""));
}
for (i = 0; i < configLen; i++) {
if (CFStringHasPrefix(CFArrayGetValueAtIndex(config, i), pref)) {
break;
}
}
CFStringAppend(pref, newValue);
CFRelease(newValue);
if (i < configLen) {
CFArraySetValueAtIndex(config, i, pref);
} else {
CFArrayInsertValueAtIndex(config, configLen-1, pref);
}
CFRelease(pref);
return;
}
__private_extern__
void
configRemove(CFMutableArrayRef config, CFStringRef key)
{
CFMutableStringRef pref;
CFIndex configLen = CFArrayGetCount(config);
CFIndex i;
pref = CFStringCreateMutableCopy(NULL, 0, key);
CFStringAppend(pref, CFSTR("="));
for (i = 0; i < configLen; i++) {
if (CFStringHasPrefix(CFArrayGetValueAtIndex(config, i), pref)) {
CFArrayRemoveValueAtIndex(config, i);
break;
}
}
CFRelease(pref);
return;
}
#endif