moo_normalize_file_path()

master
Yevgen Muntyan 2006-08-01 20:18:35 -05:00
parent 14db61af5e
commit 71d64d8245
4 changed files with 138 additions and 53 deletions

View File

@ -3,9 +3,6 @@
<make>
<n_jobs>3</n_jobs>
</make>
<commands>
<compile>cd $(top_builddir) &amp;&amp; $(make) $(base).o</compile>
</commands>
<configurations>
<debug>
<run>

View File

@ -26,6 +26,7 @@
#include "mooutils/mooutils-misc.h"
#include "mooutils/mooutils-gobject.h"
#include "mooutils/moofilewatch.h"
#include "mooutils/mooutils-fs.h"
#include <string.h>
@ -57,7 +58,7 @@ static WindowInfo *window_list_find (MooEditor *editor,
MooEditWindow *win);
static WindowInfo *window_list_find_doc (MooEditor *editor,
MooEdit *edit);
static WindowInfo *window_list_find_filename (MooEditor *editor,
static WindowInfo *window_list_find_file (MooEditor *editor,
const char *filename,
MooEdit **edit);
@ -821,8 +822,8 @@ static int
filename_and_doc_cmp (WindowInfo *w, gpointer user_data)
{
struct {
const char *filename;
MooEdit *edit;
char *filename;
MooEdit *edit;
} *data = user_data;
g_return_val_if_fail (w != NULL, TRUE);
@ -832,22 +833,30 @@ filename_and_doc_cmp (WindowInfo *w, gpointer user_data)
}
static WindowInfo*
window_list_find_filename (MooEditor *editor,
const char *filename,
MooEdit **edit)
window_list_find_file (MooEditor *editor,
const char *filename,
MooEdit **edit)
{
GSList *link;
struct {
const char *filename;
MooEdit *edit;
} data = {filename, NULL};
char *filename;
MooEdit *edit;
} data;
GSList *l = g_slist_find_custom (editor->priv->windows, &data,
(GCompareFunc) filename_and_doc_cmp);
if (l)
data.filename = moo_normalize_file_path (filename);
data.edit = NULL;
link = g_slist_find_custom (editor->priv->windows, &data,
(GCompareFunc) filename_and_doc_cmp);
g_free (data.filename);
if (link)
{
g_assert (data.edit != NULL);
if (edit) *edit = data.edit;
return l->data;
if (edit)
*edit = data.edit;
return link->data;
}
if (window_info_find (editor->priv->windowless, filename))
@ -1035,23 +1044,6 @@ moo_editor_new_doc (MooEditor *editor,
}
static char *
filename_make_absolute (const char *name)
{
char *abs_name, *dir;
if (g_path_is_absolute (name))
return g_strdup (name);
/* XXX normalize it */
dir = g_get_current_dir ();
abs_name = g_build_filename (dir, name, NULL);
g_free (dir);
return abs_name;
}
gboolean
moo_editor_open (MooEditor *editor,
MooEditWindow *window,
@ -1096,9 +1088,9 @@ moo_editor_open (MooEditor *editor,
MooEdit *doc = NULL;
char *filename;
filename = filename_make_absolute (info->filename);
filename = moo_normalize_file_path (info->filename);
if (window_list_find_filename (editor, filename, &bring_to_front))
if (window_list_find_file (editor, filename, &bring_to_front))
{
moo_history_list_add_filename (editor->priv->history, filename);
g_free (filename);
@ -1663,7 +1655,8 @@ moo_editor_open_file_line (MooEditor *editor,
int line,
MooEditWindow *window)
{
MooEdit *doc;
MooEdit *doc = NULL;
char *freeme = NULL;
g_return_val_if_fail (MOO_IS_EDITOR (editor), NULL);
g_return_val_if_fail (filename != NULL, NULL);
@ -1679,6 +1672,12 @@ moo_editor_open_file_line (MooEditor *editor,
return doc;
}
freeme = moo_normalize_file_path (filename);
filename = freeme;
if (!g_file_test (filename, G_FILE_TEST_EXISTS))
goto out;
doc = moo_editor_open_file (editor, window, NULL, filename, NULL);
g_return_val_if_fail (doc != NULL, NULL);
@ -1687,6 +1686,9 @@ moo_editor_open_file_line (MooEditor *editor,
if (line >= 0)
moo_text_view_move_cursor (MOO_TEXT_VIEW (doc), line, 0, FALSE, TRUE);
gtk_widget_grab_focus (GTK_WIDGET (doc));
out:
g_free (freeme);
return doc;
}
@ -1712,7 +1714,7 @@ moo_editor_new_file (MooEditor *editor,
return moo_editor_open_file (editor, window, parent,
filename, encoding);
freeme = filename_make_absolute (filename);
freeme = moo_normalize_file_path (filename);
filename = freeme;
if (!window)
@ -2021,17 +2023,12 @@ moo_editor_get_doc (MooEditor *editor,
const char *filename)
{
MooEdit *doc = NULL;
char *abs;
g_return_val_if_fail (MOO_IS_EDITOR (editor), NULL);
g_return_val_if_fail (filename != NULL, NULL);
abs = filename_make_absolute (filename);
g_return_val_if_fail (abs != NULL, NULL);
window_list_find_file (editor, filename, &doc);
window_list_find_filename (editor, abs, &doc);
g_free (abs);
return doc;
}

View File

@ -148,6 +148,8 @@ rm_r (const char *path,
{
char *file_path = g_build_filename (path, file, NULL);
errno = 0;
if (m_remove (file_path))
{
int err = errno;
@ -174,14 +176,19 @@ rm_r (const char *path,
g_dir_close (dir);
if (success && m_remove (path))
if (success)
{
int err = errno;
success = FALSE;
g_set_error (error, MOO_FILE_ERROR,
moo_file_error_from_errno (err),
"Could not remove '%s': %s", path,
g_strerror (err));
errno = 0;
if (m_remove (path) != 0)
{
int err = errno;
success = FALSE;
g_set_error (error, MOO_FILE_ERROR,
moo_file_error_from_errno (err),
"Could not remove '%s': %s", path,
g_strerror (err));
}
}
return success;
@ -198,7 +205,9 @@ moo_rmdir (const char *path,
if (!recursive)
{
if (m_rmdir (path))
errno = 0;
if (m_rmdir (path) != 0)
{
int err = errno;
char *path_utf8 = g_filename_to_utf8 (path, -1, NULL, NULL, NULL);
@ -233,7 +242,9 @@ moo_mkdir (const char *path,
g_return_val_if_fail (path != NULL, FALSE);
if (stat (path, &buf) == -1 && errno != ENOENT)
errno = 0;
if (stat (path, &buf) != 0 && errno != ENOENT)
{
int err_code = errno;
utf8_path = g_filename_to_utf8 (path, -1, NULL, NULL, NULL);
@ -251,6 +262,8 @@ moo_mkdir (const char *path,
if (errno != 0)
{
errno = 0;
if (m_mkdir (path) == -1)
{
int err_code = errno;
@ -362,6 +375,62 @@ moo_filename_from_locale (const char *file)
}
char *
moo_normalize_file_path (const char *filename)
{
char *freeme = NULL;
char *working_dir, *basename, *dirname;
char *real_filename = NULL;
g_return_val_if_fail (filename != NULL, NULL);
working_dir = g_get_current_dir ();
g_return_val_if_fail (working_dir != NULL, g_strdup (filename));
if (!g_path_is_absolute (filename))
{
freeme = g_build_filename (working_dir, filename, NULL);
filename = freeme;
}
dirname = g_path_get_dirname (filename);
basename = g_path_get_basename (filename);
g_return_val_if_fail (dirname && basename, g_strdup (filename));
errno = 0;
if (m_chdir (dirname) != 0)
{
int err = errno;
g_warning ("%s: %s", G_STRLOC, g_strerror (err));
}
else
{
char *real_dirname = g_get_current_dir ();
real_filename = g_build_filename (real_dirname, basename, NULL);
g_free (real_dirname);
errno = 0;
if (m_chdir (working_dir) != 0)
{
int err = errno;
g_warning ("%s: %s", G_STRLOC, g_strerror (err));
}
}
if (!real_filename)
real_filename = g_strdup (filename);
g_free (freeme);
g_free (dirname);
g_free (basename);
g_free (working_dir);
return real_filename;
}
/**********************************************************************/
/* MSLU for poor
*/
@ -401,6 +470,8 @@ G_STMT_START { \
return -1; \
} \
\
errno = 0; \
\
if (use_wide_char_api) \
retval = _WFunc (converted); \
else \
@ -436,6 +507,17 @@ m_rmdir (const char *path)
}
int
m_chdir (const char *path)
{
#ifdef __WIN32__
CCALL_1 (_chdir, _wchdir, path);
#else
return chdir (path);
#endif
}
int
m_mkdir (const char *path)
{
@ -464,6 +546,8 @@ m_remove (const char *path)
return -1;
}
errno = 0;
if (use_wide_char_api)
retval = _wremove (converted);
else
@ -519,6 +603,8 @@ m_fopen (const char *path,
return NULL;
}
errno = 0;
if (use_wide_char_api)
retval = _wfopen (path_conv, mode_conv);
else
@ -566,6 +652,8 @@ m_rename (const char *old_name,
return -1;
}
errno = 0;
if (use_wide_char_api)
retval = _wrename (old_conv, new_conv);
else

View File

@ -59,6 +59,8 @@ gboolean moo_mkdir (const char *path,
char **moo_filenames_from_locale (char **files);
char *moo_filename_from_locale (const char *file);
char *moo_normalize_file_path (const char *filename);
/*
* C library and WinAPI functions wrappers analogous to glib/gstdio.h
*/
@ -71,6 +73,7 @@ gpointer m_fopen (const char *path,
const char *mode);
int m_rename (const char *old_name,
const char *new_name);
int m_chdir (const char *path);
G_END_DECLS