This repository was archived by the owner on May 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcat.hpp
More file actions
62 lines (52 loc) · 1.61 KB
/
cat.hpp
File metadata and controls
62 lines (52 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#pragma once
#include <all.hpp>
class CatCommand : public liteshell::BaseCommand
{
public:
CatCommand()
: liteshell::BaseCommand(
"cat",
"Read a file",
"Displays the content of a text file.",
{"type"},
liteshell::CommandConstraint("file", "The file to read", true))
{
}
DWORD run(const liteshell::Context &context)
{
auto file = CreateFileW(
utils::utf_convert(context.get("file")).c_str(),
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (file == INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
throw std::invalid_argument("The specified file does not exist");
}
throw std::runtime_error(utils::last_error("Error when opening file"));
}
auto _finalize = utils::Finalize(
[&file]()
{
CloseHandle(file);
});
char buffer[LITE_SHELL_BUFFER_SIZE];
ZeroMemory(buffer, LITE_SHELL_BUFFER_SIZE);
DWORD read = LITE_SHELL_BUFFER_SIZE;
while (read == LITE_SHELL_BUFFER_SIZE)
{
if (!ReadFile(file, buffer, LITE_SHELL_BUFFER_SIZE, &read, NULL))
{
throw std::runtime_error(utils::last_error("Error when reading file"));
}
std::cout << std::string(buffer, buffer + read) << std::flush;
}
std::cout << std::endl;
return 0;
}
};