| 1 |
/* |
| 2 |
Native File Dialog |
| 3 |
|
| 4 |
http://www.frogtoss.com/labs |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <stdlib.h> |
| 8 |
#include <assert.h> |
| 9 |
#include <string.h> |
| 10 |
#include "nfd_common.h" |
| 11 |
|
| 12 |
static char g_errorstr[NFD_MAX_STRLEN] = {0}; |
| 13 |
|
| 14 |
/* public routines */ |
| 15 |
|
| 16 |
const char *NFD_GetError( void ) |
| 17 |
{ |
| 18 |
return g_errorstr; |
| 19 |
} |
| 20 |
|
| 21 |
size_t NFD_PathSet_GetCount( const nfdpathset_t *pathset ) |
| 22 |
{ |
| 23 |
assert(pathset); |
| 24 |
return pathset->count; |
| 25 |
} |
| 26 |
|
| 27 |
nfdchar_t *NFD_PathSet_GetPath( const nfdpathset_t *pathset, size_t num ) |
| 28 |
{ |
| 29 |
assert(pathset); |
| 30 |
assert(num < pathset->count); |
| 31 |
|
| 32 |
return pathset->buf + pathset->indices[num]; |
| 33 |
} |
| 34 |
|
| 35 |
void NFD_PathSet_Free( nfdpathset_t *pathset ) |
| 36 |
{ |
| 37 |
assert(pathset); |
| 38 |
NFDi_Free( pathset->indices ); |
| 39 |
NFDi_Free( pathset->buf ); |
| 40 |
} |
| 41 |
|
| 42 |
/* internal routines */ |
| 43 |
|
| 44 |
void *NFDi_Malloc( size_t bytes ) |
| 45 |
{ |
| 46 |
void *ptr = malloc(bytes); |
| 47 |
if ( !ptr ) |
| 48 |
NFDi_SetError("NFDi_Malloc failed."); |
| 49 |
|
| 50 |
return ptr; |
| 51 |
} |
| 52 |
|
| 53 |
void NFDi_Free( void *ptr ) |
| 54 |
{ |
| 55 |
assert(ptr); |
| 56 |
free(ptr); |
| 57 |
} |
| 58 |
|
| 59 |
void NFDi_SetError( const char *msg ) |
| 60 |
{ |
| 61 |
int bTruncate = NFDi_SafeStrncpy( g_errorstr, msg, NFD_MAX_STRLEN ); |
| 62 |
assert( !bTruncate ); _NFD_UNUSED(bTruncate); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
int NFDi_SafeStrncpy( char *dst, const char *src, size_t maxCopy ) |
| 67 |
{ |
| 68 |
size_t n = maxCopy; |
| 69 |
char *d = dst; |
| 70 |
|
| 71 |
assert( src ); |
| 72 |
assert( dst ); |
| 73 |
|
| 74 |
while ( n > 0 && *src != '\0' ) |
| 75 |
{ |
| 76 |
*d++ = *src++; |
| 77 |
--n; |
| 78 |
} |
| 79 |
|
| 80 |
/* Truncation case - |
| 81 |
terminate string and return true */ |
| 82 |
if ( n == 0 ) |
| 83 |
{ |
| 84 |
dst[maxCopy-1] = '\0'; |
| 85 |
return 1; |
| 86 |
} |
| 87 |
|
| 88 |
/* No truncation. Append a single NULL and return. */ |
| 89 |
*d = '\0'; |
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
/* adapted from microutf8 */ |
| 95 |
int32_t NFDi_UTF8_Strlen( const nfdchar_t *str ) |
| 96 |
{ |
| 97 |
/* This function doesn't properly check validity of UTF-8 character |
| 98 |
sequence, it is supposed to use only with valid UTF-8 strings. */ |
| 99 |
|
| 100 |
int32_t character_count = 0; |
| 101 |
int32_t i = 0; /* Counter used to iterate over string. */ |
| 102 |
nfdchar_t maybe_bom[4]; |
| 103 |
|
| 104 |
/* If there is UTF-8 BOM ignore it. */ |
| 105 |
if (strlen(str) > 2) |
| 106 |
{ |
| 107 |
strncpy(maybe_bom, str, 3); |
| 108 |
maybe_bom[3] = 0; |
| 109 |
if (strcmp(maybe_bom, (nfdchar_t*)NFD_UTF8_BOM) == 0) |
| 110 |
i += 3; |
| 111 |
} |
| 112 |
|
| 113 |
while(str[i]) |
| 114 |
{ |
| 115 |
if (str[i] >> 7 == 0) |
| 116 |
{ |
| 117 |
/* If bit pattern begins with 0 we have ascii character. */ |
| 118 |
++character_count; |
| 119 |
} |
| 120 |
else if (str[i] >> 6 == 3) |
| 121 |
{ |
| 122 |
/* If bit pattern begins with 11 it is beginning of UTF-8 byte sequence. */ |
| 123 |
++character_count; |
| 124 |
} |
| 125 |
else if (str[i] >> 6 == 2) |
| 126 |
; /* If bit pattern begins with 10 it is middle of utf-8 byte sequence. */ |
| 127 |
else |
| 128 |
{ |
| 129 |
/* In any other case this is not valid UTF-8. */ |
| 130 |
return -1; |
| 131 |
} |
| 132 |
++i; |
| 133 |
} |
| 134 |
|
| 135 |
return character_count; |
| 136 |
} |
| 137 |
|
| 138 |
int NFDi_IsFilterSegmentChar( char ch ) |
| 139 |
{ |
| 140 |
return (ch==','||ch==';'||ch=='\0'); |
| 141 |
} |
| 142 |
|