miscellaneous enum cleanups, including making the file-writing

functions' append parameter an enum instead of an int


git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2966 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
This commit is contained in:
David Lawrence Ramsey 2005-08-01 18:27:10 +00:00
parent 5d552195ea
commit bf0e47dc37
5 changed files with 44 additions and 35 deletions

View File

@ -148,6 +148,8 @@ CVS code -
- Assert that filename isn't NULL, and don't do anything special - Assert that filename isn't NULL, and don't do anything special
if it's blank, as the the former case shouldn't occur, and the if it's blank, as the the former case shouldn't occur, and the
latter case is now handled elsewhere. (DLR) latter case is now handled elsewhere. (DLR)
write_file(), write_marked_file(), do_writeout()
- Make append an append_type enum instead of an int. (DLR)
input_tab() input_tab()
- Make columns an int instead of a size_t, since it's limited by - Make columns an int instead of a size_t, since it's limited by
COLS. (DLR) COLS. (DLR)
@ -222,6 +224,8 @@ CVS code -
(DLR) (DLR)
- Move stdlib.h, dirent.h, regex.h, and assert.h includes here, - Move stdlib.h, dirent.h, regex.h, and assert.h includes here,
as every source file needs them. (DLR) as every source file needs them. (DLR)
- Rename the updown enum scroll_dir and the centernone enum
update_type for clarity, and add an append_type enum. (DLR)
- rcfile.c: - rcfile.c:
nregcomp() nregcomp()
- Return TRUE when the compilation succeeds and FALSE otherwise, - Return TRUE when the compilation succeeds and FALSE otherwise,

View File

@ -1130,15 +1130,15 @@ int copy_file(FILE *inn, FILE *out)
* tmp means we are writing a temporary file in a secure fashion. We * tmp means we are writing a temporary file in a secure fashion. We
* use it when spell checking or dumping the file on an error. * use it when spell checking or dumping the file on an error.
* *
* append == 1 means we are appending instead of overwriting. * append == APPEND means we are appending instead of overwriting.
* append == 2 means we are prepending instead of overwriting. * append == PREPEND means we are prepending instead of overwriting.
* *
* nonamechange means don't change the current filename. It is ignored * nonamechange means don't change the current filename. It is ignored
* if tmp is FALSE or if we're appending/prepending. * if tmp is FALSE or if we're appending/prepending.
* *
* Return 0 on success or -1 on error. */ * Return 0 on success or -1 on error. */
int write_file(const char *name, FILE *f_open, bool tmp, int append, int write_file(const char *name, FILE *f_open, bool tmp, append_type
bool nonamechange) append, bool nonamechange)
{ {
int retval = -1; int retval = -1;
/* Instead of returning in this function, you should always /* Instead of returning in this function, you should always
@ -1213,9 +1213,9 @@ int write_file(const char *name, FILE *f_open, bool tmp, int append,
* aren't appending, prepending, or writing a selection, we backup * aren't appending, prepending, or writing a selection, we backup
* only if the file has not been modified by someone else since nano * only if the file has not been modified by someone else since nano
* opened it. */ * opened it. */
if (ISSET(BACKUP_FILE) && !tmp && realexists && ((append != 0 || if (ISSET(BACKUP_FILE) && !tmp && realexists && ((append !=
openfile->mark_set) || openfile->current_stat->st_mtime == OVERWRITE || openfile->mark_set) ||
st.st_mtime)) { openfile->current_stat->st_mtime == st.st_mtime)) {
FILE *backup_file; FILE *backup_file;
char *backupname; char *backupname;
struct utimbuf filetime; struct utimbuf filetime;
@ -1345,7 +1345,7 @@ int write_file(const char *name, FILE *f_open, bool tmp, int append,
} }
/* If we're prepending, copy the file to a temp file. */ /* If we're prepending, copy the file to a temp file. */
if (append == 2) { if (append == PREPEND) {
int fd_source; int fd_source;
FILE *f_source = NULL; FILE *f_source = NULL;
@ -1385,9 +1385,9 @@ int write_file(const char *name, FILE *f_open, bool tmp, int append,
/* Now open the file in place. Use O_EXCL if tmp is TRUE. This /* Now open the file in place. Use O_EXCL if tmp is TRUE. This
* is copied from joe, because wiggy says so *shrug*. */ * is copied from joe, because wiggy says so *shrug*. */
fd = open(realname, O_WRONLY | O_CREAT | fd = open(realname, O_WRONLY | O_CREAT |
((append == 1) ? O_APPEND : (tmp ? O_EXCL : O_TRUNC)), ((append == APPEND) ? O_APPEND : (tmp ? O_EXCL :
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | O_TRUNC)), S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
S_IWOTH); S_IROTH | S_IWOTH);
/* Set the umask back to the user's original value. */ /* Set the umask back to the user's original value. */
umask(original_umask); umask(original_umask);
@ -1403,7 +1403,7 @@ int write_file(const char *name, FILE *f_open, bool tmp, int append,
goto cleanup_and_exit; goto cleanup_and_exit;
} }
f = fdopen(fd, (append == 1) ? "ab" : "wb"); f = fdopen(fd, (append == APPEND) ? "ab" : "wb");
if (f == NULL) { if (f == NULL) {
statusbar(_("Error writing %s: %s"), realname, statusbar(_("Error writing %s: %s"), realname,
@ -1462,7 +1462,7 @@ int write_file(const char *name, FILE *f_open, bool tmp, int append,
} }
/* If we're prepending, open the temp file, and append it to f. */ /* If we're prepending, open the temp file, and append it to f. */
if (append == 2) { if (append == PREPEND) {
int fd_source; int fd_source;
FILE *f_source = NULL; FILE *f_source = NULL;
@ -1492,7 +1492,7 @@ int write_file(const char *name, FILE *f_open, bool tmp, int append,
goto cleanup_and_exit; goto cleanup_and_exit;
} }
if (!tmp && append == 0) { if (!tmp && append == OVERWRITE) {
if (!nonamechange) { if (!nonamechange) {
openfile->filename = mallocstrcpy(openfile->filename, openfile->filename = mallocstrcpy(openfile->filename,
realname); realname);
@ -1537,8 +1537,8 @@ int write_file(const char *name, FILE *f_open, bool tmp, int append,
#ifndef NANO_SMALL #ifndef NANO_SMALL
/* Write a marked selection from a file out. */ /* Write a marked selection from a file out. */
int write_marked_file(const char *name, FILE *f_open, bool tmp, int int write_marked_file(const char *name, FILE *f_open, bool tmp,
append) append_type append)
{ {
int retval = -1; int retval = -1;
bool old_modified = openfile->modified; bool old_modified = openfile->modified;
@ -1582,8 +1582,8 @@ int write_marked_file(const char *name, FILE *f_open, bool tmp, int
int do_writeout(bool exiting) int do_writeout(bool exiting)
{ {
int i; int i, retval = 0;
int retval = 0, append = 0; append_type append = OVERWRITE;
char *ans; char *ans;
/* The last answer the user typed on the statusbar. */ /* The last answer the user typed on the statusbar. */
#ifdef NANO_EXTRA #ifdef NANO_EXTRA
@ -1619,13 +1619,14 @@ int do_writeout(bool exiting)
backupstr = ISSET(BACKUP_FILE) ? N_(" [Backup]") : ""; backupstr = ISSET(BACKUP_FILE) ? N_(" [Backup]") : "";
if (openfile->mark_set && !exiting) if (openfile->mark_set && !exiting)
msg = (append == 2) ? N_("Prepend Selection to File") : msg = (append == PREPEND) ?
(append == 1) ? N_("Append Selection to File") : N_("Prepend Selection to File") : (append == APPEND) ?
N_("Append Selection to File") :
N_("Write Selection to File"); N_("Write Selection to File");
else else
#endif /* !NANO_SMALL */ #endif /* !NANO_SMALL */
msg = (append == 2) ? N_("File Name to Prepend to") : msg = (append == PREPEND) ? N_("File Name to Prepend to") :
(append == 1) ? N_("File Name to Append to") : (append == APPEND) ? N_("File Name to Append to") :
N_("File Name to Write"); N_("File Name to Write");
/* If we're using restricted mode, the filename isn't blank, /* If we're using restricted mode, the filename isn't blank,
@ -1679,10 +1680,10 @@ int do_writeout(bool exiting)
} else } else
#endif /* !NANO_SMALL */ #endif /* !NANO_SMALL */
if (i == NANO_PREPEND_KEY) { if (i == NANO_PREPEND_KEY) {
append = (append == 2) ? 0 : 2; append = (append == PREPEND) ? OVERWRITE : PREPEND;
continue; continue;
} else if (i == NANO_APPEND_KEY) { } else if (i == NANO_APPEND_KEY) {
append = (append == 1) ? 0 : 1; append = (append == APPEND) ? OVERWRITE : APPEND;
continue; continue;
} }
@ -1699,7 +1700,7 @@ int do_writeout(bool exiting)
break; break;
} }
#endif #endif
if (append == 0 && strcmp(answer, if (append == OVERWRITE && strcmp(answer,
openfile->filename) != 0) { openfile->filename) != 0) {
struct stat st; struct stat st;

View File

@ -150,13 +150,17 @@ typedef enum {
NIX_FILE, DOS_FILE, MAC_FILE NIX_FILE, DOS_FILE, MAC_FILE
} file_format; } file_format;
typedef enum {
OVERWRITE, APPEND, PREPEND
} append_type;
typedef enum { typedef enum {
UP, DOWN UP, DOWN
} updown; } scroll_dir;
typedef enum { typedef enum {
CENTER, NONE CENTER, NONE
} centernone; } update_type;
/* Structure types. */ /* Structure types. */
typedef struct filestruct { typedef struct filestruct {

View File

@ -257,11 +257,11 @@ bool check_operating_dir(const char *currpath, bool allow_tabcomp);
void init_backup_dir(void); void init_backup_dir(void);
#endif #endif
int copy_file(FILE *inn, FILE *out); int copy_file(FILE *inn, FILE *out);
int write_file(const char *name, FILE *f_open, bool tmp, int append, int write_file(const char *name, FILE *f_open, bool tmp, append_type
bool nonamechange); append, bool nonamechange);
#ifndef NANO_SMALL #ifndef NANO_SMALL
int write_marked_file(const char *name, FILE *f_open, bool tmp, int int write_marked_file(const char *name, FILE *f_open, bool tmp,
append); append_type append);
#endif #endif
int do_writeout(bool exiting); int do_writeout(bool exiting);
void do_writeout_void(void); void do_writeout_void(void);
@ -666,10 +666,10 @@ void edit_add(const filestruct *fileptr, const char *converted, int
void update_line(const filestruct *fileptr, size_t index); void update_line(const filestruct *fileptr, size_t index);
int need_horizontal_update(size_t old_pww); int need_horizontal_update(size_t old_pww);
int need_vertical_update(size_t old_pww); int need_vertical_update(size_t old_pww);
void edit_scroll(updown direction, int nlines); void edit_scroll(scroll_dir direction, int nlines);
void edit_redraw(const filestruct *old_current, size_t old_pww); void edit_redraw(const filestruct *old_current, size_t old_pww);
void edit_refresh(void); void edit_refresh(void);
void edit_update(centernone location); void edit_update(update_type location);
int do_yesno(bool all, const char *msg); int do_yesno(bool all, const char *msg);
void total_redraw(void); void total_redraw(void);
void total_refresh(void); void total_refresh(void);

View File

@ -3477,7 +3477,7 @@ int need_vertical_update(size_t old_pww)
* and nlines is the number of lines to scroll. We change edittop, and * and nlines is the number of lines to scroll. We change edittop, and
* assume that current and current_x are up to date. We also assume * assume that current and current_x are up to date. We also assume
* that scrollok(edit) is FALSE. */ * that scrollok(edit) is FALSE. */
void edit_scroll(updown direction, int nlines) void edit_scroll(scroll_dir direction, int nlines)
{ {
bool do_redraw = need_vertical_update(0); bool do_redraw = need_vertical_update(0);
const filestruct *foo; const filestruct *foo;
@ -3649,7 +3649,7 @@ void edit_refresh(void)
* same place. location determines how we move it: if it's CENTER, we * same place. location determines how we move it: if it's CENTER, we
* center current, and if it's NONE, we put current current_y lines * center current, and if it's NONE, we put current current_y lines
* below edittop. */ * below edittop. */
void edit_update(centernone location) void edit_update(update_type location)
{ {
filestruct *foo = openfile->current; filestruct *foo = openfile->current;
int goal; int goal;