diff --git a/src/xtd.core/include/xtd/internal/__print.hpp b/src/xtd.core/include/xtd/internal/__print.hpp index 18d5dcd64f3..436a4040506 100644 --- a/src/xtd.core/include/xtd/internal/__print.hpp +++ b/src/xtd.core/include/xtd/internal/__print.hpp @@ -12,11 +12,13 @@ #include #include #include "../io/io_exception.hpp" +#include "../environment.hpp" #include "../null_pointer_exception.hpp" /// @cond -void __xtd_print_with_file_write__(FILE* file, const std::string& s) { +void __xtd_print_with_file_write__(bool new_line, FILE* file, xtd::string&& s) { if (!file) throw xtd::null_pointer_exception {}; + if (new_line) s += xtd::environment::new_line(); if (fwrite(s.c_str(), 1, s.length(), file) != s.length()) { auto exception = xtd::io::io_exception {}; exception.h_result(errno); @@ -24,8 +26,9 @@ void __xtd_print_with_file_write__(FILE* file, const std::string& s) { } } -void __xtd_print_with_ostream_write__(std::ostream& os, const std::string& s) { +void __xtd_print_with_ostream_write__(bool new_line, std::ostream& os, xtd::string&& s) { if (!os.good()) throw xtd::io::io_exception {}; + if (new_line) s += xtd::environment::new_line(); os.write(s.c_str(), s.length()); } /// @endcond diff --git a/src/xtd.core/include/xtd/print.hpp b/src/xtd.core/include/xtd/print.hpp index 26145841789..0f8df294033 100644 --- a/src/xtd.core/include/xtd/print.hpp +++ b/src/xtd.core/include/xtd/print.hpp @@ -17,13 +17,13 @@ namespace xtd { /// @exceprion xtd::null_pointer_exception the `file`pointer is null. template void print(FILE* file, arg_t&& value) { - __xtd_print_with_file_write__(file, string::format("{}", value)); + __xtd_print_with_file_write__(false, file, string::format("{}", value)); } /// @cond template void print(FILE* file, std::initializer_list&& il) { - __xtd_print_with_file_write__(file, string::format("{}", il)); + __xtd_print_with_file_write__(false, file, string::format("{}", il)); } /// @endcond @@ -35,7 +35,7 @@ namespace xtd { /// @exceprion xtd::null_pointer_exception the `file`pointer is null. template void print(FILE* file, const xtd::string& fmt, args_t&& ... values) { - __xtd_print_with_file_write__(file, string::format(fmt, std::forward(values)...)); + __xtd_print_with_file_write__(false, file, string::format(fmt, std::forward(values)...)); } /// @brief Writes the text representation of the specified list of values to the file output stream using the specified format information. /// @tparam ...args_t Types of the values to write. @@ -45,7 +45,7 @@ namespace xtd { /// @exceprion xtd::null_pointer_exception the `file`pointer is null. template void print(FILE* file, const char* fmt, args_t&& ... values) { - __xtd_print_with_file_write__(file, string::format(xtd::string {fmt}, std::forward(values)...)); + __xtd_print_with_file_write__(false, file, string::format(xtd::string {fmt}, std::forward(values)...)); } #if defined(__xtd__cpp_lib_char8_t) /// @brief Writes the text representation of the specified list of values to the file output stream using the specified format information. @@ -56,7 +56,7 @@ namespace xtd { /// @exceprion xtd::null_pointer_exception the `file`pointer is null. template void print(FILE* file, const char8_t* fmt, args_t&& ... values) { - __xtd_print_with_file_write__(file, string::format(xtd::string {fmt}, std::forward(values)...)); + __xtd_print_with_file_write__(false, file, string::format(xtd::string {fmt}, std::forward(values)...)); } #endif /// @brief Writes the text representation of the specified list of values to the file output stream using the specified format information. @@ -67,7 +67,7 @@ namespace xtd { /// @exceprion xtd::null_pointer_exception the `file`pointer is null. template void print(FILE* file, const char16_t* fmt, args_t&& ... values) { - __xtd_print_with_file_write__(file, string::format(xtd::string {fmt}, std::forward(values)...)); + __xtd_print_with_file_write__(false, file, string::format(xtd::string {fmt}, std::forward(values)...)); } /// @brief Writes the text representation of the specified list of values to the file output stream using the specified format information. /// @tparam ...args_t Types of the values to write. @@ -77,7 +77,7 @@ namespace xtd { /// @exceprion xtd::null_pointer_exception the `file`pointer is null. template void print(FILE* file, const char32_t* fmt, args_t&& ... values) { - __xtd_print_with_file_write__(file, string::format(xtd::string {fmt}, std::forward(values)...)); + __xtd_print_with_file_write__(false, file, string::format(xtd::string {fmt}, std::forward(values)...)); } /// @brief Writes the text representation of the specified list of values to the file output stream using the specified format information. /// @tparam ...args_t Types of the values to write. @@ -87,7 +87,7 @@ namespace xtd { /// @exceprion xtd::null_pointer_exception the `file`pointer is null. template void print(FILE* file, const wchar_t* fmt, args_t&& ... values) { - __xtd_print_with_file_write__(file, string::format(xtd::string {fmt}, std::forward(values)...)); + __xtd_print_with_file_write__(false, file, string::format(xtd::string {fmt}, std::forward(values)...)); } /// @brief Writes the text representation of the specified value to the output stream. @@ -96,13 +96,13 @@ namespace xtd { /// @param value The value to write, template void print(std::ostream& os, arg_t&& value) { - __xtd_print_with_ostream_write__(os, string::format("{}", value)); + __xtd_print_with_ostream_write__(false, os, string::format("{}", value)); } /// @cond template void print(std::ostream& os, std::initializer_list&& il) { - __xtd_print_with_ostream_write__(os, string::format("{}", il)); + __xtd_print_with_ostream_write__(false, os, string::format("{}", il)); } /// @endcond @@ -113,7 +113,7 @@ namespace xtd { /// @param values Values to write, template void print(std::ostream& os, const xtd::string& fmt, args_t&& ... values) { - __xtd_print_with_ostream_write__(os, string::format(fmt, std::forward(values)...)); + __xtd_print_with_ostream_write__(false, os, string::format(fmt, std::forward(values)...)); } /// @brief Writes the text representation of the specified list of values to the output stream using the specified format information. /// @tparam ...args_t Types of the values to write. @@ -122,7 +122,7 @@ namespace xtd { /// @param values Values to write, template void print(std::ostream& os, const char* fmt, args_t&& ... values) { - __xtd_print_with_ostream_write__(os, string::format(xtd::string {fmt}, std::forward(values)...)); + __xtd_print_with_ostream_write__(false, os, string::format(xtd::string {fmt}, std::forward(values)...)); } #if defined(__xtd__cpp_lib_char8_t) /// @brief Writes the text representation of the specified list of values to the output stream using the specified format information. @@ -132,7 +132,7 @@ namespace xtd { /// @param values Values to write, template void print(std::ostream& os, const char8_t* fmt, args_t&& ... values) { - __xtd_print_with_ostream_write__(os, string::format(xtd::string {fmt}, std::forward(values)...)); + __xtd_print_with_ostream_write__(false, os, string::format(xtd::string {fmt}, std::forward(values)...)); } #endif /// @brief Writes the text representation of the specified list of values to the output stream using the specified format information. @@ -142,7 +142,7 @@ namespace xtd { /// @param values Values to write, template void print(std::ostream& os, const char16_t* fmt, args_t&& ... values) { - __xtd_print_with_ostream_write__(os, string::format(xtd::string {fmt}, std::forward(values)...)); + __xtd_print_with_ostream_write__(false, os, string::format(xtd::string {fmt}, std::forward(values)...)); } /// @brief Writes the text representation of the specified list of values to the output stream using the specified format information. /// @tparam ...args_t Types of the values to write. @@ -151,7 +151,7 @@ namespace xtd { /// @param values Values to write, template void print(std::ostream& os, const char32_t* fmt, args_t&& ... values) { - __xtd_print_with_ostream_write__(os, string::format(xtd::string {fmt}, std::forward(values)...)); + __xtd_print_with_ostream_write__(false, os, string::format(xtd::string {fmt}, std::forward(values)...)); } /// @brief Writes the text representation of the specified list of values to the output stream using the specified format information. /// @tparam ...args_t Types of the values to write. @@ -160,7 +160,7 @@ namespace xtd { /// @param values Values to write, template void print(std::ostream& os, const wchar_t* fmt, args_t&& ... values) { - __xtd_print_with_ostream_write__(os, string::format(xtd::string {fmt}, std::forward(values)...)); + __xtd_print_with_ostream_write__(false, os, string::format(xtd::string {fmt}, std::forward(values)...)); } /// @brief Writes the text representation of the specified value to the standard output stream. diff --git a/src/xtd.core/include/xtd/println.hpp b/src/xtd.core/include/xtd/println.hpp index 598e2633759..ee33c3437e5 100644 --- a/src/xtd.core/include/xtd/println.hpp +++ b/src/xtd.core/include/xtd/println.hpp @@ -2,14 +2,18 @@ /// @brief Contains xtd::println methods. /// @copyright Copyright (c) 2025 Gammasoft. All rights reserved. #pragma once -#include "environment.hpp" -#include "print.hpp" +#define __XTD_CORE_INTERNAL__ +#include "internal/__print.hpp" +#undef __XTD_CORE_INTERNAL__ +#include "string.hpp" /// @brief The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more. namespace xtd { /// @brief Writes the current line terminator to the file output stream using the specified format information. /// @param file A file output stream. - inline void println(FILE* file) {xtd::print(file, environment::new_line());} + inline void println(FILE* file) { + __xtd_print_with_file_write__(true, file, ""); + } /// @brief Writes the text representation of the specified value, followed by the current line terminator, to the file output stream. /// @tparam arg_t The type of the value to write. @@ -17,15 +21,13 @@ namespace xtd { /// @param value The value to write, template void println(FILE* file, arg_t&& value) { - xtd::print(file, value); - xtd::print(file, environment::new_line()); + __xtd_print_with_file_write__(true, file, string::format("{}", value)); } /// @cond template void println(FILE* file, const std::initializer_list& il) { - xtd::print(file, il); - xtd::print(file, environment::new_line()); + __xtd_print_with_file_write__(true, file, string::format("{}", il)); } /// @endcond @@ -36,8 +38,7 @@ namespace xtd { /// @param values Values to write, template void println(FILE* file, const xtd::string& fmt, args_t&& ... values) { - xtd::print(file, fmt, std::forward(values)...); - xtd::print(file, environment::new_line()); + __xtd_print_with_file_write__(true, file, string::format(fmt, std::forward(values)...)); } /// @brief Writes the text representation of the specified list of values, followed by the current line terminator, to the file output stream using the specified format information. /// @tparam ...args_t Types of the values to write. @@ -46,8 +47,7 @@ namespace xtd { /// @param values Values to write, template void println(FILE* file, const char* fmt, args_t&& ... values) { - xtd::print(file, fmt, std::forward(values)...); - xtd::print(file, environment::new_line()); + __xtd_print_with_file_write__(true, file, string::format(xtd::string {fmt}, std::forward(values)...)); } #if defined(__xtd__cpp_lib_char8_t) /// @brief Writes the text representation of the specified list of values, followed by the current line terminator, to the file output stream using the specified format information. @@ -57,8 +57,7 @@ namespace xtd { /// @param values Values to write, template void println(FILE* file, const char8_t* fmt, args_t&& ... values) { - xtd::print(file, fmt, std::forward(values)...); - xtd::print(file, environment::new_line()); + __xtd_print_with_file_write__(true, file, string::format(xtd::string {fmt}, std::forward(values)...)); } #endif /// @brief Writes the text representation of the specified list of values, followed by the current line terminator, to the file output stream using the specified format information. @@ -68,8 +67,7 @@ namespace xtd { /// @param values Values to write, template void println(FILE* file, const char16_t* fmt, args_t&& ... values) { - xtd::print(file, fmt, std::forward(values)...); - xtd::print(file, environment::new_line()); + __xtd_print_with_file_write__(true, file, string::format(xtd::string {fmt}, std::forward(values)...)); } /// @brief Writes the text representation of the specified list of values, followed by the current line terminator, to the file output stream using the specified format information. /// @tparam ...args_t Types of the values to write. @@ -78,8 +76,7 @@ namespace xtd { /// @param values Values to write, template void println(FILE* file, const char32_t* fmt, args_t&& ... values) { - xtd::print(file, fmt, std::forward(values)...); - xtd::print(file, environment::new_line()); + __xtd_print_with_file_write__(true, file, string::format(xtd::string {fmt}, std::forward(values)...)); } /// @brief Writes the text representation of the specified list of values, followed by the current line terminator, to the file output stream using the specified format information. /// @tparam ...args_t Types of the values to write. @@ -88,13 +85,14 @@ namespace xtd { /// @param values Values to write, template void println(FILE* file, const wchar_t* fmt, args_t&& ... values) { - xtd::print(file, fmt, std::forward(values)...); - xtd::print(file, environment::new_line()); + __xtd_print_with_file_write__(true, file, string::format(xtd::string {fmt}, std::forward(values)...)); } /// @brief Writes the current line terminator to the output stream using the specified format information. /// @param os The output stream to insert data into. - inline void println(std::ostream& os) {xtd::print(os, environment::new_line());} + inline void println(std::ostream& os) { + __xtd_print_with_ostream_write__(true, os, ""); + } /// @brief Writes the text representation of the specified value, followed by the current line terminator, to the output stream. /// @tparam arg_t The type of the value to write. @@ -102,15 +100,13 @@ namespace xtd { /// @param value The value to write, template void println(std::ostream& os, arg_t&& value) { - xtd::print(os, value); - xtd::print(os, environment::new_line()); + __xtd_print_with_ostream_write__(true, os, string::format("{}", value)); } /// @cond template void println(std::ostream& os, const std::initializer_list& il) { - xtd::print(os, il); - xtd::print(os, environment::new_line()); + __xtd_print_with_ostream_write__(true, os, string::format("{}", il)); } /// @endcond @@ -121,8 +117,7 @@ namespace xtd { /// @param values Values to write, template void println(std::ostream& os, const xtd::string& fmt, args_t&& ... values) { - xtd::print(os, fmt, std::forward(values)...); - xtd::print(os, environment::new_line()); + __xtd_print_with_ostream_write__(true, os, string::format(fmt, std::forward(values)...)); } /// @brief Writes the text representation of the specified list of values, followed by the current line terminator, to the output stream using the specified format information. /// @tparam ...args_t Types of the values to write. @@ -131,8 +126,7 @@ namespace xtd { /// @param values Values to write, template void println(std::ostream& os, const char* fmt, args_t&& ... values) { - xtd::print(os, fmt, std::forward(values)...); - xtd::print(os, environment::new_line()); + __xtd_print_with_ostream_write__(true, os, string::format(xtd::string {fmt}, std::forward(values)...)); } #if defined(__xtd__cpp_lib_char8_t) /// @brief Writes the text representation of the specified list of values, followed by the current line terminator, to the output stream using the specified format information. @@ -142,8 +136,7 @@ namespace xtd { /// @param values Values to write, template void println(std::ostream& os, const char8_t* fmt, args_t&& ... values) { - xtd::print(os, fmt, std::forward(values)...); - xtd::print(os, environment::new_line()); + __xtd_print_with_ostream_write__(true, os, string::format(xtd::string {fmt}, std::forward(values)...)); } #endif /// @brief Writes the text representation of the specified list of values, followed by the current line terminator, to the output stream using the specified format information. @@ -153,8 +146,7 @@ namespace xtd { /// @param values Values to write, template void println(std::ostream& os, const char16_t* fmt, args_t&& ... values) { - xtd::print(os, fmt, std::forward(values)...); - xtd::print(os, environment::new_line()); + __xtd_print_with_ostream_write__(true, os, string::format(xtd::string {fmt}, std::forward(values)...)); } /// @brief Writes the text representation of the specified list of values, followed by the current line terminator, to the output stream using the specified format information. /// @tparam ...args_t Types of the values to write. @@ -163,8 +155,7 @@ namespace xtd { /// @param values Values to write, template void println(std::ostream& os, const char32_t* fmt, args_t&& ... values) { - xtd::print(os, fmt, std::forward(values)...); - xtd::print(os, environment::new_line()); + __xtd_print_with_ostream_write__(true, os, string::format(xtd::string {fmt}, std::forward(values)...)); } /// @brief Writes the text representation of the specified list of values, followed by the current line terminator, to the output stream using the specified format information. /// @tparam ...args_t Types of the values to write. @@ -173,28 +164,21 @@ namespace xtd { /// @param values Values to write, template void println(std::ostream& os, const wchar_t* fmt, args_t&& ... values) { - xtd::print(os, fmt, std::forward(values)...); - xtd::print(os, environment::new_line()); + __xtd_print_with_ostream_write__(true, os, string::format(xtd::string {fmt}, std::forward(values)...)); } /// @brief Writes the current line terminator to the standard output stream using the specified format information. - inline void println() {xtd::print(environment::new_line());} + inline void println() {xtd::println(stdout);} /// @brief Writes the text representation of the specified value, followed by the current line terminator, to the standard output stream. /// @tparam arg_t The type of the value to write. /// @param value The value to write, template - void println(arg_t&& value) { - xtd::print(value); - xtd::print(environment::new_line()); - } + void println(arg_t&& value) {xtd::println(stdout, value);} /// @cond template - void println(const std::initializer_list& il) { - xtd::print(il); - xtd::print(environment::new_line()); - } + void println(const std::initializer_list& il) {xtd::println(stdout, il);} /// @endcond /// @brief Writes the text representation of the specified list of values, followed by the current line terminator, to the standard output stream using the specified format information. @@ -202,55 +186,37 @@ namespace xtd { /// @param fmt A composite format string. /// @param values Values to write, template - void println(const xtd::string& fmt, args_t&& ... values) { - xtd::print(fmt, std::forward(values)...); - xtd::print(environment::new_line()); - } + void println(const xtd::string& fmt, args_t&& ... values) {xtd::println(stdout, fmt, std::forward(values)...);} /// @brief Writes the text representation of the specified list of values, followed by the current line terminator, to the standard output stream using the specified format information. /// @tparam ...args_t Types of the values to write. /// @param fmt A composite format string. /// @param values Values to write, template - void println(const char* fmt, args_t&& ... values) { - xtd::print(fmt, std::forward(values)...); - xtd::print(environment::new_line()); - } + void println(const char* fmt, args_t&& ... values) {xtd::println(stdout, fmt, std::forward(values)...);} #if defined(__xtd__cpp_lib_char8_t) /// @brief Writes the text representation of the specified list of values, followed by the current line terminator, to the standard output stream using the specified format information. /// @tparam ...args_t Types of the values to write. /// @param fmt A composite format string. /// @param values Values to write, template - void println(const char8_t* fmt, args_t&& ... values) { - xtd::print(fmt, std::forward(values)...); - xtd::print(environment::new_line()); - } + void println(const char8_t* fmt, args_t&& ... values) {xtd::println(stdout, fmt, std::forward(values)...);} #endif /// @brief Writes the text representation of the specified list of values, followed by the current line terminator, to the standard output stream using the specified format information. /// @tparam ...args_t Types of the values to write. /// @param fmt A composite format string. /// @param values Values to write, template - void println(const char16_t* fmt, args_t&& ... values) { - xtd::print(fmt, std::forward(values)...); - xtd::print(environment::new_line()); - } + void println(const char16_t* fmt, args_t&& ... values) {xtd::println(stdout, fmt, std::forward(values)...);} /// @brief Writes the text representation of the specified list of values, followed by the current line terminator, to the standard output stream using the specified format information. /// @tparam ...args_t Types of the values to write. /// @param fmt A composite format string. /// @param values Values to write, template - void println(const char32_t* fmt, args_t&& ... values) { - xtd::print(fmt, std::forward(values)...); - xtd::print(environment::new_line()); - } + void println(const char32_t* fmt, args_t&& ... values) {xtd::println(stdout, fmt, std::forward(values)...);} /// @brief Writes the text representation of the specified list of values, followed by the current line terminator, to the standard output stream using the specified format information. /// @tparam ...args_t Types of the values to write. /// @param fmt A composite format string. /// @param values Values to write, template - void println(const wchar_t* fmt, args_t&& ... values) { - xtd::print(fmt, std::forward(values)...); - xtd::print(environment::new_line()); - } + void println(const wchar_t* fmt, args_t&& ... values) {xtd::println(stdout, fmt, std::forward(values)...);} }