aboutsummaryrefslogtreecommitdiff
path: root/SrcShared/EmMapFile.cpp
blob: 185b8ada650e30ecb42cb00512185669f7e33dec (plain)
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
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
	Copyright (c) 1999-2001 Palm, Inc. or its subsidiaries.
	All rights reserved.

	This file is part of the Palm OS Emulator.

	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.
\* ===================================================================== */

#include "EmCommon.h"
#include "EmMapFile.h"

#include "ChunkFile.h"			// Chunk, EmStreamChunk
#include "EmFileRef.h"			// EmFileRef
#include "EmStreamFile.h"		// EmStreamFile
#include "Miscellaneous.h"		// Strip

Bool
EmMapFile::Read (const EmFileRef& f, StringStringMap& mapData)
{
	try
	{
		EmStreamFile	fileStream (f, kOpenExistingForRead | kOpenText);	// Will throw if fnf
		return Read (fileStream, mapData);
	}
	catch (...)
	{
		return false;
	}

	return true;
}

Bool
EmMapFile::Read (EmStream& stream, StringStringMap& mapData)
{
	try
	{
		StringStringMap	theData;
		string			line;
		char			ch;
		ErrCode			err;

		do
		{
			err = stream.GetBytes (&ch, 1);

			if (ch == '\n' || ch == '\r' || err != 0)		// End of line or end of file.
			{
				// Skip leading WS
				line = Strip (line.c_str (), " \t", true, false);

				// 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 ());
				
				key		= Strip (key.c_str (), " \t", true, true);
				value	= Strip (value.c_str (), " \t", true, true);

				theData[key] = value;
				
				line.erase ();
			}
			else
			{
				line += ch;
			}
		}
		while (err == 0);

		mapData = theData;
	}
	catch (...)
	{
		return false;
	}

	return true;
}



Bool
EmMapFile::Write (const EmFileRef& f, const StringStringMap& mapData)
{
	try
	{
		// Get the data in fairly raw form.

		Chunk			chunk;
		EmStreamChunk	chunkStream (chunk);

		if (!Write (chunkStream, mapData))
			return false;

		// Now write that data to the file, letting the EmStreamFile
		// class sort out line endings.

		EmStreamFile	fileStream (f, kCreateOrEraseForUpdate | kOpenText,
								kFileCreatorCodeWarrior, kFileTypeText);	// Will throw if can't create
		return fileStream.PutBytes (chunk.GetPointer (), chunk.GetLength ());
	}
	catch (...)
	{
		return false;
	}

	return true;
}


Bool
EmMapFile::Write (EmStream& stream, const StringStringMap& mapData)
{
	StringStringMap::const_iterator	iter;

	for (iter = mapData.begin(); iter != mapData.end(); ++iter)
	{
		string	line = iter->first + "=" + iter->second + "\n";
		stream.PutBytes (line.c_str (), line.size ());
	}

	return true;
}