Skip to content

Commit

Permalink
nix sprintf in userlogs, sql_file & graph gen
Browse files Browse the repository at this point in the history
  • Loading branch information
yakra committed May 12, 2024
1 parent fd96ac6 commit 2349b1f
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 86 deletions.
12 changes: 4 additions & 8 deletions siteupdate/cplusplus/classes/GraphGeneration/HGEdge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,25 +146,21 @@ void HGEdge::detach(unsigned char fmt_mask)
}

// write line to tmg collapsed edge file
void HGEdge::collapsed_tmg_line(std::ofstream& file, char* fstr, unsigned int threadnum, std::list<HighwaySystem*> *systems)
void HGEdge::collapsed_tmg_line(std::ofstream& file, unsigned int threadnum, std::list<HighwaySystem*> *systems)
{ file << vertex1->c_vertex_num[threadnum] << ' ' << vertex2->c_vertex_num[threadnum] << ' ';
segment->write_label(file, systems);
for (HGVertex *intermediate : intermediate_points)
{ sprintf(fstr, " %.15g %.15g", intermediate->lat, intermediate->lng);
file << fstr;
}
file << ' ' << intermediate->lat << ' ' << intermediate->lng;
file << '\n';
}

// write line to tmg traveled edge file
void HGEdge::traveled_tmg_line(std::ofstream& file, char* fstr, unsigned int threadnum, std::list<HighwaySystem*> *systems, bool trav, char* code)
void HGEdge::traveled_tmg_line(std::ofstream& file, unsigned int threadnum, std::list<HighwaySystem*> *systems, bool trav, char* code)
{ file << vertex1->t_vertex_num[threadnum] << ' ' << vertex2->t_vertex_num[threadnum] << ' ';
segment->write_label(file, systems);
file << ' ' << (trav ? segment->clinchedby_code(code, threadnum) : "0");
for (HGVertex *intermediate : intermediate_points)
{ sprintf(fstr, " %.15g %.15g", intermediate->lat, intermediate->lng);
file << fstr;
}
file << ' ' << intermediate->lat << ' ' << intermediate->lng;
file << '\n';
}

Expand Down
4 changes: 2 additions & 2 deletions siteupdate/cplusplus/classes/GraphGeneration/HGEdge.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class HGEdge

void detach();
void detach(unsigned char);
void collapsed_tmg_line(std::ofstream&, char*, unsigned int, std::list<HighwaySystem*>*);
void traveled_tmg_line (std::ofstream&, char*, unsigned int, std::list<HighwaySystem*>*, bool, char*);
void collapsed_tmg_line(std::ofstream&, unsigned int, std::list<HighwaySystem*>*);
void traveled_tmg_line (std::ofstream&, unsigned int, std::list<HighwaySystem*>*, bool, char*);
std::string debug_tmg_line(std::list<HighwaySystem*> *, unsigned int);
std::string str();
std::string intermediate_point_string();
Expand Down
42 changes: 22 additions & 20 deletions siteupdate/cplusplus/classes/GraphGeneration/HighwayGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ void HighwayGraph::clear_vbit(HGVertex* v, const int threadnum)
// for intermediate "shaping points" along the edge, ordered from endpoint 1 to endpoint 2.
//
void HighwayGraph::write_master_graphs_tmg()
{ std::ofstream simplefile(Args::graphfilepath + "/tm-master-simple.tmg");
std::ofstream collapfile(Args::graphfilepath + "/tm-master.tmg");
std::ofstream travelfile(Args::graphfilepath + "/tm-master-traveled.tmg");
{ std::ofstream simplefile(Args::graphfilepath + "/tm-master-simple.tmg"); simplefile.precision(15);
std::ofstream collapfile(Args::graphfilepath + "/tm-master.tmg"); collapfile.precision(15);
std::ofstream travelfile(Args::graphfilepath + "/tm-master-traveled.tmg"); travelfile.precision(15);
simplefile << "TMG 1.0 simple\n";
collapfile << "TMG 1.0 collapsed\n";
travelfile << "TMG 2.0 traveled\n";
Expand All @@ -213,13 +213,14 @@ void HighwayGraph::write_master_graphs_tmg()
unsigned int sv = 0;
unsigned int cv = 0;
unsigned int tv = 0;
char fstr[57];
for (HGVertex& v : vertices)
{ sprintf(fstr, " %.15g %.15g", v.lat, v.lng);
{ auto write_vertex = [&](std::ofstream& tmg)
{ tmg << *(v.unique_name) << ' ' << v.lat << ' ' << v.lng << '\n';
};
switch (v.visibility) // fall-thru is a Good Thing!
{ case 2: collapfile << *(v.unique_name) << fstr << '\n'; v.c_vertex_num[0] = cv++;
case 1: travelfile << *(v.unique_name) << fstr << '\n'; v.t_vertex_num[0] = tv++;
default: simplefile << *(v.unique_name) << fstr << '\n'; v.s_vertex_num[0] = sv++;
{ case 2: write_vertex(collapfile); v.c_vertex_num[0] = cv++;
case 1: write_vertex(travelfile); v.t_vertex_num[0] = tv++;
default: write_vertex(simplefile); v.s_vertex_num[0] = sv++;
}
}
// now edges, only write if not already written
Expand All @@ -232,13 +233,13 @@ void HighwayGraph::write_master_graphs_tmg()
{ case 2: for (HGEdge *e : v.incident_c_edges)
if (!(e->written[0] & HGEdge::collapsed))
{ e->written[0] |= HGEdge::collapsed;
e->collapsed_tmg_line(collapfile, fstr, 0, 0);
e->collapsed_tmg_line(collapfile, 0, 0);
}
case 1: for (HGEdge *e : v.incident_t_edges)
if (!(e->written[0] & HGEdge::traveled))
{ e->written[0] |= HGEdge::traveled;
for (char*n=cbycode; n<cbycode+nibbles; ++n) *n = '0';
e->traveled_tmg_line(travelfile, fstr, 0, 0, TravelerList::allusers.size, cbycode);
e->traveled_tmg_line(travelfile, 0, 0, TravelerList::allusers.size, cbycode);
}
default: for (HGEdge *e : v.incident_s_edges)
if (!(e->written[0] & HGEdge::simple))
Expand Down Expand Up @@ -274,9 +275,9 @@ void HighwayGraph::write_subgraphs_tmg
{ unsigned int cv_count = 0;
unsigned int tv_count = 0;
GraphListEntry* g = GraphListEntry::entries.data()+graphnum;
std::ofstream simplefile(Args::graphfilepath+'/'+g -> filename());
std::ofstream collapfile(Args::graphfilepath+'/'+g[1].filename());
std::ofstream travelfile(Args::graphfilepath+'/'+g[2].filename());
std::ofstream simplefile(Args::graphfilepath+'/'+g -> filename()); simplefile.precision(15);
std::ofstream collapfile(Args::graphfilepath+'/'+g[1].filename()); collapfile.precision(15);
std::ofstream travelfile(Args::graphfilepath+'/'+g[2].filename()); travelfile.precision(15);
std::vector<HGVertex*> mv; // vertices matching all criteria
std::vector<HGEdge*> mse, mce, mte; // matching simple/collapsed/traveled edges
std::vector<TravelerList*> traveler_lists;
Expand Down Expand Up @@ -311,13 +312,14 @@ void HighwayGraph::write_subgraphs_tmg
unsigned int sv = 0;
unsigned int cv = 0;
unsigned int tv = 0;
char fstr[57];
for (HGVertex *v : mv)
{ sprintf(fstr, " %.15g %.15g", v->lat, v->lng);
{ auto write_vertex = [&](std::ofstream& tmg)
{ tmg << *(v->unique_name) << ' ' << v->lat << ' ' << v->lng << '\n';
};
switch(v->visibility) // fall-thru is a Good Thing!
{ case 2: collapfile << *(v->unique_name) << fstr << '\n'; v->c_vertex_num[threadnum] = cv++;
case 1: travelfile << *(v->unique_name) << fstr << '\n'; v->t_vertex_num[threadnum] = tv++;
default: simplefile << *(v->unique_name) << fstr << '\n'; v->s_vertex_num[threadnum] = sv++;
{ case 2: write_vertex(collapfile); v->c_vertex_num[threadnum] = cv++;
case 1: write_vertex(travelfile); v->t_vertex_num[threadnum] = tv++;
default: write_vertex(simplefile); v->s_vertex_num[threadnum] = sv++;
}
}
// write edges
Expand All @@ -328,14 +330,14 @@ void HighwayGraph::write_subgraphs_tmg
simplefile << '\n';
}
for (HGEdge *e : mce)
e->collapsed_tmg_line(collapfile, fstr, threadnum, g->systems);
e->collapsed_tmg_line(collapfile, threadnum, g->systems);
size_t nibbles = ceil(double(travnum)/4);
char* cbycode = new char[nibbles+1];
// deleted after writing edges
cbycode[nibbles] = 0;
for (HGEdge *e : mte)
{ for (char*n=cbycode; n<cbycode+nibbles; ++n) *n = '0';
e->traveled_tmg_line (travelfile, fstr, threadnum, g->systems, travnum, cbycode);
e->traveled_tmg_line (travelfile, threadnum, g->systems, travnum, cbycode);
}
delete[] cbycode;
// traveler names
Expand Down
67 changes: 43 additions & 24 deletions siteupdate/cplusplus/classes/TravelerList/userlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,23 @@
#include <fstream>

void TravelerList::userlog(const double total_active_only_miles, const double total_active_preview_miles)
{ char fstr[112];
std::cout << "." << std::flush;
{ std::cout << "." << std::flush;
std::ofstream log(Args::logfilepath+"/users/"+traveler_name+".log", std::ios::app);
log.setf(std::ios::fixed);
log.precision(2);
auto log_clinched_mi = [&](double clinched, double total)->std::ofstream&
{ log << clinched << " of " << total << " mi ";
if (total)
log << "(" << 100*clinched/total << "%)";
else log << "-.--%";
return log;
};

log << "Clinched Highway Statistics\n";
log << "Overall in active systems: " << format_clinched_mi(fstr, active_only_miles(), total_active_only_miles) << '\n';
log << "Overall in active+preview systems: " << format_clinched_mi(fstr, active_preview_miles(), total_active_preview_miles) << '\n';
log << "Overall in active systems: ";
log_clinched_mi(active_only_miles(), total_active_only_miles) << '\n';
log << "Overall in active+preview systems: ";
log_clinched_mi(active_preview_miles(), total_active_preview_miles) << '\n';

log << "Overall by region: (each line reports active only then active+preview)\n";
std::list<Region*> travregions;
Expand All @@ -24,8 +35,9 @@ void TravelerList::userlog(const double total_active_only_miles, const double to
{ double t_active_miles = 0;
if (active_only_mileage_by_region.count(region))
t_active_miles = active_only_mileage_by_region.at(region);
log << region->code << ": " << format_clinched_mi(fstr, t_active_miles, region->active_only_mileage) << ", "
<< format_clinched_mi(fstr, active_preview_mileage_by_region.at(region), region->active_preview_mileage) << '\n';
log << region->code << ": ";
log_clinched_mi(t_active_miles, region->active_only_mileage) << ", ";
log_clinched_mi(active_preview_mileage_by_region.at(region), region->active_preview_mileage) << '\n';
}
unsigned int active_systems_traveled = 0;
unsigned int active_systems_clinched = 0;
Expand All @@ -46,8 +58,9 @@ void TravelerList::userlog(const double total_active_only_miles, const double to
// the DB, but add to logs only if it's been traveled at
// all and it covers multiple regions
auto& sysmbr = h->mileage_by_region;
log << "System " << h->systemname << " (" << h->level_name() << ") overall: "
<< format_clinched_mi(fstr, t_system_overall, h->total_mileage()) << '\n';
log.precision(2);
log << "System " << h->systemname << " (" << h->level_name() << ") overall: ";
log_clinched_mi(t_system_overall, h->total_mileage()) << '\n';
if (sysmbr.size() > 1)
{ log << "System " << h->systemname << " by region:\n";
std::list<Region*> sysregions;
Expand All @@ -59,7 +72,8 @@ void TravelerList::userlog(const double total_active_only_miles, const double to
auto it = system_region_mileages.at(h).find(region);
if (it != system_region_mileages.at(h).end())
system_region_mileage = it->second;
log << " " << region->code << ": " << format_clinched_mi(fstr, system_region_mileage, sysmbr.at(region)) << '\n';
log << " " << region->code << ": ";
log_clinched_mi(system_region_mileage, sysmbr.at(region)) << '\n';
}
}

Expand All @@ -85,12 +99,14 @@ void TravelerList::userlog(const double total_active_only_miles, const double to
{ num_con_rtes_traveled += 1;
num_con_rtes_clinched += (con_clinched_miles == cr.mileage);
ccr_values.emplace_back(&cr, con_clinched_miles);
log << cr.readable_name() << ": " << format_clinched_mi(fstr, con_clinched_miles, cr.mileage) << '\n';
log << cr.readable_name() << ": ";
log_clinched_mi(con_clinched_miles, cr.mileage) << '\n';
if (roots.size() == 1)
log << " (" << roots[0]->readable_name() << " only)\n";
else { for (auto& rm : chop_mi)
log << " " << rm.first->readable_name() << ": "
<< format_clinched_mi(fstr, rm.second,rm.first->mileage) << "\n";
{ log << " " << rm.first->readable_name() << ": ";
log_clinched_mi(rm.second,rm.first->mileage) << "\n";
}
log << '\n';
}
}
Expand All @@ -99,23 +115,26 @@ void TravelerList::userlog(const double total_active_only_miles, const double to
if (h->active())
active_systems_clinched++;
else preview_systems_clinched++;
sprintf(fstr, " connected routes traveled: %i of %i (%.1f%%), clinched: %i of %i (%.1f%%).",
num_con_rtes_traveled, (int)h->con_routes.size, 100*(double)num_con_rtes_traveled/h->con_routes.size,
num_con_rtes_clinched, (int)h->con_routes.size, 100*(double)num_con_rtes_clinched/h->con_routes.size);
log << "System " << h->systemname << fstr << '\n';
log.precision(1);
log << "System " << h->systemname
<< " connected routes traveled: " << num_con_rtes_traveled << " of " << h->con_routes.size
<< " (" << 100*(double)num_con_rtes_traveled/h->con_routes.size
<< "%), clinched: " << num_con_rtes_clinched << " of " << h->con_routes.size
<< " (" << 100*(double)num_con_rtes_clinched/h->con_routes.size << "%).\n";
}
}
log.precision(1); // for the few users with no traveled mileage

// grand summary, active only
sprintf(fstr,"\nTraveled %i of %i (%.1f%%), Clinched %i of %i (%.1f%%) active systems",
active_systems_traveled, HighwaySystem::num_active, 100*(double)active_systems_traveled/HighwaySystem::num_active,
active_systems_clinched, HighwaySystem::num_active, 100*(double)active_systems_clinched/HighwaySystem::num_active);
log << fstr << '\n';
log << "\nTraveled " << active_systems_traveled << " of " << HighwaySystem::num_active
<< " (" << 100*(double)active_systems_traveled/HighwaySystem::num_active
<< "%), Clinched " << active_systems_clinched << " of " << HighwaySystem::num_active
<< " (" << 100*(double)active_systems_clinched/HighwaySystem::num_active << "%) active systems\n";
// grand summary, active+preview
sprintf(fstr,"Traveled %i of %i (%.1f%%), Clinched %i of %i (%.1f%%) preview systems",
preview_systems_traveled, HighwaySystem::num_preview, 100*(double)preview_systems_traveled/HighwaySystem::num_preview,
preview_systems_clinched, HighwaySystem::num_preview, 100*(double)preview_systems_clinched/HighwaySystem::num_preview);
log << fstr << '\n';
log << "Traveled " << preview_systems_traveled << " of " << HighwaySystem::num_preview
<< " (" << 100*(double)preview_systems_traveled/HighwaySystem::num_preview
<< "%), Clinched " << preview_systems_clinched << " of " << HighwaySystem::num_preview
<< " (" << 100*(double)preview_systems_clinched/HighwaySystem::num_preview << "%) preview systems\n";

// updated routes, sorted by date
log << "\nMost recent updates for listed routes:\n";
Expand Down
40 changes: 18 additions & 22 deletions siteupdate/cplusplus/functions/sql_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ void sqlfile1
std::list<std::string*> *updates,
std::list<std::string*> *systemupdates,
std::mutex* term_mtx
){ char fstr[65];
// Once all data is read in and processed, create a .sql file that will
){ // Once all data is read in and processed, create a .sql file that will
// create all of the DB tables to be used by other parts of the project
std::ofstream sqlfile(Args::databasename+".sql");
sqlfile.setf(std::ios::floatfield);
sqlfile.precision(16);
// Note: removed "USE" line, DB name must be specified on the mysql command line

// we have to drop tables in the right order to avoid foreign key errors
Expand Down Expand Up @@ -138,9 +139,8 @@ void sqlfile1
for (Route& r : h.routes)
{ if (!first) sqlfile << ',';
first = 0;
sprintf(fstr, "%.17g", r.mileage);
sqlfile << "('" << r.system->systemname << "','" << r.region->code << "','" << r.route << "','" << r.banner << "','" << r.abbrev
<< "','" << double_quotes(r.city) << "','" << r.root << "','" << fstr << "','" << r.rootOrder << "','" << csvOrder << "')\n";
<< "','" << double_quotes(r.city) << "','" << r.root << "','" << r.mileage << "','" << r.rootOrder << "','" << csvOrder << "')\n";
csvOrder += 1;
}
sqlfile << ";\n";
Expand All @@ -162,9 +162,8 @@ void sqlfile1
for (ConnectedRoute& cr : h.con_routes)
{ if (!first) sqlfile << ',';
first = 0;
sprintf(fstr, "','%.17g'", cr.mileage);
sqlfile << "('" << cr.system->systemname << "','" << cr.route << "','" << cr.banner << "','" << double_quotes(cr.groupname)
<< "','" << (cr.roots.size() ? cr.roots[0]->root.data() : "ERROR_NO_ROOTS") << fstr << ",'" << csvOrder << "')\n";
sqlfile << "('" << cr.system->systemname << "','" << cr.route << "','" << cr.banner << "','" << double_quotes(cr.groupname) << "','"
<< (cr.roots.size() ? cr.roots[0]->root.data() : "ERROR_NO_ROOTS") << "','" << cr.mileage << "','" << csvOrder << "')\n";
csvOrder += 1;
}
sqlfile << ";\n";
Expand Down Expand Up @@ -192,6 +191,8 @@ void sqlfile1
#ifndef threading_enabled
std::cout << et->et() << "...waypoints" << std::endl;
#endif
sqlfile.unsetf(std::ios::floatfield);
sqlfile.precision(15);
sqlfile << "CREATE TABLE waypoints (pointId INTEGER, pointName VARCHAR(" << DBFieldLength::label
<< "), latitude DOUBLE, longitude DOUBLE, root VARCHAR(" << DBFieldLength::root
<< "), PRIMARY KEY(pointId), FOREIGN KEY (root) REFERENCES routes(root));\n";
Expand All @@ -203,9 +204,8 @@ void sqlfile1
for (Waypoint& w : r.points)
{ if (!first) sqlfile << ',';
first = 0;
sqlfile << "('" << point_num << "','" << w.label; int
e=sprintf(fstr,"','%.15g",w.lat); if (w.lat==int(w.lat)) strcpy(fstr+e,".0"); sqlfile << fstr;
e=sprintf(fstr,"','%.15g",w.lng); if (w.lng==int(w.lng)) strcpy(fstr+e,".0"); sqlfile << fstr;
sqlfile << "('" << point_num << "','" << w.label;
sqlfile << "','" << w.lat << "','" << w.lng;
sqlfile << "','" << r.root + "')\n";
point_num+=1;
}
Expand All @@ -220,6 +220,8 @@ void sqlfile1
#ifndef threading_enabled
std::cout << et->et() << "...segments" << std::endl;
#endif
sqlfile.setf(std::ios::floatfield);
sqlfile.precision(16);
sqlfile << "CREATE TABLE segments (segmentId INTEGER, waypoint1 INTEGER, waypoint2 INTEGER, root VARCHAR(" << DBFieldLength::root
<< "), PRIMARY KEY (segmentId), FOREIGN KEY (waypoint1) REFERENCES waypoints(pointId), "
<< "FOREIGN KEY (waypoint2) REFERENCES waypoints(pointId), FOREIGN KEY (root) REFERENCES routes(root));\n";
Expand Down Expand Up @@ -275,8 +277,7 @@ void sqlfile1
{ if (region.active_only_mileage+region.active_preview_mileage == 0) continue;
if (!first) sqlfile << ',';
first = 0;
sprintf(fstr, "','%.17g','%.17g')\n", region.active_only_mileage, region.active_preview_mileage);
sqlfile << "('" << region.code << fstr;
sqlfile << "('" << region.code << "','" << region.active_only_mileage << "','" << region.active_preview_mileage << "')\n";
}
sqlfile << ";\n";

Expand All @@ -295,8 +296,7 @@ void sqlfile1
for (std::pair<Region* const,double>& rm : h.mileage_by_region)
{ if (!first) sqlfile << ',';
first = 0;
sprintf(fstr, "','%.17g')\n", rm.second);
sqlfile << "('" << h.systemname << "','" << rm.first->code << fstr;
sqlfile << "('" << h.systemname << "','" << rm.first->code << "','" << rm.second << "')\n";
}
sqlfile << ";\n";

Expand All @@ -316,8 +316,7 @@ void sqlfile1
first = 0;
auto it = t.active_only_mileage_by_region.find(rm.first);
double active_miles = (it != t.active_only_mileage_by_region.end()) ? it->second : 0;
sprintf(fstr, "','%.17g','%.17g')\n", active_miles, rm.second);
sqlfile << "('" << rm.first->code << "','" << t.traveler_name << fstr;
sqlfile << "('" << rm.first->code << "','" << t.traveler_name << "','" << active_miles << "','" << rm.second << "')\n";
}
sqlfile << ";\n";

Expand All @@ -338,8 +337,7 @@ void sqlfile1
for (auto& rm : csmbr.second)
{ if (!first) sqlfile << ',';
first = 0;
sprintf(fstr, "%.17g", rm.second);
sqlfile << "('" << systemname << "','" << rm.first->code << "','" << t.traveler_name << "','" << fstr << "')\n";
sqlfile << "('" << systemname << "','" << rm.first->code << "','" << t.traveler_name << "','" << rm.second << "')\n";
}
}
sqlfile << ";\n";
Expand All @@ -359,9 +357,8 @@ void sqlfile1
for (auto& rm : t.ccr_values)
{ if (!first) sqlfile << ',';
first = 0;
sprintf(fstr, "%.17g", rm.second);
sqlfile << "('" << rm.first->roots[0]->root << "','" << t.traveler_name << "','"
<< fstr << "','" << (rm.second == rm.first->mileage) << "')\n";
<< rm.second << "','" << (rm.second == rm.first->mileage) << "')\n";
}
sqlfile << ";\n";
}
Expand All @@ -380,9 +377,8 @@ void sqlfile1
for (std::pair<Route*,double>& rm : t.cr_values)
{ if (!first) sqlfile << ',';
first = 0;
sprintf(fstr, "%.17g", rm.second);
sqlfile << "('" << rm.first->root << "','" << t.traveler_name << "','"
<< fstr << "','" << (rm.second >= rm.first->mileage) << "')\n";
<< rm.second << "','" << (rm.second >= rm.first->mileage) << "')\n";
}
sqlfile << ";\n";
}
Expand Down
Loading

0 comments on commit 2349b1f

Please sign in to comment.