Skip to content

Commit

Permalink
Rework file input (#147)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jt-traub authored Oct 12, 2023
1 parent c38361d commit c15f958
Show file tree
Hide file tree
Showing 26 changed files with 400 additions and 505 deletions.
125 changes: 67 additions & 58 deletions aregion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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; j<i; j++) {
for (int j = 0; j < i; j++) {
Object *temp = new Object(this);
temp->Readin(f, facs, v);
temp->Readin(f, facs);
if (temp->num >= buildingseq)
buildingseq = temp->num + 1;
objects.Add(temp);
Expand Down Expand Up @@ -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);

Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions aregion.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class TownInfo
TownInfo();
~TownInfo();

void Readin(Ainfile *, ATL_VER &);
void Readin(istream &f);
void Writeout(Aoutfile *);
int TownType();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 10 additions & 3 deletions astring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ char *AString::Str()
return str;
}


const char *AString::const_str() const
{
return str;
}

int AString::Len()
{
return len;
Expand Down Expand Up @@ -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());
Expand Down
3 changes: 2 additions & 1 deletion astring.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -58,6 +59,7 @@ class AString : public AListElem {
AString & operator=(const char *);

char *Str();
const char *const_str() const;
int Len();

AString *gettoken();
Expand All @@ -69,7 +71,6 @@ class AString : public AListElem {
AString *StripWhite();

private:

int len;
char *str;
int isEqual(const char *) const;
Expand Down
49 changes: 25 additions & 24 deletions faction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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<n; i++) {
f >> 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()
Expand Down Expand Up @@ -993,4 +994,4 @@ bool Faction::IsActivityRecorded(ARegion *region, FactionActivity type) {
}

return regionActivity.find(type) != std::end(regionActivity);
}
}
4 changes: 2 additions & 2 deletions faction.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand Down
Loading

0 comments on commit c15f958

Please sign in to comment.