view anagram/guisupport/readprofile.cpp @ 15:f5acaf0c8a29

Don't cast through "volatile int". Causes a gcc warning nowadays. XXX: should put something else back here to frighten the optimizer
author David A. Holland
date Tue, 31 May 2022 01:00:55 -0400
parents 13d2b8934445
children
line wrap: on
line source

/*
 * 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 */