Working on backing files.

Also started a sketches project to illustrate quick ideas.
This commit is contained in:
2023-10-11 23:27:42 -07:00
parent fd6e0c6899
commit 2dcc577f57
13 changed files with 590 additions and 30 deletions

35
sketches/CMakeLists.txt Normal file
View File

@@ -0,0 +1,35 @@
cmake_minimum_required(VERSION 3.25)
project(ke_sketches
DESCRIPTION "sketches and small test programs for kyle's editor"
LANGUAGES CXX
VERSION 0.0.1)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_VERBOSE_MAKEFILES TRUE)
set(VERBOSE YES)
if (MSVC)
add_compile_options("/W4" "$<$<CONFIG:RELEASE>:/O2>")
else ()
add_compile_options(
"-Wall"
"-Wextra"
"-Werror"
"-static"
"$<$<CONFIG:DEBUG>:-g>"
"$<$<CONFIG:RELEASE>:-O2>")
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
add_compile_options(
"-stdlib=libc++"
"-fsanitize=address"
"-fno-omit-frame-pointer"
"-fsanitize-address-use-after-scope"
)
else ()
# nothing special for gcc at the moment
endif ()
endif ()
add_executable(readFile ReadFile.cc)
add_executable(enoent enoent.cc)

87
sketches/ReadFile.cc Normal file
View File

@@ -0,0 +1,87 @@
///
/// \file ReadFile.cc
/// \author kyle
/// \created 2023-10-11
/// \brief ReadFile tests reading a file.
///
/// \section COPYRIGHT
/// Copyright 2023 K. Isom <kyle@imap.cc>
///
/// Permission to use, copy, modify, and/or distribute this software for
/// any purpose with or without fee is hereby granted, provided that the
/// above copyright notice and this permission notice appear in all copies.
///
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
/// BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
/// OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
/// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
/// ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
/// SOFTWARE.
///
#include <cstdint>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
int
readFile(const std::string path)
{
std::cout << "[+] opening " << path << "\n";
std::ifstream input(path, std::ios::in);
if (!input.good()) {
std::cerr << "[!] failed to open " << path << "\n";
input.close();
return -1;
}
std::cout << "[+] reading from " << path << "\n";
std::vector<std::vector<uint8_t>> doc;
while (input.good()) {
std::string temp;
while (std::getline(input, temp)) {
std::cout << "[+] line: " << temp << "\n";
std::vector<uint8_t> row(temp.begin(), temp.end());
doc.push_back(row);
}
}
std::cout << "[+] finished reading document\n";
std::cout << "\tgood: " << input.good() << "\n";
std::cout << "\tfail: " << input.fail() << "\n";
std::cout << "\t bad: " << input.bad() << "\n";
std::cout << "\t eof: " << input.eof() << "\n";
input.close();
std::cout << "[+] dumping document of " << doc.size() << " lines.\n";
for (size_t i = 0; i < doc.size(); i++) {
std::cout << "line " << std::setw(3) << i << ": ";
std::copy(doc[i].begin(), doc[i].end(),
std::ostream_iterator<uint8_t>(std::cout, ""));
std::cout << "\n";
}
return 0;
}
int
main(int argc, char *argv[])
{
for (int i = 1; i < argc; i++) {
std::cout << "[+] input file: " << argv[i] << "\n";
if (readFile(std::string(argv[i])) != 0) {
std::cerr << "[!] failed\n";
} else {
std::cout << "[+] OK\n";
}
}
}

93
sketches/enoent.cc Normal file
View File

@@ -0,0 +1,93 @@
///
/// \file enoent.cc
/// \author kyle
/// \created 2023-10-11
/// \brief basic file system checks
///
/// \section COPYRIGHT
/// Copyright 2023 K. Isom <kyle@imap.cc>
///
/// Permission to use, copy, modify, and/or distribute this software for
/// any purpose with or without fee is hereby granted, provided that the
/// above copyright notice and this permission notice appear in all copies.
///
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
/// BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
/// OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
/// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
/// ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
/// SOFTWARE.
///
#include <algorithm>
#include <iostream>
#include <iterator>
#include <fstream>
#include <filesystem>
#include <vector>
using namespace std;
int
main()
{
namespace fs = std::filesystem;
std::string input = "ohai\n";
std::vector<uint8_t> buffer(input.begin(), input.end());
auto f = fs::path("ENOENT2");
if (fs::exists(f)) {
std::cout << "[+] the file exists\n";
std::cout << "\tfile size: " << fs::file_size(f) << "\n";
} else {
std::cout << "the file does not exist\n";
}
cout << "[+] attempting to open the file.\n";
ofstream handle(f, std::ios::in | std::ios::out | std::ios::trunc);
if (!handle.good()) {
cerr << "[!] file operation failed\n";
cerr << "\tfile fail: " << handle.fail() << "\n";
cerr << "\t file eof: " << handle.eof() << "\n";
cerr << "\t file bad: " << handle.bad() << "\n";
return 1;
}
if (fs::exists(f)) {
std::cout << "[+] the file exists\n";
std::cout << "\tfile size: " << fs::file_size(f) << "\n";
} else {
std::cout << "the file does not exist\n";
}
handle.flush();
if (fs::exists(f)) {
std::cout << "[+] the file exists\n";
std::cout << "\tfile size: " << fs::file_size(f) << "\n";
} else {
std::cout << "the file does not exist\n";
}
cout << "[+] file open successfully. attempting to write.\n";
handle << "Hello, world.\r\n";
if (fs::exists(f)) {
std::cout << "\tfile size: " << fs::file_size(f) << "\n";
}
cout << "[+] attempting to write the vector to file.\n";
std::copy(buffer.begin(), buffer.end(), std::ostream_iterator<uint8_t>(handle, ""));
cout << "[+] closing the file.\n";
handle.close();
if (fs::exists(f)) {
std::cout << "\tfile size: " << fs::file_size(f) << "\n";
}
return 0;
}

9
sketches/testShort.txt Normal file
View File

@@ -0,0 +1,9 @@
this is a short text file.
just wanted to see what it looks like.
of course
of course. This is more of a paragraph that should end |
at the pipes, which should be a border. It's one way to|
test it's being read correctly.