26 #if defined (HAVE_CONFIG_H)
44 #if defined (OCTAVE_USE_WINDOWS_API)
62 #if defined (OCTAVE_USE_WINDOWS_API)
65 return _wsystem (wcmd_str.c_str ());
76 #if defined (OCTAVE_USE_WINDOWS_API)
77 wchar_t *tmp = _wgetcwd (
nullptr, 0);
80 (*current_liboctave_error_handler) (
"unable to find current directory");
82 std::wstring tmp_wstr (tmp);
97 (*current_liboctave_error_handler) (
"unable to find current directory");
111 #if defined (OCTAVE_USE_WINDOWS_API)
112 if (path.length () == 2 && path[1] ==
':')
125 #if defined (OCTAVE_USE_WINDOWS_API)
126 _WIN32_FIND_DATAW ffd;
128 std::string path_name (
dirname);
129 if (path_name.empty ())
132 if (path_name.back () ==
'\\' || path_name.back () ==
'/')
133 path_name.push_back (
'*');
135 path_name.append (R
"(\*)");
139 HANDLE hFind = FindFirstFileW (wpath_name.c_str (), &ffd);
140 if (INVALID_HANDLE_VALUE == hFind)
142 DWORD errCode = GetLastError ();
143 char *errorText =
nullptr;
144 FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM |
145 FORMAT_MESSAGE_ALLOCATE_BUFFER |
146 FORMAT_MESSAGE_IGNORE_INSERTS,
148 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
149 reinterpret_cast <char *
> (&errorText), 0,
nullptr);
150 if (errorText !=
nullptr)
152 msg = std::string (errorText);
153 LocalFree (errorText);
158 std::list<std::string> dirlist_str;
161 while (FindNextFileW (hFind, &ffd) != 0);
177 dirlist = dir.
read ();
185 #if defined (OCTAVE_USE_WINDOWS_API)
188 check_fseek_ftell_workaround_needed (
bool set_nonbuffered_mode)
213 if (tmpname.empty ())
215 (*current_liboctave_warning_handler)
216 (
"fseek/ftell bug check failed (tmp name creation)!");
224 (*current_liboctave_warning_handler)
225 (
"fseek/ftell bug check failed (opening tmp file for writing)!");
229 fprintf (
fptr,
"%s",
"foo\nbar\nbaz\n");
237 (*current_liboctave_warning_handler)
238 (
"fseek/ftell bug check failed (opening tmp file for reading)!");
248 if (set_nonbuffered_mode)
249 ::setvbuf (
fptr,
nullptr, _IONBF, 0);
253 int c = fgetc (
fptr);
257 (*current_liboctave_warning_handler)
258 (
"fseek/ftell bug check failed (skipping first line)!");
272 int c = fgetc (
fptr);
276 (*current_liboctave_warning_handler)
277 (
"fseek/ftell bug check failed (reading second line)!");
284 buf1[i++] =
static_cast<char> (c);
294 int c = fgetc (
fptr);
298 (*current_liboctave_warning_handler)
299 (
"fseek/ftell bug check failed (reading after repositioning)!");
306 buf2[i++] =
static_cast<char> (c);
310 return strcmp (buf1, buf2);
314 get_formatted_last_error ()
316 std::string msg =
"";
318 DWORD last_error = GetLastError ();
320 wchar_t *error_text =
nullptr;
321 FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
322 FORMAT_MESSAGE_ALLOCATE_BUFFER |
323 FORMAT_MESSAGE_IGNORE_INSERTS,
325 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
326 reinterpret_cast <wchar_t *
> (&error_text), 0,
nullptr);
328 if (error_text !=
nullptr)
331 LocalFree (error_text);
334 msg =
"Unknown error.";
345 #if defined (OCTAVE_USE_WINDOWS_API)
348 DWORD f_attr = GetFileAttributesW (w_fn.c_str ());
350 return ((f_attr != INVALID_FILE_ATTRIBUTES)
351 && (is_dir || ! (f_attr & FILE_ATTRIBUTE_DIRECTORY)));
356 return (fs && (fs.
is_reg () || (is_dir && fs.
is_dir ())));
362 file_exists (
const std::string& filename,
bool is_dir, std::string& msg)
366 #if defined (OCTAVE_USE_WINDOWS_API)
369 DWORD f_attr = GetFileAttributesW (w_fn.c_str ());
371 if (f_attr == INVALID_FILE_ATTRIBUTES)
372 msg = get_formatted_last_error ();
374 return ((f_attr != INVALID_FILE_ATTRIBUTES)
375 && (is_dir || ! (f_attr & FILE_ATTRIBUTE_DIRECTORY)));
383 return (fs && (fs.
is_reg () || (is_dir && fs.
is_dir ())));
392 #if defined (OCTAVE_USE_WINDOWS_API)
395 DWORD f_attr = GetFileAttributesW (w_dn.c_str ());
397 return ((f_attr != INVALID_FILE_ATTRIBUTES)
398 && (f_attr & FILE_ATTRIBUTE_DIRECTORY));
403 return (fs && fs.
is_dir ());
412 #if defined (OCTAVE_USE_WINDOWS_API)
415 DWORD f_attr = GetFileAttributesW (w_dn.c_str ());
417 if (f_attr == INVALID_FILE_ATTRIBUTES)
418 msg = get_formatted_last_error ();
420 return ((f_attr != INVALID_FILE_ATTRIBUTES)
421 && (f_attr & FILE_ATTRIBUTE_DIRECTORY));
429 return (fs && fs.
is_dir ());
437 same_file (
const std::string& file1,
const std::string& file2)
439 #if defined (OCTAVE_USE_WINDOWS_API)
448 const wchar_t *f1 = file1w.c_str ();
449 const wchar_t *f2 = file2w.c_str ();
451 bool f1_is_dir = GetFileAttributesW (f1) & FILE_ATTRIBUTE_DIRECTORY;
452 bool f2_is_dir = GetFileAttributesW (f2) & FILE_ATTRIBUTE_DIRECTORY;
457 DWORD share = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
460 = CreateFileW (f1, 0, share, 0, OPEN_EXISTING,
461 f1_is_dir ? FILE_FLAG_BACKUP_SEMANTICS : 0, 0);
463 if (hfile1 != INVALID_HANDLE_VALUE)
466 = CreateFileW (f2, 0, share, 0, OPEN_EXISTING,
467 f2_is_dir ? FILE_FLAG_BACKUP_SEMANTICS : 0, 0);
469 if (hfile2 != INVALID_HANDLE_VALUE)
471 BY_HANDLE_FILE_INFORMATION hfi1;
472 BY_HANDLE_FILE_INFORMATION hfi2;
474 if (GetFileInformationByHandle (hfile1, &hfi1)
475 && GetFileInformationByHandle (hfile2, &hfi2))
477 retval = (hfi1.dwVolumeSerialNumber == hfi2.dwVolumeSerialNumber
478 && hfi1.nFileIndexHigh == hfi2.nFileIndexHigh
479 && hfi1.nFileIndexLow == hfi2.nFileIndexLow
480 && hfi1.nFileSizeHigh == hfi2.nFileSizeHigh
481 && hfi1.nFileSizeLow == hfi2.nFileSizeLow
482 && hfi1.ftLastWriteTime.dwLowDateTime
483 == hfi2.ftLastWriteTime.dwLowDateTime
484 && hfi1.ftLastWriteTime.dwHighDateTime
485 == hfi2.ftLastWriteTime.dwHighDateTime);
488 CloseHandle (hfile2);
491 CloseHandle (hfile1);
500 sys::file_stat fs_file1 (file1);
501 sys::file_stat fs_file2 (file2);
503 return (fs_file1 && fs_file2
504 && fs_file1.ino () == fs_file2.ino ()
505 && fs_file1.dev () == fs_file2.dev ());
511 fopen (
const std::string& filename,
const std::string& mode)
513 #if defined (OCTAVE_USE_WINDOWS_API)
518 std::FILE *
fptr = _wfopen (wfilename.c_str (), wmode.c_str ());
520 static bool fseek_ftell_bug_workaround_needed =
false;
521 static bool fseek_ftell_bug_checked =
false;
523 if (! fseek_ftell_bug_checked && mode.find (
't') != std::string::npos)
559 if (check_fseek_ftell_workaround_needed (
false))
561 if (check_fseek_ftell_workaround_needed (
true))
562 (*current_liboctave_warning_handler)
563 (
"fseek/ftell may fail for files opened in text mode");
565 fseek_ftell_bug_workaround_needed =
true;
568 fseek_ftell_bug_checked =
true;
571 if (fseek_ftell_bug_workaround_needed
572 && mode.find (
't') != std::string::npos)
573 ::setvbuf (
fptr,
nullptr, _IONBF, 0);
578 return std::fopen (filename.c_str (), mode.c_str ());
583 fopen_tmp (
const std::string& name,
const std::string& mode)
585 #if defined (OCTAVE_USE_WINDOWS_API)
589 std::string tmp_mode = mode +
"D";
591 return std::fopen (name.c_str (), tmp_mode.c_str ());
607 fstream (
const std::string& filename,
const std::ios::openmode mode)
609 #if defined (OCTAVE_USE_WINDOWS_API)
621 ifstream (
const std::string& filename,
const std::ios::openmode mode)
623 #if defined (OCTAVE_USE_WINDOWS_API)
635 ofstream (
const std::string& filename,
const std::ios::openmode mode)
637 #if defined (OCTAVE_USE_WINDOWS_API)
651 std::string new_env = name +
"=" + value;
660 #if defined (OCTAVE_USE_WINDOWS_API)
663 int len = (new_wenv.length () + 1) *
sizeof (
wchar_t);
667 wcscpy (new_item, new_wenv.c_str());
669 if (_wputenv (new_item) < 0)
670 (*current_liboctave_error_handler)
671 (
"putenv (%s) failed", new_env.c_str());
673 int len = new_env.length () + 1;
677 std::strcpy (new_item, new_env.c_str());
680 (*current_liboctave_error_handler) (
"putenv (%s) failed", new_item);
687 #if defined (OCTAVE_USE_WINDOWS_API)
689 wchar_t *
env = _wgetenv (wname.c_str ());
692 char *
env = ::getenv (name.c_str ());
700 #if defined (OCTAVE_USE_WINDOWS_API)
704 return (SetEnvironmentVariableW (wname.c_str (),
nullptr) ? 0 : -1);
713 #if defined (OCTAVE_USE_WINDOWS_API)
715 wchar_t *
env = _wgetenv (wname.c_str ());
717 char *
env = ::getenv (name.c_str ());
726 static std::wstring_convert<std::codecvt_utf8<wchar_t>,
wchar_t>
729 std::wstring retval = L
"";
733 retval = wchar_conv.from_bytes (utf8_string);
735 catch (
const std::range_error& e)
749 static std::wstring_convert<std::codecvt_utf8<wchar_t>,
wchar_t>
752 std::string retval =
"";
756 retval = wchar_conv.to_bytes (wchar_string);
758 catch (
const std::range_error& e)
815 const bool allow_locale)
817 #if defined (OCTAVE_USE_WINDOWS_API)
827 std::string::const_iterator first_non_ASCII
828 = std::find_if (orig_file_name.begin (), orig_file_name.end (),
829 [](
char c) { return (c < 0 || c >= 128); });
831 if (first_non_ASCII == orig_file_name.end ())
832 return orig_file_name;
841 const uint8_t *name_u8 =
reinterpret_cast<const uint8_t *
>
842 (orig_file_name.c_str ());
843 std::size_t length = 0;
846 orig_file_name.length () + 1, &length);
849 std::string file_name_locale (name_locale, length);
851 return file_name_locale;
858 std::wstring w_orig_file_name_str =
u8_to_wstring (orig_file_name);
859 const wchar_t *w_orig_file_name = w_orig_file_name_str.c_str ();
862 wchar_t w_full_file_name[_MAX_PATH];
863 if (_wfullpath (w_full_file_name, w_orig_file_name, _MAX_PATH) ==
nullptr)
864 return orig_file_name;
866 std::wstring w_full_file_name_str = w_full_file_name;
870 long length = GetShortPathNameW (w_full_file_name,
nullptr, 0);
878 GetShortPathNameW (w_full_file_name, w_short_file_name, length);
880 std::wstring w_short_file_name_str
881 = std::wstring (w_short_file_name, length);
883 if (w_short_file_name_str.compare (0, length-1, w_full_file_name_str) != 0)
886 std::string short_file_name
889 = std::find_if (short_file_name.begin (),
890 short_file_name.end (),
891 [](
char c) { return (c < 0 || c >= 128); });
892 if (first_non_ASCII == short_file_name.end ())
893 return short_file_name;
900 std::wstring::iterator w_first_non_ASCII
901 = std::find_if (w_full_file_name_str.begin (), w_full_file_name_str.end (),
902 [](
wchar_t c) { return (c < 0 || c >= 128); });
903 std::wstring tmp_substr
904 = std::wstring (w_full_file_name_str.begin (), w_first_non_ASCII);
915 std::string oct_ascii_dir = par_dir +
".oct_ascii";
918 if (test_dir.empty ())
921 int status =
sys::mkdir (oct_ascii_dir, 0777, msg);
924 return orig_file_name;
927 SetFileAttributesA (oct_ascii_dir.c_str (), FILE_ATTRIBUTE_HIDDEN);
931 std::string filename_hash
939 if (! abs_filename_hash.empty ())
944 std::wstring w_filename_hash (filename_hash.begin (),
945 filename_hash.end ());
947 if (CreateHardLinkW (w_filename_hash.c_str (), w_orig_file_name,
nullptr))
948 return filename_hash;
952 octave_unused_parameter (allow_locale);
956 return orig_file_name;
959 OCTAVE_END_NAMESPACE(sys)
960 OCTAVE_END_NAMESPACE(
octave)
std::string error() const
std::string error() const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
std::string dir_sep_str()
std::string dir_sep_chars()
std::string dirname(const std::string &path)
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)
bool dir_exists(const std::string &dirname)
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)
bool file_exists(const std::string &filename, bool is_dir)
bool isenv_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)
bool same_file(const std::string &file1, const std::string &file2)
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)
std::string tilde_expand(const std::string &name)
std::string tempnam(const std::string &dir, const std::string &pfx)
int mkdir(const std::string &nm, mode_t md)
std::string canonicalize_file_name(const std::string &name)
int unlink(const std::string &name)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
bool strcmp(const T &str_a, const T &str_b)
Octave string utility functions.
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)
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)