From c9f4e91df88112ea4ff5f5d9a993c3655672ce93 Mon Sep 17 00:00:00 2001 From: Siteshwar Vashisht Date: Sat, 18 Feb 2012 22:41:22 +0530 Subject: [PATCH] Modified most functions in wutil.h/.cpp to use wcstring instead of wchar_t*, removing calls to c_str() while calling these functions in other files. --- autoload.cpp | 4 ++-- common.cpp | 4 ++-- common.h | 2 +- highlight.cpp | 8 ++++---- history.cpp | 10 +++++----- path.cpp | 4 ++-- wildcard.cpp | 6 +++--- wutil.cpp | 34 +++++++++++++++++----------------- wutil.h | 30 +++++++++++++++--------------- 9 files changed, 51 insertions(+), 51 deletions(-) diff --git a/autoload.cpp b/autoload.cpp index de8576323..dd3dafbf3 100644 --- a/autoload.cpp +++ b/autoload.cpp @@ -20,11 +20,11 @@ static const int kAutoloadStalenessInterval = 15; file_access_attempt_t access_file(const wcstring &path, int mode) { file_access_attempt_t result = {0}; struct stat statbuf; - if (wstat(path.c_str(), &statbuf)) { + if (wstat(path, &statbuf)) { result.error = errno; } else { result.mod_time = statbuf.st_mtime; - if (waccess(path.c_str(), mode)) { + if (waccess(path, mode)) { result.error = errno; } else { result.accessible = true; diff --git a/common.cpp b/common.cpp index 33bbc8ac2..6a1680e7c 100644 --- a/common.cpp +++ b/common.cpp @@ -1781,7 +1781,7 @@ bool string_prefixes_string(const wcstring &proposed_prefix, const wcstring &val return prefix_size <= value.size() && value.compare(0, prefix_size, proposed_prefix) == 0; } -int create_directory( const wchar_t *d ) +int create_directory( const wcstring &d ) { int ok = 0; struct stat buf; @@ -1805,7 +1805,7 @@ int create_directory( const wchar_t *d ) if( errno == ENOENT ) { wcstring dir = wdirname(d); - if( !create_directory( dir.c_str() ) ) + if( !create_directory( dir ) ) { if( !wmkdir( d, 0700 ) ) { diff --git a/common.h b/common.h index cdefd5543..3cd469b99 100644 --- a/common.h +++ b/common.h @@ -559,7 +559,7 @@ void tokenize_variable_array( const wcstring &val, wcstring_list_t &out); \return 0 if, at the time of function return the directory exists, -1 otherwise. */ -int create_directory( const wchar_t *d ); +int create_directory( const wcstring &d ); /** Print a short message about how to file a bug report to stderr diff --git a/highlight.cpp b/highlight.cpp index 059ccbfe1..1f34cd4cd 100644 --- a/highlight.cpp +++ b/highlight.cpp @@ -128,7 +128,7 @@ static bool is_potential_path( const wcstring &cpath ) must_be_dir = cleaned_path[cleaned_path.length()-1] == L'/'; if( must_be_dir ) { - dir = wopendir( cleaned_path.c_str() ); + dir = wopendir( cleaned_path ); res = !!dir; if( dir ) { @@ -144,12 +144,12 @@ static bool is_potential_path( const wcstring &cpath ) { res = true; } - else if( (dir = wopendir( dir_name.c_str() )) ) + else if( (dir = wopendir( dir_name)) ) { wcstring ent; while (wreaddir(dir, ent)) { - if( wcsncmp( ent.c_str(), base_name.c_str(), base_name.length() ) == 0 ) + if( ent == base_name ) { res = true; break; @@ -802,7 +802,7 @@ static void tokenize( const wchar_t * const buff, int * const color, const int p if( slash_idx != wcstring::npos ) { dir.resize(slash_idx); - if( wstat( dir.c_str(), &buff ) == -1 ) + if( wstat( dir, &buff ) == -1 ) { color[ tok_get_pos( &tok ) ] = HIGHLIGHT_ERROR; if( error ) diff --git a/history.cpp b/history.cpp index ad9abca27..b31d76941 100644 --- a/history.cpp +++ b/history.cpp @@ -409,7 +409,7 @@ void history_t::load_old_if_needed(void) if( ! filename.empty() ) { - if( ( fd = wopen( filename.c_str(), O_RDONLY ) ) > 0 ) + if( ( fd = wopen( filename, O_RDONLY ) ) > 0 ) { off_t len = lseek( fd, 0, SEEK_END ); if( len != (off_t)-1) @@ -595,7 +595,7 @@ void history_t::save_internal() if( ! tmp_name.empty() ) { FILE *out; - if( (out=wfopen( tmp_name.c_str(), "w" ) ) ) + if( (out=wfopen( tmp_name, "w" ) ) ) { /* Load old */ @@ -639,7 +639,7 @@ void history_t::save_internal() else { wcstring new_name = history_filename(name, wcstring()); - wrename(tmp_name.c_str(), new_name.c_str()); + wrename(tmp_name, new_name); } } } @@ -664,7 +664,7 @@ void history_t::clear(void) { old_item_offsets.clear(); wcstring filename = history_filename(name, L""); if (! filename.empty()) - wunlink(filename.c_str()); + wunlink(filename); this->clear_file_state(); } @@ -710,7 +710,7 @@ int file_detection_context_t::perform_file_detection(bool test_all) { if (path.at(0) != '/') { path.insert(0, working_directory); } - path_is_valid = (0 == waccess(path.c_str(), F_OK)); + path_is_valid = (0 == waccess(path, F_OK)); } diff --git a/path.cpp b/path.cpp index 8a2d26e1e..b866d6ef1 100644 --- a/path.cpp +++ b/path.cpp @@ -81,10 +81,10 @@ bool path_get_path_string(const wcstring &cmd_str, wcstring &output, const env_v if (path_len == 0) continue; append_path_component(new_cmd, cmd_str); - if( waccess( new_cmd.c_str(), X_OK )==0 ) + if( waccess( new_cmd, X_OK )==0 ) { struct stat buff; - if( wstat( new_cmd.c_str(), &buff )==-1 ) + if( wstat( new_cmd, &buff )==-1 ) { if( errno != EACCES ) { diff --git a/wildcard.cpp b/wildcard.cpp index 01bfbc4b2..49b61a9af 100644 --- a/wildcard.cpp +++ b/wildcard.cpp @@ -593,7 +593,7 @@ static void wildcard_completion_allocate( std::vector &list, non-symlinks by first doing an lstat, and if the file is not a link we copy the results over to the regular stat buffer. */ - if( ( lstat_res = lwstat( fullname.c_str(), &lbuf ) ) ) + if( ( lstat_res = lwstat( fullname, &lbuf ) ) ) { sz=-1; stat_res = lstat_res; @@ -603,7 +603,7 @@ static void wildcard_completion_allocate( std::vector &list, if( S_ISLNK(lbuf.st_mode)) { - if( ( stat_res = wstat( fullname.c_str(), &buf ) ) ) + if( ( stat_res = wstat( fullname, &buf ) ) ) { sz=-1; } @@ -863,7 +863,7 @@ static int wildcard_expand_internal( const wchar_t *wc, will be added in the next pass. */ struct stat buf; - if( !wstat( long_name.c_str(), &buf ) ) + if( !wstat( long_name, &buf ) ) { skip = S_ISDIR(buf.st_mode); } diff --git a/wutil.cpp b/wutil.cpp index c3dd80fc0..07fe9f1dc 100644 --- a/wutil.cpp +++ b/wutil.cpp @@ -118,25 +118,25 @@ wchar_t *wgetcwd( wchar_t *buff, size_t sz ) return ret; } -int wchdir( const wchar_t * dir ) +int wchdir( const wcstring &dir ) { cstring tmp = wcs2string(dir); return chdir( tmp.c_str() ); } -FILE *wfopen(const wchar_t *path, const char *mode) +FILE *wfopen(const wcstring &path, const char *mode) { cstring tmp = wcs2string(path); return fopen(tmp.c_str(), mode); } -FILE *wfreopen(const wchar_t *path, const char *mode, FILE *stream) +FILE *wfreopen(const wcstring &path, const char *mode, FILE *stream) { cstring tmp = wcs2string(path); return freopen(tmp.c_str(), mode, stream); } -int wopen(const wchar_t *pathname, int flags, ...) +int wopen(const wcstring &pathname, int flags, ...) { cstring tmp = wcs2string(pathname); int res=-1; @@ -154,25 +154,25 @@ int wopen(const wchar_t *pathname, int flags, ...) return res; } -int wcreat(const wchar_t *pathname, mode_t mode) +int wcreat(const wcstring &pathname, mode_t mode) { cstring tmp = wcs2string(pathname); return creat(tmp.c_str(), mode); } -DIR *wopendir(const wchar_t *name) +DIR *wopendir(const wcstring &name) { cstring tmp = wcs2string(name); return opendir(tmp.c_str()); } -int wstat(const wchar_t *file_name, struct stat *buf) +int wstat(const wcstring &file_name, struct stat *buf) { cstring tmp = wcs2string(file_name); return stat(tmp.c_str(), buf); } -int lwstat(const wchar_t *file_name, struct stat *buf) +int lwstat(const wcstring &file_name, struct stat *buf) { // fprintf(stderr, "%s\n", __PRETTY_FUNCTION__); cstring tmp = wcs2string(file_name); @@ -180,31 +180,31 @@ int lwstat(const wchar_t *file_name, struct stat *buf) } -int waccess(const wchar_t *file_name, int mode) +int waccess(const wcstring &file_name, int mode) { cstring tmp = wcs2string(file_name); return access(tmp.c_str(), mode); } -int wunlink(const wchar_t *file_name) +int wunlink(const wcstring &file_name) { cstring tmp = wcs2string(file_name); return unlink(tmp.c_str()); } -void wperror(const wchar_t *s) +void wperror(const wcstring &s) { int e = errno; - if( s != 0 ) + if( !s.empty() ) { - fwprintf( stderr, L"%ls: ", s ); + fwprintf( stderr, L"%ls: ", s.c_str() ); } fwprintf( stderr, L"%s\n", strerror( e ) ); } #ifdef HAVE_REALPATH_NULL -wchar_t *wrealpath(const wchar_t *pathname, wchar_t *resolved_path) +wchar_t *wrealpath(const wcstring &pathname, wchar_t *resolved_path) { cstring tmp = wcs2string(pathname); char *narrow_res = realpath( tmp.c_str(), 0 ); @@ -346,7 +346,7 @@ wcstring wgettext2(const wcstring &in) { return result; } -const wchar_t *wgetenv( const wchar_t *name ) +const wchar_t *wgetenv( const wcstring &name ) { ASSERT_IS_MAIN_THREAD(); cstring name_narrow = wcs2string(name); @@ -361,13 +361,13 @@ const wchar_t *wgetenv( const wchar_t *name ) } -int wmkdir( const wchar_t *name, int mode ) +int wmkdir( const wcstring &name, int mode ) { cstring name_narrow = wcs2string(name); return mkdir( name_narrow.c_str(), mode ); } -int wrename( const wchar_t *old, const wchar_t *newv ) +int wrename( const wcstring &old, const wcstring &newv ) { cstring old_narrow = wcs2string(old); cstring new_narrow =wcs2string(newv); diff --git a/wutil.h b/wutil.h index d3e241d65..5d81662ad 100644 --- a/wutil.h +++ b/wutil.h @@ -44,53 +44,53 @@ void wutil_destroy(); /** Wide character version of fopen(). */ -FILE *wfopen(const wchar_t *path, const char *mode); +FILE *wfopen(const wcstring &path, const char *mode); /** Wide character version of freopen(). */ -FILE *wfreopen(const wchar_t *path, const char *mode, FILE *stream); +FILE *wfreopen(const wcstring &path, const char *mode, FILE *stream); /** Wide character version of open(). */ -int wopen(const wchar_t *pathname, int flags, ...); +int wopen(const wcstring &pathname, int flags, ...); /** Wide character version of creat(). */ -int wcreat(const wchar_t *pathname, mode_t mode); +int wcreat(const wcstring &pathname, mode_t mode); /** Wide character version of opendir(). */ -DIR *wopendir(const wchar_t *name); +DIR *wopendir(const wcstring &name); /** Wide character version of stat(). */ -int wstat(const wchar_t *file_name, struct stat *buf); +int wstat(const wcstring &file_name, struct stat *buf); /** Wide character version of lstat(). */ -int lwstat(const wchar_t *file_name, struct stat *buf); +int lwstat(const wcstring &file_name, struct stat *buf); /** Wide character version of access(). */ -int waccess(const wchar_t *pathname, int mode); +int waccess(const wcstring &pathname, int mode); /** Wide character version of unlink(). */ -int wunlink(const wchar_t *pathname); +int wunlink(const wcstring &pathname); /** Wide character version of perror(). */ -void wperror(const wchar_t *s); +void wperror(const wcstring &s); /** Wide character version of getcwd(). @@ -100,7 +100,7 @@ wchar_t *wgetcwd( wchar_t *buff, size_t sz ); /** Wide character version of chdir() */ -int wchdir( const wchar_t * dir ); +int wchdir( const wcstring &dir ); /** Wide character version of realpath function. Just like the GNU @@ -108,7 +108,7 @@ int wchdir( const wchar_t * dir ); second argument, in which case the result will be allocated using malloc, and must be free'd by the user. */ -wchar_t *wrealpath(const wchar_t *pathname, wchar_t *resolved_path); +wchar_t *wrealpath(const wcstring &pathname, wchar_t *resolved_path); /** Wide character version of readdir() @@ -139,16 +139,16 @@ wcstring wgettext2(const wcstring &in); /** Wide character version of getenv */ -const wchar_t *wgetenv( const wchar_t *name ); +const wchar_t *wgetenv( const wcstring &name ); /** Wide character version of mkdir */ -int wmkdir( const wchar_t *dir, int mode ); +int wmkdir( const wcstring &dir, int mode ); /** Wide character version of rename */ -int wrename( const wchar_t *oldName, const wchar_t *newName ); +int wrename( const wcstring &oldName, const wcstring &newName ); #endif