1/*
2
3The following two functions let you convert
4between a color and an ASCII string suitable
5for writing to defaults or storing in text documents.
6
7Call convertColorToString() with a color and a
8string at least COLORSTRINGLENGTH long. Later
9call getColorFromString() with a string obtained
10from convertColorToString() and a pointer to a
11color; if the function returns YES a color was
12successfully parsed from the string.
13
14 void convertColorToString (NXColor color, char *str)
15 BOOL getColorFromString (const char *str, NXColor *color)
16
17The following two functions let you read/write colors
18in the defaults database of the application.
19
20 void writeColorToDefaults (NXColor color, const char *defaultName)
21 BOOL readColorFromDefaults (const char *defaultName, NXColor *color)
22
23Written by Ali Ozer, 5/29/92.
24
25You may freely copy, distribute and reuse the code in this example.
26NeXT disclaims any warranty of any kind, expressed or implied,
27as to its fitness for any particular use.
28
29*/
30
31#import <AppKit/AppKit.h>
32
33#import "colorAsAscii.h"
34
35void writeColorToDefaults (NSColor * color, const char *defaultName)
36{
37    char str[1024];
38    convertColorToString(color, str);
39#warning DefaultsConversion: [<NSUserDefaults> setObject:...forKey:...] used to be NXWriteDefault([[[NSProcessInfo processInfo] processName] cString], defaultName, str). Defaults will be synchronized within 30 seconds after this change.  For immediate synchronization, call '-synchronize'. Also note that the first argument of NXWriteDefault is now ignored; to write into a domain other than the apps default, see the NSUserDefaults API.
40    [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithCString:str] forKey:[NSString stringWithCString:defaultName]];
41}
42
43BOOL readColorFromDefaults (const char *defaultName, NSColor *color)
44{
45    const char *tmp = [[[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithCString:defaultName]] cString];
46    return (tmp && getColorFromString (tmp, color)) ? YES : NO;
47}
48
49void convertColorToString (NSColor * color, char *str)
50{
51    static const char hexDigits[16] = "0123456789ABCDEF";
52    NSData *colorStream = [[NSMutableData alloc] init];
53#error ArchiverConversion: '[[NSUnarchiver alloc] init...]' used to be 'NXOpenTypedStream(...)'; colorStream should be converted to 'NSMutableData *'.
54    NSArchiver *ts = colorStream ? [[NSArchiver alloc] initForWritingWithMutableData:colorStream] : NULL;
55
56    if (ts) {
57	int i, pos;
58	[ts encodeObject:color];
59#warning ArchiverConvesion: [ts release] was NXCloseTypedStream(ts); ts has been converted to an NSArchiver instance (was NXTypedStream); if ts was opened with NXOpenTypedStreamForFile(<filename>, NX_WRITEONLY) contents of ts must be explicitly written to <filename>; a warning will have appeared if this is the case
60	[ts release];
61#error StreamConversion: NXTell should be converted to an NSData method
62	pos = NXTell(colorStream);
63#error StreamConversion: NXSeek should be converted to an NSData method
64    	NXSeek(colorStream, 0, NX_FROMSTART);
65	i = 0;
66	while (i++ < pos) {
67#error StreamConversion: NXGetc should be converted to an NSData method
68	    unsigned char ch = NXGetc(colorStream);
69	    *str++ = hexDigits[(ch>>4) & 0xF];
70	    *str++ = hexDigits[ch & 0xF];
71        }
72    }
73    *str = 0;
74    if (colorStream) [colorStream release];
75}
76
77#define BAD 255
78#define HEX(c) (((c)>='A' && (c)<='F') ? ((c)-'A'+10): (((c)>='0'&&(c)<='9') ? ((c)-'0') : BAD))
79
80BOOL getColorFromString (const char *str, NSColor *color)
81{
82    unsigned char binaryBuffer[COLORSTRINGLENGTH];
83    NSData *stream;
84    NSArchiver *ts;
85    int len = 0;
86    BOOL success = NO;
87
88    while (*str) {
89	unsigned char first = HEX(str[0]), second = HEX(str[1]);
90	if (first == BAD || second == BAD) return NO;
91	binaryBuffer[len] = (first << 4) + second;
92	str += 2;
93	len++;
94    }
95
96    if (len &&
97	(stream = [[NSData alloc] initWithBytes:binaryBuffer length:len]) &&
98#error ArchiverConversion: '[[NSUnarchiver alloc] init...]' used to be 'NXOpenTypedStream(...)'; stream should be converted to 'NSData *'.
99    	(ts = [[NSUnarchiver alloc] initForReadingWithData:stream])) {
100	NS_DURING
101#warning ColorConversion: [[ts decodeNXColor] retain] used to be NXReadColor(ts).  Use 'decodeNXColor' to read old style colors, but use 'decodeObject' to read OpenStep colors.
102	    *color = [[ts decodeNXColor] retain];
103	    success = YES;
104	NS_HANDLER
105	NS_ENDHANDLER
106    }
107#warning ArchiverConvesion: [ts release] was NXCloseTypedStream(ts); ts has been converted to an NSArchiver instance (was NXTypedStream); if ts was opened with NXOpenTypedStreamForFile(<filename>, NX_WRITEONLY) contents of ts must be explicitly written to <filename>; a warning will have appeared if this is the case
108    if (ts) [ts release];
109#error StreamConversion: NXCloseMemory should be converted to an NSData method
110    if (stream) NXCloseMemory(stream, NX_SAVEBUFFER);
111
112    return success;
113}
114
115