Skip to content

Commit

Permalink
porting_layer: trivial syntax tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
jserv committed Mar 11, 2015
1 parent f6c8aeb commit 66faee6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/porting_layer/src/plat_mmap_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,4 @@ void plat_mmap_unmap(plat_mmap *handle)
}
}

#endif /* UNDER_POSIX */
#endif /* UNDER_POSIX */
43 changes: 31 additions & 12 deletions src/porting_layer/src/plat_mmap_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,43 +55,57 @@ size_t plat_mmap_create(plat_mmap *handle, const char *file, int fileAccessAttr)
# ifdef _WIN32_WCE
handle->fd_file = CreateFileForMappingA(file,
GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
FILE_SHARE_READ, NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
# else /* !_WIN32_WCE */
handle->fd_file = CreateFileA(file,
GENERIC_READ,
FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY | FILE_FLAG_RANDOM_ACCESS, 0);
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_READONLY |
FILE_FLAG_RANDOM_ACCESS, 0);
# endif /* _WIN32_WCE */

if (INVALID_HANDLE_VALUE == handle->fd_file)
return 0;

sizet.LowPart = GetFileSize(handle->fd_file, (LPDWORD) & sizet.HighPart);
handle->fd_map = CreateFileMappingA(handle->fd_file, NULL, PAGE_READONLY, sizet.HighPart, sizet.LowPart, 0);
sizet.LowPart = GetFileSize(handle->fd_file,
(LPDWORD) & sizet.HighPart);
handle->fd_map = CreateFileMappingA(handle->fd_file, NULL,
PAGE_READONLY,
sizet.HighPart, sizet.LowPart, 0);
} else {
# ifdef _WIN32_WCE
handle->fd_file = CreateFileForMappingA(file,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
FILE_SHARE_WRITE, NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
# else /* !_WIN32_WCE */
handle->fd_file = CreateFileA(file,
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
FILE_SHARE_WRITE, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
# endif /* _WIN32_WCE */

if (INVALID_HANDLE_VALUE == handle->fd_file)
return 0;

sizet.LowPart = 0;
sizet.HighPart = 1;
handle->fd_map = CreateFileMapping(handle->fd_file, NULL, PAGE_READWRITE, 0, sizet.LowPart, 0);
handle->fd_map = CreateFileMapping(handle->fd_file, NULL,
PAGE_READWRITE, 0,
sizet.LowPart, 0);
sizet.LowPart = 1024 * 1024 * 1024;
sizet.HighPart = 0;

while (!handle->fd_map) {
DWORD error;

handle->fd_map = CreateFileMapping(handle->fd_file, NULL, PAGE_READWRITE, 0, sizet.LowPart, 0);
handle->fd_map = CreateFileMapping(handle->fd_file, NULL,
PAGE_READWRITE, 0,
sizet.LowPart, 0);
error = GetLastError();

if (ERROR_NOT_ENOUGH_MEMORY == error || ERROR_DISK_FULL == error)
Expand All @@ -103,7 +117,8 @@ size_t plat_mmap_create(plat_mmap *handle, const char *file, int fileAccessAttr)

handle->fAccessAttr = fileAccessAttr;

if (FLAG_ATTRIBUTE_READ & fileAccessAttr || 16 * 1024 * 1024 <= sizet.LowPart) {
if (FLAG_ATTRIBUTE_READ & fileAccessAttr ||
16 * 1024 * 1024 <= sizet.LowPart) {
if (handle->fd_map)
return (size_t) sizet.QuadPart;
}
Expand Down Expand Up @@ -139,10 +154,14 @@ void *plat_mmap_set_view(plat_mmap *handle, size_t * offset, size_t * sizet)

if (FLAG_ATTRIBUTE_READ & handle->fAccessAttr) {
handle->address = MapViewOfFile(handle->fd_map,
FILE_MAP_READ, t_offset.HighPart, t_offset.LowPart, t_sizet.LowPart);
FILE_MAP_READ,
t_offset.HighPart,
t_offset.LowPart, t_sizet.LowPart);
} else {
handle->address = MapViewOfFile(handle->fd_map,
FILE_MAP_WRITE, t_offset.HighPart, t_offset.LowPart, t_sizet.LowPart);
FILE_MAP_WRITE,
t_offset.HighPart,
t_offset.LowPart, t_sizet.LowPart);
}

return handle->address;
Expand Down Expand Up @@ -200,4 +219,4 @@ void plat_mmap_unmap(plat_mmap *handle)
}
}

#endif /* defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE) */
#endif /* defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE) */
46 changes: 24 additions & 22 deletions src/porting_layer/src/plat_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ int get_search_path(char *path, size_t path_len)
} else {
home = getenv("HOME");
if (home) {
snprintf(path, path_len, "%s/.chewing" SEARCH_PATH_SEP CHEWING_DATADIR, home);
snprintf(path, path_len,
"%s/.chewing" SEARCH_PATH_SEP CHEWING_DATADIR, home);
} else {
// No HOME ?
/* No HOME ? */
strncpy(path, SEARCH_PATH_SEP CHEWING_DATADIR, path_len);
}
}
Expand All @@ -59,7 +60,7 @@ int get_search_path(char *path, size_t path_len)

chewing_path = getenv("CHEWING_PATH");
if (chewing_path) {
// FIXME: Check for truncated.
/* FIXME: Check for truncated. */
strncpy(path, chewing_path, path_len);
} else {

Expand All @@ -69,26 +70,23 @@ int get_search_path(char *path, size_t path_len)
* - %CSIDL_PROGRAM_FILESX86%/ChewingTextService/Dictionary
* - %CSIDL_PROGRAM_FILES%/ChewingTextService/Dictionary
*/

if (path_len < MAX_PATH) {
if (path_len < MAX_PATH)
return -1;
}

result = SHGetFolderPathA(NULL, CSIDL_PROGRAM_FILESX86, NULL, 0, path);
if(result != S_OK) {
if (result != S_OK)
result = SHGetFolderPathA(NULL, CSIDL_PROGRAM_FILES, NULL, 0, path);
}

if (result != S_OK) {
if (result != S_OK)
return -1;
}

len = strlen(path);
path += len;
path_len -= len;

// FIXME: Check for truncated.
snprintf(path, path_len, "\\%s\\%s", "ChewingTextService", "Dictionary");
/* FIXME: Check for truncated. */
snprintf(path, path_len,
"\\%s\\%s", "ChewingTextService", "Dictionary");
}

return 0;
Expand All @@ -115,10 +113,10 @@ char *strtok_r(char *s, const char *delim, char **save_ptr)
/* Find the end of the token. */
token = s;
s = strpbrk(token, delim);
if (s == NULL)
if (s == NULL) {
/* This token finishes the string. */
*save_ptr = token + strlen(token);
else {
} else {
/* Terminate the token and make *SAVE_PTR point past it. */
*s = '\0';
*save_ptr = s + 1;
Expand Down Expand Up @@ -152,24 +150,28 @@ int asprintf(char **strp, const char *fmt, ...)
}
#endif

static int are_all_files_readable(const char *path, const char *const *files, char *output, size_t output_len)
static int are_all_files_readable(const char *path,
const char *const *files,
char *output, size_t output_len)
{
int i;

assert(path);
assert(files);

for (i = 0; files[i] != NULL; ++i) {
snprintf(output, output_len, "%s" PLAT_SEPARATOR "%s", path, files[i]);
if (access(output, R_OK) != 0) {
snprintf(output, output_len,
"%s" PLAT_SEPARATOR "%s", path, files[i]);
if (access(output, R_OK) != 0)
return 0;
}
}

return 1;
}

int find_path_by_files(const char *search_path, const char *const *files, char *output, size_t output_len)
int find_path_by_files(const char *search_path,
const char *const *files,
char *output, size_t output_len)
{
char buffer[PATH_MAX + 1] = {0};
char *path;
Expand All @@ -181,11 +183,11 @@ int find_path_by_files(const char *search_path, const char *const *files, char *
assert(output);
assert(output_len);

// strtok_r will modify its first parameter.
/* strtok_r will modify its first parameter. */
strncpy(buffer, search_path, sizeof(buffer) - 1);

for (path = strtok_r(buffer, SEARCH_PATH_SEP, &saveptr); path; path = strtok_r(NULL, SEARCH_PATH_SEP, &saveptr)) {

for (path = strtok_r(buffer, SEARCH_PATH_SEP, &saveptr); path;
path = strtok_r(NULL, SEARCH_PATH_SEP, &saveptr)) {
ret = are_all_files_readable(path, files, output, output_len);
if (ret) {
snprintf(output, output_len, "%s", path);
Expand Down

0 comments on commit 66faee6

Please sign in to comment.