VirtualC64 v5.0 beta
Commodore 64 Emulator
Loading...
Searching...
No Matches
PETName.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 "CoreObject.h"
16#include <assert.h>
17
18namespace vc64 {
19
20template <int len> class PETName : CoreObject {
21
22 // PETSCII representation
23 u8 pet[len + 1];
24
25 // ASCII representation
26 char asc[len + 1];
27
28 // The pad characters (usually 0xA0)
29 u8 pad;
30
31public:
32
33 static u8 petscii2printable(u8 c, u8 subst)
34 {
35 if (c >= 0x20 && c <= 0x7E) return c; // 0x20 = ' ', 0x7E = '~'
36 return subst;
37 }
38
39 static u8 ascii2pet(u8 asciichar)
40 {
41 if (asciichar == 0x00) return 0x00;
42
43 asciichar = (u8)std::toupper(asciichar);
44 return asciichar >= 0x20 && asciichar <= 0x5D ? asciichar : ' ';
45 }
46
47 PETName(const u8 *_pet, u8 _pad = 0xA0) : pad(_pad)
48 {
49 assert(_pet);
50
51 memset(pet, pad, sizeof(pet));
52 memset(asc, 0x0, sizeof(asc));
53
54 for (int i = 0; i < len && _pet[i] != pad; i++) {
55
56 asc[i] = petscii2printable(_pet[i], '_');
57 pet[i] = _pet[i];
58 }
59 }
60
61 PETName(const char *_str, u8 _pad = 0xA0) : pad(_pad)
62 {
63 assert(_str);
64
65 memset(pet, pad, sizeof(pet));
66 memset(asc, 0x0, sizeof(asc));
67
68 for (int i = 0; i < len && _str[i] != 0x00; i++) {
69
70 asc[i] = _str[i];
71 pet[i] = ascii2pet(_str[i]);
72 }
73 }
74
75 PETName(string str) : PETName(str.c_str()) { }
76
77 void setPad(u8 _pad) {
78
79 for (int i = 0; i < len; i++) {
80 if (pet[i] == pad) pet[i] = _pad;
81 }
82 pad = _pad;
83 }
84
85 const char *objectName() const override { return "PETName"; }
86
87 bool operator== (PETName &rhs)
88 {
89 int i = 0;
90
91 while (pet[i] != pad || rhs.pet[i] != pad) {
92 if (pet[i] != rhs.pet[i]) return false;
93 i++;
94 }
95 return true;
96 }
97
98 PETName<len> stripped(u8 c)
99 {
100 PETName<len> name = *this;
101
102 auto length = isize(strlen(name.asc));
103 for (isize i = length; i > 0 && name.asc[i - 1] == c; i--) {
104
105 name.asc[i - 1] = 0;
106 name.pet[i - 1] = 0xA0;
107 }
108
109 return name;
110 }
111
112 void write(u8 *p, isize length)
113 {
114 assert(p);
115 assert(length <= len);
116
117 for (isize i = 0; i < length; i++) p[i] = pet[i];
118 }
119
120 void write(u8 *p) { write(p, len); }
121
122 const char *c_str() { return asc; }
123 string str() { return string(asc); }
124};
125
126}
VirtualC64 project namespace.
Definition CmdQueue.cpp:16