- buf[0] = '\0';
- if (!file) {
- return -1;
- }
-
- if (file[0] != '~') {
- (void)strnfmt(buf, max, "%s", file);
- return 0;
- }
-
- concptr u = file + 1;
- concptr s = angband_strstr(u, PATH_SEP);
- char user[128];
- if (s && (s >= u + sizeof(user))) {
- return 1;
- }
-
- if (s) {
+ /*
+ * Extract a "parsed" path from an initial filename
+ * Normally, we simply copy the filename into the buffer
+ * But leading tilde symbols must be handled in a special way
+ * Replace "~user/" by the home directory of the user named "user"
+ * Replace "~/" by the home directory of the current user
+ */
+ if (file.empty() || (file[0] != '~')) {
+ return file;
+ }
+
+ auto u = file.data() + 1;
+ auto s = angband_strstr(u, PATH_SEP);
+ constexpr auto user_size = 128;
+ char user[user_size]{};
+ if ((s != nullptr) && (s >= u + user_size)) {
+ throw std::runtime_error("User name is too long!");
+ }
+
+ if (s != nullptr) {