forked from vgmrips/vgmtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
160 lines (126 loc) · 3.26 KB
/
common.h
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
#ifndef __COMMON_H__
#define __COMMON_H__
// Defined functions:
//static void RemoveNewLines(char* String);
//static void RemoveQuotationMarks(char* String);
//static void ReadFilename(char* buffer, size_t bufsize);
//INLINE void DblClickWait(const char* argv0);
//static void PrintMinSec(const UINT32 SamplePos, char* TempStr); [WIN32 only]
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
#include <string.h>
#include <stdio.h> // for sprintf()
#ifdef _WIN32
#include <conio.h> // for _getch()
#include <windows.h> // for OemToChar()
#else
#include <limits.h>
#define MAX_PATH PATH_MAX
#endif
#ifdef _MSC_VER
#define strdup _strdup
#define stricmp _stricmp
#define strnicmp _strnicmp
#else
#define _getch getchar
#define stricmp strcasecmp
#define strnicmp strncasecmp
#endif
#ifdef _WIN32
// Note: Both, MS VC++ and MinGW use _wcsicmp.
#define wcsicmp _wcsicmp
#else
#define wcsicmp wcscasecmp
#endif
// Console Quotation Mark (Windows: ", Linux: ')
#ifdef _WIN32
#define QMARK_CHR '\"'
#else
#define QMARK_CHR '\''
#endif
#define ZLIB_SEEKBUG_CHECK(vgmhdr) \
if (vgmhdr.fccVGM != FCC_VGM) \
{ \
printf("VGM signature matched on the first read, but not on the second one!\n"); \
printf("This is a known zlib bug where gzseek fails. Please install a fixed zlib.\n"); \
goto OpenErr; \
}
static void RemoveNewLines(char* String)
{
char* strPtr;
strPtr = String + strlen(String) - 1;
while(strPtr >= String && (UINT8)*strPtr < 0x20) // <0x20 -> is Control Character
{
*strPtr = '\0';
strPtr --;
}
return;
}
static void RemoveQuotationMarks(char* String)
{
size_t strLen;
char* endQMark;
if (String[0] != QMARK_CHR)
return;
strLen = strlen(String);
memmove(String, String + 1, strLen); // Remove first char
endQMark = strrchr(String, QMARK_CHR);
if (endQMark != NULL)
*endQMark = '\0'; // Remove last Quot.-Mark
return;
}
static void ReadFilename(char* buffer, size_t bufsize)
{
char* retStr;
retStr = fgets(buffer, bufsize, stdin);
if (retStr == NULL)
buffer[0] = '\0';
#ifdef _WIN32
if (GetConsoleCP() == GetOEMCP())
OemToChar(buffer, buffer); // OEM -> ANSI conversion
#endif
RemoveNewLines(buffer);
RemoveQuotationMarks(buffer);
return;
}
INLINE void DblClickWait(const char* argv0)
{
#ifdef _WIN32
char* msystem;
if (argv0[1] != ':')
return; // not called with an absolute path
msystem = getenv("MSYSTEM");
if (msystem != NULL)
{
// return, if we're within a MinGW/MSYS environment
// (MSYS always calls using absolute paths)
if (! strncmp(msystem, "MINGW", 5) ||
! strncmp(msystem, "MSYS", 4))
return;
}
// Executed by Double-Clicking (or Drag and Drop),
// so let's keep the window open until the user presses a key.
if (_kbhit())
_getch();
_getch();
return;
#else
// Double-clicking a console application on Unix doesn't open
// a console window, so don't do anything.
return;
#endif
}
// Note: Really used by vgm2txt and vgmlpfnd only
// most other tools use it for progress display under Windows
static void PrintMinSec(const UINT32 SamplePos, char* TempStr)
{
float TimeSec;
UINT16 TimeMin;
TimeSec = (float)SamplePos / 44100.0f;
TimeMin = (UINT16)(TimeSec / 60);
TimeSec -= TimeMin * 60;
sprintf(TempStr, "%02u:%05.2f", TimeMin, TimeSec);
return;
}
#endif // __COMMON_H__