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

Added note and colour tagging functionality #42

Open
wants to merge 3 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
5 changes: 5 additions & 0 deletions display.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ void initCurses(void)
init_pair(1, COLOR_RED, -1); /* null zeros */
init_pair(2, COLOR_GREEN, -1); /* control chars */
init_pair(3, COLOR_BLUE, -1); /* extended chars */
init_pair(5, COLOR_CYAN, -1); /* custom tag color */
init_pair(6, COLOR_MAGENTA, -1); /* custom tag color */
init_pair(7, COLOR_YELLOW, -1); /* custom tag color */
}
init_pair(4, COLOR_BLUE, COLOR_YELLOW); /* current cursor position*/
#endif
Expand Down Expand Up @@ -207,6 +210,7 @@ void display(void)
if (MAX(fileSize, lastEditedLoc)) printw("/0x%llX", (long long) getfilesize());
printw("--%i%%", 100 * (base + cursor + getfilesize()/200) / getfilesize() );
if (mode == bySector) printw("--sector %lld", (long long) ((base + cursor) / SECTOR_SIZE));
if (notes_size >= (base+cursor)) if (notes[base+cursor].note) printw("--note: %s", notes[base+cursor].note);

move(cursor / lineLength, computeCursorXCurrentPos());
}
Expand All @@ -225,6 +229,7 @@ void displayLine(int offset, int max)
#ifdef HAVE_COLORS
(!colored ? 0 :
(cursor == i && hexOrAscii == 0 ? mark_color :
bufferAttr[i] & TAGGED ? 0 :
buffer[i] == 0 ? (int) COLOR_PAIR(1) :
buffer[i] < ' ' ? (int) COLOR_PAIR(2) :
buffer[i] >= 127 ? (int) COLOR_PAIR(3) : 0)
Expand Down
151 changes: 151 additions & 0 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,155 @@ int is_file(char *name)
return stat(name, &st) != -1 && !S_ISDIR(st.st_mode);
}

void openTagFile(void)
{
// tagFile 0 - doesn't exist, 1 - problem, 2 - read only, 3 - read write

char tagFileName[BLOCK_SEARCH_SIZE];
snprintf(tagFileName, BLOCK_SEARCH_SIZE, "%s.tags", fileName);

if (!is_file(tagFileName))
tagFile = 0;
else {
tagfd = fopen(tagFileName, "rw");
if (tagfd != NULL)
tagFile = 3;
else
{
tagfd = fopen(tagFileName, "r");
tagFile = tagfd != NULL ? 2 : 1;
}
}

if (tagfd != NULL && tagFile > 1)
readTagFile();
}

void readTagFile(void)
{
char raw[BLOCK_SEARCH_SIZE], *line, *token;

while (fgets(raw, BLOCK_SEARCH_SIZE, tagfd) != NULL)
{
line = (char*)&raw;
int loc = 0;

token = strsep(&line, " ");
if (strncmp(token, "0x0", BLOCK_SEARCH_SIZE) == 0)
loc = 0;
else
{
loc = (int)strtol(token, NULL, 16);
if ( loc == 0) //Couldn't covert the position, skip the line
continue;
}

token = strsep(&line, " ");
if ( token == NULL ) //Couldn't find a token skip rest of line
continue;

// Note n:
if (strncmp(token,"n:",BLOCK_SEARCH_SIZE) == 0)
{
char *note = strsep(&line, "\n");
if (loc > notes_size)
{
notes_size = loc+NOTE_SIZE;
notes = (noteStruct*) realloc(notes,notes_size*sizeof(noteStruct));
}
notes[loc].note = (char*) malloc(NOTE_SIZE);
memset(notes[loc].note,'\0',NOTE_SIZE);
snprintf(notes[loc].note,NOTE_SIZE,"%s",note);
}

// Colour c: 1-CYAN, 2-MAGENTA, 3-YELLOW
if (strncmp(token,"c:",BLOCK_SEARCH_SIZE) == 0)
{
char *restofline;
int end = 0, color = 0;
int col = (int)strtol(line, &restofline, 10);
if (col < 1 || col > 3) //conversion error or out of range
continue;
if (restofline)
end = (int)strtol(restofline, NULL, 10);

switch (col) {
case 1: color = (int) COLOR_PAIR(5); break;
case 2: color = (int) COLOR_PAIR(6); break;
case 3: color = (int) COLOR_PAIR(7); break;
}
if (end != 0)
for (int i = loc; i < loc+end; i++)
{
int m = bufferAttr[i] & MARKED;
int b = bufferAttr[i] & A_BOLD;
bufferAttr[i] = color;
bufferAttr[i] |= TAGGED;
bufferAttr[i] |= m;
bufferAttr[i] |= b;
}
else
{
int m = bufferAttr[loc] & MARKED;
int b = bufferAttr[loc] & A_BOLD;
bufferAttr[loc] = color;
bufferAttr[loc] |= TAGGED;
bufferAttr[loc] |= m;
bufferAttr[loc] |= b;
}
}
}
}

void writeTagFile(void)
{
if (tagFile == 1 || tagFile == 2) return; // tag file not writeable

if (tagFile == 3 || tagFile == 0) // writeable and open
{
displayOneLineMessage("Write tags to file? (y/N)");
if (tolower(getch()) != 'y') return;

char tagFileName[BLOCK_SEARCH_SIZE];
snprintf(tagFileName, BLOCK_SEARCH_SIZE, "%s.tags", fileName);
tagfd = freopen(tagFileName, "w", tagfd);

int start=0, end=0, col=0, lastcol=0;
for (int i = 0; i < fileSize; i++)
{
if (notes[i].note) fprintf(tagfd, "0x%x n: %s\n", i, notes[i].note);
if (bufferAttr[i] & TAGGED)
{
col = (bufferAttr[i] & COLOR_PAIR(7)) == COLOR_PAIR(7) ? 3 :
(bufferAttr[i] & COLOR_PAIR(6)) == COLOR_PAIR(6) ? 2 :
(bufferAttr[i] & COLOR_PAIR(5)) == COLOR_PAIR(5) ? 1 : 0;

if ( start != 0 && i == end+1 && col == lastcol )
end = i;
if ( start != 0 && i == end+1 && col != lastcol )
{
fprintf(tagfd, "0x%x c: %d %d\n", start, lastcol, 1+end-start);
start = i;
end = i;
}
if ( start == 0 )
{
start = i;
end = i;
}
lastcol = col;
}
else
{
if ( start != 0 && i == end+1 )
{
fprintf(tagfd, "0x%x c: %d %d\n", start, lastcol, 1+end-start);
start = 0;
end = 0;
}
}
}

fclose(tagfd);
}
}
16 changes: 13 additions & 3 deletions hexedit.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ int sizeCopyBuffer, *bufferAttr;
char *progName, *fileName, *baseName;
unsigned char *buffer, *copyBuffer;
typePage *edited;
noteStruct *notes;
size_t notes_size = NOTE_SIZE;
int tagFile = 0;
FILE *tagfd;

char *lastFindFile = NULL, *lastYankToAFile = NULL, *lastAskHexString = NULL, *lastAskAsciiString = NULL, *lastFillWithStringHexa = NULL, *lastFillWithStringAscii = NULL;

char *lastFindFile = NULL, *lastYankToAFile = NULL, *lastAskHexString = NULL, *lastAskAsciiString = NULL, *lastFillWithStringHexa = NULL, *lastFillWithStringAscii = NULL, *lastNote = NULL;

const modeParams modes[LAST] = {
{ 8, 16, 256 },
Expand All @@ -53,6 +56,7 @@ const char * const usage = "usage: %s [-s | --sector] [-m | --maximize] [-l<n> |
/*******************************************************************************/
int main(int argc, char **argv)
{

progName = basename(argv[0]);
argv++; argc--;

Expand Down Expand Up @@ -101,6 +105,7 @@ int main(int argc, char **argv)
openFile();
}
readFile();
openTagFile();
do display();
while (key_to_function(getch()));
quit();
Expand All @@ -119,6 +124,7 @@ void init(void)
hexOrAscii = TRUE;
copyBuffer = NULL;
edited = NULL;
notes = (noteStruct*) calloc(notes_size,sizeof(noteStruct));
}

void quit(void)
Expand All @@ -129,7 +135,11 @@ void quit(void)
free(bufferAttr);
FREE(copyBuffer);
discardEdited();
FREE(lastFindFile); FREE(lastYankToAFile); FREE(lastAskHexString); FREE(lastAskAsciiString); FREE(lastFillWithStringHexa); FREE(lastFillWithStringAscii);
FREE(lastFindFile); FREE(lastYankToAFile); FREE(lastAskHexString); FREE(lastAskAsciiString); FREE(lastFillWithStringHexa); FREE(lastFillWithStringAscii); FREE(lastNote);
for (int i=0; i<notes_size; i++)
FREE(notes[i].note);
free(notes);
fclose(tagfd);
exit(0);
}

Expand Down
15 changes: 14 additions & 1 deletion hexedit.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#define NORMAL A_NORMAL
#define MARKED A_REVERSE
#define MODIFIED A_BOLD
#define TAGGED A_STANDOUT
#define ATTRPRINTW(attr, a) do { if (oldattr != (attr)) attrset(attr), oldattr = (attr); printw a; } while (0)
#define MAXATTRPRINTW(attr, a) do { if (oldattr & (~(attr))) attrset(attr & oldattr), oldattr &= (attr); printw a; } while (0)
#define PRINTW(a) ATTRPRINTW(NORMAL, a)
Expand Down Expand Up @@ -102,7 +103,16 @@ extern char *progName, *fileName, *baseName;
extern unsigned char *buffer, *copyBuffer;
extern typePage *edited;

extern char *lastFindFile, *lastYankToAFile, *lastAskHexString, *lastAskAsciiString, *lastFillWithStringHexa, *lastFillWithStringAscii;
#define NOTE_SIZE 100
typedef struct _noteStruct {
char *note;
} noteStruct;
extern noteStruct *notes;
extern size_t notes_size;
extern int tagFile;
extern FILE *tagfd;

extern char *lastFindFile, *lastYankToAFile, *lastAskHexString, *lastAskAsciiString, *lastFillWithStringHexa, *lastFillWithStringAscii, *lastNote;


/*******************************************************************************/
Expand All @@ -115,6 +125,9 @@ void quit(void);
int tryloc(INT loc);
void openFile(void);
void readFile(void);
void openTagFile(void);
void readTagFile(void);
void writeTagFile(void);
int findFile(void);
int computeLineSize(void);
int computeCursorXCurrentPos(void);
Expand Down
Loading