-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
176 lines (147 loc) · 5.45 KB
/
main.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
171
172
173
174
175
176
#include <windows.h>
#include <string>
#include "main.h"
int main(int argc, char **argv) {
bool status;
printf("Hello, world\n");
int tempBufLen = MAX_PATH+1;
char tempPath[tempBufLen];
if (argc <= 1) {
int tempPathLen = GetTempPath(tempBufLen, (TCHAR*)tempPath);
if (tempPathLen > tempBufLen) {
printf("Could not receive temp path, allocated: %d, needed: %d\n", tempBufLen, tempPathLen);
return 1;
}
} else {
printf("Temp path specified by command line: '%s'\n", argv[argc - 1]);
strcpy(tempPath, argv[argc - 1]); // override
}
printf("Temp path: '%s'\n", tempPath);
char const *sourceName = "sourcefile.txt";
char const *lowerName = "case.txt";
char const *upperName = "CASE.txt";
char *sourcePath = (char*) malloc(strlen(sourceName) + strlen(tempPath) + 1);
strcpy(sourcePath, tempPath);
strcat(sourcePath, sourceName);
char *lowerPath = (char*) malloc(strlen(lowerName) + strlen(tempPath) + 1);
strcpy(lowerPath, tempPath);
strcat(lowerPath, lowerName);
char *upperPath = (char*) malloc(strlen(upperName) + strlen(tempPath) + 1);
strcpy(upperPath, tempPath);
strcat(upperPath, upperName);
// delete after previous run if exists
DeleteFile(upperPath);
printf("Creating temporary source file: '%s'\n", sourcePath);
HANDLE sourceFile = CreateFile(
sourcePath,
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (sourceFile == INVALID_HANDLE_VALUE) {
ShowLastErrorMsg();
return 1;
}
printf("Moving file from '%s' to '%s'\n", sourcePath, lowerPath);
status = MoveFileEx(sourcePath, lowerPath, 0);
if (!status) {
ShowLastErrorMsg();
return 1;
}
/**
printf("Changing capitalization from '%s' to '%s'\n", lowerPath, upperPath);
status = MoveFileEx(lowerPath, upperPath, 0);
if (!status) {
ShowLastErrorMsg();
return 1;
}
*/
HANDLE caseFile = CreateFile(
upperPath,
GENERIC_READ | DELETE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_POSIX_SEMANTICS,
NULL
);
if (caseFile == INVALID_HANDLE_VALUE) {
ShowLastErrorMsg();
return 1;
}
puts("Opening the resulting case file...");
LPBY_HANDLE_FILE_INFORMATION caseInfo;
status = GetFileInformationByHandle(caseFile, caseInfo);
if (!status) {
ShowLastErrorMsg();
return 1;
}
int finalBufLen = MAX_PATH+1;
TCHAR caseFileFinalPath[finalBufLen];
DWORD statusD;
statusD = GetFinalPathNameByHandle(caseFile, caseFileFinalPath, finalBufLen, FILE_NAME_NORMALIZED);
if (statusD == 0) {
ShowLastErrorMsg();
return 1;
} else if (statusD > finalBufLen) {
printf("Buffer too small to hold final path length for upper case: %d\n", statusD);
return 1;
}
printf("Resulting case file path: '%s'\n", caseFileFinalPath);
char const *anotherPath = upperPath; //"C:\\Users\\Alex\\AppData\\Local\\Temp\\Another.txt";
int wchars_num = MultiByteToWideChar( CP_UTF8 , 0 , anotherPath, -1, NULL , 0 );
wchar_t* wstr = new wchar_t[wchars_num];
int wcharsSize = sizeof(wchar_t) * wchars_num;
MultiByteToWideChar( CP_UTF8 , 0 , anotherPath, -1, wstr, wchars_num );
wprintf(L"Wide path is (%d chars, %d bytes): '%ls'\n", wchars_num, wcharsSize, wstr);
BY_HANDLE_FILE_INFORMATION fi = {0};
GetFileInformationByHandle(caseFile, &fi);
int renameInfoSize = sizeof(FILE_RENAME_INFO) + wcharsSize + sizeof(wchar_t);
FILE_RENAME_INFO *renameInfo = (FILE_RENAME_INFO*)malloc(renameInfoSize);
renameInfo->ReplaceIfExists = true;
renameInfo->RootDirectory = NULL;
renameInfo->FileNameLength = wcharsSize;
wchar_t *infoNewName = renameInfo->FileName;
memcpy(renameInfo->FileName, wstr, wcharsSize);
puts("Renaming file with SetFileInformationByHandle\n");
status = SetFileInformationByHandle(caseFile, FileRenameInfo, (LPVOID)renameInfo, renameInfoSize);
if (!status) {
ShowLastErrorMsg();
return 1;
}
statusD = GetFinalPathNameByHandle(caseFile, caseFileFinalPath, finalBufLen, FILE_NAME_NORMALIZED);
if (statusD == 0) {
ShowLastErrorMsg();
return 1;
} else if (statusD > finalBufLen) {
printf("Buffer too small to hold final path length for upper case: %d\n", statusD);
return 1;
}
printf("Resulting case file path: '%s'\n", caseFileFinalPath);
CloseHandle(caseFile);
printf("Done!\n");
return 0;
}
LPCTSTR ErrorMessage(DWORD error)
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0,
NULL);
return((LPCTSTR)lpMsgBuf);
}
void ShowLastErrorMsg() {
LPCTSTR errMsg = ErrorMessage(GetLastError());
printf("GetLastError: %s", errMsg);
//::MessageBox( 0,errMsg, "GetLastError", MB_OK );
LocalFree((HLOCAL)errMsg);
}