X-Git-Url: https://git.distorted.org.uk/u/mdw/putty/blobdiff_plain/8269f2d3dbef7e3c97ede2407cae94bb31550cea..3f935d5bf975b26836da2fac5cd6e3df67a5e184:/mac/macstore.c diff --git a/mac/macstore.c b/mac/macstore.c index 29e9f365..548b7ac2 100644 --- a/mac/macstore.c +++ b/mac/macstore.c @@ -1,4 +1,4 @@ -/* $Id: macstore.c,v 1.12 2003/01/21 00:27:24 ben Exp $ */ +/* $Id: macstore.c,v 1.19 2003/04/01 18:10:25 simon Exp $ */ /* * macstore.c: Macintosh-specific impementation of the interface @@ -116,16 +116,20 @@ struct write_settings { FSSpec dstfile; }; -void *open_settings_w(char const *sessionname) { +void *open_settings_w(char const *sessionname, char **errmsg) { short sessVRefNum; long sessDirID; OSErr error; Str255 psessionname; FSSpec dstfile; - + + *errmsg = NULL; + error = get_session_dir(kCreateFolder, &sessVRefNum, &sessDirID); if (error != noErr) return NULL; + if (!sessionname || !*sessionname) + sessionname = "Default Settings"; c2pstrcpy(psessionname, sessionname); error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &dstfile); if (error == fnfErr) { @@ -147,7 +151,7 @@ void *open_settings_w_fsp(FSSpec *dstfile) OSErr error; Str255 tmpname; - ws = smalloc(sizeof *ws); + ws = snew(struct write_settings); ws->dstfile = *dstfile; /* Create a temporary file to save to first. */ @@ -183,6 +187,7 @@ void write_setting_s(void *handle, char const *key, char const *value) { Handle h; int id; OSErr error; + Str255 pkey; UseResFile(fd); if (ResError() != noErr) @@ -195,7 +200,8 @@ void write_setting_s(void *handle, char const *key, char const *value) { id = Unique1ID(FOUR_CHAR_CODE('TEXT')); if (ResError() != noErr) fatalbox("Failed to get ID for resource %s (%d)", key, ResError()); - addresource(h, FOUR_CHAR_CODE('TEXT'), id, key); + c2pstrcpy(pkey, key); + AddResource(h, FOUR_CHAR_CODE('TEXT'), id, pkey); if (ResError() != noErr) fatalbox("Failed to add resource %s (%d)", key, ResError()); } @@ -205,6 +211,7 @@ void write_setting_i(void *handle, char const *key, int value) { Handle h; int id; OSErr error; + Str255 pkey; UseResFile(fd); if (ResError() != noErr) @@ -219,7 +226,8 @@ void write_setting_i(void *handle, char const *key, int value) { id = Unique1ID(FOUR_CHAR_CODE('Int ')); if (ResError() != noErr) fatalbox("Failed to get ID for resource %s (%d)", key, ResError()); - addresource(h, FOUR_CHAR_CODE('Int '), id, key); + c2pstrcpy(pkey, key); + AddResource(h, FOUR_CHAR_CODE('Int '), id, pkey); if (ResError() != noErr) fatalbox("Failed to add resource %s (%d)", key, ResError()); } @@ -252,6 +260,8 @@ void *open_settings_r(char const *sessionname) error = get_session_dir(kDontCreateFolder, &sessVRefNum, &sessDirID); + if (!sessionname || !*sessionname) + sessionname = "Default Settings"; c2pstrcpy(psessionname, sessionname); error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &sessfile); if (error != noErr) goto out; @@ -270,7 +280,7 @@ void *open_settings_r_fsp(FSSpec *sessfile) fd = FSpOpenResFile(sessfile, fsRdPerm); if (fd == 0) {error = ResError(); goto out;} - handle = safemalloc(sizeof *handle); + handle = snew(int); *handle = fd; return handle; @@ -282,12 +292,14 @@ char *read_setting_s(void *handle, char const *key, char *buffer, int buflen) { int fd; Handle h; size_t len; + Str255 pkey; if (handle == NULL) goto out; fd = *(int *)handle; UseResFile(fd); if (ResError() != noErr) goto out; - h = get1namedresource(FOUR_CHAR_CODE('TEXT'), key); + c2pstrcpy(pkey, key); + h = Get1NamedResource(FOUR_CHAR_CODE('TEXT'), pkey); if (h == NULL) goto out; len = GetHandleSize(h); @@ -307,12 +319,14 @@ int read_setting_i(void *handle, char const *key, int defvalue) { int fd; Handle h; int value; + Str255 pkey; if (handle == NULL) goto out; fd = *(int *)handle; UseResFile(fd); if (ResError() != noErr) goto out; - h = get1namedresource(FOUR_CHAR_CODE('Int '), key); + c2pstrcpy(pkey, key); + h = Get1NamedResource(FOUR_CHAR_CODE('Int '), pkey); if (h == NULL) goto out; value = *(int *)*h; ReleaseResource(h); @@ -323,6 +337,135 @@ int read_setting_i(void *handle, char const *key, int defvalue) { return defvalue; } +int read_setting_fontspec(void *handle, const char *name, FontSpec *result) +{ + char *settingname; + FontSpec ret; + char tmp[256]; + + if (!read_setting_s(handle, name, tmp, sizeof(tmp))) + return 0; + c2pstrcpy(ret.name, tmp); + settingname = dupcat(name, "Face", NULL); + ret.face = read_setting_i(handle, settingname, 0); + sfree(settingname); + settingname = dupcat(name, "Height", NULL); + ret.size = read_setting_i(handle, settingname, 0); + sfree(settingname); + if (ret.size == 0) return 0; + *result = ret; + return 1; +} + +void write_setting_fontspec(void *handle, const char *name, FontSpec font) +{ + char *settingname; + char tmp[256]; + + p2cstrcpy(tmp, font.name); + write_setting_s(handle, name, tmp); + settingname = dupcat(name, "Face", NULL); + write_setting_i(handle, settingname, font.face); + sfree(settingname); + settingname = dupcat(name, "Size", NULL); + write_setting_i(handle, settingname, font.size); + sfree(settingname); +} + +int read_setting_filename(void *handle, const char *key, Filename *result) +{ + int fd; + AliasHandle h; + Boolean changed; + OSErr err; + Str255 pkey; + + if (handle == NULL) goto out; + fd = *(int *)handle; + UseResFile(fd); + if (ResError() != noErr) goto out; + c2pstrcpy(pkey, key); + h = (AliasHandle)Get1NamedResource(rAliasType, pkey); + if (h == NULL) goto out; + if ((*h)->userType == 'pTTY' && (*h)->aliasSize == sizeof(**h)) + memset(result, 0, sizeof(*result)); + else { + err = ResolveAlias(NULL, h, &result->fss, &changed); + if (err != noErr && err != fnfErr) goto out; + if ((*h)->userType == 'pTTY') { + long dirid; + StrFileName fname; + + /* Tail of record is pascal string contaning leafname */ + if (FSpGetDirID(&result->fss, &dirid, FALSE) != noErr) goto out; + memcpy(fname, (char *)*h + (*h)->aliasSize, + GetHandleSize((Handle)h) - (*h)->aliasSize); + err = FSMakeFSSpec(result->fss.vRefNum, dirid, fname, + &result->fss); + if (err != noErr && err != fnfErr) goto out; + } + } + ReleaseResource((Handle)h); + if (ResError() != noErr) goto out; + return 1; + + out: + return 0; +} + +void write_setting_filename(void *handle, const char *key, Filename fn) +{ + int fd = *(int *)handle; + AliasHandle h; + int id; + OSErr error; + Str255 pkey; + + UseResFile(fd); + if (ResError() != noErr) + fatalbox("Failed to open saved session (%d)", ResError()); + + if (filename_is_null(fn)) { + /* Generate a special "null" alias */ + h = (AliasHandle)NewHandle(sizeof(**h)); + if (h == NULL) + fatalbox("Failed to create fake alias"); + (*h)->userType = 'pTTY'; + (*h)->aliasSize = sizeof(**h); + } else { + error = NewAlias(NULL, &fn.fss, &h); + if (error == fnfErr) { + /* + * NewAlias can't create an alias for a nonexistent file. + * Create an alias for the directory, and record the + * filename as well. + */ + FSSpec tmpfss; + + FSMakeFSSpec(fn.fss.vRefNum, fn.fss.parID, NULL, &tmpfss); + error = NewAlias(NULL, &tmpfss, &h); + if (error != noErr) + fatalbox("Failed to create alias"); + (*h)->userType = 'pTTY'; + SetHandleSize((Handle)h, (*h)->aliasSize + fn.fss.name[0] + 1); + if (MemError() != noErr) + fatalbox("Failed to create alias"); + memcpy((char *)*h + (*h)->aliasSize, fn.fss.name, + fn.fss.name[0] + 1); + } + if (error != noErr) + fatalbox("Failed to create alias"); + } + /* Put the data in a resource. */ + id = Unique1ID(rAliasType); + if (ResError() != noErr) + fatalbox("Failed to get ID for resource %s (%d)", key, ResError()); + c2pstrcpy(pkey, key); + AddResource((Handle)h, rAliasType, id, pkey); + if (ResError() != noErr) + fatalbox("Failed to add resource %s (%d)", key, ResError()); +} + void close_settings_r(void *handle) { int fd; @@ -331,7 +474,7 @@ void close_settings_r(void *handle) { CloseResFile(fd); if (ResError() != noErr) fatalbox("Close of saved session failed (%d)", ResError()); - safefree(handle); + sfree(handle); } void del_settings(char const *sessionname) {