Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ca04899

Browse files
committed
Visualize local codes
1 parent a1f919a commit ca04899

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

‎include/Tracer.h‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
#define CPP_TRACER_H
33

44
#include <string>
5+
#include <cstdlib>
56
#include <fstream>
67
#include <nlohmann/json.hpp>
8+
#include <curl/curl.h>
79

810
#define MAX_TRACES 1000000
911
#define MAX_TRACERS 100
@@ -43,10 +45,52 @@ class Tracer {
4345
}
4446

4547
public:
48+
static size_t writeFunction(void *ptr, size_t size, size_t nmemb, std::string *data) {
49+
data->append((char *) ptr, size * nmemb);
50+
return size * nmemb;
51+
}
52+
4653
static void onExit() {
47-
std::ofstream fout("traces.json");
48-
fout << traces.dump();
49-
fout.close();
54+
string content = traces.dump();
55+
if (std::getenv("ALGORITHM_VISUALIZER")) {
56+
std::ofstream fout("traces.json");
57+
fout << content;
58+
fout.close();
59+
} else {
60+
auto curl = curl_easy_init();
61+
if (curl) {
62+
string params = "content=" + content;
63+
64+
curl_easy_setopt(curl, CURLOPT_URL, "https://algorithm-visualizer.org/api/visualizations");
65+
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, params.c_str());
66+
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
67+
curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);
68+
69+
string header;
70+
string response;
71+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunction);
72+
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &header);
73+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
74+
75+
curl_easy_perform(curl);
76+
long response_code;
77+
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
78+
79+
curl_easy_cleanup(curl);
80+
curl = NULL;
81+
82+
if (response_code == 200) {
83+
#ifdef linux
84+
string command = "xdg-open " + response;
85+
#elif _WIN32
86+
string command = "start " + response;
87+
#else
88+
string command = "open " + response;
89+
#endif
90+
system(command.c_str());
91+
}
92+
}
93+
}
5094
}
5195
};
5296

‎test/CMakeLists.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
find_package(CURL REQUIRED)
2+
13
add_executable(test main.cpp)
4+
5+
target_link_libraries(test ${CURL_LIBRARIES})

‎test/main.cpp‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,5 @@ int main() {
1212
Randomize::Graph<int>(N, 1, *(new Randomize::Integer(1, 9))).weighted().directed(false).fill(&array[0][0]);
1313
GraphTracer graphTracer;
1414
graphTracer.set(array);
15-
for (int i = 0; i < N; i++) {
16-
for (int j = 0; j < N; j++) {
17-
cout << array[i][j] << " ";
18-
}
19-
cout << endl;
20-
}
2115
return 0;
2216
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /