Remove char_u: modname()

This commit is contained in:
Mark Bainter 2015-04-20 11:33:52 +00:00
parent f813fdce38
commit 1f76857232
4 changed files with 75 additions and 73 deletions

View File

@ -1530,7 +1530,7 @@ void write_viminfo(char_u *file, int forceit)
#endif #endif
// Make tempname // Make tempname
tempname = modname(fname, (char_u *)".tmp", FALSE); tempname = (char_u *)modname((char *)fname, ".tmp", FALSE);
if (tempname != NULL) { if (tempname != NULL) {
/* /*
* Check if tempfile already exists. Never overwrite an * Check if tempfile already exists. Never overwrite an

View File

@ -2814,7 +2814,7 @@ buf_write (
/* /*
* Make backup file name. * Make backup file name.
*/ */
backup = modname(rootname, backup_ext, FALSE); backup = (char_u *)modname((char *)rootname, (char *)backup_ext, FALSE);
if (backup == NULL) { if (backup == NULL) {
xfree(rootname); xfree(rootname);
some_error = TRUE; /* out of memory */ some_error = TRUE; /* out of memory */
@ -2985,7 +2985,7 @@ nobackup:
if (rootname == NULL) if (rootname == NULL)
backup = NULL; backup = NULL;
else { else {
backup = modname(rootname, backup_ext, FALSE); backup = (char_u *)modname((char *)rootname, (char *)backup_ext, FALSE);
xfree(rootname); xfree(rootname);
} }
@ -3595,7 +3595,7 @@ restore_backup:
* the backup file our 'original' file. * the backup file our 'original' file.
*/ */
if (*p_pm && dobackup) { if (*p_pm && dobackup) {
char *org = (char *)modname(fname, p_pm, FALSE); char *org = modname((char *)fname, (char *)p_pm, FALSE);
if (backup != NULL) { if (backup != NULL) {
/* /*
@ -4333,108 +4333,110 @@ void shorten_fnames(int force)
redraw_tabline = TRUE; redraw_tabline = TRUE;
} }
/* /// Get new filename ended by given extension.
* add extension to file name - change path/fo.o.h to path/fo.o.h.ext ///
* /// @param fname The original filename.
* Assumed that fname is a valid name found in the filesystem we assure that /// If NULL, use current directory name and ext to
* the return value is a different name and ends in 'ext'. /// compute new filename.
* "ext" MUST be at most 4 characters long if it starts with a dot, 3 /// @param ext The extension to add to the filename.
* characters otherwise. /// 4 chars max if prefixed with a dot, 3 otherwise.
* Space for the returned name is allocated, must be freed later. /// @param prepend_dot If true, prefix ext with a dot.
* Returns NULL when out of memory. /// Does nothing if ext already starts with a dot, or
*/ /// if fname is NULL.
char_u * ///
modname ( /// @return [allocated] - A new filename, made up from:
char_u *fname, /// * fname + ext, if fname not NULL.
char_u *ext, /// * current dir + ext, if fname is NULL.
int prepend_dot /* may prepend a '.' to file name */ /// On Windows, and if ext starts with ".", a "_" is
) /// preprended to ext (for filename to be valid).
/// Result is guaranteed to:
/// * be ended by <ext>.
/// * have a basename with at most BASENAMELEN chars:
/// original basename is truncated if necessary.
/// * be different than original: basename chars are
/// replaced by "_" if necessary. If that can't be done
/// because truncated value of original filename was
/// made of all underscores, replace first "_" by "v".
/// - NULL, if fname is NULL and there was a problem trying
/// to get current directory.
char *modname(const char *fname, const char *ext, bool prepend_dot)
FUNC_ATTR_NONNULL_ARG(2)
{ {
char_u *retval; char *retval;
char_u *s; size_t fnamelen;
char_u *e; size_t extlen = strlen(ext);
char_u *ptr;
int fnamelen, extlen;
extlen = (int)STRLEN(ext); // If there is no file name we must get the name of the current directory
// (we need the full path in case :cd is used).
/*
* If there is no file name we must get the name of the current directory
* (we need the full path in case :cd is used).
*/
if (fname == NULL || *fname == NUL) { if (fname == NULL || *fname == NUL) {
retval = xmalloc(MAXPATHL + extlen + 3); retval = xmalloc(MAXPATHL + extlen + 3); // +3 for PATHSEP, "_" (Win), NUL
if (os_dirname(retval, MAXPATHL) == FAIL || if (os_dirname((char_u *)retval, MAXPATHL) == FAIL ||
(fnamelen = (int)STRLEN(retval)) == 0) { (fnamelen = strlen(retval)) == 0) {
xfree(retval); xfree(retval);
return NULL; return NULL;
} }
if (!after_pathsep((char *)retval, (char *)retval + fnamelen)) { if (!after_pathsep(retval, retval + fnamelen)) {
retval[fnamelen++] = PATHSEP; retval[fnamelen++] = PATHSEP;
retval[fnamelen] = NUL; retval[fnamelen] = NUL;
} }
prepend_dot = FALSE; /* nothing to prepend a dot to */ prepend_dot = FALSE; // nothing to prepend a dot to
} else { } else {
fnamelen = (int)STRLEN(fname); fnamelen = strlen(fname);
retval = xmalloc(fnamelen + extlen + 3); retval = xmalloc(fnamelen + extlen + 3);
STRCPY(retval, fname); strcpy(retval, fname);
} }
/* // Search backwards until we hit a '/', '\' or ':'.
* search backwards until we hit a '/', '\' or ':'. // Then truncate what is after the '/', '\' or ':' to BASENAMELEN characters.
* Then truncate what is after the '/', '\' or ':' to BASENAMELEN characters. char *ptr = NULL;
*/
for (ptr = retval + fnamelen; ptr > retval; mb_ptr_back(retval, ptr)) { for (ptr = retval + fnamelen; ptr > retval; mb_ptr_back(retval, ptr)) {
if (vim_ispathsep(*ptr)) { if (vim_ispathsep(*ptr)) {
++ptr; ptr++;
break; break;
} }
} }
/* the file name has at most BASENAMELEN characters. */ // the file name has at most BASENAMELEN characters.
if (STRLEN(ptr) > BASENAMELEN) if (strlen(ptr) > BASENAMELEN) {
ptr[BASENAMELEN] = '\0'; ptr[BASENAMELEN] = '\0';
}
s = ptr + STRLEN(ptr); char *s;
s = ptr + strlen(ptr);
#if defined(WIN3264) #if defined(WIN3264)
/* // If there is no file name, and the extension starts with '.', put a
* If there is no file name, and the extension starts with '.', put a // '_' before the dot, because just ".ext" may be invalid if it's on a
* '_' before the dot, because just ".ext" may be invalid if it's on a // FAT partition, and on HPFS it doesn't matter.
* FAT partition, and on HPFS it doesn't matter. else if ((fname == NULL || *fname == NUL) && *ext == '.') {
*/
else if ((fname == NULL || *fname == NUL) && *ext == '.')
*s++ = '_'; *s++ = '_';
}
#endif #endif
/* // Append the extension.
* Append the extension. // ext can start with '.' and cannot exceed 3 more characters.
* ext can start with '.' and cannot exceed 3 more characters. strcpy(s, ext);
*/
STRCPY(s, ext);
/* char *e;
* Prepend the dot. // Prepend the dot if needed.
*/ if (prepend_dot && *(e = (char *)path_tail((char_u *)retval)) != '.') {
if (prepend_dot && *(e = path_tail(retval)) != '.') {
STRMOVE(e + 1, e); STRMOVE(e + 1, e);
*e = '.'; *e = '.';
} }
/* // Check that, after appending the extension, the file name is really
* Check that, after appending the extension, the file name is really // different.
* different. if (fname != NULL && strcmp(fname, retval) == 0) {
*/ // we search for a character that can be replaced by '_'
if (fname != NULL && STRCMP(fname, retval) == 0) {
/* we search for a character that can be replaced by '_' */
while (--s >= ptr) { while (--s >= ptr) {
if (*s != '_') { if (*s != '_') {
*s = '_'; *s = '_';
break; break;
} }
} }
if (s < ptr) /* fname was "________.<ext>", how tricky! */ if (s < ptr) { // fname was "________.<ext>", how tricky!
*ptr = 'v'; *ptr = 'v';
}
} }
return retval; return retval;
} }

View File

@ -133,7 +133,7 @@
# define mb_cptr_adv(p) p += \ # define mb_cptr_adv(p) p += \
enc_utf8 ? utf_ptr2len(p) : has_mbyte ? (*mb_ptr2len)(p) : 1 enc_utf8 ? utf_ptr2len(p) : has_mbyte ? (*mb_ptr2len)(p) : 1
/* Backup multi-byte pointer. Only use with "p" > "s" ! */ /* Backup multi-byte pointer. Only use with "p" > "s" ! */
# define mb_ptr_back(s, p) p -= has_mbyte ? ((*mb_head_off)(s, p - 1) + 1) : 1 # define mb_ptr_back(s, p) p -= has_mbyte ? ((*mb_head_off)((char_u *)s, (char_u *)p - 1) + 1) : 1
/* get length of multi-byte char, not including composing chars */ /* get length of multi-byte char, not including composing chars */
# define mb_cptr2len(p) (enc_utf8 ? utf_ptr2len(p) : (*mb_ptr2len)(p)) # define mb_cptr2len(p) (enc_utf8 ? utf_ptr2len(p) : (*mb_ptr2len)(p))

View File

@ -1364,7 +1364,7 @@ recover_names (
* Try finding a swap file by simply adding ".swp" to the file name. * Try finding a swap file by simply adding ".swp" to the file name.
*/ */
if (*dirp == NUL && file_count + num_files == 0 && fname != NULL) { if (*dirp == NUL && file_count + num_files == 0 && fname != NULL) {
char_u *swapname = modname(fname_res, (char_u *)".swp", TRUE); char_u *swapname = (char_u *)modname((char *)fname_res, ".swp", TRUE);
if (swapname != NULL) { if (swapname != NULL) {
if (os_file_exists(swapname)) { if (os_file_exists(swapname)) {
files = (char_u **)xmalloc(sizeof(char_u *)); files = (char_u **)xmalloc(sizeof(char_u *));
@ -1565,7 +1565,7 @@ static int recov_file_names(char_u **names, char_u *path, int prepend_dot)
// May also add the file name with a dot prepended, for swap file in same // May also add the file name with a dot prepended, for swap file in same
// dir as original file. // dir as original file.
if (prepend_dot) { if (prepend_dot) {
names[num_names] = modname(path, (char_u *)".sw?", TRUE); names[num_names] = (char_u *)modname((char *)path, ".sw?", TRUE);
if (names[num_names] == NULL) if (names[num_names] == NULL)
return num_names; return num_names;
++num_names; ++num_names;
@ -3069,7 +3069,7 @@ char_u *makeswapname(char_u *fname, char_u *ffname, buf_T *buf, char_u *dir_name
if (after_pathsep((char *)dir_name, (char *)s) && s[-1] == s[-2]) { /* Ends with '//', Use Full path */ if (after_pathsep((char *)dir_name, (char *)s) && s[-1] == s[-2]) { /* Ends with '//', Use Full path */
r = NULL; r = NULL;
if ((s = make_percent_swname(dir_name, fname)) != NULL) { if ((s = make_percent_swname(dir_name, fname)) != NULL) {
r = modname(s, (char_u *)".swp", FALSE); r = (char_u *)modname((char *)s, ".swp", FALSE);
xfree(s); xfree(s);
} }
return r; return r;
@ -3083,7 +3083,7 @@ char_u *makeswapname(char_u *fname, char_u *ffname, buf_T *buf, char_u *dir_name
#endif #endif
// Prepend a '.' to the swap file name for the current directory. // Prepend a '.' to the swap file name for the current directory.
r = modname(fname_res, (char_u *)".swp", r = (char_u *)modname((char *)fname_res, ".swp",
dir_name[0] == '.' && dir_name[1] == NUL); dir_name[0] == '.' && dir_name[1] == NUL);
if (r == NULL) /* out of memory */ if (r == NULL) /* out of memory */
return NULL; return NULL;