Skip to content

Commit d7e4f32

Browse files
authored
1) Introduced a source file for maintaining utilites (xutils.cpp) (#25)
2) Added test for extract_filename function
1 parent 15d4e99 commit d7e4f32

File tree

6 files changed

+160
-79
lines changed

6 files changed

+160
-79
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ set(XEUS_CPP_SRC
130130
src/xinterpreter.cpp
131131
src/xoptions.cpp
132132
src/xparser.cpp
133+
src/xutils.cpp
133134
)
134135

135136
if(EMSCRIPTEN)

include/xeus-cpp/xbuffer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace xcpp
5252
{
5353
std::lock_guard<std::mutex> lock(m_mutex);
5454
// Called for a string of characters.
55-
m_output.append(s, count);
55+
m_output.append(s, static_cast<std::size_t>(count));
5656
return count;
5757
}
5858

include/xeus-cpp/xutils.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/************************************************************************************
2+
* Copyright (c) 2023, xeus-cpp contributors *
3+
* *
4+
* Distributed under the terms of the BSD 3-Clause License. *
5+
* *
6+
* The full license is in the file LICENSE, distributed with this software. *
7+
************************************************************************************/
8+
9+
#ifndef XEUS_CPP_UTILS_HPP
10+
#define XEUS_CPP_UTILS_HPP
11+
12+
#include "xinterpreter.hpp"
13+
14+
using interpreter_ptr = std::unique_ptr<xcpp::interpreter>;
15+
16+
namespace xcpp
17+
{
18+
19+
#ifdef __GNUC__
20+
XEUS_CPP_API
21+
void handler(int sig);
22+
#endif
23+
24+
XEUS_CPP_API
25+
void stop_handler(int sig);
26+
27+
XEUS_CPP_API
28+
bool should_print_version(int argc, char* argv[]);
29+
30+
XEUS_CPP_API
31+
std::string extract_filename(int &argc, char* argv[]);
32+
33+
XEUS_CPP_API
34+
interpreter_ptr build_interpreter(int argc, char** argv);
35+
}
36+
37+
#endif

src/main.cpp

Lines changed: 7 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -27,81 +27,11 @@
2727

2828
#include "xeus-cpp/xeus_cpp_config.hpp"
2929
#include "xeus-cpp/xinterpreter.hpp"
30-
31-
#ifdef __GNUC__
32-
void handler(int sig)
33-
{
34-
void* array[10];
35-
36-
// get void*'s for all entries on the stack
37-
std::size_t size = backtrace(array, 10);
38-
39-
// print out all the frames to stderr
40-
fprintf(stderr, "Error: signal %d:\n", sig);
41-
backtrace_symbols_fd(array, size, STDERR_FILENO);
42-
exit(1);
43-
}
44-
#endif
45-
46-
void stop_handler(int /*sig*/)
47-
{
48-
exit(0);
49-
}
50-
51-
bool should_print_version(int argc, char* argv[])
52-
{
53-
for (int i = 0; i < argc; ++i)
54-
{
55-
if (std::string(argv[i]) == "--version")
56-
{
57-
return true;
58-
}
59-
}
60-
return false;
61-
}
62-
63-
std::string extract_filename(int argc, char* argv[])
64-
{
65-
std::string res = "";
66-
for (int i = 0; i < argc; ++i)
67-
{
68-
if ((std::string(argv[i]) == "-f") && (i + 1 < argc))
69-
{
70-
res = argv[i + 1];
71-
for (int j = i; j < argc - 2; ++j)
72-
{
73-
argv[j] = argv[j + 2];
74-
}
75-
argc -= 2;
76-
break;
77-
}
78-
}
79-
return res;
80-
}
81-
82-
using interpreter_ptr = std::unique_ptr<xcpp::interpreter>;
83-
84-
interpreter_ptr build_interpreter(int argc, char** argv)
85-
{
86-
int interpreter_argc = argc; // + 1; // ...
87-
const char** interpreter_argv = new const char*[interpreter_argc];
88-
interpreter_argv[0] = "xeus-cpp";
89-
// Copy all arguments in the new array excepting the process name.
90-
for (int i = 1; i < argc; i++)
91-
{
92-
interpreter_argv[i] = argv[i];
93-
}
94-
// std::string include_dir = std::string(LLVM_DIR) + std::string("/include"); // ...
95-
// interpreter_argv[interpreter_argc - 1] = include_dir.c_str(); // ...
96-
97-
interpreter_ptr interp_ptr = interpreter_ptr(new xcpp::interpreter(interpreter_argc, interpreter_argv));
98-
delete[] interpreter_argv;
99-
return interp_ptr;
100-
}
30+
#include "xeus-cpp/xutils.hpp"
10131

10232
int main(int argc, char* argv[])
10333
{
104-
if (should_print_version(argc, argv))
34+
if (xcpp::should_print_version(argc, argv))
10535
{
10636
std::clog << "xcpp " << XEUS_CPP_VERSION << std::endl;
10737
return 0;
@@ -120,16 +50,16 @@ int main(int argc, char* argv[])
12050
// Registering SIGSEGV handler
12151
#ifdef __GNUC__
12252
std::clog << "registering handler for SIGSEGV" << std::endl;
123-
signal(SIGSEGV, handler);
53+
signal(SIGSEGV, xcpp::handler);
12454

12555
// Registering SIGINT and SIGKILL handlers
126-
signal(SIGKILL, stop_handler);
56+
signal(SIGKILL, xcpp::stop_handler);
12757
#endif
128-
signal(SIGINT, stop_handler);
58+
signal(SIGINT, xcpp::stop_handler);
12959

130-
std::string file_name = extract_filename(argc, argv);
60+
std::string file_name = xcpp::extract_filename(argc, argv);
13161

132-
interpreter_ptr interpreter = build_interpreter(argc, argv);
62+
interpreter_ptr interpreter = xcpp::build_interpreter(argc, argv);
13363

13464
auto context = xeus::make_context<zmq::context_t>();
13565

src/xutils.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/************************************************************************************
2+
* Copyright (c) 2023, xeus-cpp contributors *
3+
* *
4+
* Distributed under the terms of the BSD 3-Clause License. *
5+
* *
6+
* The full license is in the file LICENSE, distributed with this software. *
7+
************************************************************************************/
8+
9+
#include <cstddef>
10+
#include <memory>
11+
#include <string>
12+
#include <utility>
13+
14+
#include <signal.h>
15+
16+
#ifdef __GNUC__
17+
#include <stdio.h>
18+
#ifndef XEUS_CPP_EMSCRIPTEN_WASM_BUILD
19+
#include <execinfo.h>
20+
#endif
21+
#include <stdlib.h>
22+
#include <unistd.h>
23+
#endif
24+
25+
26+
#include "xeus-cpp/xutils.hpp"
27+
#include "xeus-cpp/xinterpreter.hpp"
28+
29+
namespace xcpp
30+
{
31+
32+
#if defined(__GNUC__) && !defined(XEUS_CPP_EMSCRIPTEN_WASM_BUILD)
33+
void handler(int sig)
34+
{
35+
void* array[10];
36+
37+
// get void*'s for all entries on the stack
38+
std::size_t size = backtrace(array, 10);
39+
40+
// print out all the frames to stderr
41+
fprintf(stderr, "Error: signal %d:\n", sig);
42+
backtrace_symbols_fd(array, size, STDERR_FILENO);
43+
exit(1);
44+
}
45+
#endif
46+
47+
void stop_handler(int /*sig*/)
48+
{
49+
exit(0);
50+
}
51+
52+
bool should_print_version(int argc, char* argv[])
53+
{
54+
for (int i = 0; i < argc; ++i)
55+
{
56+
if (std::string(argv[i]) == "--version")
57+
{
58+
return true;
59+
}
60+
}
61+
return false;
62+
}
63+
64+
std::string extract_filename(int &argc, char* argv[])
65+
{
66+
std::string res = "";
67+
for (int i = 0; i < argc; ++i)
68+
{
69+
if ((std::string(argv[i]) == "-f") && (i + 1 < argc))
70+
{
71+
res = argv[i + 1];
72+
for (int j = i; j < argc - 2; ++j)
73+
{
74+
argv[j] = argv[j + 2];
75+
}
76+
argc -= 2;
77+
break;
78+
}
79+
}
80+
return res;
81+
}
82+
83+
interpreter_ptr build_interpreter(int argc, char** argv)
84+
{
85+
int interpreter_argc = argc; // + 1;
86+
const char** interpreter_argv = new const char*[interpreter_argc];
87+
interpreter_argv[0] = "xeus-cpp";
88+
// Copy all arguments in the new array excepting the process name.
89+
for (int i = 1; i < argc; i++)
90+
{
91+
interpreter_argv[i] = argv[i];
92+
}
93+
94+
interpreter_ptr interp_ptr = std::make_unique<interpreter>(interpreter_argc, interpreter_argv);
95+
delete[] interpreter_argv;
96+
return interp_ptr;
97+
}
98+
99+
}

test/test_interpreter.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "doctest/doctest.h"
1010
#include "xeus-cpp/xinterpreter.hpp"
11+
#include "xeus-cpp/xutils.hpp"
1112

1213
TEST_SUITE("execute_request")
1314
{
@@ -33,4 +34,17 @@ TEST_SUITE("execute_request")
3334
REQUIRE(result["found"] == true);
3435
REQUIRE(result["status"] == "ok");
3536
}
36-
}
37+
}
38+
39+
TEST_SUITE("extract_filename")
40+
{
41+
TEST_CASE("extract_filename_basic_test")
42+
{
43+
const char* arguments[] = {"argument1", "-f", "filename.txt", "argument4"};
44+
int argc = sizeof(arguments) / sizeof(arguments[0]);
45+
46+
std::string result = xcpp::extract_filename(argc, const_cast<char**>(arguments));
47+
REQUIRE(result == "filename.txt");
48+
REQUIRE(argc == 2);
49+
}
50+
}

0 commit comments

Comments
 (0)