From c15f9586de332f1f27c329893bc9f62426749cb7 Mon Sep 17 00:00:00 2001 From: JT Traub Date: Thu, 12 Oct 2023 12:49:13 -0700 Subject: [PATCH] Rework file input (#147) This changes the existing file input to use std::istream throughout the bulk of the code rather than Aorderfile and Ainputfile. Output is still done via the custom file classes, but those will be changed eventually. as well. --- aregion.cpp | 125 +++++++++++++++++++++++-------------------- aregion.h | 6 +-- astring.cpp | 13 +++-- astring.h | 3 +- faction.cpp | 49 ++++++++--------- faction.h | 4 +- fileio.cpp | 102 +---------------------------------- fileio.h | 30 ----------- game.cpp | 137 ++++++++++++++++++++++++------------------------ game.h | 68 ++++++++++++------------ genrules.cpp | 14 ++--- items.cpp | 17 +++--- items.h | 4 +- market.cpp | 36 ++++++------- market.h | 4 +- object.cpp | 40 +++++++------- object.h | 4 +- parseorders.cpp | 51 ++++++++---------- production.cpp | 32 +++++------ production.h | 4 +- quests.cpp | 59 +++++++++------------ quests.h | 2 +- skills.cpp | 21 ++++---- skills.h | 4 +- unit.cpp | 74 ++++++++++++++------------ unit.h | 2 +- 26 files changed, 400 insertions(+), 505 deletions(-) diff --git a/aregion.cpp b/aregion.cpp index e27771819..eab84e12b 100644 --- a/aregion.cpp +++ b/aregion.cpp @@ -103,11 +103,13 @@ TownInfo::~TownInfo() if (name) delete name; } -void TownInfo::Readin(Ainfile *f, ATL_VER &v) +void TownInfo::Readin(istream &f) { - name = f->GetStr(); - pop = f->GetInt(); - hab = f->GetInt(); + AString temp; + f >> ws >> temp; + name = new AString(temp); + f >> pop; + f >> hab; } void TownInfo::Writeout(Aoutfile *f) @@ -1065,61 +1067,65 @@ int LookupRegionType(AString *token) return -1; } -void ARegion::Readin(Ainfile *f, AList *facs, ATL_VER v) +void ARegion::Readin(istream &f, AList *facs) { - AString *temp; - - name = f->GetStr(); - - num = f->GetInt(); - temp = f->GetStr(); - type = LookupRegionType(temp); - delete temp; - buildingseq = f->GetInt(); - gate = f->GetInt(); - if (gate > 0) gatemonth = f->GetInt(); - - temp = f->GetStr(); - race = LookupItem(temp); - delete temp; - - population = f->GetInt(); - basepopulation = f->GetInt(); - wages = f->GetInt(); - maxwages = f->GetInt(); - wealth = f->GetInt(); - - elevation = f->GetInt(); - humidity = f->GetInt(); - temperature = f->GetInt(); - vegetation = f->GetInt(); - culture = f->GetInt(); + AString temp; + + f >> ws >> temp; + name = new AString(temp); + + f >> num; + f >> ws >> temp; + type = LookupRegionType(&temp); + + f >> buildingseq; + f >> gate; + if (gate > 0) f >> gatemonth; + + f >> ws >> temp; + race = LookupItem(&temp); - habitat = f->GetInt(); - development = f->GetInt(); - maxdevelopment = f->GetInt(); + f >> population; + f >> basepopulation; + f >> wages; + f >> maxwages; + f >> wealth; - if (f->GetInt()) { + f >> elevation; + f >> humidity; + f >> temperature; + f >> vegetation; + f >> culture; + + f >> habitat; + f >> development; + f >> maxdevelopment; + + int n; + f >> n; + if (n) { town = new TownInfo; - town->Readin(f, v); + town->Readin(f); town->dev = TownDevelopment(); } else { town = 0; } - xloc = f->GetInt(); - yloc = f->GetInt(); - zloc = f->GetInt(); - visited = f->GetInt(); + f >> xloc; + f >> yloc; + f >> zloc; + f >> visited; products.Readin(f); markets.Readin(f); - int i = f->GetInt(); + int i; + f >> i; + buildingseq = 1; - for (int j=0; jReadin(f, facs, v); + temp->Readin(f, facs); if (temp->num >= buildingseq) buildingseq = temp->num + 1; objects.Add(temp); @@ -2081,36 +2087,37 @@ void ARegionList::WriteRegions(Aoutfile *f) } } -int ARegionList::ReadRegions(Ainfile *f, AList *factions, ATL_VER v) +int ARegionList::ReadRegions(istream &f, AList *factions) { - int num = f->GetInt(); + int num; + f >> num; - numLevels = f->GetInt(); + f >> numLevels; CreateLevels(numLevels); int i; for (i = 0; i < numLevels; i++) { - int curX = f->GetInt(); - int curY = f->GetInt(); - AString *name = f->GetStr(); + int curX, curY; + f >> curX >> curY; + AString name; + f >> ws >> name; ARegionArray *pRegs = new ARegionArray(curX, curY); - if (*name == "none") { + if (name == "none") { pRegs->strName = 0; - delete name; } else { - pRegs->strName = name; + pRegs->strName = new AString(name); } - pRegs->levelType = f->GetInt(); + f >> pRegs->levelType; pRegionArrays[i] = pRegs; } - numberofgates = f->GetInt(); + f >> numberofgates; ARegionFlatArray fa(num); Awrite("Reading the regions..."); for (i = 0; i < num; i++) { ARegion *temp = new ARegion; - temp->Readin(f, factions, v); + temp->Readin(f, factions); fa.SetRegion(temp->num, temp); Add(temp); @@ -2120,11 +2127,13 @@ int ARegionList::ReadRegions(Ainfile *f, AList *factions, ATL_VER v) Awrite("Setting up the neighbors..."); { - delete f->GetStr(); + AString temp; + f >> ws >> temp; // eat the "Neighbors" line forlist(this) { ARegion *reg = (ARegion *) elem; for (i = 0; i < NDIRS; i++) { - int j = f->GetInt(); + int j; + f >> j; if (j != -1) { reg->neighbors[i] = fa.GetRegion(j); } else { diff --git a/aregion.h b/aregion.h index 642e33b29..1379651f9 100644 --- a/aregion.h +++ b/aregion.h @@ -151,7 +151,7 @@ class TownInfo TownInfo(); ~TownInfo(); - void Readin(Ainfile *, ATL_VER &); + void Readin(istream &f); void Writeout(Aoutfile *); int TownType(); @@ -191,7 +191,7 @@ class ARegion : public AListElem void SetName(char const *); void Writeout(Aoutfile *); - void Readin(Ainfile *, AList *, ATL_VER v); + void Readin(istream &f, AList *); int CanMakeAdv(Faction *, int); int HasItem(Faction *, int); @@ -453,7 +453,7 @@ class ARegionList : public AList ARegion *GetRegion(int); ARegion *GetRegion(int, int, int); - int ReadRegions(Ainfile *f, AList *, ATL_VER v); + int ReadRegions(istream &f, AList *); void WriteRegions(Aoutfile *f); Location *FindUnit(int); Location *GetUnitId(UnitId *id, int faction, ARegion *cur); diff --git a/astring.cpp b/astring.cpp index 0e24799f7..5064a3c34 100644 --- a/astring.cpp +++ b/astring.cpp @@ -196,6 +196,12 @@ char *AString::Str() return str; } + +const char *AString::const_str() const +{ + return str; +} + int AString::Len() { return len; @@ -381,16 +387,17 @@ int AString::strict_value() //this cannot handle negative numbers! return ret; } -ostream & operator <<(ostream & os,const AString & s) +ostream& operator<<(ostream &os,const AString &s) { os << s.str; return os; } -istream & operator >>(istream & is,AString & s) +istream& operator>>(istream &is,AString &s) { + // We expect to read a line at a time from the file, not a string at a time since we do tokenization internally. string buf; - is >> buf; + getline(is, buf); s.len = strlen(buf.c_str()); s.str = new char[s.len + 1]; strcpy(s.str,buf.c_str()); diff --git a/astring.h b/astring.h index 3da14ae9d..bb8b3ffce 100644 --- a/astring.h +++ b/astring.h @@ -35,6 +35,7 @@ using namespace std; class AString : public AListElem { friend ostream & operator <<(ostream &os, const AString &); friend istream & operator >>(istream &is, AString &); + public: AString(); @@ -58,6 +59,7 @@ class AString : public AListElem { AString & operator=(const char *); char *Str(); + const char *const_str() const; int Len(); AString *gettoken(); @@ -69,7 +71,6 @@ class AString : public AListElem { AString *StripWhite(); private: - int len; char *str; int isEqual(const char *) const; diff --git a/faction.cpp b/faction.cpp index dccdc24ab..ef930fd29 100644 --- a/faction.cpp +++ b/faction.cpp @@ -110,10 +110,10 @@ void Attitude::Writeout(Aoutfile *f) f->PutInt(attitude); } -void Attitude::Readin(Ainfile *f, ATL_VER v) +void Attitude::Readin(istream &f) { - factionnum = f->GetInt(); - attitude = f->GetInt(); + f >> factionnum; + f >> attitude; } Faction::Faction() @@ -201,40 +201,41 @@ void Faction::Writeout(Aoutfile *f) forlist((&attitudes)) ((Attitude *) elem)->Writeout(f); } -void Faction::Readin(Ainfile *f, ATL_VER v) +void Faction::Readin(istream& f) { - num = f->GetInt(); - int i; + f >> num; for (auto &ft : *FactionTypes) { - type[ft] = f->GetInt(); + f >> type[ft]; } - lastchange = f->GetInt(); - lastorders = f->GetInt(); - unclaimed = f->GetInt(); + f >> lastchange; + f >> lastorders; + f >> unclaimed; - name = f->GetStr(); - address = f->GetStr(); - password = f->GetStr(); - times = f->GetInt(); - showunitattitudes = f->GetInt(); - temformat = f->GetInt(); + AString tmp; + f >> ws >> tmp; + name = new AString(tmp); + f >> ws >> tmp; + address = new AString(tmp); + f >> ws >> tmp; + password = new AString(tmp); + f >> times; + f >> showunitattitudes; + f >> temformat; skills.Readin(f); items.Readin(f); - defaultattitude = f->GetInt(); - int n = f->GetInt(); - for (i=0; i> defaultattitude; + int n; + f >> n; + for (int i = 0; i < n; i++) { Attitude* a = new Attitude; - a->Readin(f, v); + a->Readin(f); if (a->factionnum == num) delete a; else attitudes.Add(a); } - - // if (skills.GetDays(S_BUILDING) > 1) - // shows.Add(new ShowSkill(S_BUILDING, 2)); } void Faction::View() @@ -993,4 +994,4 @@ bool Faction::IsActivityRecorded(ARegion *region, FactionActivity type) { } return regionActivity.find(type) != std::end(regionActivity); -} \ No newline at end of file +} diff --git a/faction.h b/faction.h index 0e4557489..bd1236783 100644 --- a/faction.h +++ b/faction.h @@ -109,7 +109,7 @@ class Attitude : public AListElem { Attitude(); ~Attitude(); void Writeout(Aoutfile * ); - void Readin( Ainfile *, ATL_VER version ); + void Readin(istream &f); int factionnum; int attitude; @@ -132,7 +132,7 @@ class Faction : public AListElem Faction(int); ~Faction(); - void Readin( Ainfile *, ATL_VER version ); + void Readin(istream &f); void Writeout( Aoutfile * ); void View(); diff --git a/fileio.cpp b/fileio.cpp index abf15f926..673cd4640 100644 --- a/fileio.cpp +++ b/fileio.cpp @@ -33,8 +33,6 @@ using namespace std; extern long _ftype,_fcreator; -static char buf[1024]; - Aoutfile::Aoutfile() { file = new ofstream; @@ -45,26 +43,6 @@ Aoutfile::~Aoutfile() delete file; } -Ainfile::Ainfile() -{ - file = new ifstream; -} - -Ainfile::~Ainfile() -{ - delete file; -} - -Aorders::Aorders() -{ - file = new ifstream; -} - -Aorders::~Aorders() -{ - delete file; -} - Areport::Areport() { file = new ofstream; @@ -111,38 +89,11 @@ int Aoutfile::OpenByName(const AString &s) return 0; } -void Ainfile::Open(const AString &s) -{ - while (!(file->rdbuf()->is_open())) { - AString *name = getfilename(s); - file->open(name->Str(),ios::in); - delete name; - } -} - -int Ainfile::OpenByName(const AString &s) -{ - AString temp = s; - file->open(temp.Str(),ios::in); - if (!(file->rdbuf()->is_open())) return -1; - return 0; -} - void Aoutfile::Close() { file->close(); } -void Ainfile::Close() -{ - file->close(); -} - -void Aorders::Close() -{ - file->close(); -} - void Areport::Close() { file->close(); @@ -153,7 +104,7 @@ void Arules::Close() file->close(); } -void skipwhite(ifstream *f) +void skipwhite(istream *f) { if (f->eof()) return; int ch = f->peek(); @@ -165,30 +116,6 @@ void skipwhite(ifstream *f) } } -AString * Ainfile::GetStr() -{ - skipwhite(file); - if (file->peek() == -1 || file->eof()) return 0; - file->getline(buf,1023,F_ENDLINE); - AString * s = new AString((char *) &(buf[0])); - return s; -} - -AString * Ainfile::GetStrNoSkip() -{ - if (file->peek() == -1 || file->eof()) return 0; - file->getline(buf,1023,F_ENDLINE); - AString * s = new AString((char *) &(buf[0])); - return s; -} - -int Ainfile::GetInt() -{ - int x; - *file >> x; - return x; -} - void Aoutfile::PutInt(int x) { *file << x; @@ -205,33 +132,6 @@ void Aoutfile::PutStr(const AString &s) *file << s << F_ENDLINE; } -void Aorders::Open(const AString &s) -{ - while (!(file->rdbuf()->is_open())) { - AString *name = getfilename(s); - file->open(name->Str(),ios::in); - delete name; - } -} - -int Aorders::OpenByName(const AString &s) -{ - AString temp = s; - file->open(temp.Str(),ios::in); - if (!(file->rdbuf()->is_open())) return -1; - return 0; -} - -AString * Aorders::GetLine() -{ - skipwhite(file); - if (file->eof()) return 0; - if (file->peek() == -1) return 0; - file->getline(buf,1023,F_ENDLINE); - AString *s = new AString((char *) &(buf[0])); - return s; -} - void Areport::Open(const AString &s) { while(!(file->rdbuf()->is_open())) { diff --git a/fileio.h b/fileio.h index 6b6c20e75..45e57de66 100644 --- a/fileio.h +++ b/fileio.h @@ -31,22 +31,6 @@ #include using namespace std; -class Ainfile { - public: - Ainfile(); - ~Ainfile(); - - void Open(const AString &); - int OpenByName(const AString &); - void Close(); - - AString *GetStr(); - AString *GetStrNoSkip(); - int GetInt(); - - ifstream *file; -}; - class Aoutfile { public: Aoutfile(); @@ -63,20 +47,6 @@ class Aoutfile { ofstream *file; }; -class Aorders { - public: - Aorders(); - ~Aorders(); - - void Open(const AString &); - int OpenByName(const AString &); - void Close(); - - AString *GetLine(); - - ifstream *file; -}; - class Areport { public: Areport(); diff --git a/game.cpp b/game.cpp index 880a2f07d..6f1b22976 100644 --- a/game.cpp +++ b/game.cpp @@ -318,27 +318,26 @@ int Game::OpenGame() // // The order here must match the order in SaveGame // - Ainfile f; - if (f.OpenByName("game.in") == -1) return(0); - + ifstream f; + f.open("game.in", ios::in); + if (!f.is_open()) return(0); // // Read in Globals - // - AString *s1 = f.GetStr(); - if (!s1) return(0); + AString s1; + f >> ws >> s1; + if (f.eof()) return(0); - AString *s2 = s1->gettoken(); - delete s1; + AString *s2 = s1.gettoken(); if (!s2) return(0); if (!(*s2 == "atlantis_game")) { delete s2; - f.Close(); return(0); } delete s2; - ATL_VER eVersion = f.GetInt(); + ATL_VER eVersion; + f >> eVersion; Awrite(AString("Saved Game Engine Version: ") + ATL_VER_STRING(eVersion)); if (ATL_VER_MAJOR(eVersion) != ATL_VER_MAJOR(CURRENT_ATL_VER) || ATL_VER_MINOR(eVersion) != ATL_VER_MINOR(CURRENT_ATL_VER)) { @@ -350,15 +349,17 @@ int Game::OpenGame() return(0); } - AString *gameName = f.GetStr(); - if (!gameName) return(0); + AString gameName; + f >> ws >> gameName; + if (f.eof()) return(0); - if (!(*gameName == Globals->RULESET_NAME)) { + if (!(gameName == Globals->RULESET_NAME)) { Awrite("Incompatible rule-set!"); return(0); } - ATL_VER gVersion = f.GetInt(); + ATL_VER gVersion; + f >> gVersion; Awrite(AString("Saved Rule-Set Version: ") + ATL_VER_STRING(gVersion)); if (ATL_VER_MAJOR(gVersion) < ATL_VER_MAJOR(Globals->RULESET_VERSION)) { @@ -395,39 +396,43 @@ int Game::OpenGame() ATL_VER_PATCH(Globals->RULESET_VERSION)); } - year = f.GetInt(); - month = f.GetInt(); - seedrandom(f.GetInt()); - factionseq = f.GetInt(); - unitseq = f.GetInt(); - shipseq = f.GetInt(); - guardfaction = f.GetInt(); - monfaction = f.GetInt(); + f >> year; + f >> month; + + int seed; + f >> seed; + seedrandom(seed); + + f >> factionseq; + f >> unitseq; + f >> shipseq; + f >> guardfaction; + f >> monfaction; // // Read in the Factions // - int i = f.GetInt(); + int i; + f >> i; - for (int j=0; jReadin(&f, eVersion); + temp->Readin(f); factions.Add(temp); } // // Read in the ARegions // - i = regions.ReadRegions(&f, &factions, eVersion); + i = regions.ReadRegions(f, &factions); if (!i) return 0; // read in quests - if (!quests.ReadQuests(&f)) + if (!quests.ReadQuests(f)) return 0; SetupUnitNums(); - f.Close(); return(1); } @@ -514,10 +519,11 @@ int Game::WritePlayers() int Game::ReadPlayers() { - Aorders f; - if (f.OpenByName("players.in") == -1) return(0); + ifstream f; + f.open("players.in", ios::in); + if (!f.is_open()) return(0); - AString *pLine = 0; + AString pLine; AString *pToken = 0; // @@ -529,19 +535,18 @@ int Game::ReadPlayers() // // The first line of the file should match. // - pLine = f.GetLine(); - if (!(*pLine == PLAYERS_FIRST_LINE)) break; - SAFE_DELETE(pLine); + f >> ws >> pLine; + if (!(pLine == PLAYERS_FIRST_LINE)) break; // // Get the file version number. // - pLine = f.GetLine(); - pToken = pLine->gettoken(); + f >> ws >> pLine; + pToken = pLine.gettoken(); if (!pToken || !(*pToken == "Version:")) break; SAFE_DELETE(pToken); - pToken = pLine->gettoken(); + pToken = pLine.gettoken(); if (!pToken) break; int nVer = pToken->value(); @@ -553,23 +558,21 @@ int Game::ReadPlayers() break; } SAFE_DELETE(pToken); - SAFE_DELETE(pLine); // // Ignore the turn number line. // - pLine = f.GetLine(); - SAFE_DELETE(pLine); + f >> ws >> pLine; // // Next, the game status. // - pLine = f.GetLine(); - pToken = pLine->gettoken(); + f >> ws >> pLine; + pToken = pLine.gettoken(); if (!pToken || !(*pToken == "GameStatus:")) break; SAFE_DELETE(pToken); - pToken = pLine->gettoken(); + pToken = pLine.gettoken(); if (!pToken) break; if (*pToken == "New") @@ -589,7 +592,7 @@ int Game::ReadPlayers() // // Now, we should have a list of factions. // - pLine = f.GetLine(); + f >> ws >> pLine; Faction *pFac = 0; int lastWasNew = 0; @@ -600,11 +603,10 @@ int Game::ReadPlayers() // rc = 1; - while(pLine) { - pToken = pLine->gettoken(); + while(!f.eof()) { + pToken = pLine.gettoken(); if (!pToken) { - SAFE_DELETE(pLine); - pLine = f.GetLine(); + f >> ws >> pLine; continue; } @@ -613,25 +615,25 @@ int Game::ReadPlayers() // Get the new faction // SAFE_DELETE(pToken); - pToken = pLine->gettoken(); + pToken = pLine.gettoken(); if (!pToken) { rc = 0; break; } if (*pToken == "new") { - AString save = *pLine; + AString save = pLine; int noleader = 0; int x, y, z; ARegion *pReg = NULL; /* Check for the noleader flag */ SAFE_DELETE(pToken); - pToken = pLine->gettoken(); + pToken = pLine.gettoken(); if (pToken && *pToken == "noleader") { noleader = 1; SAFE_DELETE(pToken); - pToken = pLine->gettoken(); + pToken = pLine.gettoken(); /* Initialize pReg to something useful */ pReg = regions.GetRegion(0, 0, 0); } @@ -640,11 +642,11 @@ int Game::ReadPlayers() y = -1; z = -1; SAFE_DELETE(pToken); - pToken = pLine->gettoken(); + pToken = pLine.gettoken(); if (pToken) { y = pToken->value(); SAFE_DELETE(pToken); - pToken = pLine->gettoken(); + pToken = pLine.gettoken(); if (pToken) { z = pToken->value(); pReg = regions.GetRegion(x, y, z); @@ -673,24 +675,21 @@ int Game::ReadPlayers() lastWasNew = 0; } } else if (pFac) { - if (!ReadPlayersLine(pToken, pLine, pFac, lastWasNew)) { + if (!ReadPlayersLine(pToken, &pLine, pFac, lastWasNew)) { rc = 0; break; } } SAFE_DELETE(pToken); - SAFE_DELETE(pLine); - pLine = f.GetLine(); + f >> ws >> pLine; } if (pFac && lastWasNew) { WriteNewFac(pFac); } } while(0); - SAFE_DELETE(pLine); SAFE_DELETE(pToken); - f.Close(); return(rc); } @@ -1019,8 +1018,9 @@ void Game::WriteNewFac(Faction *pFac) int Game::DoOrdersCheck(const AString &strOrders, const AString &strCheck) { - Aorders ordersFile; - if (ordersFile.OpenByName(strOrders) == -1) { + ifstream ordersFile; + ordersFile.open(strOrders.const_str(), ios::in); + if (!ordersFile.is_open()) { Awrite("No such orders file!"); return(0); } @@ -1034,9 +1034,9 @@ int Game::DoOrdersCheck(const AString &strOrders, const AString &strCheck) OrdersCheck check; check.pCheckFile = &checkFile; - ParseOrders(0, &ordersFile, &check); + ParseOrders(0, ordersFile, &check); - ordersFile.Close(); + ordersFile.close(); checkFile.Close(); return(1); @@ -1150,10 +1150,11 @@ void Game::ReadOrders() AString str = "orders."; str += fac->num; - Aorders file; - if (file.OpenByName(str) != -1) { - ParseOrders(fac->num, &file, 0); - file.Close(); + ifstream file; + file.open(str.const_str(), ios::in); + if(file.is_open()) { + ParseOrders(fac->num, file, 0); + file.close(); } DefaultWorkOrder(); } @@ -2185,4 +2186,4 @@ int Game::CountItem (Faction * fac, int item) } return all; -} \ No newline at end of file +} diff --git a/game.h b/game.h index 221463d09..f07d67f60 100644 --- a/game.h +++ b/game.h @@ -59,6 +59,7 @@ I can see what the Item.cpp docs look like. class Game { friend class Faction; + public: Game(); ~Game(); @@ -75,17 +76,17 @@ class Game int WritePlayers(); int ReadPlayers(); int ReadPlayersLine(AString *pToken, AString *pLine, Faction *pFac, - int newPlayer); + int newPlayer); void WriteNewFac(Faction *pFac); int ViewMap(const AString &, const AString &); // LLS void UnitFactionMap(); int GenRules(const AString &, const AString &, const AString &); - void WriteFactionTypeDescription(std::ostringstream& buffer, Faction &fac); + void WriteFactionTypeDescription(std::ostringstream &buffer, Faction &fac); int DoOrdersCheck(const AString &strOrders, const AString &strCheck); - Faction *AddFaction(int noleader=0, ARegion *pStart = NULL); + Faction *AddFaction(int noleader = 0, ARegion *pStart = NULL); // // Give this particular game a chance to set up the faction. This is in @@ -120,8 +121,9 @@ class Game // Functions to allow enabling/disabling parts of the data tables void ModifyTablesPerRuleset(void); - void RecordFact(FactBase* fact); + void RecordFact(FactBase *fact); void WriteWorldEvents(); + private: // // Game editing functions. @@ -131,24 +133,24 @@ class Game void EditGameCreateUnit(); void EditGameRegion(ARegion *pReg); void EditGameRegionObjects(ARegion *pReg); - void EditGameRegionTerrain(ARegion *pReg ); - void EditGameRegionMarkets(ARegion *pReg ); + void EditGameRegionTerrain(ARegion *pReg); + void EditGameRegionMarkets(ARegion *pReg); void EditGameUnit(Unit *pUnit); void EditGameUnitItems(Unit *pUnit); void EditGameUnitSkills(Unit *pUnit); void EditGameUnitMove(Unit *pUnit); void EditGameUnitDetails(Unit *pUnit); - + void PreProcessTurn(); void ReadOrders(); void RunOrders(); void ClearOrders(Faction *); void MakeFactionReportLists(); void CountAllSpecialists(); - //void CountAllMages(); - //void CountAllApprentices(); - //void CountAllQuarterMasters(); - //void CountAllTacticians(); + // void CountAllMages(); + // void CountAllApprentices(); + // void CountAllQuarterMasters(); + // void CountAllTacticians(); void WriteReport(); // LLS - write order templates void WriteTemplates(); @@ -162,7 +164,7 @@ class Game void CreateWMons(); void CreateLMons(); void CreateVMons(); - Unit *MakeManUnit(Faction*, int, int, int, int, int, int); + Unit *MakeManUnit(Faction *, int, int, int, int, int, int); // // Game-specific creation functions (see world.cpp). @@ -186,15 +188,15 @@ class Game // JLT // Functions to allow enabling/disabling parts of the data tables - void EnableSkill(int sk); // Enabled a disabled skill - void DisableSkill(int sk); // Prevents skill being studied or used + void EnableSkill(int sk); // Enabled a disabled skill + void DisableSkill(int sk); // Prevents skill being studied or used void ModifySkillDependancy(int sk, int i, char const *dep, int lev); void ModifySkillFlags(int sk, int flags); void ModifySkillCost(int sk, int cost); void ModifySkillSpecial(int sk, char const *special); void ModifySkillRange(int sk, char const *range); - void EnableItem(int it); // Enables a disabled item + void EnableItem(int it); // Enables a disabled item void DisableItem(int it); // Prevents item being generated/produced void ModifyItemName(int it, char const *name, char const *names); void ModifyItemFlags(int it, int flags); @@ -238,7 +240,7 @@ class Game void ModifyMountBonuses(char const *mount, int min, int max, int hampered); void ModifyMountSpecial(char const *mount, char const *special, int level); - void EnableObject(int ob); // Enables a disabled object + void EnableObject(int ob); // Enables a disabled object void DisableObject(int ob); // Prevents object being built void ModifyObjectFlags(int ob, int flags); void ModifyObjectDecay(int ob, int maxMaint, int maxMonthDecay, int mFact); @@ -271,7 +273,7 @@ class Game void ModifySpecialShields(char const *special, int index, int type); void ModifySpecialDefenseMods(char const *special, int index, int type, int val); void ModifySpecialDamage(char const *special, int index, int type, int min, - int val, int flags, int cls, char const *effect, int hitDamage); + int val, int flags, int cls, char const *effect, int hitDamage); void ModifyEffectFlags(char const *effect, int flags); void ModifyEffectAttackMod(char const *effect, int val); @@ -284,7 +286,7 @@ class Game void ModifyRangeLevelPenalty(char const *range, int pen); void ModifyAttribMod(char const *mod, int index, int flags, char const *ident, - int type, int val); + int type, int val); void ModifyHealing(int level, int patients, int success); AList factions; @@ -299,7 +301,8 @@ class Game int year; int month; - enum { + enum + { GAME_STATUS_UNINIT, GAME_STATUS_NEW, GAME_STATUS_RUNNING, @@ -311,24 +314,23 @@ class Game int monfaction; int doExtraInit; - Events* events; - + Events *events; + // // Parsing functions // void ParseError(OrdersCheck *pCheck, Unit *pUnit, Faction *pFac, - const AString &strError); + const AString &strError); UnitId *ParseUnit(AString *s); int ParseDir(AString *token); - - void ParseOrders(int faction, Aorders *ordersFile, OrdersCheck *pCheck); + void ParseOrders(int faction, istream& ordersFile, OrdersCheck *pCheck); void ProcessOrder(int orderNum, Unit *unit, AString *order, - OrdersCheck *pCheck); + OrdersCheck *pCheck); void ProcessMoveOrder(Unit *, AString *, OrdersCheck *pCheck); void ProcessAdvanceOrder(Unit *, AString *, OrdersCheck *pCheck); Unit *ProcessFormOrder(Unit *former, AString *order, - OrdersCheck *pCheck, int atsign); + OrdersCheck *pCheck, int atsign); void ProcessAddressOrder(Unit *, AString *, OrdersCheck *pCheck); void ProcessAvoidOrder(Unit *, AString *, OrdersCheck *pCheck); void ProcessGuardOrder(Unit *, AString *, OrdersCheck *pCheck); @@ -384,7 +386,7 @@ class Game void ProcessTransportOrder(Unit *, AString *, OrdersCheck *pCheck); void ProcessDistributeOrder(Unit *, AString *, OrdersCheck *pCheck); void ProcessShareOrder(Unit *, AString *, OrdersCheck *pCheck); - AString *ProcessTurnOrder(Unit *, Aorders *, OrdersCheck *pCheck, int); + AString *ProcessTurnOrder(Unit *, istream& f, OrdersCheck *pCheck, int); void ProcessJoinOrder(Unit *, AString *, OrdersCheck *pCheck); void RemoveInactiveFactions(); @@ -400,7 +402,7 @@ class Game int CountApprentices(Faction *); int CountQuarterMasters(Faction *); int CountTacticians(Faction *); - + void FindDeadFactions(); void DeleteEmptyUnits(); void DeleteEmptyInRegion(ARegion *); @@ -480,10 +482,10 @@ class Game // Processing regions grow after production phase void ProcessEconomics(); - + // Migration effects for alternate player-driven economy void ProcessMigration(); - + // Run setup and equilibration turns (econ-only) at start void DevelopTowns(); void Equilibrate(); @@ -508,7 +510,7 @@ class Game void CheckWMonAttack(ARegion *, Unit *); Unit *GetWMonTar(ARegion *, int, Unit *); int CountWMonTars(ARegion *, Unit *); - void AttemptAttack(ARegion *, Unit *, Unit *, int, int=0); + void AttemptAttack(ARegion *, Unit *, Unit *, int, int = 0); void DoAutoAttacks(); void DoAdvanceAttack(ARegion *, Unit *); void DoAutoAttack(ARegion *, Unit *); @@ -598,8 +600,8 @@ class Game void GetDFacs(ARegion *, Unit *, AList &); // For faction statistics - void CountItems (int **); - int CountItem (Faction *, int); + void CountItems(int **); + int CountItem(Faction *, int); }; #endif diff --git a/genrules.cpp b/genrules.cpp index 920bd44d5..3829d2a0c 100644 --- a/genrules.cpp +++ b/genrules.cpp @@ -234,7 +234,7 @@ void Game::WriteFactionTypeDescription(std::ostringstream& buffer, Faction &fac) int Game::GenRules(const AString &rules, const AString &css, const AString &intro) { - Ainfile introf; + ifstream introf; Arules f; AString temp, temp2; int cap; @@ -247,7 +247,8 @@ int Game::GenRules(const AString &rules, const AString &css, return 0; } - if (introf.OpenByName(intro) == -1) { + introf.open(intro.const_str(), ios::in); + if (!introf.is_open()) { return 0; } @@ -599,11 +600,12 @@ int Game::GenRules(const AString &rules, const AString &css, f.LinkRef("intro"); f.ClassTagText("div", "rule", ""); f.TagText("h2", "Introduction"); - AString *in; - while((in = introf.GetStr()) != NULL) { - f.PutStr(*in); - delete in; + AString in; + while (!introf.eof()) { + introf >> ws >> in; + f.PutStr(in); } + f.LinkRef("playing"); f.ClassTagText("div", "rule", ""); f.TagText("h2", "Playing Atlantis"); diff --git a/items.cpp b/items.cpp index 1c545b25d..65df22d09 100644 --- a/items.cpp +++ b/items.cpp @@ -1553,16 +1553,16 @@ void Item::Writeout(Aoutfile *f) f->PutStr(temp); } -void Item::Readin(Ainfile *f) +void Item::Readin(istream &f) { - AString *temp = f->GetStr(); - AString *token = temp->gettoken(); + AString temp; + f >> ws >> temp; + AString *token = temp.gettoken(); num = token->value(); delete token; - token = temp->gettoken(); + token = temp.gettoken(); type = LookupItem(token); delete token; - delete temp; } void ItemList::Writeout(Aoutfile *f) @@ -1571,10 +1571,11 @@ void ItemList::Writeout(Aoutfile *f) forlist (this) ((Item *) elem)->Writeout(f); } -void ItemList::Readin(Ainfile *f) +void ItemList::Readin(istream &f) { - int i = f->GetInt(); - for (int j=0; j> i; + for (int j = 0; j < i; j++) { Item *temp = new Item; temp->Readin(f); if (temp->type < 0 || temp->num < 1 || diff --git a/items.h b/items.h index c47627761..5b05f7485 100644 --- a/items.h +++ b/items.h @@ -445,7 +445,7 @@ class Item : public AListElem Item(); ~Item(); - void Readin(Ainfile *); + void Readin(istream &f); void Writeout(Aoutfile *); AString Report(int); @@ -459,7 +459,7 @@ class Item : public AListElem class ItemList : public AList { public: - void Readin(Ainfile *); + void Readin(istream &f); void Writeout(Aoutfile *); AString Report(int, int, int); diff --git a/market.cpp b/market.cpp index 46dc96a33..ba4d2e4a7 100644 --- a/market.cpp +++ b/market.cpp @@ -106,22 +106,21 @@ void Market::Writeout(Aoutfile *f) f->PutInt(baseprice); } -void Market::Readin(Ainfile *f) +void Market::Readin(istream& f) { - AString *temp; - type = f->GetInt(); - - temp = f->GetStr(); - item = LookupItem(temp); - delete temp; - - price = f->GetInt(); - amount = f->GetInt(); - minpop = f->GetInt(); - maxpop = f->GetInt(); - minamt = f->GetInt(); - maxamt = f->GetInt(); - baseprice = f->GetInt(); + AString temp; + f >> type; + + f >> ws >> temp; + item = LookupItem(&temp); + + f >> price; + f >> amount; + f >> minpop; + f >> maxpop; + f >> minamt; + f >> maxamt; + f >> baseprice; } AString Market::Report() @@ -144,10 +143,11 @@ void MarketList::Writeout(Aoutfile *f) forlist (this) ((Market *) elem)->Writeout(f); } -void MarketList::Readin(Ainfile *f) +void MarketList::Readin(istream& f) { - int n = f->GetInt(); - for (int i=0; i> n;; + for (int i = 0; i < n; i++) { Market *m = new Market; m->Readin(f); Add(m); diff --git a/market.h b/market.h index 6a02b259d..8a6297fe5 100644 --- a/market.h +++ b/market.h @@ -55,7 +55,7 @@ class Market : public AListElem { void PostTurn(int,int); void Writeout(Aoutfile * f); - void Readin(Ainfile * f); + void Readin(istream& f); AString Report(); }; @@ -63,7 +63,7 @@ class MarketList : public AList { public: void PostTurn(int,int); void Writeout(Aoutfile * f); - void Readin(Ainfile * f); + void Readin(istream& f); }; #endif diff --git a/object.cpp b/object.cpp index 87ec067c9..d5438e0ca 100644 --- a/object.cpp +++ b/object.cpp @@ -124,36 +124,39 @@ void Object::Writeout(Aoutfile *f) WriteoutFleet(f); } -void Object::Readin(Ainfile *f, AList *facs, ATL_VER v) +void Object::Readin(istream& f, AList *facs) { - AString *temp; + AString temp; - num = f->GetInt(); + f >> num; - temp = f->GetStr(); - type = LookupObject(temp); - delete temp; + f >> ws >> temp; + type = LookupObject(&temp); - incomplete = f->GetInt(); + f >> incomplete; if (name) delete name; - name = f->GetStr(); - describe = f->GetStr(); + f >> ws >> temp; + name = new AString(temp); + + f >> ws >> temp; + describe = new AString(temp); if (*describe == "none") { delete describe; describe = 0; } - inner = f->GetInt(); - prevdir = f->GetInt(); - runes = f->GetInt(); + f >> inner; + f >> prevdir; + f >> runes; // Now, fix up a save file if ALLOW_TRIVIAL_PORTAGE is allowed, just // in case it wasn't when the save file was made. if (Globals->ALLOW_TRIVIAL_PORTAGE) prevdir = -1; - int i = f->GetInt(); - for (int j=0; j> i; + for (int j = 0; j < i; j++) { Unit *temp = new Unit; - temp->Readin(f, facs, v); + temp->Readin(f, facs); if (!temp->faction) continue; temp->MoveUnit(this); @@ -447,11 +450,12 @@ void Object::WriteoutFleet(Aoutfile *f) ((Item *) elem)->Writeout(f); } -void Object::ReadinFleet(Ainfile *f) +void Object::ReadinFleet(istream &f) { if (type != O_FLEET) return; - int nships = f->GetInt(); - for (int i=0; i> nships; + for (int i = 0; i < nships; i++) { Item *ship = new Item; ship->Readin(f); if (ship->type >= 0) diff --git a/object.h b/object.h index bdb616c06..833f7ef30 100644 --- a/object.h +++ b/object.h @@ -87,7 +87,7 @@ class Object : public AListElem Object(ARegion *region); ~Object(); - void Readin(Ainfile *f, AList *, ATL_VER v); + void Readin(istream& f, AList *); void Writeout(Aoutfile *f); void Report(Areport *, Faction *, int, int, int, int, int, int, int); @@ -112,7 +112,7 @@ class Object : public AListElem void MoveObject(ARegion *toreg); // Fleets - void ReadinFleet(Ainfile *f); + void ReadinFleet(istream &f); void WriteoutFleet(Aoutfile *f); int CheckShip(int); int GetNumShips(int); diff --git a/parseorders.cpp b/parseorders.cpp index 262dd68e3..f6b76dd79 100644 --- a/parseorders.cpp +++ b/parseorders.cpp @@ -212,18 +212,18 @@ void Game::ParseError(OrdersCheck *pCheck, Unit *pUnit, Faction *pFaction, else if (pFaction) pFaction->Error(strError); } -void Game::ParseOrders(int faction, Aorders *f, OrdersCheck *pCheck) +void Game::ParseOrders(int faction, istream& f, OrdersCheck *pCheck) { Faction *fac = 0; Unit *unit = 0; int indent = 0, code, i; - AString *order, prefix; + AString order, prefix; - order = f->GetLine(); - while (order) { - AString saveorder = *order; - int getatsign = order->getat(); - AString *token = order->gettoken(); + f >> ws >> order; + while (!f.eof()) { + AString saveorder = order; + int getatsign = order.getat(); + AString *token = order.gettoken(); if (token) { code = Parse1Order(token); @@ -235,7 +235,7 @@ void Game::ParseOrders(int faction, Aorders *f, OrdersCheck *pCheck) if (fac) ParseError(pCheck, 0, fac, "No #END statement given."); delete token; - token = order->gettoken(); + token = order.gettoken(); if (!token) { ParseError(pCheck, 0, 0, "No faction number given on #atlantis line."); @@ -252,7 +252,7 @@ void Game::ParseOrders(int faction, Aorders *f, OrdersCheck *pCheck) if (!fac) break; delete token; - token = order->gettoken(); + token = order.gettoken(); if (pCheck) { if (!token) { @@ -316,7 +316,7 @@ void Game::ParseOrders(int faction, Aorders *f, OrdersCheck *pCheck) unit = 0; delete token; - token = order->gettoken(); + token = order.gettoken(); if (!token) { ParseError(pCheck, 0, fac, "UNIT without unit number."); unit = 0; @@ -348,7 +348,7 @@ void Game::ParseOrders(int faction, Aorders *f, OrdersCheck *pCheck) ParseError(pCheck, unit, fac, "FORM: cannot nest."); } else { - unit = ProcessFormOrder(unit, order, pCheck, getatsign); + unit = ProcessFormOrder(unit, &order, pCheck, getatsign); if (!pCheck && unit && unit->former && unit->former->format) unit->former->oldorders.Add(new AString(saveorder)); if (!pCheck) { @@ -392,8 +392,7 @@ void Game::ParseOrders(int faction, Aorders *f, OrdersCheck *pCheck) unit->former->oldorders.Add(new AString(saveorder)); retval = ProcessTurnOrder(unit, f, pCheck, getatsign); if (retval) { - delete order; - order = retval; + order = *retval; continue; } } else { @@ -426,7 +425,7 @@ void Game::ParseOrders(int faction, Aorders *f, OrdersCheck *pCheck) if (!pCheck && unit->former && unit->former->format) unit->former->oldorders.Add(new AString(saveorder)); - ProcessOrder(code, unit, order, pCheck); + ProcessOrder(code, unit, &order, pCheck); } else { ParseError(pCheck, 0, fac, "Order given without a unit selected."); @@ -442,7 +441,6 @@ void Game::ParseOrders(int faction, Aorders *f, OrdersCheck *pCheck) } } - delete order; if (pCheck) { if (code == O_ENDTURN || code == O_ENDFORM) indent--; @@ -452,8 +450,7 @@ void Game::ParseOrders(int faction, Aorders *f, OrdersCheck *pCheck) if (code == O_TURN || code == O_FORM) indent++; } - - order = f->GetLine(); + f >> ws >> order; } while (unit) { @@ -2024,8 +2021,7 @@ void Game::ProcessWithdrawOrder(Unit *unit, AString *o, OrdersCheck *pCheck) return; } -AString *Game::ProcessTurnOrder(Unit *unit, Aorders *f, OrdersCheck *pCheck, - int repeat) +AString *Game::ProcessTurnOrder(Unit *unit, istream& f, OrdersCheck *pCheck, int repeat) { int turnDepth = 1; int turnLast = 1; @@ -2033,19 +2029,20 @@ AString *Game::ProcessTurnOrder(Unit *unit, Aorders *f, OrdersCheck *pCheck, TurnOrder *tOrder = new TurnOrder; tOrder->repeating = repeat; - AString *order, *token; + AString order, *token; while (turnDepth) { // get the next line - order = f->GetLine(); - if (!order) { + f >> ws >> order; + if (f.eof()) { // Fake end of commands to invoke appropriate processing - order = new AString("#end"); + order = AString("#end"); } - AString saveorder = *order; + + AString saveorder = order; // In order to allow @endturn to work the same as endturn we need to check for and eat the possible @ - std::ignore = order->getat(); // we don't care about whether it was set or not, so just ignore the return value - token = order->gettoken(); + std::ignore = order.getat(); // we don't care about whether it was set or not, so just ignore the return value + token = order.gettoken(); if (token) { int i = Parse1Order(token); @@ -2114,11 +2111,9 @@ AString *Game::ProcessTurnOrder(Unit *unit, Aorders *f, OrdersCheck *pCheck, unit->former->oldorders.Add(new AString(saveorder)); delete token; } - delete order; } unit->turnorders.Add(tOrder); - return NULL; } diff --git a/production.cpp b/production.cpp index 948f28501..25c4bc467 100644 --- a/production.cpp +++ b/production.cpp @@ -63,23 +63,24 @@ void Production::Writeout(Aoutfile *f) f->PutInt(productivity); } -void Production::Readin(Ainfile *f) +void Production::Readin(istream& f) { - AString *temp; + AString temp; - temp = f->GetStr(); - itemtype = LookupItem(temp); - delete temp; + f >> ws >> temp; + itemtype = LookupItem(&temp); - amount = f->GetInt(); - baseamount = f->GetInt(); + f >> amount; + f >> baseamount; - if (itemtype == I_SILVER) temp = f->GetStr(); - else temp = new AString(ItemDefs[itemtype].pSkill); - skill = LookupSkill(temp); - delete temp; + if (itemtype == I_SILVER) + f >> ws >> temp; + else + temp = AString(ItemDefs[itemtype].pSkill); - productivity = f->GetInt(); + skill = LookupSkill(&temp); + + f >> productivity; } AString Production::WriteReport() @@ -94,10 +95,11 @@ void ProductionList::Writeout(Aoutfile *f) forlist(this) ((Production *) elem)->Writeout(f); } -void ProductionList::Readin(Ainfile *f) +void ProductionList::Readin(istream& f) { - int n = f->GetInt(); - for (int i=0; i> n; + for (int i = 0; i < n; i++) { Production *p = new Production; p->Readin(f); Add(p); diff --git a/production.h b/production.h index 0e57f573c..e1ff26d11 100644 --- a/production.h +++ b/production.h @@ -38,7 +38,7 @@ class Production : public AListElem { Production(); void Writeout(Aoutfile *); - void Readin(Ainfile *); + void Readin(istream& f); AString WriteReport(); int itemtype; @@ -55,7 +55,7 @@ class ProductionList : public AList { void AddProd(Production *); void Writeout(Aoutfile *); - void Readin(Ainfile *); + void Readin(istream& f); }; #endif diff --git a/quests.cpp b/quests.cpp index e874301a0..540b67460 100644 --- a/quests.cpp +++ b/quests.cpp @@ -77,69 +77,62 @@ AString Quest::GetRewardsStr() return quest_rewards; } -int QuestList::ReadQuests(Ainfile *f) +int QuestList::ReadQuests(istream& f) { int count, dests, rewards; Quest *quest; - AString *name; + AString name; Item *item; quests.DeleteAll(); - count = f->GetInt(); + f >> count; if (count < 0) return 0; while (count-- > 0) { quest = new Quest(); - quest->type = f->GetInt(); + f >> quest->type; switch (quest->type) { case Quest::SLAY: - quest->target = f->GetInt(); + f >> quest->target; break; case Quest::HARVEST: quest->objective.Readin(f); - quest->regionnum = f->GetInt(); + f >> quest->regionnum; break; case Quest::BUILD: - name = f->GetStr(); - quest->building = LookupObject(name); - delete name; - name = f->GetStr(); - quest->regionname = *name; - delete name; + f >> ws >> name; + quest->building = LookupObject(&name); + f >> ws >> quest->regionname; break; case Quest::VISIT: - name = f->GetStr(); - quest->building = LookupObject(name); - delete name; - dests = f->GetInt(); + f >> ws >> name; + quest->building = LookupObject(&name); + f >> dests; while (dests-- > 0) { - name = f->GetStr(); - quest->destinations.insert(name->Str()); + f >> ws >> name; + quest->destinations.insert(name.Str()); } break; case Quest::DEMOLISH: - quest->target = f->GetInt(); - quest->regionnum = f->GetInt(); + f >> quest->target; + f >> quest->regionnum; break; default: - quest->target = f->GetInt(); + f >> quest->target; quest->objective.Readin(f); - name = f->GetStr(); - quest->building = LookupObject(name); - delete name; - quest->regionnum = f->GetInt(); - name = f->GetStr(); - quest->regionname = *name; - delete name; - dests = f->GetInt(); + f >> ws >> name; + quest->building = LookupObject(&name); + f >> quest->regionnum; + f >> ws >> quest->regionname; + f >> dests; while (dests-- > 0) { - name = f->GetStr(); - quest->destinations.insert(name->Str()); + f >> ws >> name; + quest->destinations.insert(name.Str()); } break; } - rewards = f->GetInt(); + f >> rewards; while (rewards-- > 0) { item = new Item(); item->Readin(f); @@ -150,7 +143,7 @@ int QuestList::ReadQuests(Ainfile *f) quests.Add(quest); } - return 1; + return 1; } void QuestList::WriteQuests(Aoutfile *f) diff --git a/quests.h b/quests.h index 1b6a60853..df214c807 100644 --- a/quests.h +++ b/quests.h @@ -63,7 +63,7 @@ class Quest : public AListElem class QuestList : public AList { public: - int ReadQuests(Ainfile *f); + int ReadQuests(istream& f); void WriteQuests(Aoutfile *f); int CheckQuestKillTarget(Unit *u, ItemList *reward, AString *quest_rewards); diff --git a/skills.cpp b/skills.cpp index 87a6b2d66..0e8d0760c 100644 --- a/skills.cpp +++ b/skills.cpp @@ -227,27 +227,25 @@ ShowSkill::ShowSkill(int s, int l) level = l; } -void Skill::Readin(Ainfile *f) +void Skill::Readin(istream &f) { - AString *temp, *token; + AString temp, *token; - temp = f->GetStr(); - token = temp->gettoken(); + f >> ws >> temp; + token = temp.gettoken(); type = LookupSkill(token); delete token; - token = temp->gettoken(); + token = temp.gettoken(); days = token->value(); delete token; exp = 0; if (Globals->REQUIRED_EXPERIENCE) { - token = temp->gettoken(); + token = temp.gettoken(); exp = token->value(); delete token; } - - delete temp; } void Skill::Writeout(Aoutfile *f) @@ -429,10 +427,11 @@ AString SkillList::Report(int nummen) return temp; } -void SkillList::Readin(Ainfile *f) +void SkillList::Readin(istream& f) { - int n = f->GetInt(); - for (int i=0; i> n; + for (int i = 0; i < n; i++) { Skill *s = new Skill; s->Readin(f); if ((s->days == 0) && (s->exp==0)) delete s; diff --git a/skills.h b/skills.h index bad4ef3cc..7b4b633e9 100644 --- a/skills.h +++ b/skills.h @@ -133,7 +133,7 @@ class ShowSkill : public AListElem { class Skill : public AListElem { public: - void Readin(Ainfile *); + void Readin(istream& f); void Writeout(Aoutfile *); Skill * Split(int,int); /* total num, num leaving */ @@ -153,7 +153,7 @@ class SkillList : public AList { int GetStudyRate(int, int); /* Skill, num of men */ SkillList * Split(int,int); /* total men, num to split */ AString Report(int); /* Number of men */ - void Readin(Ainfile *); + void Readin(istream& f); void Writeout(Aoutfile *); }; diff --git a/unit.cpp b/unit.cpp index d762f67de..e3d6e6810 100644 --- a/unit.cpp +++ b/unit.cpp @@ -209,22 +209,31 @@ void Unit::Writeout(Aoutfile *s) } } -void Unit::Readin(Ainfile *s, AList *facs, ATL_VER v) +void Unit::Readin(istream& f, AList *facs) { - name = s->GetStr(); - describe = s->GetStr(); + AString temp; + f >> ws >> temp; + name = new AString(temp); + + f >> ws >> temp; + describe = new AString(temp); if (*describe == "none") { delete describe; describe = 0; } - num = s->GetInt(); - type = s->GetInt(); - int i = s->GetInt(); + + f >> num; + f >> type; + + int i; + f >> i; + faction = GetFaction(facs, i); - guard = s->GetInt(); + f >> guard; if (guard == GUARD_ADVANCE) guard = GUARD_NONE; if (guard == GUARD_SET) guard = GUARD_GUARD; - reveal = s->GetInt(); + + f >> reveal; /* Handle the new 'ready item', ready weapons/armor, and free */ free = 0; @@ -234,33 +243,32 @@ void Unit::Readin(Ainfile *s, AList *facs, ATL_VER v) readyArmor[i] = -1; } - free = s->GetInt(); - AString *temp; - temp = s->GetStr(); - readyItem = LookupItem(temp); - delete temp; + f >> free; + + f >> ws >> temp; + readyItem = LookupItem(&temp); for (i = 0; i < MAX_READY; i++) { - temp = s->GetStr(); - readyWeapon[i] = LookupItem(temp); - delete temp; - temp = s->GetStr(); - readyArmor[i] = LookupItem(temp); - delete temp; - } - flags = s->GetInt(); - - items.Readin(s); - skills.Readin(s); - temp = s->GetStr(); - combat = LookupSkill(temp); - delete temp; - savedmovement = s->GetInt(); - savedmovedir = s->GetInt(); - i = s->GetInt(); + f >> ws >> temp; + readyWeapon[i] = LookupItem(&temp); + f >> ws >> temp; + readyArmor[i] = LookupItem(&temp); + } + + f >> flags; + + items.Readin(f); + skills.Readin(f); + + f >> ws >> temp; + combat = LookupSkill(&temp); + + f >> savedmovement; + f >> savedmovedir; + + f >> i; while (i-- > 0) { - temp = s->GetStr(); - visited.insert(temp->Str()); - delete temp; + f >> ws >> temp; + visited.insert(temp.Str()); } } diff --git a/unit.h b/unit.h index 4cca52c23..31c63dcc3 100644 --- a/unit.h +++ b/unit.h @@ -127,7 +127,7 @@ class Unit : public AListElem void MakeWMon(char const *,int,int); void Writeout( Aoutfile *f ); - void Readin( Ainfile *f, AList *, ATL_VER v ); + void Readin(istream& f, AList *); AString SpoilsReport(void); int CanGetSpoil(Item *i);