26 #if defined (HAVE_CONFIG_H)
39 #if defined (OCTAVE_USE_WINDOWS_API)
57 #if defined (OCTAVE_USE_WINDOWS_API)
58 wchar_t *tmp = _wgetcwd (
nullptr, 0);
61 (*current_liboctave_error_handler) (
"unable to find current directory");
63 std::wstring tmp_wstr (tmp);
78 (*current_liboctave_error_handler) (
"unable to find current directory");
88 chdir (
const std::string& path_arg)
92 #if defined (OCTAVE_USE_WINDOWS_API)
93 if (
path.length () == 2 &&
path[1] ==
':')
106 #if defined (OCTAVE_USE_WINDOWS_API)
107 _WIN32_FIND_DATAW ffd;
109 std::string path_name (
dirname);
110 if (path_name.empty ())
113 if (path_name.back () ==
'\\' || path_name.back () ==
'/')
114 path_name.push_back (
'*');
116 path_name.append (R
"(\*)");
120 HANDLE hFind = FindFirstFileW (wpath_name.c_str (), &ffd);
121 if (INVALID_HANDLE_VALUE == hFind)
123 DWORD errCode = GetLastError ();
124 char *errorText =
nullptr;
125 FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM |
126 FORMAT_MESSAGE_ALLOCATE_BUFFER |
127 FORMAT_MESSAGE_IGNORE_INSERTS,
129 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
130 reinterpret_cast <char *
> (&errorText), 0,
nullptr);
131 if (errorText !=
nullptr)
133 msg = std::string (errorText);
134 LocalFree (errorText);
139 std::list<std::string> dirlist_str;
142 while (FindNextFileW (hFind, &ffd) != 0);
158 dirlist = dir.
read ();
166 #if defined (OCTAVE_USE_WINDOWS_API)
168 static bool check_fseek_ftell_workaround_needed (
bool set_nonbuffered_mode)
193 if (tmpname.empty ())
195 (*current_liboctave_warning_handler)
196 (
"fseek/ftell bug check failed (tmp name creation)!");
204 (*current_liboctave_warning_handler)
205 (
"fseek/ftell bug check failed (opening tmp file for writing)!");
209 fprintf (
fptr,
"%s",
"foo\nbar\nbaz\n");
217 (*current_liboctave_warning_handler)
218 (
"fseek/ftell bug check failed (opening tmp file for reading)!");
222 unwind_action act ([
fptr, tmpname] () {
227 if (set_nonbuffered_mode)
228 ::setvbuf (
fptr,
nullptr, _IONBF, 0);
232 int c = fgetc (
fptr);
236 (*current_liboctave_warning_handler)
237 (
"fseek/ftell bug check failed (skipping first line)!");
251 int c = fgetc (
fptr);
255 (*current_liboctave_warning_handler)
256 (
"fseek/ftell bug check failed (reading second line)!");
263 buf1[i++] =
static_cast<char> (c);
273 int c = fgetc (
fptr);
277 (*current_liboctave_warning_handler)
278 (
"fseek/ftell bug check failed (reading after repositioning)!");
285 buf2[i++] =
static_cast<char> (c);
289 return strcmp (buf1, buf2);
295 fopen (
const std::string& filename,
const std::string& mode)
297 #if defined (OCTAVE_USE_WINDOWS_API)
302 std::FILE *
fptr = _wfopen (wfilename.c_str (), wmode.c_str ());
304 static bool fseek_ftell_bug_workaround_needed =
false;
305 static bool fseek_ftell_bug_checked =
false;
307 if (! fseek_ftell_bug_checked && mode.find (
't') != std::string::npos)
343 if (check_fseek_ftell_workaround_needed (
false))
345 if (check_fseek_ftell_workaround_needed (
true))
346 (*current_liboctave_warning_handler)
347 (
"fseek/ftell may fail for files opened in text mode");
349 fseek_ftell_bug_workaround_needed =
true;
352 fseek_ftell_bug_checked =
true;
355 if (fseek_ftell_bug_workaround_needed
356 && mode.find (
't') != std::string::npos)
357 ::setvbuf (
fptr,
nullptr, _IONBF, 0);
362 return std::fopen (filename.c_str (), mode.c_str ());
367 fstream (
const std::string& filename,
const std::ios::openmode mode)
369 #if defined (OCTAVE_USE_WINDOWS_API)
381 ifstream (
const std::string& filename,
const std::ios::openmode mode)
383 #if defined (OCTAVE_USE_WINDOWS_API)
395 ofstream (
const std::string& filename,
const std::ios::openmode mode)
397 #if defined (OCTAVE_USE_WINDOWS_API)
415 int new_len =
name.length () + value.length () + 2;
421 char *new_item =
static_cast<char *
> (
std::malloc (new_len));
424 sprintf (new_item,
"%s=%s",
name.c_str (), value.c_str ());
429 #if defined (OCTAVE_USE_WINDOWS_API)
433 if (_wputenv (wnew_item) < 0)
434 (*current_liboctave_error_handler) (
"putenv (%s) failed", new_item);
444 #if defined (OCTAVE_USE_WINDOWS_API)
446 wchar_t *
env = _wgetenv (wname.c_str ());
449 char *
env = ::getenv (
name.c_str ());
457 #if defined (OCTAVE_USE_WINDOWS_API)
461 return (SetEnvironmentVariableW (wname.c_str (),
nullptr) ? 0 : -1);
470 size_t srclen = utf8_string.length ();
471 const uint8_t *src =
reinterpret_cast<const uint8_t *
>
472 (utf8_string.c_str ());
475 wchar_t *wchar =
reinterpret_cast<wchar_t *
>
479 std::wstring
retval = L
"";
480 if (wchar !=
nullptr)
482 retval = std::wstring (wchar,
length /
sizeof (
wchar_t));
483 free (
static_cast<void *
> (wchar));
492 size_t srclen = wchar_string.length () *
sizeof (wchar_t);
493 const char *src =
reinterpret_cast<const char *
> (wchar_string.c_str ());
496 char *mbchar =
reinterpret_cast<char *
>
501 if (mbchar !=
nullptr)
504 free (
static_cast<void *
> (mbchar));
553 #if defined (OCTAVE_USE_WINDOWS_API)
563 std::string::const_iterator first_non_ASCII
564 = std::find_if (orig_file_name.begin (), orig_file_name.end (),
565 [](
char c) { return (c < 0 || c >= 128); });
567 if (first_non_ASCII == orig_file_name.end ())
568 return orig_file_name;
573 std::wstring w_orig_file_name_str =
u8_to_wstring (orig_file_name);
574 const wchar_t *w_orig_file_name = w_orig_file_name_str.c_str ();
577 wchar_t w_full_file_name[_MAX_PATH];
578 if (_wfullpath (w_full_file_name, w_orig_file_name, _MAX_PATH) ==
nullptr)
579 return orig_file_name;
581 std::wstring w_full_file_name_str = w_full_file_name;
585 long length = GetShortPathNameW (w_full_file_name,
nullptr, 0);
592 wchar_t *w_short_file_name =
new wchar_t[
length];
593 GetShortPathNameW (w_full_file_name, w_short_file_name,
length);
595 std::wstring w_short_file_name_str
596 = std::wstring (w_short_file_name,
length);
599 if (w_short_file_name_str.compare (0,
length-1, w_full_file_name_str) != 0)
600 return short_file_name;
606 std::wstring::iterator w_first_non_ASCII
607 = std::find_if (w_full_file_name_str.begin (), w_full_file_name_str.end (),
608 [](
wchar_t c) { return (c < 0 || c >= 128); });
609 std::wstring tmp_substr
610 = std::wstring (w_full_file_name_str.begin (), w_first_non_ASCII);
621 std::string oct_ascii_dir = par_dir +
".oct_ascii";
624 if (test_dir.empty ())
627 int status =
sys::mkdir (oct_ascii_dir, 0777, msg);
630 return orig_file_name;
633 SetFileAttributesA (oct_ascii_dir.c_str (), FILE_ATTRIBUTE_HIDDEN);
637 std::string filename_hash
643 if (! abs_filename_hash.empty ())
646 wchar_t w_filename_hash[filename_hash.length ()+1] = {0};
648 for (
size_t i=0; i < filename_hash.length (); i++)
649 w_filename_hash[i] = filename_hash.at (i);
651 if (CreateHardLinkW (w_filename_hash, w_orig_file_name,
nullptr))
652 return filename_hash;
656 return orig_file_name;
void add_fcn(void(*fcn)(Params...), Args &&... args)
std::string error(void) const
int octave_fseeko_wrapper(FILE *fp, off_t offset, int whence)
off_t octave_ftello_wrapper(FILE *fp)
OCTAVE_NORETURN liboctave_error_handler current_liboctave_error_handler
std::string hash(hash_fptr hash_fcn, const std::string &str, int result_buf_len)
FloatComplex(* fptr)(const FloatComplex &, float, int, octave_idx_type &)
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 dir_sep_str(void)
std::string tilde_expand(const std::string &name)
std::string dir_sep_chars(void)
std::string get_ASCII_filename(const std::string &orig_file_name)
std::string tempnam(const std::string &dir, const std::string &pfx)
std::string canonicalize_file_name(const std::string &name)
std::FILE * fopen(const std::string &filename, const std::string &mode)
int mkdir(const std::string &nm, mode_t md)
std::string u8_from_wstring(const std::wstring &wchar_string)
int unsetenv_wrapper(const std::string &name)
std::ifstream ifstream(const std::string &filename, const std::ios::openmode mode)
int chdir(const std::string &path_arg)
void putenv_wrapper(const std::string &name, const std::string &value)
std::fstream fstream(const std::string &filename, const std::ios::openmode mode)
int unlink(const std::string &name)
std::string getenv_wrapper(const std::string &name)
std::ofstream ofstream(const std::string &filename, const std::ios::openmode mode)
bool get_dirlist(const std::string &dirname, string_vector &dirlist, std::string &msg)
std::wstring u8_to_wstring(const std::string &utf8_string)
octave_value::octave_value(const Array< char > &chm, char type) return retval
int octave_putenv_wrapper(char *str)
uint8_t * octave_u8_conv_from_encoding(const char *fromcode, const char *src, size_t srclen, size_t *lengthp)
char * octave_u8_conv_to_encoding(const char *tocode, const uint8_t *src, size_t srclen, size_t *lengthp)
wchar_t * u8_to_wchar(const char *u8)
int octave_chdir_wrapper(const char *nm)
char * octave_getcwd_wrapper(char *nm, size_t len)
int octave_unsetenv_wrapper(const char *name)