Created new data types `Filename' and `FontSpec', intended to be
[u/mdw/putty] / winstore.c
index 651fbad..a69d58d 100644 (file)
@@ -6,6 +6,7 @@
 #include <windows.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <limits.h>
 #include "putty.h"
 #include "storage.h"
 
@@ -13,9 +14,9 @@ static const char *const puttystr = PUTTY_REG_POS "\\Sessions";
 
 static char seedpath[2 * MAX_PATH + 10] = "\0";
 
-static char hex[16] = "0123456789ABCDEF";
+static const char hex[16] = "0123456789ABCDEF";
 
-static void mungestr(char *in, char *out)
+static void mungestr(const char *in, char *out)
 {
     int candot = 0;
 
@@ -35,7 +36,7 @@ static void mungestr(char *in, char *out)
     return;
 }
 
-static void unmungestr(char *in, char *out, int outlen)
+static void unmungestr(const char *in, char *out, int outlen)
 {
     while (*in) {
        if (*in == '%' && in[1] && in[2]) {
@@ -60,7 +61,7 @@ static void unmungestr(char *in, char *out, int outlen)
     return;
 }
 
-void *open_settings_w(char *sessionname)
+void *open_settings_w(const char *sessionname)
 {
     HKEY subkey1, sesskey;
     int ret;
@@ -82,14 +83,14 @@ void *open_settings_w(char *sessionname)
     return (void *) sesskey;
 }
 
-void write_setting_s(void *handle, char *key, char *value)
+void write_setting_s(void *handle, const char *key, const char *value)
 {
     if (handle)
        RegSetValueEx((HKEY) handle, key, 0, REG_SZ, value,
                      1 + strlen(value));
 }
 
-void write_setting_i(void *handle, char *key, int value)
+void write_setting_i(void *handle, const char *key, int value)
 {
     if (handle)
        RegSetValueEx((HKEY) handle, key, 0, REG_DWORD,
@@ -101,7 +102,7 @@ void close_settings_w(void *handle)
     RegCloseKey((HKEY) handle);
 }
 
-void *open_settings_r(char *sessionname)
+void *open_settings_r(const char *sessionname)
 {
     HKEY subkey1, sesskey;
     char *p;
@@ -123,7 +124,7 @@ void *open_settings_r(char *sessionname)
     return (void *) sesskey;
 }
 
-char *read_setting_s(void *handle, char *key, char *buffer, int buflen)
+char *read_setting_s(void *handle, const char *key, char *buffer, int buflen)
 {
     DWORD type, size;
     size = buflen;
@@ -136,7 +137,7 @@ char *read_setting_s(void *handle, char *key, char *buffer, int buflen)
        return buffer;
 }
 
-int read_setting_i(void *handle, char *key, int defvalue)
+int read_setting_i(void *handle, const char *key, int defvalue)
 {
     DWORD type, val, size;
     size = sizeof(val);
@@ -150,12 +151,73 @@ int read_setting_i(void *handle, char *key, int defvalue)
        return val;
 }
 
+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;
+    settingname = dupcat(name, "CharSet", NULL);
+    ret.charset = read_setting_i(handle, settingname, -1);
+    sfree(settingname);
+    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;
+    if (ret.height < 0) {
+       int oldh, newh;
+       HDC hdc = GetDC(NULL);
+       int logpix = GetDeviceCaps(hdc, LOGPIXELSY);
+       ReleaseDC(NULL, hdc);
+
+       oldh = -ret.height;
+       newh = MulDiv(oldh, 72, logpix) + 1;
+       if (MulDiv(newh, logpix, 72) > oldh)
+           newh--;
+       ret.height = newh;
+    }
+    *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, "CharSet", NULL);
+    write_setting_i(handle, settingname, font.charset);
+    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)
 {
     RegCloseKey((HKEY) handle);
 }
 
-void del_settings(char *sessionname)
+void del_settings(const char *sessionname)
 {
     HKEY subkey1;
     char *p;
@@ -181,7 +243,7 @@ void *enum_settings_start(void)
     struct enumsettings *ret;
     HKEY key;
 
-    if (RegCreateKey(HKEY_CURRENT_USER, puttystr, &key) != ERROR_SUCCESS)
+    if (RegOpenKey(HKEY_CURRENT_USER, puttystr, &key) != ERROR_SUCCESS)
        return NULL;
 
     ret = smalloc(sizeof(*ret));
@@ -198,14 +260,14 @@ char *enum_settings_next(void *handle, char *buffer, int buflen)
     struct enumsettings *e = (struct enumsettings *) handle;
     char *otherbuf;
     otherbuf = smalloc(3 * buflen);
-    if (otherbuf && RegEnumKey(e->key, e->i++, otherbuf,
-                              3 * buflen) == ERROR_SUCCESS) {
+    if (RegEnumKey(e->key, e->i++, otherbuf, 3 * buflen) == ERROR_SUCCESS) {
        unmungestr(otherbuf, buffer, buflen);
        sfree(otherbuf);
        return buffer;
-    } else
+    } else {
+       sfree(otherbuf);
        return NULL;
-
+    }
 }
 
 void enum_settings_finish(void *handle)
@@ -215,8 +277,8 @@ void enum_settings_finish(void *handle)
     sfree(e);
 }
 
-static void hostkey_regname(char *buffer, char *hostname,
-                           int port, char *keytype)
+static void hostkey_regname(char *buffer, const char *hostname,
+                           int port, const char *keytype)
 {
     int len;
     strcpy(buffer, keytype);
@@ -226,7 +288,8 @@ static void hostkey_regname(char *buffer, char *hostname,
     mungestr(hostname, buffer + strlen(buffer));
 }
 
-int verify_host_key(char *hostname, int port, char *keytype, char *key)
+int verify_host_key(const char *hostname, int port,
+                   const char *keytype, const char *key)
 {
     char *otherstr, *regname;
     int len;
@@ -246,8 +309,8 @@ int verify_host_key(char *hostname, int port, char *keytype, char *key)
 
     hostkey_regname(regname, hostname, port, keytype);
 
-    if (RegCreateKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
-                    &rkey) != ERROR_SUCCESS)
+    if (RegOpenKey(HKEY_CURRENT_USER, PUTTY_REG_POS "\\SshHostKeys",
+                  &rkey) != ERROR_SUCCESS)
        return 1;                      /* key does not exist in registry */
 
     readlen = len;
@@ -331,7 +394,8 @@ int verify_host_key(char *hostname, int port, char *keytype, char *key)
        return 0;                      /* key matched OK in registry */
 }
 
-void store_host_key(char *hostname, int port, char *keytype, char *key)
+void store_host_key(const char *hostname, int port,
+                   const char *keytype, const char *key)
 {
     char *regname;
     HKEY rkey;