-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSGen.cpp
102 lines (89 loc) · 3.03 KB
/
JSGen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "stdafx.h"
#include "JSGen.h"
#include "Stats.h"
#include <string>
void FillPlaceHolder(const char* srcFile, const char* tgtFile, const char* placeHolder, const std::string& str)
{
FILE* f = nullptr;
char * buf = new char[4000000 + 10];
size_t read = 0;
fopen_s(&f, srcFile, "rt");
if(f != nullptr)
{
read = fread_s(buf, 4000000, 1, 4000000, f);
buf[read] = '\0';
fclose(f);
}
char *ph = strstr(buf, placeHolder);
char *afterPlaceholder = ph + strlen(placeHolder);
char *end = buf + read;
fopen_s(&f, tgtFile, "wt");
if(f != nullptr)
{
fwrite(buf, sizeof(char), ph - buf, f);
fwrite(str.c_str(), sizeof(char), str.size(), f);
fwrite(afterPlaceholder, sizeof(char), end - afterPlaceholder, f);
fclose(f);
}
delete buf;
}
void WriteDays(const std::vector<Day>& vecDays)
{
std::string strDays = "var days = [\n";
size_t pathVerticesSkipped = 0;
size_t pathVerticesUsed = 0;
char buf[4096];
for(size_t dayIndex = 0; dayIndex < vecDays.size(); ++dayIndex)
{
const Day& day = vecDays[dayIndex];
if(day.nodes.empty() && day.path.empty())
{
continue;
}
// ================= Fill one day
strDays += " {\n";
strDays += " date: '"; strDays += day.date.c_str(); strDays += "',\n";
strDays += " line:null,\n";
strDays += " showNodes:true,\n";
strDays += " showPath:true,\n";
// ================= Day's path
strDays += " path: [";
for(auto itVertex = day.path.begin(); itVertex != day.path.end(); ++itVertex)
{
sprintf_s(buf, sizeof(buf), " {lat: %f, lng: %f},\n", itVertex->lat, itVertex->lon);
strDays += buf;
}
strDays += " ],\n";
// ================= Day's nodes
strDays += " nodes: [\n";
for(auto itNode = day.nodes.begin(); itNode != day.nodes.end(); ++itNode)
{
sprintf_s(buf, sizeof(buf), " { geo: {lat: %f, lng: %f},\n", itNode->latLon.lat, itNode->latLon.lon);
strDays += buf;
strDays += " marker:null, alt:'-999', addr:'', places:[],\n";
strDays += " images: [\n";
for(auto itImage = itNode->images.begin(); itImage != itNode->images.end(); ++itImage)
{
sprintf_s(buf, sizeof(buf), " {name:'%s', dt:'%s', link:'', id:''},\n",
itImage->fileName.c_str(), itImage->dateTime.c_str(), itImage->fileName.c_str());
strDays += buf;
}
strDays += " ]},\n";
}
strDays += " ],\n";
strDays += " },\n";
}
strDays += "];";
const char* src = "C:/Projects/GoogleMapTrip/Data/CanadaTrip.html";
const char* final = "C:/Projects/GoogleMapTrip/Data/CanadaTripFinal.html";
if(editMode)
{
FillPlaceHolder(src, final, "var editMode = false;", "var editMode = true;");
src = final;
}
#ifdef _DEBUG
FillPlaceHolder(src, final, "var createDebugSpan = false;", "var createDebugSpan = true;");
src = final;
#endif
FillPlaceHolder(src, final, "var days;", strDays);
}