-
-
Save re3turn/9a58f8a250485cfd4cdca9d07a4f346d to your computer and use it in GitHub Desktop.
絶対パス変換して出力
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <Windows.h> | |
#include <cassert> | |
enum { FIELD_WIDTH = 17 }; | |
int print_fullpath(char *source_path); | |
int print_getfullpathname(char *source_path); | |
void print_result(char *function_name, char *full_path); | |
void print_error(char *function_name, DWORD error_number); | |
void test(void); | |
int main(int argc, char *argv[]) | |
{ | |
char *source_path; | |
char return_code = 0; | |
//test(); | |
if(argc == 1){ | |
printf("Usage: fullpath source_path\n"); | |
return 1; | |
} | |
source_path = argv[1]; | |
print_result("Source path", source_path); | |
return_code |= print_fullpath(source_path); | |
return_code |= print_getfullpathname(source_path); | |
return return_code; | |
} | |
int print_fullpath(char *source_path) | |
{ | |
char full_path[_MAX_PATH]; | |
char *is_fullpath; | |
is_fullpath = _fullpath(full_path, source_path, _MAX_PATH); | |
if(is_fullpath == NULL){ | |
print_error("_fullpath()", errno); | |
return 1; | |
} | |
print_result("_fullpath()", full_path); | |
return 0; | |
} | |
int print_getfullpathname(char *source_path) | |
{ | |
char full_path[_MAX_PATH]; | |
DWORD path_length; | |
path_length = GetFullPathName(source_path, _MAX_PATH, full_path, NULL); | |
if(path_length == 0){ | |
errno = GetLastError(); | |
print_error("GetFullPathName()", errno); | |
return 1; | |
} | |
print_result("GetFullPathName()", full_path); | |
return 0; | |
} | |
void print_result(char *function_name, char *full_path) | |
{ | |
printf("%*s = \"%s\"\n", FIELD_WIDTH, function_name, full_path); | |
} | |
void print_error(char *function_name, DWORD error_number) | |
{ | |
printf("%*s error, errno= \"%ld\"\n", FIELD_WIDTH, function_name, error_number); | |
} | |
void test(void) | |
{ | |
assert(print_fullpath("D:") == 0); | |
assert(print_fullpath("\\") == 0); | |
assert(print_fullpath(".") == 0); | |
assert(print_fullpath("text.txt") == 0); | |
assert(print_fullpath("\\text.txt") == 0); | |
assert(print_fullpath("D:\\dir\\..\\text.txt") == 0); | |
assert(print_fullpath("D:\\aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ \ | |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ \ | |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ \ | |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ \ | |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") == 1); | |
assert(print_getfullpathname("D:") == 0); | |
assert(print_getfullpathname("\\") == 0); | |
assert(print_getfullpathname(".") == 0); | |
assert(print_getfullpathname("text.txt") == 0); | |
assert(print_getfullpathname("\\text.txt") == 0); | |
assert(print_getfullpathname("D:\\dir\\..\\text.txt") == 0); | |
assert(print_getfullpathname("D:\\aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ \ | |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ \ | |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ \ | |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\ \ | |
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") == 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment