Mercurial > ~dholland > hg > ag > index.cgi
diff anagram/guisupport/readprofile.cpp @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/anagram/guisupport/readprofile.cpp Sat Dec 22 17:52:45 2007 -0500 @@ -0,0 +1,147 @@ +/* + * AnaGram, A System for Syntax Directed Programming + * Copyright 1997-2002 Parsifal Software. All Rights Reserved. + * Copyright 2006 David A. Holland. All Rights Reserved. + * See the file COPYING for license and usage terms. + * + * readprofile.cpp + */ + +#include <stdio.h> +#include <stdlib.h> +#include <limits.h> + +#ifdef __IBMCPP__ +#include <iprofile.hpp> +#endif + +#include "port.h" + +#include "agstring.h" +#include "assert.h" +#include "readprofile.h" +#include "textfile.h" + +//#define INCLUDE_LOGGING +#include "log.h" + + +//////////////////////////////////////////////////////////// + +#ifdef AG_ON_WINDOWS + +/* + * Store and retrieve the profile from the Windows registry. + */ + +#ifdef __IBMCPP__ + +class AgProfile : public IProfile { +public: + AgProfile(); + void store(const char *key, AgString value); + void retrieve(const char *key, AgString &result); +}; + +AgProfile::AgProfile() + : IProfile("Parsifal Software") +{ + setDefaultApplicationName("AnaGram"); +} + +void AgProfile::store(const char *key, AgString value) { + addOrReplaceElementWithKey(key, IString(value.pointer())); +} + +void AgProfile::retrieve(const char *key, AgString &result) { + try { result = (char *) elementWithKey(key); } + catch(...) {} +} + +void AgStoreProfile(const char *key, AgString value) { + LOGSECTION("AgStoreProfile"); + LOGV(key); + + AgProfile p; + LOGS("profile object created"); + + p.store(key, value); + LOGS("store completed"); +} + +AgString AgFetchProfile(const char *key) { + LOGSECTION("AgFetchProfile"); + LOGV(key); + + AgProfile p; + LOGS("profile object created"); + + AgString result; + p.retrieve(key, result); + LOGS("fetch completed"); + + LOGV(result); + return result; +} + +#endif /* __IBMCPP__ */ + +#endif /* AG_ON_WINDOWS */ + +//////////////////////////////////////////////////////////// + +#ifdef AG_ON_UNIX + +/* + * Store and retrieve the profile from ~/.AnaGram. + */ + +static void getdotfilepath(char *buf, size_t len) { + const char *home = getenv("HOME"); + if (!home) { + // use root directory + home = ""; + } + snprintf(buf, len, "%s/.AnaGram", home); +} + +void AgStoreProfile(const char *key, AgString value) { + LOGSECTION("AgStoreProfile"); + LOGV(key); + + assert(!strcmp(key, "initializationData")); + + char path[PATH_MAX]; + getdotfilepath(path, sizeof(path)); + LOGV(path); + + FILE *f = fopen(path, "w"); + if (!f) { + // give up + LOGS("fopen failed"); + return; + } + + fputs(value.pointer(), f); + fclose(f); + LOGS("complete"); +} + +AgString AgFetchProfile(const char *key) { + LOGSECTION("AgFetchProfile"); + LOGV(key); + + assert(!strcmp(key, "initializationData")); + + char path[PATH_MAX]; + getdotfilepath(path, sizeof(path)); + LOGV(path); + + text_file tf(path); + AgString result = tf.text; + + LOGV(result); + return result; +} + +#endif /* AG_ON_UNIX */