-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtextio.cpp
172 lines (118 loc) · 3.71 KB
/
textio.cpp
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <dos.h>
#define TIMESTAMP_FORMAT "%Y-%m-%d %H:%M:%S"
#define TIMESTAMP_SIZE 30
char timestampStr[TIMESTAMP_SIZE];
FILE *historyFile = NULL;
void updateTimeStamp(){
time_t currentTime;
struct tm* timeInfo;
time(¤tTime);
timeInfo = localtime(¤tTime);
memset(timestampStr, 0, TIMESTAMP_SIZE);
strftime(timestampStr, TIMESTAMP_SIZE, TIMESTAMP_FORMAT, timeInfo);
}
//Reference https://www.equestionanswers.com/c/c-int86-dos-bios-system-interrupts.php
int getScreenColumns(){
union REGS input_regs, output_regs;
int numCols;
input_regs.h.ah = 0x0F;
int86(0x10, &input_regs, &output_regs);
numCols = output_regs.h.ah;
return numCols;
}
void io_timestamp(){
updateTimeStamp();
#define TIMESTAMP_PRINT_FORMAT "[%s]\n"
printf(TIMESTAMP_PRINT_FORMAT, timestampStr);
if(historyFile){
fprintf(historyFile, TIMESTAMP_PRINT_FORMAT, timestampStr);
}
}
void io_app_error(char * str, int length){
#define APP_ERROR_FORMAT "App Error:\n%.*s\n"
printf(APP_ERROR_FORMAT, length, str);
if(historyFile){
fprintf(historyFile, APP_ERROR_FORMAT, length, str);
}
}
void io_server_error(char * str, int length){
#define GPT_ERROR_FORMAT "Server Error:\n%.*s\n"
printf(GPT_ERROR_FORMAT, length, str);
if(historyFile){
fprintf(historyFile, GPT_ERROR_FORMAT, length, str);
}
}
void io_str_newline(char * str){
//First part of this function will do word wrapping
int columns = getScreenColumns();
int len = strlen(str);
int startPos = 0;
int i;
while (startPos < len) {
int charactersRemaining = len - startPos;
// Find and print the last chunk
if((charactersRemaining) <= columns){
printf("%.*s", charactersRemaining, str + startPos);
break;
}
int endOfLine = startPos + columns - 1;
int endPosOfCurrentString = endOfLine;
//Look for newlines so we can break prematurely
if(char * charPos = (char *) memchr(str + startPos, '\n', columns - 1)){
endPosOfCurrentString = (int) (charPos - str);
} else {
// Move backwards until we find a space
for(int i = endOfLine; i > startPos; i--){
char currentChar = str[i];
if(currentChar == ' '){
endPosOfCurrentString = i;
break;
}
}
}
int lengthToPrint = endPosOfCurrentString - startPos;
printf("%.*s\n", lengthToPrint, str + startPos);
startPos = endPosOfCurrentString + 1;
}
printf("\n");
//printf("%s\n", str);
//This part writes the non-wrapped portion to file.
if(historyFile){
fprintf(historyFile, "%s\n", str);
}
}
void io_write_str_no_print(char * str, int length){
if(historyFile){
fprintf(historyFile, "%.*s", length, str);
}
}
void io_char(char c){
printf("%c", c);
if(historyFile){
fprintf(historyFile, "%c", c);
}
}
void io_request_info(unsigned int port, int promptTokens, int completionTokens){
#define INFO_FORMAT "[Outgoing port %u, %d prompt tokens, %d completion tokens]\n"
printf(INFO_FORMAT, port, promptTokens, completionTokens);
if(historyFile){
fprintf(historyFile, INFO_FORMAT, port, promptTokens, completionTokens);
}
}
bool io_open_history_file(char * filePath){
historyFile = fopen(filePath, "a");
if(historyFile == NULL){
return false;
} else {
return true;
}
}
void io_close_history_file(){
if(historyFile != NULL){
fclose(historyFile);
historyFile = NULL;
}
}