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
|
/* -*- mode: C++; tab-width: 4 -*- */
/* ================================================================================== */
/* Copyright (c) 1998-1999 3Com Corporation or its subsidiaries. All rights reserved. */
/* ================================================================================== */
#include "EmulatorCommon.h"
#include "EmMapFile.h"
#include "Platform_Files.h"
Bool
EmMapFile::Read(const FileReference& f, StringStringMap& mapData)
{
StringStringMap theData;
FILE* pFile = f.OpenAsFILE ("r"); // for read, fail on fnf
if (pFile == NULL)
return false;
string line;
int ch;
do {
ch = fgetc (pFile);
if (ch == '\n' || ch == EOF)
{
// Skip leading WS
while ((line[0] == ' ') || (line[0] == '\t'))
{
line.erase (0, 1);
}
// Skip comment and empty lines
if ((line[0] == ';' ) || (line[0] == '#' ) || (line[0] == '\0'))
{
line.erase ();
continue;
}
// Everything else is data
string::size_type p = line.find ('=');
if (p == string::npos)
{
line.erase ();
continue;
}
// Split up the line into key/value sections and
// add it to our hash table.
string key (line, 0, p);
string value (line, p + 1, line.size ());
theData[key] = value;
line.erase ();
}
else
{
line += ch;
}
}
while (ch != EOF);
fclose (pFile);
mapData = theData;
return true;
}
Bool
EmMapFile::Write(const FileReference& f, const StringStringMap& mapData)
{
FILE *pFile = f.OpenAsFILE("w");
if (pFile == NULL)
return false;
StringStringMap::const_iterator iter;
for (iter = mapData.begin(); iter != mapData.end(); ++iter)
{
fprintf (pFile, "%s=%s\n", iter->first.c_str(), iter->second.c_str());
}
fclose(pFile);
return true;
}
|