aboutsummaryrefslogtreecommitdiffhomepage
path: root/wutil.cpp
diff options
context:
space:
mode:
authorGravatar ridiculousfish <corydoras@ridiculousfish.com>2012-11-18 16:30:30 -0800
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2012-11-18 16:30:30 -0800
commit9992b8eb0e3366ff8a3948aa0b66a19c3c12c737 (patch)
tree6dda0fef85812016fbba9ea067c9d586092b506d /wutil.cpp
parentbab69f26724028d16054a3daf5c78aad7c67bb2d (diff)
Apply new indentation, brace, and whitespace style
Diffstat (limited to 'wutil.cpp')
-rw-r--r--wutil.cpp285
1 files changed, 153 insertions, 132 deletions
diff --git a/wutil.cpp b/wutil.cpp
index fdd6680b..730ad3d3 100644
--- a/wutil.cpp
+++ b/wutil.cpp
@@ -68,27 +68,36 @@ void wutil_destroy()
bool wreaddir_resolving(DIR *dir, const std::wstring &dir_path, std::wstring &out_name, bool *out_is_dir)
{
- struct dirent *d = readdir( dir );
- if ( !d ) return false;
+ struct dirent *d = readdir(dir);
+ if (!d) return false;
out_name = str2wcstring(d->d_name);
- if (out_is_dir) {
+ if (out_is_dir)
+ {
/* The caller cares if this is a directory, so check */
bool is_dir;
- if (d->d_type == DT_DIR) {
+ if (d->d_type == DT_DIR)
+ {
is_dir = true;
- } else if (d->d_type == DT_LNK || d->d_type == DT_UNKNOWN) {
+ }
+ else if (d->d_type == DT_LNK || d->d_type == DT_UNKNOWN)
+ {
/* We want to treat symlinks to directories as directories. Use stat to resolve it. */
cstring fullpath = wcs2string(dir_path);
fullpath.push_back('/');
fullpath.append(d->d_name);
struct stat buf;
- if (stat(fullpath.c_str(), &buf) != 0) {
+ if (stat(fullpath.c_str(), &buf) != 0)
+ {
is_dir = false;
- } else {
- is_dir = !! (S_ISDIR(buf.st_mode));
}
- } else {
+ else
+ {
+ is_dir = !!(S_ISDIR(buf.st_mode));
+ }
+ }
+ else
+ {
is_dir = false;
}
*out_is_dir = is_dir;
@@ -98,66 +107,67 @@ bool wreaddir_resolving(DIR *dir, const std::wstring &dir_path, std::wstring &ou
bool wreaddir(DIR *dir, std::wstring &out_name)
{
- struct dirent *d = readdir( dir );
- if ( !d ) return false;
+ struct dirent *d = readdir(dir);
+ if (!d) return false;
out_name = str2wcstring(d->d_name);
return true;
}
-wchar_t *wgetcwd( wchar_t *buff, size_t sz )
+wchar_t *wgetcwd(wchar_t *buff, size_t sz)
{
- char *buffc = (char *)malloc( sz*MAX_UTF8_BYTES);
- char *res;
- wchar_t *ret = 0;
-
- if( !buffc )
- {
- errno = ENOMEM;
- return 0;
- }
-
- res = getcwd( buffc, sz*MAX_UTF8_BYTES );
- if( res )
- {
- if( (size_t)-1 != mbstowcs( buff, buffc, sizeof( wchar_t ) * sz ) )
+ char *buffc = (char *)malloc(sz*MAX_UTF8_BYTES);
+ char *res;
+ wchar_t *ret = 0;
+
+ if (!buffc)
{
- ret = buff;
+ errno = ENOMEM;
+ return 0;
}
- }
- free( buffc );
+ res = getcwd(buffc, sz*MAX_UTF8_BYTES);
+ if (res)
+ {
+ if ((size_t)-1 != mbstowcs(buff, buffc, sizeof(wchar_t) * sz))
+ {
+ ret = buff;
+ }
+ }
- return ret;
+ free(buffc);
+
+ return ret;
}
-int wchdir( const wcstring &dir )
+int wchdir(const wcstring &dir)
{
cstring tmp = wcs2string(dir);
- return chdir( tmp.c_str() );
+ return chdir(tmp.c_str());
}
FILE *wfopen(const wcstring &path, const char *mode)
{
int permissions = 0, options = 0;
size_t idx = 0;
- switch (mode[idx++]) {
- case 'r':
- permissions = O_RDONLY;
- break;
- case 'w':
- permissions = O_WRONLY;
- options = O_CREAT | O_TRUNC;
- break;
- case 'a':
- permissions = O_WRONLY;
- options = O_CREAT | O_APPEND;
- break;
- default:
- errno = EINVAL;
- return NULL;
- break;
+ switch (mode[idx++])
+ {
+ case 'r':
+ permissions = O_RDONLY;
+ break;
+ case 'w':
+ permissions = O_WRONLY;
+ options = O_CREAT | O_TRUNC;
+ break;
+ case 'a':
+ permissions = O_WRONLY;
+ options = O_CREAT | O_APPEND;
+ break;
+ default:
+ errno = EINVAL;
+ return NULL;
+ break;
}
/* Skip binary */
if (mode[idx] == 'b')
@@ -182,13 +192,19 @@ FILE *wfreopen(const wcstring &path, const char *mode, FILE *stream)
return freopen(tmp.c_str(), mode, stream);
}
-bool set_cloexec(int fd) {
+bool set_cloexec(int fd)
+{
int flags = fcntl(fd, F_GETFD, 0);
- if (flags < 0) {
+ if (flags < 0)
+ {
return false;
- } else if (flags & FD_CLOEXEC) {
+ }
+ else if (flags & FD_CLOEXEC)
+ {
return true;
- } else {
+ }
+ else
+ {
return fcntl(fd, F_SETFD, flags | FD_CLOEXEC) >= 0;
}
}
@@ -199,13 +215,15 @@ static int wopen_internal(const wcstring &pathname, int flags, mode_t mode, bool
cstring tmp = wcs2string(pathname);
/* Prefer to use O_CLOEXEC. It has to both be defined and nonzero */
#ifdef O_CLOEXEC
- if (cloexec && O_CLOEXEC) {
+ if (cloexec && O_CLOEXEC)
+ {
flags |= O_CLOEXEC;
cloexec = false;
}
#endif
int fd = ::open(tmp.c_str(), flags, mode);
- if (cloexec && fd >= 0 && ! set_cloexec(fd)) {
+ if (cloexec && fd >= 0 && ! set_cloexec(fd))
+ {
close(fd);
fd = -1;
}
@@ -246,7 +264,7 @@ int wstat(const wcstring &file_name, struct stat *buf)
int lwstat(const wcstring &file_name, struct stat *buf)
{
- // fprintf(stderr, "%s\n", __PRETTY_FUNCTION__);
+ // fprintf(stderr, "%s\n", __PRETTY_FUNCTION__);
cstring tmp = wcs2string(file_name);
return lstat(tmp.c_str(), buf);
}
@@ -266,40 +284,40 @@ int wunlink(const wcstring &file_name)
void wperror(const wcstring &s)
{
- int e = errno;
- if( !s.empty() )
- {
- fwprintf( stderr, L"%ls: ", s.c_str() );
- }
- fwprintf( stderr, L"%s\n", strerror( e ) );
+ int e = errno;
+ if (!s.empty())
+ {
+ fwprintf(stderr, L"%ls: ", s.c_str());
+ }
+ fwprintf(stderr, L"%s\n", strerror(e));
}
#ifdef HAVE_REALPATH_NULL
wchar_t *wrealpath(const wcstring &pathname, wchar_t *resolved_path)
{
- cstring tmp = wcs2string(pathname);
- char *narrow_res = realpath( tmp.c_str(), 0 );
- wchar_t *res;
-
- if( !narrow_res )
- return 0;
-
- if( resolved_path )
- {
- wchar_t *tmp2 = str2wcs( narrow_res );
- wcslcpy( resolved_path, tmp2, PATH_MAX );
- free( tmp2 );
- res = resolved_path;
- }
- else
- {
- res = str2wcs( narrow_res );
- }
-
- free( narrow_res );
-
- return res;
+ cstring tmp = wcs2string(pathname);
+ char *narrow_res = realpath(tmp.c_str(), 0);
+ wchar_t *res;
+
+ if (!narrow_res)
+ return 0;
+
+ if (resolved_path)
+ {
+ wchar_t *tmp2 = str2wcs(narrow_res);
+ wcslcpy(resolved_path, tmp2, PATH_MAX);
+ free(tmp2);
+ res = resolved_path;
+ }
+ else
+ {
+ res = str2wcs(narrow_res);
+ }
+
+ free(narrow_res);
+
+ return res;
}
#else
@@ -307,53 +325,54 @@ wchar_t *wrealpath(const wcstring &pathname, wchar_t *resolved_path)
wchar_t *wrealpath(const wcstring &pathname, wchar_t *resolved_path)
{
cstring tmp = wcs2string(pathname);
- char narrow_buff[PATH_MAX];
- char *narrow_res = realpath( tmp.c_str(), narrow_buff );
- wchar_t *res;
-
- if( !narrow_res )
- return 0;
-
- if( resolved_path )
- {
- wchar_t *tmp2 = str2wcs( narrow_res );
- wcslcpy( resolved_path, tmp2, PATH_MAX );
- free( tmp2 );
- res = resolved_path;
- }
- else
- {
- res = str2wcs( narrow_res );
- }
- return res;
+ char narrow_buff[PATH_MAX];
+ char *narrow_res = realpath(tmp.c_str(), narrow_buff);
+ wchar_t *res;
+
+ if (!narrow_res)
+ return 0;
+
+ if (resolved_path)
+ {
+ wchar_t *tmp2 = str2wcs(narrow_res);
+ wcslcpy(resolved_path, tmp2, PATH_MAX);
+ free(tmp2);
+ res = resolved_path;
+ }
+ else
+ {
+ res = str2wcs(narrow_res);
+ }
+ return res;
}
#endif
-wcstring wdirname( const wcstring &path )
+wcstring wdirname(const wcstring &path)
{
char *tmp = wcs2str(path.c_str());
- char *narrow_res = dirname( tmp );
+ char *narrow_res = dirname(tmp);
wcstring result = format_string(L"%s", narrow_res);
free(tmp);
return result;
}
-wcstring wbasename( const wcstring &path )
+wcstring wbasename(const wcstring &path)
{
char *tmp = wcs2str(path.c_str());
- char *narrow_res = basename( tmp );
+ char *narrow_res = basename(tmp);
wcstring result = format_string(L"%s", narrow_res);
free(tmp);
return result;
}
/* Really init wgettext */
-static void wgettext_really_init() {
+static void wgettext_really_init()
+{
pthread_mutex_init(&wgettext_lock, NULL);
- bindtextdomain( PACKAGE_NAME, LOCALEDIR );
- textdomain( PACKAGE_NAME );
+ bindtextdomain(PACKAGE_NAME, LOCALEDIR);
+ textdomain(PACKAGE_NAME);
}
/**
@@ -365,63 +384,65 @@ static void wgettext_init_if_necessary()
pthread_once(&once, wgettext_really_init);
}
-const wchar_t *wgettext( const wchar_t *in )
+const wchar_t *wgettext(const wchar_t *in)
{
- if( !in )
- return in;
+ if (!in)
+ return in;
- // preserve errno across this since this is often used in printing error messages
- int err = errno;
+ // preserve errno across this since this is often used in printing error messages
+ int err = errno;
wgettext_init_if_necessary();
- wcstring key = in;
+ wcstring key = in;
scoped_lock lock(wgettext_lock);
wcstring *& val = wgettext_map[key];
- if (val == NULL) {
+ if (val == NULL)
+ {
cstring mbs_in = wcs2string(key);
char *out = gettext(mbs_in.c_str());
val = new wcstring(format_string(L"%s", out));
}
- errno = err;
- return val->c_str();
+ errno = err;
+ return val->c_str();
}
-wcstring wgettext2(const wcstring &in) {
+wcstring wgettext2(const wcstring &in)
+{
wgettext_init_if_necessary();
std::string mbs_in = wcs2string(in);
- char *out = gettext( mbs_in.c_str() );
+ char *out = gettext(mbs_in.c_str());
wcstring result = format_string(L"%s", out);
return result;
}
-const wchar_t *wgetenv( const wcstring &name )
+const wchar_t *wgetenv(const wcstring &name)
{
ASSERT_IS_MAIN_THREAD();
cstring name_narrow = wcs2string(name);
- char *res_narrow = getenv( name_narrow.c_str() );
- static wcstring out;
+ char *res_narrow = getenv(name_narrow.c_str());
+ static wcstring out;
- if( !res_narrow )
- return 0;
+ if (!res_narrow)
+ return 0;
out = format_string(L"%s", res_narrow);
- return out.c_str();
+ return out.c_str();
}
-int wmkdir( const wcstring &name, int mode )
+int wmkdir(const wcstring &name, int mode)
{
- cstring name_narrow = wcs2string(name);
- return mkdir( name_narrow.c_str(), mode );
+ cstring name_narrow = wcs2string(name);
+ return mkdir(name_narrow.c_str(), mode);
}
-int wrename( const wcstring &old, const wcstring &newv )
+int wrename(const wcstring &old, const wcstring &newv)
{
cstring old_narrow = wcs2string(old);
- cstring new_narrow =wcs2string(newv);
- return rename( old_narrow.c_str(), new_narrow.c_str() );
+ cstring new_narrow =wcs2string(newv);
+ return rename(old_narrow.c_str(), new_narrow.c_str());
}
int fish_wcstoi(const wchar_t *str, wchar_t ** endptr, int base)