Don't try to use reserved device name on win32

master
Yevgen Muntyan 2006-06-20 21:20:18 -05:00
parent 094569ca86
commit 9e14d45b0f
1 changed files with 63 additions and 2 deletions

View File

@ -16,6 +16,7 @@
#include "mooutils/mooprefs.h" #include "mooutils/mooprefs.h"
#include "mooutils/mooutils-misc.h" #include "mooutils/mooutils-misc.h"
#include "mooutils/moocompat.h" #include "mooutils/moocompat.h"
#include <string.h>
struct _MooFileDialogPrivate { struct _MooFileDialogPrivate {
@ -454,6 +455,58 @@ set_filenames (MooFileDialog *dialog,
} }
static gboolean
filename_is_valid (G_GNUC_UNUSED const char *filename,
G_GNUC_UNUSED char **msg)
{
#ifndef __WIN32__
return TRUE;
#else
struct name {
const char *name;
guint len;
} names[] = {
{ "con", 3 }, { "aux", 3 }, { "prn", 3 }, { "nul", 3 },
{ "com1", 4 }, { "com2", 4 }, { "com3", 4 }, { "com4", 4 },
{ "lpt1", 4 }, { "lpt2", 4 }, { "lpt3", 4 }
};
guint i;
const char *basename;
gboolean invalid = FALSE;
basename = strrchr (filename, '\\');
if (!basename)
basename = strrchr (filename, '/');
if (!basename)
basename = filename;
else
basename += 1;
for (i = 0; i < G_N_ELEMENTS (names); ++i)
{
if (!g_ascii_strncasecmp (basename, names[i].name, names[i].len))
{
if (!basename[names[i].len] || basename[names[i].len] == '.')
invalid = TRUE;
break;
}
}
if (invalid)
{
char *base = g_path_get_basename (filename);
*msg = g_strdup_printf ("Filename '%s' is a reserved device name.\n"
"Please choose another name", base);
g_free (base);
return FALSE;
}
return TRUE;
#endif
}
gboolean gboolean
moo_file_dialog_run (MooFileDialog *dialog) moo_file_dialog_run (MooFileDialog *dialog)
@ -493,9 +546,17 @@ moo_file_dialog_run (MooFileDialog *dialog)
{ {
if (GTK_RESPONSE_OK == gtk_dialog_run (GTK_DIALOG (filechooser))) if (GTK_RESPONSE_OK == gtk_dialog_run (GTK_DIALOG (filechooser)))
{ {
char *msg;
filename = file_chooser_get_filename (filechooser); filename = file_chooser_get_filename (filechooser);
if (g_file_test (filename, G_FILE_TEST_EXISTS) && if (!filename_is_valid (filename, &msg))
{
moo_error_dialog (filechooser, msg, NULL);
g_free (filename);
g_free (msg);
}
else if (g_file_test (filename, G_FILE_TEST_EXISTS) &&
! g_file_test (filename, G_FILE_TEST_IS_REGULAR)) ! g_file_test (filename, G_FILE_TEST_IS_REGULAR))
{ {
moo_error_dialog (filechooser, moo_error_dialog (filechooser,