view anagram/guisupport/readprofile.cpp @ 21:1c9dac05d040

Add lint-style FALLTHROUGH annotations to fallthrough cases. (in the parse engine and thus the output code) Document this, because the old output causes warnings with gcc10.
author David A. Holland
date Mon, 13 Jun 2022 00:04:38 -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 */