text files are now read with scan(fhstream&...) functions and written with print(...) functions

This commit is contained in:
Zeno Rogue 2019-05-21 23:59:07 +02:00
parent ae100b1e72
commit 8518f97ded
5 changed files with 87 additions and 85 deletions

19
hyper.h
View File

@ -674,6 +674,7 @@ string XLAT(string x); // translate the sentence x
string XLATN(string x); // translate the sentence x
string cts(char c); // character to string
string its(int i); // int to string
string itsh8(int i); // int to string (8 hex digits)
// a random integer from [0..i), generated by the game's main generator
// we want the same world to be generated if the seed is the same. For this purpose,
@ -4652,6 +4653,23 @@ template<class C, class C1, class... CS> void print(hstream& hs, const C& c, con
template<class... CS> void println(hstream& hs, const CS&... cs) { print(hs, cs...); hs.write_char('\n'); }
inline string spaced(int i) { return its(i); }
inline string spaced(color_t col) { return itsh8(col); }
inline string spaced(const string& s) { return s; }
inline string spaced(ld x) { return fts(x, 10); }
template<class T> string spaced_of(T a[], int q) { string s = spaced(a[0]); for(int i=1; i<q; i++) s += ' ', s += spaced(a[i]); return s; }
template<class T, int i> string spaced(const array<T,i>& a) { return spaced_of(&a[0], isize(a)); }
template<class C, class C1, class... CS> string spaced(const C& c, const C1& c1, const CS&... cs) { return spaced(c) + " " + spaced(c1, cs...); }
bool scan(fhstream& hs, int&);
bool scan(fhstream& hs, ld&);
bool scan(fhstream& hs, string&);
bool scan(fhstream& hs, color_t& c);
template<class C, class C1, class... CS> bool scan(fhstream& hs, C& c, C1& c1, CS&... cs) { return scan(hs, c) && scan(hs, c1, cs...); }
string scanline(fhstream& hs);
template<class T> T scan(fhstream& hs) { T t {}; scan(hs, t); return t; }
// copied from: https://stackoverflow.com/questions/16387354/template-tuple-calling-a-function-on-each-element
namespace detail
@ -4681,6 +4699,7 @@ void for_each_in_tuple(std::tuple<Ts...> const& t, F f)
inline void print(hstream& hs, const string& s) { hs.write_chars(s.c_str(), isize(s)); }
inline void print(hstream& hs, int i) { print(hs, its(i)); }
inline void print(hstream& hs, ld x) { print(hs, fts(x)); }
inline void print(hstream& hs, color_t col) { print(hs, itsh8(col)); }
template<class T> void print(hstream& hs, const walker<T>& w) { print(hs, "[", w.at, "/", w.spin, "/", w.mirrored, "]"); }

View File

@ -812,36 +812,35 @@ void start_game_on_created_map() {
}
bool save_map(const string& fname) {
FILE *f = fopen(fname.c_str(), "wt");
if(!f) return false;
fhstream f(fname, "wt");
if(!f.f) return false;
auto& all = base->allcells();
int origcells = 0;
for(cellinfo& ci: cells)
if(ci.generation == 0)
origcells++;
fprintf(f, "%d %d %d\n", geometry, isize(all), origcells);
println(f, spaced(int(geometry), isize(all), origcells));
for(auto h: all) {
origcells = 0;
for(auto i: cells_of_heptagon[h->master])
if(cells[i].generation == 0)
origcells++;
fprintf(f, "%d\n", origcells);
println(f, origcells);
for(auto i: cells_of_heptagon[h->master]) if(cells[i].generation == 0) {
auto &ci = cells[i];
fprintf(f, "%lf %lf %lf\n", double(ci.p[0]), double(ci.p[1]), double(ci.p[GDIM]));
println(f, spaced(ci.p[0], ci.p[1], ci.p[GDIM]));
}
}
fclose(f);
return true;
}
bool load_map(const string &fname) {
FILE *f = fopen(fname.c_str(), "rt");
if(!f) return false;
fhstream f(fname, "rt");
if(!f.f) return false;
auto& all = base->allcells();
int g, sa;
ignore(fscanf(f, "%d %d %d\n", &g, &sa, &cellcount));
scan(f, g, sa, cellcount);
if(sa != isize(all) || g != geometry) { printf("bad parameters\n"); addMessage(XLAT("bad format or bad map geometry")); return false; }
density = cellcount * 1. / isize(all);
@ -849,20 +848,19 @@ bool load_map(const string &fname) {
for(auto h: all) {
int q = 0;
ignore(fscanf(f, "%d\n", &q));
scan(f, q);
if(q < 0 || q > cellcount) { runlevel = 0; return false; }
while(q--) {
cells.emplace_back();
cellinfo& s = cells.back();
s.patterndir = -1;
double a, b, c;
ignore(fscanf(f, "%lf%lf%lf", &a, &b, &c));
scan(f, a, b, c);
s.p = hpxyz(a, b, c);
for(auto c0: all) s.relmatrices[c0] = calc_relative_matrix(c0, h, s.p);
s.owner = h;
}
}
fclose(f);
make_cells_of_heptagon();
runlevel = 2;

View File

@ -1622,19 +1622,13 @@ namespace mapeditor {
if(uni == COLORKEY) dsCur->color = colortouse;
}
void writeHyperpoint(FILE *f, hyperpoint h) {
for(int i=0; i<MDIM; i++) fprintf(f, "%lf ", double(h[i]));
fprintf(f, "\n");
void writeHyperpoint(hstream& f, hyperpoint h) {
println(f, spaced_of(&h[0], MDIM));
}
hyperpoint readHyperpoint(FILE *f) {
hyperpoint readHyperpoint(fhstream& f) {
hyperpoint h;
for(int i=0; i<MDIM; i++) {
double d;
int err = fscanf(f, "%lf", &d);
if(err != 1) printf("Warning: read error\n");
h[i] = d;
}
for(int i=0; i<MDIM; i++) scan(f, h[i]);
return h;
}
@ -1645,24 +1639,24 @@ namespace mapeditor {
bool onelayeronly;
bool loadPicFile(const string& s) {
FILE *f = fopen(picfile.c_str(), "rt");
if(!f) {
fhstream f(picfile, "rt");
if(!f.f) {
addMessage(XLAT("Failed to load pictures from %1", picfile));
return false;
}
int err;
char buf[200];
if(!fgets(buf, 200, f)) {
addMessage(XLAT("Failed to load pictures from %1", picfile));
fclose(f); return false;
}
int vernum; err = fscanf(f, "%x", &vernum);
scanline(f);
color_t vernum;
scan(f, vernum);
printf("vernum = %x\n", vernum);
if(vernum == 0) {
addMessage(XLAT("Failed to load pictures from %1", picfile));
return false;
}
if(vernum >= 0xA0A0) {
int tg, wp;
int nt;
hr::ignore(fscanf(f, "%d%d%d%d\n", &tg, &nt, &wp, &patterns::subpattern_flags));
scan(f, tg, nt, wp, patterns::subpattern_flags);
patterns::whichPattern = patterns::ePattern(wp);
set_geometry(eGeometry(tg));
set_variation(eVariation(nt));
@ -1670,9 +1664,10 @@ namespace mapeditor {
}
while(true) {
int i, j, l, sym, rots, color, siz;
err = fscanf(f, "%d%d%d%d%d%x%d", &i, &j, &l, &sym, &rots, &color, &siz);
if(i == -1 || err < 6) break;
int i, j, l, sym, rots, siz;
color_t color;
if(!scan(f, i, j, l, sym, rots, color, siz)) break;
if(i == -1) break;
if(siz < 0 || siz > 1000) break;
if(i >= 4) {
@ -1683,19 +1678,18 @@ namespace mapeditor {
initShape(i, j);
usershapelayer& ds(usershapes[i][j]->d[l]);
if(VERNUM_HEX >= 0xA608) { double z; err = fscanf(f, "%lf", &z); ds.zlevel = z; }
if(vernum >= 0xA608) scan(f, ds.zlevel);
ds.shift = readHyperpoint(f);
ds.spin = readHyperpoint(f);
ds.list.clear();
for(int i=0; i<siz; i++) {
ds.list.push_back(readHyperpoint(f));
writeHyperpoint(stdout, ds.list[i]);
writeHyperpoint(hlog, ds.list[i]);
}
ds.sym = sym;
ds.rots = rots;
ds.color = color;
}
fclose(f);
addMessage(XLAT("Pictures loaded from %1", picfile));
buildpolys();
@ -1703,33 +1697,31 @@ namespace mapeditor {
}
bool savePicFile(const string& s) {
FILE *f = fopen(picfile.c_str(), "wt");
if(!f) {
fhstream f(picfile, "wt");
if(!f.f) {
addMessage(XLAT("Failed to save pictures to %1", picfile));
return false;
}
fprintf(f, "HyperRogue saved picture\n");
fprintf(f, "%x\n", VERNUM_HEX);
println(f, "HyperRogue saved picture");
println(f, format("%x\n", VERNUM_HEX));
if(VERNUM_HEX >= 0xA0A0)
fprintf(f, "%d %d %d %d\n", geometry, int(variation), patterns::whichPattern, patterns::subpattern_flags);
println(f, spaced(geometry, int(variation), patterns::whichPattern, patterns::subpattern_flags));
for(int i=0; i<USERSHAPEGROUPS; i++) for(auto usp: usershapes[i]) {
usershape *us = usp.second;
if(!us) continue;
for(int l=0; l<USERLAYERS; l++) if(isize(us->d[l].list)) {
usershapelayer& ds(us->d[l]);
fprintf(f, "\n%d %d %d %d %d %6x %d\n",
i, usp.first, l, ds.sym, ds.rots, ds.color, int(isize(ds.list)));
fprintf(f, "\n%lf", double(ds.zlevel));
println(f, spaced(i, usp.first, l, ds.sym, ds.rots, ds.color, int(isize(ds.list))));
print(f, spaced(ds.zlevel), " ");
writeHyperpoint(f, ds.shift);
writeHyperpoint(f, ds.spin);
fprintf(f,"\n");
println(f);
for(int i=0; i<isize(ds.list); i++)
writeHyperpoint(f, ds.list[i]);
}
}
fprintf(f, "\n-1\n");
fclose(f);
println(f, "-1");
addMessage(XLAT("Pictures saved to %1", picfile));
return true;
}
@ -1916,13 +1908,13 @@ namespace mapeditor {
for(int l=0; l<USERLAYERS; l++) if(isize(us->d[l].list)) {
usershapelayer& ds(us->d[l]);
printf("// %d %d %d [%06X %lf]\n", i, usp.first, l, ds.color, double(ds.zlevel));
printf(" ID, %d, %d, ", us->d[l].rots, us->d[l].sym?2:1);
println(hlog, spaced("//", i, usp.first, l, "[", ds.color, double(ds.zlevel), "]"));
print(hlog, " ID, ", us->d[l].rots, ", ", us->d[l].sym?2:1, ", ");
for(int i=0; i<isize(us->d[l].list); i++) {
for(int d=0; d<DIM; d++) printf("%lf,", double(us->d[l].list[i][d]));
printf(" ");
for(int d=0; d<DIM; d++) print(hlog, fts(us->d[l].list[i][d]), ", ");
print(hlog, " ");
}
printf("\n");
println(hlog);
}
}
}

View File

@ -134,28 +134,25 @@ namespace hr { namespace netgen {
void loadData() {
FILE *f = fopen("papermodeldata.txt", "rt");
if(!f) return;
fhstream f("papermodeldata.txt", "rt");
if(!f.f) return;
int err = fscanf(f, "%d %d %d %d %d %d %d %lf %d\n\n",
&CELLS, &SX, &SY, &PX, &PY, &SCALE, &BASE, &el, &created);
if(err != 9) { fclose(f); return; }
if(!scan(f, CELLS, SX, SY, PX, PY, SCALE, BASE, el, created)) return;
loaded = true;
if(!created) { fclose(f); return; }
if(!created) return;
for(int i=0; i<CELLS; i++) err = fscanf(f, "%d", &ct[i]);
for(int i=0; i<CELLS; i++) scan(f, ct[i]);
for(int i=0; i<CELLS; i++) for(int j=0; j<16; j++)
err = fscanf(f, "%lf" ,&vx[i][j]);
for(int i=0; i<CELLS; i++) for(int j=0; j<16; j++) scan(f, vx[i][j]);
for(int i=0; i<CELLS; i++)
for(int j=0; j<7; j++) nei[i][j] = -1;
while(true) {
int a, b, c;
err = fscanf(f, "%d%d%d", &a, &b, &c);
int a, b, c;
scan(f, a, b, c);
if(a < 0) break;
else nei[a][c] = b;
}
@ -163,51 +160,41 @@ namespace hr { namespace netgen {
for(int i=0; i<CELLS; i++) {
double dx, dy, dr;
int g;
err = fscanf(f, "%lf%lf%lf%d\n", &dx, &dy, &dr, &g);
scan(f, dx, dy, dr, g);
center[i] = vec(dx, dy);
rot[i] = dr;
glued[i] = g;
}
fclose(f);
}
void saveData() {
// global parameters
FILE *f = fopen("papermodeldata2.txt", "wt");
if(!f) {
fhstream f("papermodeldata2.txt", "wt");
if(!f.f) {
addMessage("Could not save the paper model data");
return;
}
fprintf(f, "%d %d %d %d %d %d %d %lf %d\n\n", CELLS, SX, SY, PX, PY, SCALE, BASE, el, created);
println(f, spaced(CELLS, SX, SY, PX, PY, SCALE, BASE, el, created), "\n");
// net parameters: cell types
for(int i=0; i<CELLS; i++)
fprintf(f, "%d ", ct[i]);
fprintf(f, "\n");
println(f, spaced_of(ct, CELLS));
// net parameters: hcenters
for(int i=0; i<CELLS; i++) {
for(int k=0; k<16; k++)
fprintf(f, "%9.6lf ", vx[i][k]);
fprintf(f, "\n");
println(f, spaced_of(vx[i], 16));
}
fprintf(f, "\n\n");
println(f, "\n");
// create netgen
for(int i=0; i<CELLS; i++) for(int j=0; j<CELLS; j++) {
for(int k=0; k<ct[i]; k++) if(nei[i][k] == j)
fprintf(f, "%d %d %d ", i, j, k);
print(f, spaced(i, j, k), " ");
}
fprintf(f, "-1 -1 -1\n\n");
println(f, "-1 -1 -1");
// graphics
for(int i=0; i<CELLS; i++)
fprintf(f, "%12.7lf %12.7lf %10.7lf %d\n",
center[i].x, center[i].y, rot[i], glued[i]
);
fclose(f);
println(f, spaced(center[i].x, center[i].y, rot[i], glued[i]));
}
// Simple graphical functions

View File

@ -39,6 +39,12 @@ string ftssmart(ld x) {
return buf;
}
bool scan(fhstream& hs, int& i) { return fscanf(hs.f, "%d", &i) == 1; }
bool scan(fhstream& hs, color_t& c) { return fscanf(hs.f, "%x", &c) == 1; }
bool scan(fhstream& hs, ld& x) { return fscanf(hs.f, "%lf", &x) == 1; }
bool scan(fhstream& hs, string& s) { char t[10000]; t[0] = 0; int err = fscanf(hs.f, "%9500s", t); s = t; return err == 1 && t[0]; }
string scanline(fhstream& hs) { char buf[10000]; buf[0] = 0; fgets(buf, 10000, hs.f); return buf; }
/*
string fts_smartdisplay(ld x, int maxdisplay) {
string rv;