Jelajahi Sumber

Put definition in separate .cpp

Daniele Bartolini 8 tahun lalu
induk
melakukan
9d149ee806

+ 61 - 0
src/core/command_line.cpp

@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2012-2017 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#include "command_line.h"
+#include "string_utils.h"
+
+namespace crown
+{
+namespace
+{
+	bool is_shortopt(const char* arg, char shortopt)
+	{
+		return shortopt != '\0'
+			&& strlen32(arg) > 1
+			&& arg[0] == '-'
+			&& arg[1] == shortopt
+			;
+	}
+
+	bool is_longopt(const char* arg, const char* longopt)
+	{
+		return longopt != NULL
+			&& strlen32(arg) > 2
+			&& arg[0] == '-'
+			&& arg[1] == '-'
+			&& strcmp(&arg[2], longopt) == 0
+			;
+	}
+
+	int find_option(int argc, const char** argv, const char* longopt, char shortopt)
+	{
+		for (int i = 0; i < argc; ++i)
+		{
+			if (is_longopt(argv[i], longopt) || is_shortopt(argv[i], shortopt))
+				return i;
+		}
+
+		return argc;
+	}
+}
+
+CommandLine::CommandLine(int argc, const char** argv)
+	: _argc(argc)
+	, _argv(argv)
+{
+}
+
+const char* CommandLine::get_parameter(int i, const char* longopt, char shortopt)
+{
+	int argc = find_option(_argc, _argv, longopt, shortopt);
+	return argc + i < _argc ? _argv[argc + i + 1] : NULL;
+}
+
+bool CommandLine::has_option(const char* longopt, char shortopt)
+{
+	return find_option(_argc, _argv, longopt, shortopt) < _argc;
+}
+
+} // namespace crown

+ 8 - 48
src/core/command_line.h

@@ -5,8 +5,6 @@
 
 #pragma once
 
-#include "string_utils.h"
-
 namespace crown
 {
 /// Helper for parsing command line.
@@ -17,54 +15,16 @@ struct CommandLine
 	int _argc;
 	const char** _argv;
 
-	CommandLine(int argc, const char** argv)
-		: _argc(argc)
-		, _argv(argv)
-	{
-	}
-
-	int find_argument(const char* longopt, char shortopt)
-	{
-		for (int i = 0; i < _argc; ++i)
-		{
-			if (is_longopt(_argv[i], longopt) || is_shortopt(_argv[i], shortopt))
-			{
-				return i;
-			}
-		}
-
-		return _argc;
-	}
-
-	bool is_shortopt(const char* arg, char shortopt)
-	{
-		return shortopt != '\0'
-			&& strlen32(arg) > 1
-			&& arg[0] == '-'
-			&& arg[1] == shortopt
-			;
-	}
-
-	bool is_longopt(const char* arg, const char* longopt)
-	{
-		return longopt != NULL
-			&& strlen32(arg) > 2
-			&& arg[0] == '-'
-			&& arg[1] == '-'
-			&& strcmp(&arg[2], longopt) == 0
-			;
-	}
+	/// Constructor.
+	CommandLine(int argc, const char** argv);
 
-	const char* get_parameter(int i, const char* longopt, char shortopt = '\0')
-	{
-		int argc = find_argument(longopt, shortopt);
-		return argc + i < _argc ? _argv[argc + i + 1] : NULL;
-	}
+	/// Returns the i-th parameter of the option identified by
+	/// @a longopt or @a shortopt, or NULL if the parameter does not exist.
+	const char* get_parameter(int i, const char* longopt, char shortopt = '\0');
 
-	bool has_argument(const char* longopt, char shortopt = '\0')
-	{
-		return find_argument(longopt, shortopt) < _argc;
-	}
+	/// Returns whether the command line has the option identified
+	/// by @a longopt or @a shortopt.
+	bool has_option(const char* longopt, char shortopt = '\0');
 };
 
 } // namespace crown

+ 1 - 1
src/core/unit_tests.cpp

@@ -1254,7 +1254,7 @@ static void test_command_line()
 	};
 
 	CommandLine cl(countof(argv), argv);
-	ENSURE(cl.has_argument("switch", 's'));
+	ENSURE(cl.has_option("switch", 's'));
 	const char* orange = cl.get_parameter(0, "argument");
 	ENSURE(orange != NULL && strcmp(orange, "orange") == 0);
 }

+ 6 - 6
src/device/device_options.cpp

@@ -71,13 +71,13 @@ int DeviceOptions::parse()
 {
 	CommandLine cl(_argc, _argv);
 
-	if (cl.has_argument("help", 'h'))
+	if (cl.has_option("help", 'h'))
 	{
 		help();
 		return EXIT_FAILURE;
 	}
 
-	if (cl.has_argument("version", 'v'))
+	if (cl.has_option("version", 'v'))
 	{
 		printf(CROWN_VERSION);
 		return EXIT_FAILURE;
@@ -97,7 +97,7 @@ int DeviceOptions::parse()
 		}
 	}
 
-	_do_compile = cl.has_argument("compile");
+	_do_compile = cl.has_option("compile");
 	if (_do_compile)
 	{
 		_platform = cl.get_parameter(0, "platform");
@@ -129,7 +129,7 @@ int DeviceOptions::parse()
 		}
 	}
 
-	_server = cl.has_argument("server");
+	_server = cl.has_option("server");
 	if (_server)
 	{
 		if (_source_dir.empty())
@@ -166,7 +166,7 @@ int DeviceOptions::parse()
 		}
 	}
 
-	_do_continue = cl.has_argument("continue");
+	_do_continue = cl.has_option("continue");
 
 	_boot_dir = cl.get_parameter(0, "boot-dir");
 	if (_boot_dir != NULL)
@@ -178,7 +178,7 @@ int DeviceOptions::parse()
 		}
 	}
 
-	_wait_console = cl.has_argument("wait-console");
+	_wait_console = cl.has_option("wait-console");
 
 	const char* parent = cl.get_parameter(0, "parent-window");
 	if (parent != NULL)

+ 3 - 3
src/device/main_linux.cpp

@@ -747,14 +747,14 @@ int main(int argc, char** argv)
 	using namespace crown;
 #if CROWN_BUILD_UNIT_TESTS
 	CommandLine cl(argc, (const char**)argv);
-	if (cl.has_argument("run-unit-tests"))
+	if (cl.has_option("run-unit-tests"))
 	{
 		return main_unit_tests();
 	}
 #endif // CROWN_BUILD_UNIT_TESTS
-	if (cl.has_argument("compile") || cl.has_argument("server"))
+	if (cl.has_option("compile") || cl.has_option("server"))
 	{
-		if (main_data_compiler(argc, argv) != EXIT_SUCCESS || !cl.has_argument("continue"))
+		if (main_data_compiler(argc, argv) != EXIT_SUCCESS || !cl.has_option("continue"))
 			return EXIT_FAILURE;
 	}
 

+ 3 - 3
src/device/main_windows.cpp

@@ -716,14 +716,14 @@ int main(int argc, char** argv)
 
 #if CROWN_BUILD_UNIT_TESTS
 	CommandLine cl(argc, (const char**)argv);
-	if (cl.has_argument("run-unit-tests"))
+	if (cl.has_option("run-unit-tests"))
 	{
 		return main_unit_tests();
 	}
 #endif // CROWN_BUILD_UNIT_TESTS
-	if (cl.has_argument("compile") || cl.has_argument("server"))
+	if (cl.has_option("compile") || cl.has_option("server"))
 	{
-		if (main_data_compiler(argc, argv) != EXIT_SUCCESS || !cl.has_argument("continue"))
+		if (main_data_compiler(argc, argv) != EXIT_SUCCESS || !cl.has_option("continue"))
 			return EXIT_FAILURE;
 	}