Properties.C 4.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

 /***************************************************************************/

/*
 * Portions Copyright (c) 1999 GMRS Software GmbH
 * Carl-von-Linde-Str. 38, D-85716 Unterschleissheim, http://www.gmrs.de
 * All rights reserved.
 *
 * Author: Arno Unkrig <arno@unkrig.de>
 */
 
/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License in the file COPYING for more details.
 */

 /***************************************************************************/
 
/*
 * Changes to version 1.2.2 were made by Martin Bayer <mbayer@zedat.fu-berlin.de>
 * Dates and reasons of modifications:
 * Fre Jun  8 17:24:35 CEST 2001: new method
 * Wed Jul  2 22:02:51 CEST 2003: ported to g++ 3.3
 */

 /***************************************************************************/


#include <ctype.h>
#include <iostream>

#include "Properties.h"

/* ------------------------------------------------------------------------- */

const char *
Properties::getProperty(const char *key, const char *dflt) const
{
  map<string, string>::const_iterator i;
  i = property_map.find(key);
  return i == property_map.end() ? dflt : (*i).second.c_str();
}

// neue Methode fuer leere Attribute - Johannes Geiger

const char *
Properties::getProperty(const char *key) const
{
  map<string, string>::const_iterator i;
  i = property_map.find(key);
  return i == property_map.end() ? NULL : (*i).second.c_str();
}

/* ------------------------------------------------------------------------- */

void
Properties::load(istream &is)
{
  string key, value;
  while (readProperty(is, &key, &value)) setProperty(key, value);
}

/* ------------------------------------------------------------------------- */

/*
 * Expand the escape sequence at "line[pos]". Backslash-Newline reads another
 * line from "is".
 */
static void
expandEscape(string *line_in_out, string::size_type *pos_in_out, istream &is)
{
  for (;;) {
    if (line_in_out->at(*pos_in_out) != '\\') { ++*pos_in_out; return; }
    if (*pos_in_out != line_in_out->size() - 1) break;

    string tmp;
    if (!getline(is, tmp)) {++*pos_in_out; return; }
    int j;
    for (j = 0; j < tmp.size() && isspace(tmp[j]); ++j);
    line_in_out->replace(
      *pos_in_out, string::npos,
      tmp, j, string::npos
    );
  }

  char c = line_in_out->at(*pos_in_out + 1);
  switch (c) {
  case 't': c = '\t'; break;
  case 'n': c = '\n'; break;
  case 'r': c = '\r'; break;
  }
  line_in_out->replace(*pos_in_out, 2, &c, 1);
  ++*pos_in_out;
}

/*static*/ bool
Properties::readProperty(istream &is, string *key_return, string *value_return)
{
  string            line;
  string::size_type i, l;

  // Skip empty and comment lines.
  for (;;) {
    if (!getline(is, line)) return false;
    l = line.size();

    // Skip leading white-space.
    for (i = 0; i < l && isspace(line[i]); ++i);
    if (i == l) continue;                // Line contains only white-space.

    // Ignore comment lines.
    if (line[i] == '#' || line[i] == '!') continue; // Comment line.

    break;
  }

  // Parse key.
  string::size_type bok = i;
  while (i < line.size()) {
    char c = line[i];
    if (isspace(c) || c == '=' || c == ':') break;
    expandEscape(&line, &i, is);
  }
  string::size_type eok = i;

  // Skip key terminator.
  while (i < l && isspace(line[i])) ++i;
  if (i < l && (line[i] == '=' || line[i] == ':')) {
    for (++i; i < l && isspace(line[i]); ++i);
  }

  // Substitute escape sequences in value.
  string::size_type bov = i;
  while (i < line.size()) expandEscape(&line, &i, is);

  // Return key and value.
  key_return->assign(line, bok, eok - bok);
  value_return->assign(line, bov, string::npos);
  return true;
}

/* ------------------------------------------------------------------------- */