Created new data types `Filename' and `FontSpec', intended to be
[u/mdw/putty] / mac / macstore.c
index af3503c..0ffff0b 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: macstore.c,v 1.7 2003/01/08 22:46:12 ben Exp $ */
+/* $Id: macstore.c,v 1.13 2003/02/01 12:54:40 simon Exp $ */
 
 /*
  * macstore.c: Macintosh-specific impementation of the interface
 #include <Resources.h>
 #include <TextUtils.h>
 
+#include <stdio.h>
 #include <string.h>
 
 #include "putty.h"
 #include "storage.h"
 #include "mac.h"
-
-#define PUTTY_CREATOR  FOUR_CHAR_CODE('pTTY')
-#define SESS_TYPE      FOUR_CHAR_CODE('Sess')
-#define SEED_TYPE      FOUR_CHAR_CODE('Seed')
+#include "macresid.h"
 
 
 OSErr FSpGetDirID(FSSpec *f, long *idp, Boolean makeit);
@@ -95,37 +93,69 @@ OSErr FSpGetDirID(FSSpec *f, long *idp, Boolean makeit) {
     return error;
 }
 
+/* Copy a resource into the current resource file */
+static OSErr copy_resource(ResType restype, short resid)
+{
+    Handle h;
+    Str255 resname;
+
+    h = GetResource(restype, resid);
+    if (h != NULL) {
+       GetResInfo(h, &resid, &restype, resname);
+       DetachResource(h);
+       AddResource(h, restype, resid, resname);
+       if (ResError() == noErr)
+           WriteResource(h);
+    }
+    return ResError();
+}
+
 struct write_settings {
     int fd;
     FSSpec tmpfile;
     FSSpec dstfile;
 };
 
-void *open_settings_w(char *sessionname) {
-    short sessVRefNum, tmpVRefNum;
-    long sessDirID, tmpDirID;
+void *open_settings_w(char const *sessionname) {
+    short sessVRefNum;
+    long sessDirID;
     OSErr error;
     Str255 psessionname;
-    struct write_settings *ws;
+    FSSpec dstfile;
     
-    ws = safemalloc(sizeof *ws);
     error = get_session_dir(kCreateFolder, &sessVRefNum, &sessDirID);
-    if (error != noErr) goto out;
+    if (error != noErr) return NULL;
 
     c2pstrcpy(psessionname, sessionname);
-    error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &ws->dstfile);
-    if (error != noErr && error != fnfErr) goto out;
+    error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &dstfile);
     if (error == fnfErr) {
-       FSpCreateResFile(&ws->dstfile, PUTTY_CREATOR, SESS_TYPE,
-                        smSystemScript);
-       if ((error = ResError()) != noErr) goto out;
-    }
+       FSpCreateResFile(&dstfile, PUTTY_CREATOR, SESS_TYPE, smSystemScript);
+       if ((error = ResError()) != noErr) return NULL;
+    } else if (error != noErr) return NULL;
+
+    return open_settings_w_fsp(&dstfile);
+}
+
+/*
+ * NB: Destination file must exist.
+ */
+void *open_settings_w_fsp(FSSpec *dstfile)
+{
+    short tmpVRefNum;
+    long tmpDirID;
+    struct write_settings *ws;
+    OSErr error;
+    Str255 tmpname;
+
+    ws = smalloc(sizeof *ws);
+    ws->dstfile = *dstfile;
 
     /* Create a temporary file to save to first. */
-    error = FindFolder(sessVRefNum, kTemporaryFolderType, kCreateFolder,
-                      &tmpVRefNum, &tmpDirID);
+    error = FindFolder(ws->dstfile.vRefNum, kTemporaryFolderType,
+                      kCreateFolder, &tmpVRefNum, &tmpDirID);
     if (error != noErr) goto out;
-    error = FSMakeFSSpec(tmpVRefNum, tmpDirID, psessionname, &ws->tmpfile);
+    c2pstrcpy(tmpname, tmpnam(NULL));
+    error = FSMakeFSSpec(tmpVRefNum, tmpDirID, tmpname, &ws->tmpfile);
     if (error != noErr && error != fnfErr) goto out;
     if (error == noErr) {
        error = FSpDelete(&ws->tmpfile);
@@ -137,6 +167,10 @@ void *open_settings_w(char *sessionname) {
     ws->fd = FSpOpenResFile(&ws->tmpfile, fsWrPerm);
     if (ws->fd == -1) {error = ResError(); goto out;}
 
+    /* Set up standard resources.  Doesn't matter if these fail. */
+    copy_resource('STR ', -16396);
+    copy_resource('TMPL', TMPL_Int);
+
     return ws;
 
   out:
@@ -144,7 +178,7 @@ void *open_settings_w(char *sessionname) {
     fatalbox("Failed to open session for write (%d)", error);
 }
 
-void write_setting_s(void *handle, char *key, char *value) {
+void write_setting_s(void *handle, char const *key, char const *value) {
     int fd = *(int *)handle;
     Handle h;
     int id;
@@ -166,7 +200,7 @@ void write_setting_s(void *handle, char *key, char *value) {
        fatalbox("Failed to add resource %s (%d)", key, ResError());
 }
 
-void write_setting_i(void *handle, char *key, int value) {
+void write_setting_i(void *handle, char const *key, int value) {
     int fd = *(int *)handle;
     Handle h;
     int id;
@@ -208,7 +242,7 @@ void close_settings_w(void *handle) {
     safefree(handle);
 }
 
-void *open_settings_r(char *sessionname)
+void *open_settings_r(char const *sessionname)
 {
     short sessVRefNum;
     long sessDirID;
@@ -244,7 +278,7 @@ void *open_settings_r_fsp(FSSpec *sessfile)
     return NULL;
 }
 
-char *read_setting_s(void *handle, char *key, char *buffer, int buflen) {
+char *read_setting_s(void *handle, char const *key, char *buffer, int buflen) {
     int fd;
     Handle h;
     size_t len;
@@ -269,7 +303,7 @@ char *read_setting_s(void *handle, char *key, char *buffer, int buflen) {
     return NULL;
 }
 
-int read_setting_i(void *handle, char *key, int defvalue) {
+int read_setting_i(void *handle, char const *key, int defvalue) {
     int fd;
     Handle h;
     int value;
@@ -289,6 +323,49 @@ int read_setting_i(void *handle, char *key, int defvalue) {
     return defvalue;
 }
 
+int read_setting_fontspec(void *handle, const char *name, FontSpec *result)
+{
+    char *settingname;
+    FontSpec ret;
+
+    if (!read_setting_s(handle, name, ret.name, sizeof(ret.name)))
+       return 0;
+    settingname = dupcat(name, "IsBold", NULL);
+    ret.isbold = read_setting_i(handle, settingname, -1);
+    sfree(settingname);
+    if (ret.isbold == -1) return 0;
+    if (ret.charset == -1) return 0;
+    settingname = dupcat(name, "Height", NULL);
+    ret.height = read_setting_i(handle, settingname, INT_MIN);
+    sfree(settingname);
+    if (ret.height == INT_MIN) return 0;
+    *result = ret;
+    return 1;
+}
+
+void write_setting_fontspec(void *handle, const char *name, FontSpec font)
+{
+    char *settingname;
+
+    write_setting_s(handle, name, font.name);
+    settingname = dupcat(name, "IsBold", NULL);
+    write_setting_i(handle, settingname, font.isbold);
+    sfree(settingname);
+    settingname = dupcat(name, "Height", NULL);
+    write_setting_i(handle, settingname, font.height);
+    sfree(settingname);
+}
+
+int read_setting_filename(void *handle, const char *name, Filename *result)
+{
+    return !!read_setting_s(handle, name, result->path, sizeof(result->path));
+}
+
+void write_setting_filename(void *handle, const char *name, Filename result)
+{
+    write_setting_s(handle, name, result.path);
+}
+
 void close_settings_r(void *handle) {
     int fd;
 
@@ -300,7 +377,7 @@ void close_settings_r(void *handle) {
     safefree(handle);
 }
 
-void del_settings(char *sessionname) {
+void del_settings(char const *sessionname) {
     OSErr error;
     FSSpec sessfile;
     short sessVRefNum;
@@ -390,12 +467,16 @@ void read_random_seed(noise_consumer_t consumer)
     FSClose(refnum);
 }
 
+/*
+ * We don't bother with the usual FSpExchangeFiles dance here because
+ * it doesn't really matter if the old random seed gets lost.
+ */
 void write_random_seed(void *data, int len)
 {
-    short puttyVRefNum, tmpVRefNum;
-    long puttyDirID, tmpDirID;
+    short puttyVRefNum;
+    long puttyDirID;
     OSErr error;
-    FSSpec dstfile, tmpfile;
+    FSSpec dstfile;
     short refnum;
     long count = len;
 
@@ -404,36 +485,21 @@ void write_random_seed(void *data, int len)
 
     error = FSMakeFSSpec(puttyVRefNum, puttyDirID, "\pPuTTY Random Seed",
                         &dstfile);
-    if (error != noErr && error != fnfErr) return;
-
-    /* Create a temporary file to save to first. */
-    error = FindFolder(puttyVRefNum, kTemporaryFolderType, kCreateFolder,
-                      &tmpVRefNum, &tmpDirID);
-    if (error != noErr) return;
-    error = FSMakeFSSpec(tmpVRefNum, tmpDirID, "\pPuTTY Random Seed",
-                        &tmpfile);
-    if (error != noErr && error != fnfErr) return;
-    if (error == noErr) {
-       error = FSpDelete(&tmpfile);
-       if (error != noErr) return;
-    }
-    error = FSpCreate(&tmpfile, PUTTY_CREATOR, SEED_TYPE, smRoman);
-    if (error != noErr) return;
-
-    if (FSpOpenDF(&tmpfile, fsWrPerm, &refnum) != noErr) goto fail;
-
-    if (FSWrite(refnum, &count, data) != noErr) goto fail2;
-    if (FSClose(refnum) != noErr) goto fail;
-
-    if (FSpExchangeFiles(&tmpfile, &dstfile) != noErr) goto fail;
-    if (FSpDelete(&tmpfile) != noErr) return;
+    if (error == fnfErr) {
+       /* Set up standard resources */
+       FSpCreateResFile(&dstfile, INTERNAL_CREATOR, SEED_TYPE, smRoman);
+       refnum = FSpOpenResFile(&dstfile, fsWrPerm);
+       if (ResError() == noErr) {
+           copy_resource('STR ', -16397);
+           CloseResFile(refnum);
+       }
+    } else if (error != noErr) return;
+
+    if (FSpOpenDF(&dstfile, fsWrPerm, &refnum) != noErr) return;
+    FSWrite(refnum, &count, data);
+    FSClose(refnum);
 
     return;
-    
-  fail2:
-    FSClose(refnum);
-  fail:
-    FSpDelete(&tmpfile);
 }
 
 /*