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)!");
240 unwind_action act ([=] ()
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 fstream (
const std::string& filename,
const std::ios::openmode mode)
388#if defined (OCTAVE_USE_WINDOWS_API)
400 ifstream (
const std::string& filename,
const std::ios::openmode mode)
402#if defined (OCTAVE_USE_WINDOWS_API)
414 ofstream (
const std::string& filename,
const std::ios::openmode mode)
416#if defined (OCTAVE_USE_WINDOWS_API)
430 std::string new_env =
name +
"=" + value;
439#if defined (OCTAVE_USE_WINDOWS_API)
442 int len = (new_wenv.length () + 1) *
sizeof (
wchar_t);
446 wcscpy (new_item, new_wenv.c_str());
448 if (_wputenv (new_item) < 0)
449 (*current_liboctave_error_handler)
450 (
"putenv (%s) failed", new_env.c_str());
452 int len = new_env.length () + 1;
456 std::strcpy (new_item, new_env.c_str());
459 (*current_liboctave_error_handler) (
"putenv (%s) failed", new_item);
466#if defined (OCTAVE_USE_WINDOWS_API)
468 wchar_t *
env = _wgetenv (wname.c_str ());
471 char *
env = ::getenv (
name.c_str ());
479#if defined (OCTAVE_USE_WINDOWS_API)
483 return (SetEnvironmentVariableW (wname.c_str (),
nullptr) ? 0 : -1);
493 static std::wstring_convert<std::codecvt_utf8<wchar_t>,
wchar_t>
496 std::wstring retval = L
"";
500 retval = wchar_conv.from_bytes (utf8_string);
502 catch (
const std::range_error& e)
516 static std::wstring_convert<std::codecvt_utf8<wchar_t>,
wchar_t>
519 std::string retval =
"";
523 retval = wchar_conv.to_bytes (wchar_string);
525 catch (
const std::range_error& e)
582 const bool allow_locale)
584#if defined (OCTAVE_USE_WINDOWS_API)
594 std::string::const_iterator first_non_ASCII
595 = std::find_if (orig_file_name.begin (), orig_file_name.end (),
596 [](
char c) { return (c < 0 || c >= 128); });
598 if (first_non_ASCII == orig_file_name.end ())
599 return orig_file_name;
608 const uint8_t *name_u8 =
reinterpret_cast<const uint8_t *
>
609 (orig_file_name.c_str ());
610 std::size_t length = 0;
613 orig_file_name.length () + 1, &length);
616 std::string file_name_locale (name_locale, length);
618 return file_name_locale;
625 std::wstring w_orig_file_name_str =
u8_to_wstring (orig_file_name);
626 const wchar_t *w_orig_file_name = w_orig_file_name_str.c_str ();
629 wchar_t w_full_file_name[_MAX_PATH];
630 if (_wfullpath (w_full_file_name, w_orig_file_name, _MAX_PATH) ==
nullptr)
631 return orig_file_name;
633 std::wstring w_full_file_name_str = w_full_file_name;
637 long length = GetShortPathNameW (w_full_file_name,
nullptr, 0);
645 GetShortPathNameW (w_full_file_name, w_short_file_name, length);
647 std::wstring w_short_file_name_str
648 = std::wstring (w_short_file_name, length);
650 if (w_short_file_name_str.compare (0, length-1, w_full_file_name_str) != 0)
653 std::string short_file_name
656 = std::find_if (short_file_name.begin (),
657 short_file_name.end (),
658 [](
char c) { return (c < 0 || c >= 128); });
659 if (first_non_ASCII == short_file_name.end ())
660 return short_file_name;
667 std::wstring::iterator w_first_non_ASCII
668 = std::find_if (w_full_file_name_str.begin (), w_full_file_name_str.end (),
669 [](
wchar_t c) { return (c < 0 || c >= 128); });
670 std::wstring tmp_substr
671 = std::wstring (w_full_file_name_str.begin (), w_first_non_ASCII);
682 std::string oct_ascii_dir = par_dir +
".oct_ascii";
685 if (test_dir.empty ())
688 int status =
sys::mkdir (oct_ascii_dir, 0777, msg);
691 return orig_file_name;
694 SetFileAttributesA (oct_ascii_dir.c_str (), FILE_ATTRIBUTE_HIDDEN);
698 std::string filename_hash
704 if (! abs_filename_hash.empty ())
707 wchar_t w_filename_hash[filename_hash.length ()+1] = {0};
709 for (std::size_t i=0; i < filename_hash.length (); i++)
710 w_filename_hash[i] = filename_hash.at (i);
712 if (CreateHardLinkW (w_filename_hash, w_orig_file_name,
nullptr))
713 return filename_hash;
717 octave_unused_parameter (allow_locale);
721 return orig_file_name;
std::string error(void) const
int octave_fseeko_wrapper(FILE *fp, off_t offset, int whence)
off_t octave_ftello_wrapper(FILE *fp)
const char * octave_locale_charset_wrapper(void)
std::string hash(hash_fptr hash_fcn, const std::string &str, int result_buf_len)
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
OCTAVE_API bool strcmp(const T &str_a, const T &str_b)
True if strings are the same.
std::string dirname(const std::string &path)
std::string tilde_expand(const std::string &name)
std::string dir_sep_chars(void)
std::string dir_sep_str(void)
std::fstream fstream(const std::string &filename, const std::ios::openmode mode)
std::string tempnam(const std::string &dir, const std::string &pfx)
std::ifstream ifstream(const std::string &filename, const std::ios::openmode mode)
std::string canonicalize_file_name(const std::string &name)
std::string u8_from_wstring(const std::wstring &wchar_string)
int unsetenv_wrapper(const std::string &name)
std::ofstream ofstream(const std::string &filename, const std::ios::openmode mode)
int mkdir(const std::string &nm, mode_t md)
std::wstring u8_to_wstring(const std::string &utf8_string)
std::string getenv_wrapper(const std::string &name)
std::string get_ASCII_filename(const std::string &orig_file_name, const bool allow_locale)
int unlink(const std::string &name)
void putenv_wrapper(const std::string &name, const std::string &value)
bool get_dirlist(const std::string &dirname, string_vector &dirlist, std::string &msg)
std::FILE * fopen(const std::string &filename, const std::string &mode)
int system(const std::string &cmd_str)
int chdir(const std::string &path_arg)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
int octave_putenv_wrapper(char *str)
char * octave_u8_conv_to_encoding_strict(const char *tocode, const uint8_t *src, size_t srclen, size_t *lengthp)
char * octave_getcwd_wrapper(char *nm, size_t len)
int octave_chdir_wrapper(const char *nm)
int octave_unsetenv_wrapper(const char *name)