Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add saved/unsaved status #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions include/hex.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ struct Stack { /* struct to be used for stack*/
int savedVal;
off_t currentLoc;
struct Stack *prev;
struct LinkedList *llist;
};

/* typedefs */
Expand All @@ -100,6 +99,7 @@ extern int SIZE_CH;
extern bool USE_EBCDIC;
extern char EBCDIC[256];
extern bool color_enabled;
extern bool saved;

/* macros */
/*#define currentLoc(line, col) ((line) * BASE +((col)/3)) */
Expand All @@ -108,7 +108,6 @@ extern bool color_enabled;
/* cursor location in the file*/
#define cursorLoc(line,col,editHex,b) (((line)*(b)) + ((col)/((editHex)?3:1)))
#define llalloc() (struct LinkedList *) calloc(1, sizeof(struct LinkedList))
#define isEmptyStack(stack) (((stack) == NULL) ? TRUE : FALSE)

#define UNUSED(x) (void)(x)

Expand All @@ -119,6 +118,7 @@ extern bool color_enabled;
#define MIN_COLS 70 /* screen has to be 70< cols */
#define MIN_LINES 7 /* 8 - 1 */ /* the slk crap minuses 1 line*/
#define KEY_TAB 9 /* value for the tab key */
#define SAVEPOINT -1

#define AlphabetSize (UCHAR_MAX +1) /* for portability */

Expand All @@ -138,6 +138,7 @@ char *inputLine(WINDOW *win, int line, int col);
/* file.c */
void outline(FILE *fp, off_t linenum);
off_t maxLoc(FILE *fp);
void set_saved(bool sav, WINDOW *win);
void print_usage();
off_t maxLines(off_t len);
int openfile(WINS *win);
Expand Down Expand Up @@ -181,8 +182,7 @@ void popupWin(char *msg, int time);
short int questionWin(char *msg);

/* stack.c */
void createStack(hexStack *stack);
void pushStack(hexStack **stack, hexStack *tmpStack);
void pushStack(hexStack **stack, off_t cl, int val);
void popStack(hexStack **stack);
void smashDaStack(hexStack **stack);

Expand Down
55 changes: 38 additions & 17 deletions src/acceptch.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,16 @@ int wacceptch(WINS *win, off_t len)
short int key; /* key capture */
WINDOW *Winds; /* window pointer */
hexStack *stack; /* used for stack */
hexStack *tmpStack; /* used for stack */
editHex = TRUE; /* val for editing */

SIZE_CH = FALSE; /* set winch to false */
Winds = win->hex; /* curr edit window */
maxlines = maxLines(len); /* lines in file */

bool shouldExit = false;
bool savedpoint = TRUE;

/*createStack(stack);*/ /* init the stack */
stack = NULL;
stack = NULL; /* init the stack */
temp = (char *)calloc(81, sizeof(char));

if (fpIN) /* if file opened then*/
Expand All @@ -84,7 +83,6 @@ int wacceptch(WINS *win, off_t len)

/* get keys til exit */
while(!shouldExit)
//while (!(save=quitProgram(isEmptyStack(stack),(key = wgetch(Winds)))))
{
key = wgetch(Winds);
lastRow = row;
Expand Down Expand Up @@ -118,7 +116,7 @@ int wacceptch(WINS *win, off_t len)
case CTRL_AND('q'):
case CTRL_AND('x'):
case KEY_F(8):
if (isEmptyStack(stack))
if (saved)
{
/* No pending changes */
shouldExit = true;
Expand Down Expand Up @@ -246,13 +244,8 @@ int wacceptch(WINS *win, off_t len)

/* edit list */
head = insertItem(head, cl, val);
/* calloc() is used because it NULLS out all returned memory */
tmpStack = (hexStack *) calloc(1, sizeof(hexStack));
tmpStack->currentLoc = cl;
tmpStack->llist = head;
tmpStack->savedVal = tmpval;
tmpStack->prev = NULL;
pushStack(&stack, tmpStack);
pushStack(&stack, cl, tmpval);
set_saved(FALSE, Winds);
} /* continue to next
case */

Expand Down Expand Up @@ -430,13 +423,23 @@ int wacceptch(WINS *win, off_t len)
MIN_ADDR_LENGTH = getMinimumAddressLength(maxLoc(fpIN));
curVal = getLocVal(0);
}
set_saved(TRUE, Winds);
return TRUE; /* TRUE if worked */
}
break;

case CTRL_AND('s'): /* if F2 or ^s... */
case KEY_F(2): /* save the file */
savefile(win);
if (savefile(win) == 0)
{
set_saved(TRUE, Winds);
if (stack != NULL && stack->savedVal != SAVEPOINT)
{
pushStack(&stack, -1, SAVEPOINT);
savedpoint = TRUE;
}
else if (stack == NULL) savedpoint = TRUE;
}
break;

case CTRL_AND('f'):
Expand Down Expand Up @@ -673,14 +676,32 @@ int wacceptch(WINS *win, off_t len)
case CTRL_AND('z'): /* ^z undo last mod */
getyx(Winds, row, col);

/* set previous loc */
cl = (stack == NULL) ? cl : stack->currentLoc;
if (stack != NULL && stack->savedVal == SAVEPOINT)
{
popStack(&stack);
savedpoint = FALSE;
}
if (stack != NULL)
{

/*if (stack != NULL) val = stack->savedVal; */
cl = stack->currentLoc;
val = stack->savedVal;
popStack(&stack);
/* if there is a save point in */
/* the next value of the stack */
if (stack != NULL && stack->savedVal == SAVEPOINT)
{
if (savedpoint) /* and if it's the last (controlled */
{ /* by savedpoint) go to saved mode */
savedpoint = FALSE;
set_saved(TRUE, Winds);
}
else set_saved(FALSE, Winds);
popStack(&stack);
}
else if (stack == NULL && savedpoint)
set_saved(TRUE, Winds);
else set_saved(FALSE, Winds);

head = deleteNode(head,cl);


Expand Down
25 changes: 25 additions & 0 deletions src/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ off_t maxLoc(FILE *fp)
return(ftello(fp)); /* return val at EOF */
}

/*******************************************************\
* Description: enters o exits from modified mode: *
* set or reset saved, and write or restore *
* mode indicator (*) at top of hex window *
\*******************************************************/
void set_saved(bool sav, WINDOW *win)
{
int y, x;

getyx(win, y, x);
if ( (saved = sav) ) /* set, not compare */
{
mvwaddch(windows->hex_outline, 0, MIN_ADDR_LENGTH+3, ACS_HLINE);
mvwaddch(windows->hex_outline, 0, MIN_ADDR_LENGTH+4, ACS_HLINE);
mvwaddch(windows->hex_outline, 0, MIN_ADDR_LENGTH+5, ACS_HLINE);
wnoutrefresh(windows->hex_outline);
}
else
{
mvwprintw(windows->hex_outline, 0, MIN_ADDR_LENGTH+3, " * ");
wnoutrefresh(windows->hex_outline);
}
wmove(win, y, x);
}

/******************************************************\
* Description: prints out the command line help info *
* this function does not return anything*
Expand Down
1 change: 1 addition & 0 deletions src/hexcurse.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ char EBCDIC[256],
bool printHex; /* address format */
bool USE_EBCDIC;
bool IN_HELP; /* if help displayed */
bool saved = TRUE;
int hex_win_width,
ascii_win_width,
hex_outline_width,
Expand Down
34 changes: 10 additions & 24 deletions src/stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,30 @@
\******************************************************************************/
#include "hex.h"

/******************************************************\
* Description: the just sets the stack pointer to *
* NULL; it really has no business being *
* function anyways *
\******************************************************/
void createStack(hexStack *stack)
{
/* calloc() is used because it NULLS out all returned memory */
/* stack = (hexStack *) calloc(1, sizeof(hexStack));
stack->llist = NULL;
stack->prev = NULL;
*/
stack = NULL;
stack->llist = NULL;
stack->prev = NULL;
}

/******************************************************\
* Description: this pushes a structure of type *
* hexStack to the stack. NULL being the*
* initial state of the stack *
\******************************************************/
void pushStack(hexStack **stack, hexStack *tmpStack)
void pushStack(hexStack **stack, off_t cl, int val)
{
hexStack *oldStack;
hexStack *oldStack, *newStack;
oldStack = *stack;

/* calloc() is used because it NULLS out all returned memory */
newStack = (hexStack *) calloc(1, sizeof(hexStack));
newStack->currentLoc = cl;
newStack->savedVal = val;

if (oldStack == NULL) /* begining of stack */
*stack = tmpStack;
*stack = newStack;
else
{
tmpStack->prev = oldStack;
*stack = tmpStack;

newStack->prev = oldStack;
*stack = newStack;
}
}


/******************************************************\
* Description: This function pops a structure off of *
* the stack and then free's it's *
Expand Down