Cleaned up I/O in a few places, using C++ streams where appropriate.

For those reading these logs, I hope to get back to Stella development soon.


git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@3166 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
stephena 2015-06-11 00:26:13 +00:00
parent e9e3670134
commit a8a30a1746
14 changed files with 57 additions and 95 deletions

View File

@ -230,7 +230,7 @@ void CheatManager::enable(const string& code, bool enable)
void CheatManager::loadCheatDatabase()
{
const string& cheatfile = myOSystem.cheatFile();
ifstream in(cheatfile.c_str(), ios::in);
ifstream in(cheatfile.c_str());
if(!in)
return;
@ -260,7 +260,6 @@ void CheatManager::loadCheatDatabase()
myCheatMap.insert(make_pair(md5, cheat));
}
in.close();
myListIsDirty = false;
}
@ -271,14 +270,12 @@ void CheatManager::saveCheatDatabase()
return;
const string& cheatfile = myOSystem.cheatFile();
ofstream out(cheatfile.c_str(), ios::out);
ofstream out(cheatfile.c_str());
if(!out)
return;
for(const auto& iter: myCheatMap)
out << "\"" << iter.first << "\" " << "\"" << iter.second << "\"" << endl;
out.close();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -49,28 +49,27 @@ class Variant
Variant(const string& s) : data(s) { }
Variant(const char* s) : data(s) { }
Variant(Int32 i) { buf().str(""); buf() << i; data = buf().str(); }
Variant(Int32 i) { buf().str(""); buf() << i; data = buf().str(); }
Variant(uInt32 i) { buf().str(""); buf() << i; data = buf().str(); }
Variant(float f) { buf().str(""); buf() << f; data = buf().str(); }
Variant(float f) { buf().str(""); buf() << f; data = buf().str(); }
Variant(double d) { buf().str(""); buf() << d; data = buf().str(); }
Variant(bool b) { buf().str(""); buf() << b; data = buf().str(); }
Variant(bool b) { buf().str(""); buf() << b; data = buf().str(); }
Variant(const GUI::Size& s) { buf().str(""); buf() << s; data = buf().str(); }
// Conversion methods
const string& toString() const { return data; }
const char* toCString() const { return data.c_str(); }
const Int32 toInt() const { return atoi(data.c_str()); }
const float toFloat() const { return atof(data.c_str()); }
const bool toBool() const { return data == "1" || data == "true"; }
const GUI::Size toSize() const { return GUI::Size(data); }
const string& toString() const { return data; }
const char* toCString() const { return data.c_str(); }
const Int32 toInt() const { return atoi(data.c_str()); }
const float toFloat() const { return atof(data.c_str()); }
const bool toBool() const { return data == "1" || data == "true"; }
const GUI::Size toSize() const { return GUI::Size(data); }
// Comparison
bool operator==(const Variant& v) const { return data == v.data; };
bool operator!=(const Variant& v) const { return data != v.data; };
friend ostream& operator<<(ostream& os, const Variant& v) {
os << v.data;
return os;
return os << v.data;
}
};

View File

@ -729,7 +729,6 @@ string CartDebug::loadListFile()
}
}
}
in.close();
myDebugger.rom().invalidate();
return "loaded " + node.getShortPath() + " OK";
@ -785,7 +784,6 @@ string CartDebug::loadSymbolFile()
}
}
}
in.close();
myDebugger.rom().invalidate();
return "loaded " + node.getShortPath() + " OK";
@ -895,7 +893,6 @@ string CartDebug::loadConfigFile()
}
}
}
in.close();
myDebugger.rom().invalidate();
return "loaded " + node.getShortPath() + " OK";
@ -940,7 +937,6 @@ string CartDebug::saveConfigFile()
out << "[" << b << "]" << endl;
getBankDirectives(out, myBankInfo[b]);
}
out.close();
return "saved " + node.getShortPath() + " OK";
}
@ -1148,8 +1144,6 @@ string CartDebug::saveDisassembly()
// And finally, output the disassembly
out << buf.str();
out.close();
return "saved " + node.getShortPath() + " OK";
}
@ -1160,7 +1154,7 @@ string CartDebug::saveRom()
myConsole.properties().get(Cartridge_Name) + ".a26";
FilesystemNode node(path);
ofstream out(node.getPath().c_str(), ios::out | ios::binary);
ofstream out(node.getPath().c_str(), ios::binary);
if(out.is_open() && myConsole.cartridge().save(out))
return "saved ROM as " + node.getShortPath();
else

View File

@ -619,9 +619,7 @@ bool DebuggerParser::saveScriptFile(string file)
for(const auto& cond: conds)
out << "breakif {" << cond << "}" << endl;
bool ok = out.good();
out.close();
return ok;
return out.good();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -854,7 +854,8 @@ bool PromptWidget::saveBuffer(string& filename)
if(!out.is_open())
return false;
for(int start=0; start<_promptStartPos; start+=_lineWidth) {
for(int start=0; start<_promptStartPos; start+=_lineWidth)
{
int end = start+_lineWidth-1;
// look for first non-space, printing char from end of line
@ -863,14 +864,13 @@ bool PromptWidget::saveBuffer(string& filename)
// spit out the line minus its trailing junk.
// Strip off any color/inverse bits
for(int j=start; j<=end; j++)
for(int j = start; j <= end; ++j)
out << char(_buffer[j] & 0xff);
// add a \n
out << endl;
}
out.close();
return true;
}

View File

@ -757,7 +757,6 @@ void Console::loadUserPalette()
in.seekg(0, ios::beg);
if(length < 128 * 3 * 2 + 8 * 3)
{
in.close();
cerr << "ERROR: invalid palette file " << palette << endl;
return;
}
@ -794,7 +793,6 @@ void Console::loadUserPalette()
*ptr++ = *s++;
}
in.close();
myUserPaletteDefined = true;
}

View File

@ -518,11 +518,10 @@ void EventHandler::handleKeyEvent(StellaKey key, StellaMod mod, bool state)
{
string filename = myOSystem.baseDir() +
myOSystem.console().properties().get(Cartridge_Name) + ".pro";
ofstream out(filename.c_str(), ios::out);
ofstream out(filename.c_str());
if(out)
{
myOSystem.console().properties().save(out);
out.close();
out << myOSystem.console().properties();
myOSystem.frameBuffer().showMessage("Properties saved");
}
else

View File

@ -85,7 +85,6 @@ MT24LC256::MT24LC256(const string& filename, const System& system)
in.read((char*)myData, 32768);
myDataFileExists = true;
}
in.close();
}
else
myDataFileExists = false;
@ -100,13 +99,9 @@ MT24LC256::~MT24LC256()
// Save EEPROM data to external file only when necessary
if(!myDataFileExists || myDataChanged)
{
ofstream out;
out.open(myDataFile.c_str(), ios_base::binary);
ofstream out(myDataFile.c_str(), ios_base::binary);
if(out.is_open())
{
out.write((char*)myData, 32768);
out.close();
}
}
}

View File

@ -36,19 +36,10 @@ Properties::Properties(const Properties& properties)
copy(properties);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const string& Properties::get(PropertyType key) const
{
if(key >= 0 && key < LastPropType)
return myProperties[key];
else
return EmptyString;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Properties::set(PropertyType key, const string& value)
{
if(key >= 0 && key < LastPropType)
if(key != LastPropType)
{
myProperties[key] = value;
@ -91,52 +82,54 @@ void Properties::set(PropertyType key, const string& value)
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Properties::load(istream& in)
istream& operator>>(istream& is, Properties& p)
{
setDefaults();
p.setDefaults();
// Loop reading properties
string key, value;
for(;;)
{
// Get the key associated with this property
key = readQuotedString(in);
key = p.readQuotedString(is);
// Make sure the stream is still okay
if(!in)
return;
if(!is)
return is;
// A null key signifies the end of the property list
if(key == "")
break;
// Get the value associated with this property
value = readQuotedString(in);
value = p.readQuotedString(is);
// Make sure the stream is still okay
if(!in)
return;
if(!is)
return is;
// Set the property
PropertyType type = getPropertyType(key);
set(type, value);
PropertyType type = Properties::getPropertyType(key);
p.set(type, value);
}
return is;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void Properties::save(ostream& out) const
ostream& operator<<(ostream& os, const Properties& p)
{
// Write out each of the key and value pairs
bool changed = false;
for(int i = 0; i < LastPropType; ++i)
{
// Try to save some space by only saving the items that differ from default
if(myProperties[i] != ourDefaultProperties[i])
if(p.myProperties[i] != Properties::ourDefaultProperties[i])
{
writeQuotedString(out, ourPropertyNames[i]);
out.put(' ');
writeQuotedString(out, myProperties[i]);
out.put('\n');
p.writeQuotedString(os, Properties::ourPropertyNames[i]);
os.put(' ');
p.writeQuotedString(os, p.myProperties[i]);
os.put('\n');
changed = true;
}
}
@ -144,23 +137,22 @@ void Properties::save(ostream& out) const
if(changed)
{
// Put a trailing null string so we know when to stop reading
writeQuotedString(out, "");
out.put('\n');
out.put('\n');
p.writeQuotedString(os, "");
os.put('\n');
os.put('\n');
}
return os;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string Properties::readQuotedString(istream& in)
{
char c;
// Read characters until we see a quote
char c;
while(in.get(c))
{
if(c == '"')
break;
}
// Read characters until we see the close quote
string s;

View File

@ -84,7 +84,9 @@ class Properties
@param key The key of the property to lookup
@return The value of the property
*/
const string& get(PropertyType key) const;
const string& get(PropertyType key) const {
return key != LastPropType ? myProperties[key] : EmptyString;
}
/**
Set the value associated with key to the given value.
@ -99,14 +101,14 @@ class Properties
@param in The input stream to use
*/
void load(istream& in);
friend istream& operator>>(istream& is, Properties& p);
/**
Save properties to the specified output stream
@param out The output stream to use
*/
void save(ostream& out) const;
friend ostream& operator<<(ostream& os, const Properties& p);
/**
Print the attributes of this properties object

View File

@ -37,7 +37,7 @@ PropertiesSet::PropertiesSet(const string& propsfile)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PropertiesSet::load(const string& filename)
{
ifstream in(filename.c_str(), ios::in);
ifstream in(filename.c_str());
// Loop reading properties
for(;;)
@ -48,26 +48,24 @@ void PropertiesSet::load(const string& filename)
// Get the property list associated with this profile
Properties prop;
prop.load(in);
in >> prop;
// If the stream is still good then insert the properties
if(in)
insert(prop);
}
if(in)
in.close();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool PropertiesSet::save(const string& filename) const
{
ofstream out(filename.c_str(), ios::out);
ofstream out(filename.c_str());
if(!out)
return false;
// Only save those entries in the external list
for(const auto& i: myExternalProps)
i.second.save(out);
out << i.second;
return true;
}
@ -196,10 +194,8 @@ void PropertiesSet::print() const
// The easiest way to merge the lists is to create another temporary one
// This isn't fast, but I suspect this method isn't used too often (or at all)
PropsList list;
// First insert all external props
list = myExternalProps;
PropsList list = myExternalProps;
// Now insert all the built-in ones
// Note that if we try to insert a duplicate, the insertion will fail

View File

@ -200,8 +200,6 @@ void Settings::loadConfig()
if(int idx = getInternalPos(key) != -1)
setInternal(key, value, idx, true);
}
in.close();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -263,7 +261,7 @@ string Settings::loadCommandLine(int argc, char** argv)
return key;
}
return "";
return EmptyString;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -539,8 +537,6 @@ void Settings::saveConfig()
// Write out each of the key and value pairs
for(const auto& s: myInternalSettings)
out << s.key << " = " << s.value << endl;
out.close();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -119,12 +119,9 @@ void LoggerDialog::saveLogFile()
path << "~" << BSPF_PATH_SEPARATOR << "stella.log";
FilesystemNode node(path.str());
ofstream out(node.getPath().c_str(), ios::out);
ofstream out(node.getPath().c_str());
if(out.is_open())
{
out << instance().logMessages();
out.close();
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -52,7 +52,6 @@ OSystemWINDOWS::OSystemWINDOWS()
if(in && in.is_open())
{
getline(in, basedir);
in.close();
// trim leading and trailing spaces
size_t spos = basedir.find_first_not_of(" \t");