VirtualC64 v5.0 beta
Commodore 64 Emulator
Loading...
Searching...
No Matches
IOUtils.h
1// -----------------------------------------------------------------------------
2// This file is part of VirtualC64
3//
4// Copyright (C) Dirk W. Hoffmann. www.dirkwhoffmann.de
5// This FILE is dual-licensed. You are free to choose between:
6//
7// - The GNU General Public License v3 (or any later version)
8// - The Mozilla Public License v2
9//
10// SPDX-License-Identifier: GPL-3.0-or-later OR MPL-2.0
11// -----------------------------------------------------------------------------
12
13#pragma once
14
15#include "Types.h"
16#include "StringUtils.h"
17
18#include <fcntl.h>
19#include <filesystem>
20#include <fstream>
21#include <istream>
22#include <iostream>
23#include <sstream>
24#include <sys/stat.h>
25#include <vector>
26
27namespace vc64::util {
28
29namespace fs = ::std::filesystem;
30
31//
32// Handling files
33//
34
35// Makes a file name unique if a file with the provided name already exists
36fs::path makeUniquePath(const fs::path &path);
37
38// Returns the size of a file in bytes
39isize getSizeOfFile(const fs::path &path);
40
41// Checks if a file exists
42bool fileExists(const fs::path &path);
43
44// Checks if a path points to a directory
45bool isDirectory(const fs::path &path);
46
47// Creates a directory
48bool createDirectory(const fs::path &path);
49
50// Returns the number of files in a directory
51isize numDirectoryItems(const fs::path &path);
52
53// Returns a list of files in a directory
54std::vector<fs::path> files(const fs::path &path, const string &suffix = "");
55std::vector<fs::path> files(const fs::path &path, std::vector <string> &suffixes);
56
57// Checks the header signature (magic bytes) of a stream or buffer
58bool matchingStreamHeader(std::istream &is, const u8 *header, isize len, isize offset = 0);
59bool matchingStreamHeader(std::istream &is, const string &header, isize offset = 0);
60bool matchingBufferHeader(const u8 *buffer, const u8 *header, isize len, isize offset = 0);
61
62//
63// Pretty printing
64//
65
66// Writes an integer into a string in decimal format
67void sprint8d(char *s, u8 value);
68void sprint16d(char *s, u16 value);
69
70// Writes an integer into a string in hexadecimal format
71void sprint8x(char *s, u8 value);
72void sprint16x(char *s, u16 value);
73
74// Writes an integer into a string in binary format
75void sprint8b(char *s, u8 value);
76void sprint16b(char *s, u16 value);
77
78
79//
80// Handling streams
81//
82
83isize streamLength(std::istream &stream);
84
85struct dec {
86
87 i64 value;
88
89 dec(i64 v) : value(v) { };
90 std::ostream &operator()(std::ostream &os) const;
91};
92
93struct hex {
94
95 int digits;
96 u64 value;
97
98 hex(int d, u64 v) : digits(d), value(v) { };
99 hex(u64 v) : hex(16, v) { };
100 hex(u32 v) : hex(8, v) { };
101 hex(u16 v) : hex(4, v) { };
102 hex(u8 v) : hex(2, v) { };
103 std::ostream &operator()(std::ostream &os) const;
104};
105
106struct bin {
107
108 isize digits;
109 u64 value;
110
111 bin(isize d, u64 v) : digits(d), value(v) { };
112 bin(u64 v) : bin(64, v) { };
113 bin(u32 v) : bin(32, v) { };
114 bin(u16 v) : bin(16, v) { };
115 bin(u8 v) : bin(8, v) { };
116 std::ostream &operator()(std::ostream &os) const;
117};
118
119struct flt {
120
121 double value;
122
123 flt(double v) : value(v) { };
124 flt(float v) : value(double(v)) { };
125 std::ostream &operator()(std::ostream &os) const;
126};
127
128struct tab {
129
130 int pads;
131 const string &str;
132
133 tab(int p, const string &s) : pads(p), str(s) { };
134 tab(const string &s) : tab(24, s) { };
135 std::ostream &operator()(std::ostream &os) const;
136};
137
138struct bol {
139
140 static const string& yes;
141 static const string& no;
142
143 bool value;
144 const string &str1, &str2;
145
146 bol(bool v, const string &s1, const string &s2) : value(v), str1(s1), str2(s2) { };
147 bol(bool v) : bol(v, yes, no) { };
148 std::ostream &operator()(std::ostream &os) const;
149};
150
151struct str {
152
153 isize characters;
154 u64 value;
155
156 str(isize c, u64 v) : characters(c), value(v) { };
157 str(u64 v) : str(8, v) { };
158 str(u32 v) : str(4, v) { };
159 str(u16 v) : str(2, v) { };
160 str(u8 v) : str(1, v) { };
161 std::ostream &operator()(std::ostream &os) const;
162};
163
164inline std::ostream &operator <<(std::ostream &os, dec v) { return v(os); }
165inline std::ostream &operator <<(std::ostream &os, hex v) { return v(os); }
166inline std::ostream &operator <<(std::ostream &os, bin v) { return v(os); }
167inline std::ostream &operator <<(std::ostream &os, flt v) { return v(os); }
168inline std::ostream &operator <<(std::ostream &os, tab v) { return v(os); }
169inline std::ostream &operator <<(std::ostream &os, bol v) { return v(os); }
170inline std::ostream &operator <<(std::ostream &os, str v) { return v(os); }
171
172}