summaryrefslogtreecommitdiffstats
path: root/crawl-ref
diff options
context:
space:
mode:
authordshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573>2006-12-10 08:32:25 +0000
committerdshaligram <dshaligram@c06c8d41-db1a-0410-9941-cceddc491573>2006-12-10 08:32:25 +0000
commit16c8c1117fda477278eaf4983d93df1fa83e603a (patch)
tree5d4ce9f6f49c7a7881f776223cdc2bb114a898a9 /crawl-ref
parent4ac70419ee6014d865b26f38d9c3a4fa08bdb2c2 (diff)
downloadcrawl-ref-16c8c1117fda477278eaf4983d93df1fa83e603a.tar.gz
crawl-ref-16c8c1117fda477278eaf4983d93df1fa83e603a.zip
Allow spaces, dashes and periods in character names.
git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@607 c06c8d41-db1a-0410-9941-cceddc491573
Diffstat (limited to 'crawl-ref')
-rw-r--r--crawl-ref/source/chardump.cc2
-rw-r--r--crawl-ref/source/files.cc15
-rw-r--r--crawl-ref/source/files.h6
-rw-r--r--crawl-ref/source/libutil.cc9
-rw-r--r--crawl-ref/source/libutil.h8
-rw-r--r--crawl-ref/source/mapdef.cc2
-rw-r--r--crawl-ref/source/newgame.cc27
7 files changed, 46 insertions, 23 deletions
diff --git a/crawl-ref/source/chardump.cc b/crawl-ref/source/chardump.cc
index b96e6ff18b..04b12201eb 100644
--- a/crawl-ref/source/chardump.cc
+++ b/crawl-ref/source/chardump.cc
@@ -993,7 +993,7 @@ static bool write_dump(
if (SysEnv.crawl_dir)
file_name += SysEnv.crawl_dir;
- file_name += fname;
+ file_name += strip_filename_unsafe_chars(fname);
std::string stash_file_name;
stash_file_name = file_name;
diff --git a/crawl-ref/source/files.cc b/crawl-ref/source/files.cc
index 119068ecd0..1eba6f40c5 100644
--- a/crawl-ref/source/files.cc
+++ b/crawl-ref/source/files.cc
@@ -417,8 +417,8 @@ void check_savedir(std::string &dir)
std::string sep = " ";
sep[0] = FILE_SEPARATOR;
- dir = replace_all(dir, "/", sep);
- dir = replace_all(dir, "\\", sep);
+ dir = replace_all_of(dir, "/", sep);
+ dir = replace_all_of(dir, "\\", sep);
// Suffix the separator if necessary
if (dir[dir.length() - 1] != FILE_SEPARATOR)
@@ -504,13 +504,15 @@ std::string get_savedir()
return (dir.empty()? "." : dir);
}
-std::string get_savedir_filename(const char *prefix, const char *suffix,
- const char *extension, bool suppress_uid)
+std::string get_savedir_filename(const std::string &prefix,
+ const std::string &suffix,
+ const std::string &extension,
+ bool suppress_uid)
{
std::string result = Options.save_dir;
// Shorten string as appropriate
- result += std::string(prefix).substr(0, kFileNameLen);
+ result += strip_filename_unsafe_chars(prefix).substr(0, kFileNameLen);
// Technically we should shorten the string first. But if
// MULTIUSER is set we'll have long filenames anyway. Caveat
@@ -520,7 +522,8 @@ std::string get_savedir_filename(const char *prefix, const char *suffix,
result += suffix;
- if ( *extension ) {
+ if (!extension.empty())
+ {
result += '.';
result += extension;
}
diff --git a/crawl-ref/source/files.h b/crawl-ref/source/files.h
index 3bbd358a1e..cf6997f464 100644
--- a/crawl-ref/source/files.h
+++ b/crawl-ref/source/files.h
@@ -38,8 +38,10 @@ bool travel_load_map( char branch, int absdepth );
std::vector<player> find_saved_characters();
std::string get_savedir();
-std::string get_savedir_filename(const char *pre, const char *suf,
- const char *ext, bool suppress_uid = false);
+std::string get_savedir_filename(const std::string &pre,
+ const std::string &suf,
+ const std::string &ext,
+ bool suppress_uid = false);
std::string get_prefs_filename();
diff --git a/crawl-ref/source/libutil.cc b/crawl-ref/source/libutil.cc
index 30bfc1b32c..181dd180bf 100644
--- a/crawl-ref/source/libutil.cc
+++ b/crawl-ref/source/libutil.cc
@@ -70,6 +70,11 @@ void play_sound( const char *file )
#endif
}
+std::string strip_filename_unsafe_chars(const std::string &s)
+{
+ return replace_all_of(s, " .", "");
+}
+
std::string make_stringf(const char *s, ...)
{
va_list args;
@@ -96,7 +101,7 @@ void lowercase(std::string &s)
s[i] = tolower(s[i]);
}
-std::string replace_all(std::string s,
+std::string replace_all_of(std::string s,
const std::string &tofind,
const std::string &replacement)
{
@@ -105,7 +110,7 @@ std::string replace_all(std::string s,
while ((found = s.find_first_of(tofind, start)) != std::string::npos)
{
- s.replace( found, tofind.length(), replacement );
+ s.replace( found, 1, replacement );
start = found + replacement.length();
}
diff --git a/crawl-ref/source/libutil.h b/crawl-ref/source/libutil.h
index 4378e029b3..0009f28882 100644
--- a/crawl-ref/source/libutil.h
+++ b/crawl-ref/source/libutil.h
@@ -21,11 +21,13 @@
void lowercase(std::string &s);
void uppercase(std::string &s);
+std::string strip_filename_unsafe_chars(const std::string &s);
+
std::string make_stringf(const char *format, ...);
-std::string replace_all(std::string s,
- const std::string &tofind,
- const std::string &replacement);
+std::string replace_all_of(std::string s,
+ const std::string &tofind,
+ const std::string &replacement);
int count_occurrences(const std::string &text, const std::string &searchfor);
diff --git a/crawl-ref/source/mapdef.cc b/crawl-ref/source/mapdef.cc
index 0463a13049..fc97d29148 100644
--- a/crawl-ref/source/mapdef.cc
+++ b/crawl-ref/source/mapdef.cc
@@ -582,7 +582,7 @@ int mons_list::mons_by_name(std::string name) const
{
lowercase(name);
- name = replace_all( name, "_", " " );
+ name = replace_all_of( name, "_", " " );
// Special casery:
if (name == "pandemonium demon")
diff --git a/crawl-ref/source/newgame.cc b/crawl-ref/source/newgame.cc
index f7e827fbcd..5fd1f24edc 100644
--- a/crawl-ref/source/newgame.cc
+++ b/crawl-ref/source/newgame.cc
@@ -93,7 +93,7 @@ extern std::string init_file_location;
#define MIN_START_STAT 1
static bool class_allowed(unsigned char speci, int char_class);
-static bool verifyPlayerName(void);
+static bool validate_player_name(void);
static void choose_weapon(void);
static void enterPlayerName(bool blankOK);
static void give_basic_knowledge(int which_job);
@@ -2045,7 +2045,7 @@ static bool is_good_name(char *name, bool blankOK)
return (false);
}
#endif
- return (verifyPlayerName());
+ return (validate_player_name());
}
static int newname_keyfilter(int &ch)
@@ -2142,18 +2142,25 @@ static void enterPlayerName(bool blankOK)
// If the player wants out, we bail out.
if (!read_player_name(name, kNameLen, existing_chars, char_menu))
end(0);
+
+ // Laboriously trim the damn thing.
+ std::string read_name = name;
+ trim_string(read_name);
+ strncpy(name, read_name.c_str(), kNameLen);
+ name[kNameLen - 1] = 0;
}
}
while (ask_name = !is_good_name(you.your_name, blankOK));
} // end enterPlayerName()
-static bool verifyPlayerName(void)
+static bool validate_player_name(void)
{
#if defined(DOS) || defined(WIN32CONSOLE)
static int william_tanksley_asked_for_this = 2;
// quick check for CON -- blows up real good under DOS/Windows
- if (stricmp(you.your_name, "con") == 0)
+ if (stricmp(you.your_name, "con") == 0
+ || stricmp(you.your_name, "nul") == 0)
{
cprintf(EOL "Sorry, that name gives your OS a headache." EOL);
return (false);
@@ -2180,17 +2187,20 @@ static bool verifyPlayerName(void)
}
#endif
- const size_t len = strlen( you.your_name );
- for (unsigned int i = 0; i < len; i++)
+ for (const char *pn = you.your_name; *pn; ++pn)
{
+ char c = *pn;
// Note that this includes systems which may be using the
// packaging system. The packaging system is very simple
// and doesn't take the time to escape every characters that
// might be a problem for some random shell or OS... so we
// play it very conservative here. -- bwr
- if (!isalnum( you.your_name[i] ) && you.your_name[i] != '_')
+ if (!isalnum(c) && c != '-' && c != '.' && c != '_' && c != ' ')
{
- cprintf( EOL "Alpha-numerics and underscores only, please." EOL );
+ cprintf( EOL
+ "Alpha-numerics, spaces, dashes, periods and underscores "
+ "only, please."
+ EOL );
return (false);
}
}
@@ -2198,6 +2208,7 @@ static bool verifyPlayerName(void)
#ifdef MULTIUSER
// Until we have a better way to handle the fact that this could lead
// to some confusion with where the name ends and the uid begins. -- bwr
+ const size_t len = strlen( you.your_name );
if (isdigit( you.your_name[ len - 1 ] ))
{
cprintf( EOL "Sorry, your name cannot end with a digit." EOL );