-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIO.c
More file actions
96 lines (80 loc) · 1.78 KB
/
IO.c
File metadata and controls
96 lines (80 loc) · 1.78 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
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
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <pgtypes_timestamp.h>
#ifdef _MSC_VER
#include <Windows.h>
#elif _WIN32
#include <windows.h>
#endif
int InputLine(char* stringToInput, int maxStringLength)
{
unsigned long stringLength = 0;
bool isError = false;
while (true)
{
char* fgetsReturn = fgets(stringToInput, maxStringLength, stdin);
if (fgetsReturn == NULL)
{
return -2;
}
stringLength = strlen(stringToInput);
if (stringLength == 0 || stringToInput[stringLength - 1] != '\n')
{
isError = true;
}
else
{
break;
}
}
if (!isError)
{
stringToInput[stringLength - 1] = '\0';
return (int) stringLength - 1;
}
return -1;
}
void OutputMessage(char* author, char* message, timestamp time)
{
char* timestr = PGTYPEStimestamp_to_asc(time);
printf("%-26s %15s: %s\n", timestr, author, message);
PGTYPESchar_free(timestr);
}
bool TryParseInt(char* string, int* pResult)
{
size_t length = strlen(string);
int position;
int sscanfReturns = sscanf(string, "%d%n", pResult, &position);
if (position != length || sscanfReturns < 0)
{
return false;
}
return true;
}
int ParseInt(char* string, int* pSuccessfulCount)
{
int result;
bool tryParseInt = TryParseInt(string, &result);
if (pSuccessfulCount != NULL && tryParseInt)
{
(*pSuccessfulCount)++;
}
if (!tryParseInt)
{
return 0;
}
return result;
}
void SetInputOutputToUtf8()
{
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8);
SetConsoleCP(CP_UTF8);
#endif
}
void SetUnbufferedOutput()
{
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
}