Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

src/ struct for nhexBuff, several changes to navigation, editing

  • Loading branch information...
commit 5dc6cb3915c0e4510b4af2e7f81c0f28628c1e8d 1 parent 2b110d4
Niels Horn authored November 04, 2010
10  .gitignore
@@ -2,10 +2,20 @@
2 2
 *~
3 3
 .*.swp
4 4
 # Files that will be regenerated
  5
+autom4te.cache/
5 6
 Makefile.in
  7
+Makefile
6 8
 config.h.in
  9
+config.h
7 10
 aclocal.m4
8 11
 configure
  12
+config.log
  13
+config.status
  14
+stamp-h1
  15
+# Binaries, .o files
  16
+src/nhexed
  17
+*.o
  18
+.deps/
9 19
 # Files that are links to distribution files
10 20
 depcomp
11 21
 install-sh
4  src/Makefile.am
@@ -16,5 +16,7 @@
16 16
 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 17
 #
18 18
 bin_PROGRAMS = nhexed
19  
-nhexed_SOURCES = nhexed.c nhexfile.c nhexfile.h
  19
+nhexed_SOURCES = nhexed.c nhexed.h \
  20
+		 nhexfile.c nhexfile.h nhex \
  21
+		 nhexmsg.c nhexmsg.h
20 22
 LDADD = -lncurses
341  src/nhexed.c
@@ -27,15 +27,20 @@
27 27
 #include <ncurses.h>
28 28
 #include <stdlib.h>
29 29
 #include <string.h>
30  
-#include "nhexfile.h"
31 30
 
32  
-#define MINCOLUMNS	43		/* 43=1 chunk, 34 for each extra chunk */
33  
-#define MINROWS		3
  31
+#include "nhexed.h"
  32
+#include "nhexfile.h"
  33
+#include "nhexmsg.h"
34 34
 
35 35
 static	int		iRows;		/* # of rows available for output */
36 36
 static	int		iCols;		/* # of coluns available for output */
37 37
 static	int		iChunks;	/* # of 8-byte chunks per line */
38 38
 
  39
+struct	nhexBuff	nhexFile;
  40
+	//unsigned int	iChangeAddr[MAXCHANGE];
  41
+	//unsigned char	cChangeByte[MAXCHANGE];
  42
+	//int		iChangeCnt=0;
  43
+
39 44
 /* Initialization */
40 45
 void nhexScreenSetup(void)
41 46
 {
@@ -46,7 +51,7 @@ void nhexScreenSetup(void)
46 51
 	getmaxyx(stdscr, y, iCols);
47 52
 
48 53
 	iRows=y-2;			/* -1 for menu, -1 for status */
49  
-	iChunks=(iCols-43)/34+1;
  54
+	iChunks=(iCols-45)/34+1;
50 55
 
51 56
 	mvprintw(0, 0, "%s-v%s",PACKAGE,VERSION);
52 57
 	mvprintw(0, iCols-21, "|Press <Esc> for menu");
@@ -58,41 +63,40 @@ void nhexScreenSetup(void)
58 63
 }
59 64
 
60 65
 /* Show file */
61  
-void nhexScreenShow(FILE *fp, int iOff, unsigned int iFileLength)
  66
+//void nhexScreenShow(FILE *fp, unsigned int iOff, unsigned int iFileLength)
  67
+void nhexScreenShow(struct nhexBuff *nhexFile)
62 68
 {
63  
-	int i=0, j=-1;
64  
-	int k, spacer;
65  
-	int c;
66  
-
67  
-	if(fseek(fp, (long)iOff, SEEK_SET))
68  
-	{
69  
-		printf("** Seek error\n");
70  
-		exit(1);
71  
-	}
  69
+	unsigned int	i=0;
  70
+	int		j=-1;
  71
+	int		k, spacer;
  72
+	unsigned char	c;
  73
+	char		style;
72 74
 
73  
-	for(i=iOff; i < iFileLength; i++)
  75
+	for(i=nhexFile->iOff; i < nhexFile->iFileLength; i++)
74 76
 	{
75 77
 		if(i%(iChunks*8) == 0)		/* new line */
76 78
 		{
77 79
 			j++;
78 80
 			k=0;
79 81
 			spacer=0;
80  
-			mvprintw(j+1, 0, "%06X | ", i);
81  
-			mvprintw(j+1, 9+(iChunks*8)*3+(iChunks-1), "|");
  82
+			mvprintw(j+1, 0, "%08X | ", i);
  83
+			mvprintw(j+1, 11 + (iChunks*8)*3 + (iChunks-1), "|");
82 84
 		}
83 85
 
84  
-		c=getc(fp);
85  
-
86 86
 		if(k%8 == 0 && k != -0)
87 87
 			spacer++;
88 88
 
89  
-		mvprintw(j+1, 9+k*3+spacer, "%02X", c);
90  
-		if(c < 32 || c > 126) c=(int)'.';
91  
-		mvprintw(j+1, 9+(iChunks*8)*3+(iChunks-1)+2+k+spacer, "%c",c);
  89
+		//c=nhexFileReadPos(fp, iChangeAddr, cChangeByte, iChangeCnt, i, &style);
  90
+		c=nhexFileReadPos(nhexFile, i, &style);
  91
+		if(style == 'c') attron(A_REVERSE);
  92
+		mvprintw(j+1, 11+k*3+spacer, "%02X", c);
  93
+		if(c < 32 || c > 126) c='.';
  94
+		mvprintw(j+1, 11+(iChunks*8)*3+(iChunks-1)+2+k+spacer, "%c",c);
  95
+		if(style == 'c') attroff(A_REVERSE);
92 96
 		
93 97
 		k++;
94 98
 		if(k == iChunks*8 && j == iRows-1)
95  
-			i=iFileLength;
  99
+			i=nhexFile->iFileLength;
96 100
 	}
97 101
 
98 102
 	/* clean rest of window */
@@ -102,8 +106,8 @@ void nhexScreenShow(FILE *fp, int iOff, unsigned int iFileLength)
102 106
 		{
103 107
 			if(i%8 == 0)
104 108
 				spacer++;
105  
-			mvprintw(j+1, 9+i*3+spacer, "  ");
106  
-			mvprintw(j+1, 9+(iChunks*8)*3+(iChunks-1)+2+i+spacer," ");
  109
+			mvprintw(j+1, 11+i*3+spacer, "  ");
  110
+			mvprintw(j+1, 11+(iChunks*8)*3+(iChunks-1)+2+i+spacer," ");
107 111
 		}
108 112
 	}
109 113
 	if(j < iRows-1)
@@ -119,26 +123,28 @@ void nhexScreenShow(FILE *fp, int iOff, unsigned int iFileLength)
119 123
 }
120 124
 
121 125
 /* Show details on screen */
122  
-void nhexScreenDetails(char *pFileName, int iOff, int ixPos, int iyPos, bool bPos, bool bHiLo)
  126
+//void nhexScreenDetails(FILE *fp, char *pFileName, unsigned int iOff, int ixPos, int iyPos, bool bPos, bool bHiLo)
  127
+void nhexScreenDetails(struct nhexBuff *nhexFile)
123 128
 {
124  
-	char sType[6];
125  
-	int iPos;
126  
-	int iChunkPos;
127  
-	int ixHexPos, ixAscPos, ixCurPos;
128  
-
129  
-	iPos=iOff+iyPos*8*iChunks+ixPos;
130  
-	iChunkPos=ixPos/8;
131  
-	ixHexPos=9+ixPos*3+iChunkPos;
132  
-	ixAscPos=9+(iChunks*8)*3+(iChunks-1)+2+ixPos+iChunkPos;
133  
-
134  
-	if(bPos)
  129
+	char		sType[6], style;
  130
+	unsigned char	c;
  131
+	unsigned int	iPos;
  132
+	int		iChunkPos;
  133
+	int		ixHexPos, ixAscPos, ixCurPos;
  134
+
  135
+	iPos=nhexFile->iOff + nhexFile->iyPos * 8 * iChunks + nhexFile->ixPos;
  136
+	iChunkPos=nhexFile->ixPos / 8;
  137
+	ixHexPos=11+nhexFile->ixPos * 3 + iChunkPos;
  138
+	ixAscPos=11+(iChunks*8) * 3 + (iChunks-1) + 2 + nhexFile->ixPos+iChunkPos;
  139
+
  140
+	if(nhexFile->bPos)
135 141
 	{
136 142
 		strcpy(sType, "ASCII");
137 143
 		ixCurPos=ixAscPos;
138 144
 	}
139 145
 	else
140 146
 	{
141  
-		if (bHiLo)
  147
+		if (nhexFile->bHiLo)
142 148
 		{
143 149
 			strcpy(sType, "HEX-L");
144 150
 			ixCurPos=ixHexPos+1;
@@ -152,32 +158,50 @@ void nhexScreenDetails(char *pFileName, int iOff, int ixPos, int iyPos, bool bPo
152 158
 
153 159
 	/* show filename, pos & area */
154 160
 	attron(A_REVERSE);
155  
-	mvprintw(iRows+1,0,"%s",pFileName);
156  
-	mvprintw(iRows+1,iCols-6,"|%s",sType);
157  
-	mvprintw(iRows+1,iCols-6-16,"|%08i/%06X", iPos, iPos);
  161
+	mvprintw(iRows+1, 0, "%s", nhexFile->sFileName);
  162
+	mvprintw(iRows+1, iCols-6, "|%s",sType);
  163
+	mvprintw(iRows+1, iCols-22, "|%08i/%06X", iPos, iPos);
  164
+	mvprintw(iRows+1, iCols-27, "|%04i", nhexFile->iChangeCnt);
158 165
 	attroff(A_REVERSE);
159 166
 
160 167
 	/* show edit-position */
161  
-	mvchgat(iyPos+1, ixHexPos, 2, A_BOLD | A_UNDERLINE, 0, NULL);
162  
-	mvchgat(iyPos+1, ixAscPos, 1, A_BOLD | A_UNDERLINE, 0, NULL);
  168
+	//c=nhexFileReadPos(fp, iChangeAddr, cChangeByte, iChangeCnt, iPos, &style);
  169
+	c=nhexFileReadPos(nhexFile, iPos, &style);
  170
+	if(style == 'c') attron(A_REVERSE);
  171
+	attron(A_BOLD | A_UNDERLINE);
  172
+	mvprintw(nhexFile->iyPos + 1, ixHexPos, "%02X",c);
  173
+	if(c < 32 || c > 126) c='.';
  174
+	mvprintw(nhexFile->iyPos + 1, ixAscPos, "%c", c);
  175
+	attroff(A_BOLD | A_UNDERLINE);
  176
+	if(style == 'c') attroff(A_REVERSE);
163 177
 	refresh();
164 178
 
165 179
 	/* set cursor on right position */
166  
-	move(iyPos+1,ixCurPos);
  180
+	move(nhexFile->iyPos + 1,ixCurPos);
167 181
 	return;
168 182
 }
169 183
 
170 184
 /* reset screen attributes on ixPos / iyPos */
171  
-void nhexScreenDetReset(int ixPos, int iyPos)
  185
+//void nhexScreenDetReset(FILE *fp, unsigned int iOff, int ixPos, int iyPos)
  186
+void nhexScreenDetReset(struct nhexBuff *nhexFile)
172 187
 {
173  
-	int iChunkPos, ixHexPos, ixAscPos;
174  
-
175  
-	iChunkPos=ixPos/8;
176  
-	ixHexPos=9+ixPos*3+iChunkPos;
177  
-	ixAscPos=9+(iChunks*8)*3+(iChunks-1)+2+ixPos+iChunkPos;
178  
-
179  
-	mvchgat(iyPos+1, ixHexPos, 2, 0, 0, NULL);
180  
-	mvchgat(iyPos+1, ixAscPos, 2, 0, 0, NULL);
  188
+	unsigned int	iPos;
  189
+	int		iChunkPos, ixHexPos, ixAscPos;
  190
+	unsigned char	c;
  191
+	char		style;
  192
+
  193
+	iPos=nhexFile->iOff + nhexFile->iyPos * 8 * iChunks + nhexFile->ixPos;
  194
+	iChunkPos=nhexFile->ixPos/8;
  195
+	ixHexPos=11 + nhexFile->ixPos * 3 + iChunkPos;
  196
+	ixAscPos=11 + (iChunks*8) * 3 + (iChunks-1) + 2 + nhexFile->ixPos + iChunkPos;
  197
+
  198
+	//c=nhexFileReadPos(fp, iChangeAddr, cChangeByte, iChangeCnt, iPos, &style);
  199
+	c=nhexFileReadPos(nhexFile, iPos, &style);
  200
+	if(style == 'c') attron(A_REVERSE);
  201
+	mvprintw(nhexFile->iyPos + 1, ixHexPos, "%02X",c);
  202
+	if(c < 32 || c > 126) c='.';
  203
+	mvprintw(nhexFile->iyPos + 1, ixAscPos, "%c", c);
  204
+	if(style == 'c') attroff(A_REVERSE);
181 205
 	
182 206
 	return;
183 207
 }
@@ -216,17 +240,19 @@ int nhexSanityCheck(void)
216 240
 /* main entry point */
217 241
 int main(int argc, char *argv[])
218 242
 {
219  
-	FILE		*fp;
220  
-	unsigned int 	iFileLength;
221  
-	int 		iOff=0;			/* Offset first byte on screen from file */
222  
-	int 		ixPos=0, iyPos=0;	/* Position of 'cursor' */
223  
-	bool 		bPos=false;		/* false = hex / true = ascii */
224  
-	bool 		bHiLo=false;		/* false = High / true = Low */
225  
-	//bool bNeedSave=false;
226  
-	char 		sFileName[MAXFILENAME];
  243
+	//FILE		*fp;
  244
+	//unsigned int 	iFileLength, iOff=0;	/* Offset first byte on screen from file */
  245
+	//int 		ixPos=0, iyPos=0;	/* Position of 'cursor' */
  246
+	//bool 		bPos=false;		/* false = hex / true = ascii */
  247
+	//bool 		bHiLo=false;		/* false = High / true = Low */
  248
+	//char 		sFileName[MAXFILENAME];
  249
+	struct nhexBuff	nhexFile;
227 250
 	int 		c;
228 251
 	bool 		ready, scrUpdate, scrRedraw;
229 252
 
  253
+	unsigned char	cTmp;
  254
+	char		style;
  255
+
230 256
 	if(argc != 2)
231 257
 	{
232 258
 		printf("Usage: %s <file_to_edit>\n", argv[0]);
@@ -238,29 +264,30 @@ int main(int argc, char *argv[])
238 264
 		printf("** Filename too long (max=%i)",MAXFILENAME);
239 265
 		exit(1);
240 266
 	}
241  
-	strcpy(sFileName,argv[1]);
  267
+	strcpy(nhexFile.sFileName,argv[1]);
242 268
 
243 269
 	if(nhexSanityCheck())
244 270
 	{
245 271
 		exit(1);
246 272
 	}
247 273
 
248  
-	fp=nhexFileReadOpen(sFileName, &iFileLength);
249  
-	if (fp == NULL)
  274
+	nhexFile.fp=nhexFileReadOpen(nhexFile.sFileName, &nhexFile.iFileLength);
  275
+	if (nhexFile.fp == NULL)
250 276
 	{
251 277
 		//perror("Cannot open file");
252 278
 		printf("** Error opening file\n");
253 279
 		exit(1);
254 280
 	}
255 281
 
256  
-	iOff=0;				/* initial position */
257  
-	ixPos=0;
258  
-	iyPos=0;
259  
-	bPos=false;
  282
+	nhexFile.iOff=0;				/* initial position */
  283
+	nhexFile.ixPos=0;
  284
+	nhexFile.iyPos=0;
  285
+	nhexFile.bPos=false;
  286
+	nhexFile.bHiLo=false;
260 287
 	
261 288
 	nhexScreenSetup();
262  
-	nhexScreenShow(fp, iOff, iFileLength);
263  
-	nhexScreenDetails(sFileName, iOff, ixPos, iyPos, bPos, bHiLo);
  289
+	nhexScreenShow(&nhexFile);
  290
+	nhexScreenDetails(&nhexFile);
264 291
 
265 292
 	/* Main loop */
266 293
 	noecho();
@@ -274,120 +301,129 @@ int main(int argc, char *argv[])
274 301
 		scrRedraw=false;
275 302
 		switch(c)
276 303
 		{	case KEY_UP:
277  
-				if(iyPos == 0)
  304
+				if(nhexFile.iyPos == 0)
278 305
 				{
279  
-					if(iOff >= iChunks*8)
  306
+					if(nhexFile.iOff >= iChunks*8)
280 307
 					{
281  
-						iOff-=iChunks*8;
  308
+						nhexFile.iOff-=iChunks*8;
282 309
 						scrRedraw=true;
283 310
 					}
284 311
 				}
285 312
 				else
286 313
 				{
287  
-					nhexScreenDetReset(ixPos, iyPos);
288  
-					iyPos--;
  314
+					nhexScreenDetReset(&nhexFile);
  315
+					nhexFile.iyPos--;
289 316
 					scrUpdate=true;
  317
+					nhexFile.bHiLo=false;
290 318
 				}
291 319
 				break;
292 320
 			case KEY_DOWN:
293  
-				if(iOff+(iyPos+1)*iChunks*8+ixPos <= iFileLength-1)
  321
+				if(nhexFile.iOff + (nhexFile.iyPos+1) * iChunks*8 + nhexFile.ixPos <= nhexFile.iFileLength - 1)
294 322
 				{
295  
-					if(iyPos == iRows-1)
  323
+					if(nhexFile.iyPos == iRows-1)
296 324
 					{
297  
-						iOff+=iChunks*8;
  325
+						nhexFile.iOff+=iChunks*8;
298 326
 						scrRedraw=true;
299 327
 					}
300 328
 					else
301 329
 					{
302  
-						nhexScreenDetReset(ixPos, iyPos);
303  
-						iyPos++;
  330
+						nhexScreenDetReset(&nhexFile);
  331
+						nhexFile.iyPos++;
304 332
 						scrUpdate=true;
  333
+						nhexFile.bHiLo=false;
305 334
 					}
306 335
 				}
307 336
 				break;		
308 337
 			case KEY_LEFT:
309  
-				if(ixPos > 0)
  338
+				if(nhexFile.ixPos > 0)
310 339
 				{
311  
-					nhexScreenDetReset(ixPos, iyPos);
312  
-					ixPos--;
  340
+					nhexScreenDetReset(&nhexFile);
  341
+					nhexFile.ixPos--;
313 342
 					scrUpdate=true;
  343
+					nhexFile.bHiLo=false;
314 344
 				}
315 345
 				break;
316 346
 			case KEY_RIGHT:
317  
-				if(iOff+iyPos*iChunks*8+ixPos+1 <= iFileLength-1)
  347
+				if(nhexFile.iOff + nhexFile.iyPos * iChunks*8 + nhexFile.ixPos + 1 <= nhexFile.iFileLength - 1)
318 348
 				{
319  
-					nhexScreenDetReset(ixPos, iyPos);
320  
-					ixPos++;
  349
+					nhexScreenDetReset(&nhexFile);
  350
+					nhexFile.ixPos++;
321 351
 					scrUpdate=true;
322  
-					if(ixPos == iChunks*8)
  352
+					nhexFile.bHiLo=false;
  353
+					if(nhexFile.ixPos == iChunks*8)
323 354
 					{
324  
-						ixPos=0;
325  
-						if(iyPos == iRows-1)
  355
+						nhexFile.ixPos=0;
  356
+						if(nhexFile.iyPos == iRows-1)
326 357
 						{
327  
-							iOff+=iChunks*8;
  358
+							nhexFile.iOff+=iChunks*8;
328 359
 							scrRedraw=true;
329 360
 						}
330 361
 						else
331 362
 						{
332  
-							iyPos++;
  363
+							nhexFile.iyPos++;
333 364
 						}
334 365
 					}
335 366
 				}
336 367
 				break;
337 368
 			case KEY_NPAGE:
338  
-				if(iOff+iRows*iChunks*8 < iFileLength)
  369
+				if(nhexFile.iOff + iRows * iChunks*8 < nhexFile.iFileLength)
339 370
 				{
340  
-					iOff+=iRows*iChunks*8;
341  
-					if(iOff+iyPos*iChunks*8+ixPos > iFileLength-1)
  371
+					nhexFile.iOff+=iRows * iChunks*8;
  372
+					if(nhexFile.iOff + nhexFile.iyPos * iChunks*8 + nhexFile.ixPos > nhexFile.iFileLength - 1)
342 373
 					{
343  
-						iyPos=(iFileLength-1-iOff)/(iChunks*8);
344  
-						if(ixPos > (iFileLength-1)%(iChunks*8))
345  
-							ixPos=(iFileLength-1)%(iChunks*8);
  374
+						nhexFile.iyPos=(nhexFile.iFileLength-1 - nhexFile.iOff) / (iChunks*8);
  375
+						if(nhexFile.ixPos > (nhexFile.iFileLength - 1) % (iChunks*8))
  376
+							nhexFile.ixPos=(nhexFile.iFileLength - 1) % (iChunks*8);
346 377
 					}
347 378
 					scrRedraw=true;
348 379
 				}
349 380
 				break;
350 381
 			case KEY_PPAGE:
351  
-				if(iOff > 0)
  382
+				if(nhexFile.iOff > 0)
352 383
 				{
353  
-					iOff-=iRows*iChunks*8;
354  
-					if(iOff < 0)
  384
+					if(nhexFile.iOff >= iRows*iChunks*8)
  385
+					{
  386
+						nhexFile.iOff-=iRows*iChunks*8;
  387
+					}
  388
+					else
355 389
 					{
356  
-						iOff=0;
357  
-						iyPos=0;
  390
+						nhexFile.iOff=0;
  391
+						nhexFile.iyPos=0;
358 392
 					}
359 393
 					scrRedraw=true;
360 394
 				}
361 395
 				break;
362 396
 			case KEY_SLEFT:
363  
-				nhexScreenDetReset(ixPos, iyPos);
364  
-				ixPos=0;
  397
+				nhexScreenDetReset(&nhexFile);
  398
+				nhexFile.ixPos=0;
365 399
 				scrUpdate=true;
  400
+				nhexFile.bHiLo=false;
366 401
 				break;
367 402
 			case KEY_SRIGHT:
368  
-				nhexScreenDetReset(ixPos, iyPos);
369  
-				ixPos=iChunks*8-1;
370  
-				if(iOff+iyPos*iChunks*8+ixPos > iFileLength)
371  
-					ixPos=(iFileLength-1)%(iChunks*8);
  403
+				nhexScreenDetReset(&nhexFile);
  404
+				nhexFile.ixPos=iChunks*8 - 1;
  405
+				if(nhexFile.iOff + nhexFile.iyPos * iChunks*8 + nhexFile.ixPos > nhexFile.iFileLength)
  406
+					nhexFile.ixPos=(nhexFile.iFileLength-1) % (iChunks*8);
372 407
 				scrUpdate=true;
  408
+				nhexFile.bHiLo=false;
373 409
 				break;
374 410
 			case KEY_HOME:
375  
-				iOff=0;
376  
-				ixPos=0;
377  
-				iyPos=0;
  411
+				nhexFile.iOff=0;
  412
+				nhexFile.ixPos=0;
  413
+				nhexFile.iyPos=0;
378 414
 				scrRedraw=true;
379 415
 				break;
380 416
 			case KEY_END:
381  
-				iOff=(iFileLength-1)/(iChunks*8);
382  
-				iOff=(iOff-iRows+1)*iChunks*8;
383  
-				if(iOff < 0) iOff=0;
384  
-				iyPos=(iFileLength-1-iOff)/(iChunks*8);
385  
-				ixPos=(iFileLength-1)%(iChunks*8);
  417
+				nhexFile.iOff=(nhexFile.iFileLength-1) / (iChunks*8);
  418
+				nhexFile.iOff=(nhexFile.iOff - iRows+1) * iChunks*8;
  419
+				if(nhexFile.iOff < 0) nhexFile.iOff=0;
  420
+				nhexFile.iyPos=(nhexFile.iFileLength-1 - nhexFile.iOff) / (iChunks*8);
  421
+				nhexFile.ixPos=(nhexFile.iFileLength-1) % (iChunks*8);
386 422
 				scrRedraw=true;
387 423
 				break;
388 424
 			case 9:
389  
-				bPos=!bPos;
390  
-				bHiLo=false;
  425
+				nhexFile.bPos=!nhexFile.bPos;
  426
+				nhexFile.bHiLo=false;
391 427
 				scrUpdate=true;
392 428
 			case 27:
393 429
 				/* menu */
@@ -397,18 +433,77 @@ int main(int argc, char *argv[])
397 433
 				break;
398 434
 			default:
399 435
 				/* 'normal' character */
400  
-				// check for validity hex / ascii
401  
-				mvprintw(iRows+1,20,"%d",c);
  436
+				if(!nhexFile.bPos)
  437
+				{
  438
+					/* hex editing */
  439
+					if(c >= '0' && c<= '9')
  440
+					{
  441
+						cTmp=c-48;
  442
+					}
  443
+					else
  444
+					{
  445
+						c=c & 95;
  446
+						if(c>='A' && c<='F')
  447
+						{
  448
+							cTmp=c-55;
  449
+						}
  450
+						else
  451
+							break;
  452
+					}
  453
+					if(!nhexFile.bHiLo)
  454
+					{
  455
+						cTmp=cTmp*16;
  456
+						if(nhexFile.iChangeCnt >= MAXCHANGE)
  457
+						{
  458
+							//nhexMsg(NHMSGERR + NHMSGOK, "Maximum number of changes reached");
  459
+							nhexMsg(257, "Maximum number of changes reached");
  460
+							break;
  461
+						}
  462
+						/* calculate address & change high bits */
  463
+						nhexFile.iChangeAddr[nhexFile.iChangeCnt]=nhexFile.iOff + nhexFile.iyPos * iChunks*8 + nhexFile.ixPos;
  464
+						nhexFile.cChangeByte[nhexFile.iChangeCnt]=(nhexFileReadPos(&nhexFile, \
  465
+									nhexFile.iChangeAddr[nhexFile.iChangeCnt], &style) & 15) + cTmp;
  466
+						nhexFile.iChangeCnt++;
  467
+						nhexFile.bHiLo=true;
  468
+						scrUpdate=true;
  469
+					}
  470
+					else
  471
+					{
  472
+						/* change low bits of last change */
  473
+						nhexFile.cChangeByte[nhexFile.iChangeCnt-1]=(nhexFile.cChangeByte[nhexFile.iChangeCnt-1] & 240) + cTmp;
  474
+						nhexFile.bHiLo=false;
  475
+						scrUpdate=true;
  476
+					}
  477
+				}
  478
+				else
  479
+				{
  480
+					/* ascii editing */
  481
+					if(c>=32 && c<=126)
  482
+					{
  483
+						if(nhexFile.iChangeCnt >= MAXCHANGE)
  484
+						{
  485
+							nhexMsg(257, "Maximum number of changes reached");
  486
+							break;
  487
+						}
  488
+						nhexFile.iChangeAddr[nhexFile.iChangeCnt]=nhexFile.iOff + nhexFile.iyPos * iChunks*8 + nhexFile.ixPos;
  489
+						nhexFile.cChangeByte[nhexFile.iChangeCnt]=c;
  490
+						nhexFile.iChangeCnt++;
  491
+						scrUpdate=true;
  492
+					}
  493
+				}
402 494
 				break;
403 495
 		}
404 496
 		if(scrRedraw)
405  
-			nhexScreenShow(fp, iOff, iFileLength);
  497
+		{
  498
+			nhexScreenShow(&nhexFile);
  499
+			nhexFile.bHiLo=false;
  500
+		}
406 501
 		if(scrUpdate || scrRedraw)
407  
-			nhexScreenDetails(sFileName, iOff, ixPos, iyPos, bPos, bHiLo);
  502
+			nhexScreenDetails(&nhexFile);
408 503
 		if(ready) break;
409 504
 	}
410 505
 
411  
-	fclose(fp);
  506
+	fclose(nhexFile.fp);
412 507
 
413 508
 	nhexCleanup();
414 509
 	return 0;
41  src/nhexed.h
... ...
@@ -0,0 +1,41 @@
  1
+/*
  2
+ * nhexed.h - general header for nHex-Ed
  3
+ *
  4
+ * Copyright (C) 2010 Niels Horn <niels.horn@gmail.com>
  5
+ *
  6
+ * This program is free software; you can redistribute it and/or 
  7
+ * modify it under the terms of the GNU General Public License 
  8
+ * as published by the Free Software Foundation; either 
  9
+ * version 2 of the License, or (at your option) any later
  10
+ * version.
  11
+ *
  12
+ * This program is distributed in the hope that it will be useful,
  13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15
+ * GNU General Public License for more details.
  16
+ *
  17
+ * You should have received a copy of the GNU General Public License
  18
+ * along with this program; if not, write to the Free Software
  19
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20
+ *
  21
+ */
  22
+
  23
+#define MINCOLUMNS	45		/* 45=1 chunk, 34 for each extra chunk */
  24
+#define MINROWS		3		/* minimal number of rows on screen */
  25
+#define MAXLENGTH	4294967295	/* max file length 4GB for uint */
  26
+#define MAXFILENAME	256		/* max length of filename */
  27
+#define MAXCHANGE	10		/* maximum number of changes allowed */
  28
+
  29
+struct	nhexBuff {
  30
+	FILE		*fp;			/* FILE pointer */
  31
+	char		sFileName[MAXFILENAME];	/* complete path+filename */
  32
+	unsigned int	iFileLength;		/* length of file */
  33
+	unsigned int	iOff;			/* position in file of 1st pos on screen */
  34
+	int		ixPos, iyPos;		/* current x/y pos on screen */
  35
+	bool		bPos;			/* position hex(=false)/asc(=true) */
  36
+	bool		bHiLo;			/* hi(=false)/lo(=true) nibble */
  37
+	unsigned int	iChangeAddr[MAXCHANGE];	/* array of addresses that have changed */
  38
+	unsigned char	cChangeByte[MAXCHANGE];	/* array of changed values */
  39
+	int		iChangeCnt;		/* number of changes entered */
  40
+};
  41
+
50  src/nhexfile.c
@@ -22,18 +22,22 @@
22 22
 
23 23
 #include <stdlib.h>
24 24
 #include <stdio.h>
25  
-#include "nhexfile.h"
  25
+#include <stdbool.h>
26 26
 
27  
-#define DEBUG		1
  27
+#include "nhexed.h"
  28
+#include "nhexfile.h"
  29
+#include "nhexmsg.h"
28 30
 
  31
+/* Open file, return iFileLength & *fp */
29 32
 FILE* nhexFileReadOpen(char *pFileName, unsigned int *iFileLength)
30 33
 {
31 34
 	FILE *fp;
32 35
 
33  
-	fp = fopen(pFileName, "r");
  36
+	fp = fopen(pFileName, "r+b");
34 37
 	if(fp == NULL)
35 38
 	{
36  
-		printf("** Cannot open file\n");
  39
+		//nhexMsg(NHMSGERR + NHMSGOK, "Cannot open file");
  40
+		nhexMsg(257, "Cannot open file");
37 41
 		return NULL;
38 42
 	}
39 43
 	
@@ -48,3 +52,41 @@ FILE* nhexFileReadOpen(char *pFileName, unsigned int *iFileLength)
48 52
 
49 53
 	return fp;
50 54
 }
  55
+
  56
+/* Return byte from file (or changes) */
  57
+//char nhexFileReadPos(FILE *fp, unsigned int iChangeAddr[], unsigned char cChangeByte[], int iChangeCnt, unsigned int iAddr, char *style)
  58
+char nhexFileReadPos(struct nhexBuff *nhexFile, unsigned int iAddr, char *style)
  59
+{
  60
+	int		i;
  61
+	unsigned char	c;
  62
+	bool		bChange=false;
  63
+
  64
+	*style='n';
  65
+
  66
+	/* search in list of changes first */
  67
+	for(i=nhexFile->iChangeCnt-1; i>=0; i--)
  68
+	{
  69
+		if(iAddr == nhexFile->iChangeAddr[i])
  70
+		{
  71
+			c=nhexFile->cChangeByte[i];
  72
+			bChange=true;
  73
+			break;
  74
+		}
  75
+	}
  76
+
  77
+	if(!bChange)
  78
+	{
  79
+		if(fseek(nhexFile->fp, (long)iAddr, SEEK_SET))
  80
+		{
  81
+			//nhexMsg(NHMSGERR + NHMSGOK,"Seek error");
  82
+			nhexMsg(257, "Seek error");
  83
+			exit(1);
  84
+		}
  85
+		c=getc(nhexFile->fp);
  86
+	}
  87
+
  88
+	if(bChange) *style='c';
  89
+	
  90
+	return c;
  91
+}
  92
+
6  src/nhexfile.h
@@ -20,7 +20,7 @@
20 20
  *
21 21
  */
22 22
 
23  
-#define MAXLENGTH	4294967295	/* max file length 4GB for uint */
24  
-#define MAXFILENAME	256		/* max length of filename */
25  
-
26 23
 FILE* nhexFileReadOpen(char *pFileName, unsigned int *iFileLength);
  24
+//char nhexFileReadPos(FILE *fp, unsigned int iChangeAddr[], unsigned char cChangeByte[], int iChangeCnt, unsigned int iAddr, char *style);
  25
+char nhexFileReadPos(struct nhexBuff *nhexFile, unsigned int iAddr, char *style);
  26
+
32  src/nhexmsg.c
... ...
@@ -0,0 +1,32 @@
  1
+/*
  2
+ * nhexmsg.c - message routines for nHex-Ed
  3
+ *
  4
+ * Copyright (C) 2010 Niels Horn <niels.horn@gmail.com>
  5
+ *
  6
+ * This program is free software; you can redistribute it and/or 
  7
+ * modify it under the terms of the GNU General Public License 
  8
+ * as published by the Free Software Foundation; either 
  9
+ * version 2 of the License, or (at your option) any later
  10
+ * version.
  11
+ *
  12
+ * This program is distributed in the hope that it will be useful,
  13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15
+ * GNU General Public License for more details.
  16
+ *
  17
+ * You should have received a copy of the GNU General Public License
  18
+ * along with this program; if not, write to the Free Software
  19
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20
+ *
  21
+ */
  22
+
  23
+#include <stdio.h>
  24
+
  25
+/* show popup message */
  26
+int nhexMsg(int flags, char *msg)
  27
+{
  28
+	/* for now just to a printf */
  29
+	printf("%d - %s",flags, msg);
  30
+
  31
+	return 0;
  32
+}	
32  src/nhexmsg.h
... ...
@@ -0,0 +1,32 @@
  1
+/*
  2
+ * nhexmsg.h - header for message routines for nHex-Ed
  3
+ *
  4
+ * Copyright (C) 2010 Niels Horn <niels.horn@gmail.com>
  5
+ *
  6
+ * This program is free software; you can redistribute it and/or 
  7
+ * modify it under the terms of the GNU General Public License 
  8
+ * as published by the Free Software Foundation; either 
  9
+ * version 2 of the License, or (at your option) any later
  10
+ * version.
  11
+ *
  12
+ * This program is distributed in the hope that it will be useful,
  13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15
+ * GNU General Public License for more details.
  16
+ *
  17
+ * You should have received a copy of the GNU General Public License
  18
+ * along with this program; if not, write to the Free Software
  19
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20
+ *
  21
+ */
  22
+
  23
+/* types of messages */
  24
+#define NHMSGERR	256;
  25
+#define NHMSGWARN	512;
  26
+
  27
+/* "buttons" to include */
  28
+#define NHMSGOK		1;
  29
+#define NHMSGCANCEL	2;
  30
+
  31
+int nhexMsg(int flags, char *msg);
  32
+

0 notes on commit 5dc6cb3

Please sign in to comment.
Something went wrong with that request. Please try again.