26 #if defined (HAVE_CONFIG_H)
43 #if defined (OCTAVE_USE_WINDOWS_API)
61 #if defined (OCTAVE_USE_WINDOWS_API)
64 return _wsystem (wcmd_str.c_str ());
75 #if defined (OCTAVE_USE_WINDOWS_API)
76 wchar_t *tmp = _wgetcwd (
nullptr, 0);
79 (*current_liboctave_error_handler) (
"unable to find current directory");
81 std::wstring tmp_wstr (tmp);
96 (*current_liboctave_error_handler) (
"unable to find current directory");
110 #if defined (OCTAVE_USE_WINDOWS_API)
111 if (path.length () == 2 && path[1] ==
':')
124 #if defined (OCTAVE_USE_WINDOWS_API)
125 _WIN32_FIND_DATAW ffd;
127 std::string path_name (
dirname);
128 if (path_name.empty ())
131 if (path_name.back () ==
'\\' || path_name.back () ==
'/')
132 path_name.push_back (
'*');
134 path_name.append (R
"(\*)");
138 HANDLE hFind = FindFirstFileW (wpath_name.c_str (), &ffd);
139 if (INVALID_HANDLE_VALUE == hFind)
141 DWORD errCode = GetLastError ();
142 char *errorText =
nullptr;
143 FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM |
144 FORMAT_MESSAGE_ALLOCATE_BUFFER |
145 FORMAT_MESSAGE_IGNORE_INSERTS,
147 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
148 reinterpret_cast <char *
> (&errorText), 0,
nullptr);
149 if (errorText !=
nullptr)
151 msg = std::string (errorText);
152 LocalFree (errorText);
157 std::list<std::string> dirlist_str;
160 while (FindNextFileW (hFind, &ffd) != 0);
176 dirlist = dir.
read ();
184 #if defined (OCTAVE_USE_WINDOWS_API)
186 static bool check_fseek_ftell_workaround_needed (
bool set_nonbuffered_mode)
211 if (tmpname.empty ())
213 (*current_liboctave_warning_handler)
214 (
"fseek/ftell bug check failed (tmp name creation)!");
222 (*current_liboctave_warning_handler)
223 (
"fseek/ftell bug check failed (opening tmp file for writing)!");
227 fprintf (
fptr,
"%s",
"foo\nbar\nbaz\n");
235 (*current_liboctave_warning_handler)
236 (
"fseek/ftell bug check failed (opening tmp file for reading)!");
246 if (set_nonbuffered_mode)
247 ::setvbuf (
fptr,
nullptr, _IONBF, 0);
251 int c = fgetc (
fptr);
255 (*current_liboctave_warning_handler)
256 (
"fseek/ftell bug check failed (skipping first line)!");
270 int c = fgetc (
fptr);
274 (*current_liboctave_warning_handler)
275 (
"fseek/ftell bug check failed (reading second line)!");
282 buf1[i++] =
static_cast<char> (c);
292 int c = fgetc (
fptr);
296 (*current_liboctave_warning_handler)
297 (
"fseek/ftell bug check failed (reading after repositioning)!");
304 buf2[i++] =
static_cast<char> (c);
308 return strcmp (buf1, buf2);
314 fopen (
const std::string& filename,
const std::string& mode)
316 #if defined (OCTAVE_USE_WINDOWS_API)
321 std::FILE *
fptr = _wfopen (wfilename.c_str (), wmode.c_str ());
323 static bool fseek_ftell_bug_workaround_needed =
false;
324 static bool fseek_ftell_bug_checked =
false;
326 if (! fseek_ftell_bug_checked && mode.find (
't') != std::string::npos)
362 if (check_fseek_ftell_workaround_needed (
false))
364 if (check_fseek_ftell_workaround_needed (
true))
365 (*current_liboctave_warning_handler)
366 (
"fseek/ftell may fail for files opened in text mode");
368 fseek_ftell_bug_workaround_needed =
true;
371 fseek_ftell_bug_checked =
true;
374 if (fseek_ftell_bug_workaround_needed
375 && mode.find (
't') != std::string::npos)
376 ::setvbuf (
fptr,
nullptr, _IONBF, 0);
381 return std::fopen (filename.c_str (), mode.c_str ());
386 fopen_tmp (
const std::string& name,
const std::string& mode)
388 #if defined (OCTAVE_USE_WINDOWS_API)
392 std::string tmp_mode = mode +
"D";
394 return std::fopen (name.c_str (), tmp_mode.c_str ());
410 fstream (
const std::string& filename,
const std::ios::openmode mode)
412 #if defined (OCTAVE_USE_WINDOWS_API)
424 ifstream (
const std::string& filename,
const std::ios::openmode mode)
426 #if defined (OCTAVE_USE_WINDOWS_API)
438 ofstream (
const std::string& filename,
const std::ios::openmode mode)
440 #if defined (OCTAVE_USE_WINDOWS_API)
454 std::string new_env = name +
"=" + value;
463 #if defined (OCTAVE_USE_WINDOWS_API)
466 int len = (new_wenv.length () + 1) *
sizeof (
wchar_t);
470 wcscpy (new_item, new_wenv.c_str());
472 if (_wputenv (new_item) < 0)
473 (*current_liboctave_error_handler)
474 (
"putenv (%s) failed", new_env.c_str());
476 int len = new_env.length () + 1;
480 std::strcpy (new_item, new_env.c_str());
483 (*current_liboctave_error_handler) (
"putenv (%s) failed", new_item);
490 #if defined (OCTAVE_USE_WINDOWS_API)
492 wchar_t *
env = _wgetenv (wname.c_str ());
495 char *
env = ::getenv (name.c_str ());
503 #if defined (OCTAVE_USE_WINDOWS_API)
507 return (SetEnvironmentVariableW (wname.c_str (),
nullptr) ? 0 : -1);
517 static std::wstring_convert<std::codecvt_utf8<wchar_t>,
wchar_t>
520 std::wstring retval = L
"";
524 retval = wchar_conv.from_bytes (utf8_string);
526 catch (
const std::range_error& e)
540 static std::wstring_convert<std::codecvt_utf8<wchar_t>,
wchar_t>
543 std::string retval =
"";
547 retval = wchar_conv.to_bytes (wchar_string);
549 catch (
const std::range_error& e)
606 const bool allow_locale)
608 #if defined (OCTAVE_USE_WINDOWS_API)
618 std::string::const_iterator first_non_ASCII
619 = std::find_if (orig_file_name.begin (), orig_file_name.end (),
620 [](
char c) { return (c < 0 || c >= 128); });
622 if (first_non_ASCII == orig_file_name.end ())
623 return orig_file_name;
632 const uint8_t *name_u8 =
reinterpret_cast<const uint8_t *
>
633 (orig_file_name.c_str ());
634 std::size_t length = 0;
637 orig_file_name.length () + 1, &length);
640 std::string file_name_locale (name_locale, length);
642 return file_name_locale;
649 std::wstring w_orig_file_name_str =
u8_to_wstring (orig_file_name);
650 const wchar_t *w_orig_file_name = w_orig_file_name_str.c_str ();
653 wchar_t w_full_file_name[_MAX_PATH];
654 if (_wfullpath (w_full_file_name, w_orig_file_name, _MAX_PATH) ==
nullptr)
655 return orig_file_name;
657 std::wstring w_full_file_name_str = w_full_file_name;
661 long length = GetShortPathNameW (w_full_file_name,
nullptr, 0);
669 GetShortPathNameW (w_full_file_name, w_short_file_name, length);
671 std::wstring w_short_file_name_str
672 = std::wstring (w_short_file_name, length);
674 if (w_short_file_name_str.compare (0, length-1, w_full_file_name_str) != 0)
677 std::string short_file_name
680 = std::find_if (short_file_name.begin (),
681 short_file_name.end (),
682 [](
char c) { return (c < 0 || c >= 128); });
683 if (first_non_ASCII == short_file_name.end ())
684 return short_file_name;
691 std::wstring::iterator w_first_non_ASCII
692 = std::find_if (w_full_file_name_str.begin (), w_full_file_name_str.end (),
693 [](
wchar_t c) { return (c < 0 || c >= 128); });
694 std::wstring tmp_substr
695 = std::wstring (w_full_file_name_str.begin (), w_first_non_ASCII);
706 std::string oct_ascii_dir = par_dir +
".oct_ascii";
709 if (test_dir.empty ())
712 int status =
sys::mkdir (oct_ascii_dir, 0777, msg);
715 return orig_file_name;
718 SetFileAttributesA (oct_ascii_dir.c_str (), FILE_ATTRIBUTE_HIDDEN);
722 std::string filename_hash
730 if (! abs_filename_hash.empty ())
735 std::wstring w_filename_hash (filename_hash.begin (),
736 filename_hash.end ());
738 if (CreateHardLinkW (w_filename_hash.c_str (), w_orig_file_name,
nullptr))
739 return filename_hash;
743 octave_unused_parameter (allow_locale);
747 return orig_file_name;
std::string error(void) const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
int unlink(const std::string &name)
std::string canonicalize_file_name(const std::string &name)
std::string tempnam(const std::string &dir, const std::string &pfx)
std::string dirname(const std::string &path)
std::string dir_sep_str(void)
int mkdir(const std::string &nm, mode_t md)
std::string tilde_expand(const std::string &name)
int octave_fseeko_wrapper(FILE *fp, off_t offset, int whence)
off_t octave_ftello_wrapper(FILE *fp)
std::string hash(hash_fptr hash_fcn, const std::string &str, int result_buf_len)
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
int system(const std::string &cmd_str)
std::string u8_from_wstring(const std::wstring &wchar_string)
std::string get_ASCII_filename(const std::string &orig_file_name, const bool allow_locale)
std::fstream fstream(const std::string &filename, const std::ios::openmode mode)
std::FILE * fopen(const std::string &filename, const std::string &mode)
std::string getenv_wrapper(const std::string &name)
std::ofstream ofstream(const std::string &filename, const std::ios::openmode mode)
int chdir(const std::string &path_arg)
std::ifstream ifstream(const std::string &filename, const std::ios::openmode mode)
std::wstring u8_to_wstring(const std::string &utf8_string)
void putenv_wrapper(const std::string &name, const std::string &value)
bool get_dirlist(const std::string &dirname, string_vector &dirlist, std::string &msg)
int unsetenv_wrapper(const std::string &name)
std::FILE * fopen_tmp(const std::string &name, const std::string &mode)
const char * octave_locale_charset_wrapper(void)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
OCTAVE_API bool strcmp(const T &str_a, const T &str_b)
Octave string utility functions.
int octave_putenv_wrapper(char *str)
static std::string dir_sep_chars
char * octave_u8_conv_to_encoding_strict(const char *tocode, const uint8_t *src, size_t srclen, size_t *lengthp)
int octave_unlink_wrapper(const char *nm)
int octave_chdir_wrapper(const char *nm)
char * octave_getcwd_wrapper(char *nm, size_t len)
int octave_unsetenv_wrapper(const char *name)