remove another useless file
[rofl0r-hexedit0r.git] / mark.c
blob9d3f32930516928465c1e9edf0e5365b8d0e425e
1 /* hexedit -- Hexadecimal Editor for Binary Files
2 Copyright (C) 1998 Pixel (Pascal Rigaux)
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/
17 #include "hexedit.h"
19 /*******************************************************************************/
20 /* Mark functions */
21 /*******************************************************************************/
22 void markRegion(INT a, INT b) { int i; for (i = MAX(a - base, 0); i <= MIN(b - base, nbBytes - 1); i++) markIt(i); }
23 void unmarkRegion(INT a, INT b) { int i; for (i = MAX(a - base, 0); i <= MIN(b - base, nbBytes - 1); i++) unmarkIt(i); }
24 void markSelectedRegion(void) { markRegion(mark_min, mark_max); }
25 void unmarkAll(void) { unmarkRegion(base, base + nbBytes - 1); }
26 void markIt(int i) { bufferAttr[i] |= MARKED; }
27 void unmarkIt(int i) { bufferAttr[i] &= ~MARKED; }
29 void copy_region(void)
31 typePage *p;
33 if (!mark_set) { displayMessageAndWaitForKey("Nothing to copy"); return; }
34 sizeCopyBuffer = mark_max - mark_min + 1;
35 if (sizeCopyBuffer == 0) return;
36 if (sizeCopyBuffer > BIGGEST_COPYING) {
37 displayTwoLineMessage("Hey, don't you think that's too big?!", "Really copy (Yes/No)");
38 if (tolower(getch()) != 'y') return;
40 FREE(copyBuffer);
41 if ((copyBuffer = malloc(sizeCopyBuffer)) == NULL) {
42 displayMessageAndWaitForKey("Can't allocate that much memory");
43 return;
45 if (LSEEK_(fd, mark_min) == -1 || read(fd, copyBuffer, sizeCopyBuffer) == -1) {
46 displayMessageAndWaitForKey(strerror(errno));
47 return;
50 for (p = edited; p; p = p->next) {
51 if (mark_min < p->base + p->size && p->base <= mark_max) {
52 INT min = MIN(p->base, mark_min);
53 memcpy(copyBuffer + p->base - min,
54 p->vals + mark_min - min,
55 MIN(p->base + p->size, mark_max) - MAX(p->base, mark_min) + 1);
58 unmarkAll();
59 mark_set = FALSE;
62 void yank(void)
64 if (copyBuffer == NULL) { displayMessageAndWaitForKey("Nothing to paste"); return; }
65 if (isReadOnly) { displayMessageAndWaitForKey("File is read-only!"); return; }
66 addToEdited(base + cursor, sizeCopyBuffer, copyBuffer);
67 readFile();
70 void yank_to_a_file(void)
72 char tmp[BLOCK_SEARCH_SIZE];
73 int f;
75 if (copyBuffer == NULL) { displayMessageAndWaitForKey("Nothing to paste"); return; }
77 if (!displayMessageAndGetString("File name: ", &lastYankToAFile, tmp, sizeof(tmp))) return;
79 if ((f = open(tmp, O_RDONLY)) != -1) {
80 close(f);
81 displayTwoLineMessage("File exists", "Overwrite it (Yes/No)");
82 if (tolower(getch()) != 'y') return;
84 if ((f = creat(tmp, 0666)) == -1 || write(f, copyBuffer, sizeCopyBuffer) == -1) {
85 displayMessageAndWaitForKey(strerror(errno));
86 return;
89 close(f);
92 void fill_with_string(void)
94 char *msg = hexOrAscii ? "Hexa string to fill with: " : "Ascii string to fill with: ";
95 char **last = hexOrAscii ? &lastFillWithStringHexa : &lastFillWithStringAscii;
96 char tmp2[BLOCK_SEARCH_SIZE];
97 unsigned char *tmp1;
98 int i, l1, l2;
100 if (!mark_set) return;
101 if (isReadOnly) { displayMessageAndWaitForKey("File is read-only!"); return; }
102 if (sizeCopyBuffer > BIGGEST_COPYING) {
103 displayTwoLineMessage("Hey, don't you think that's too big?!", "Really fill (Yes/No)");
104 if (tolower(getch()) != 'y') return;
106 if (!displayMessageAndGetString(msg, last, tmp2, sizeof(tmp2))) return;
107 l1 = mark_max - mark_min + 1;
108 l2 = strlen(tmp2);
109 if (hexOrAscii) {
110 if (strlen(tmp2) == 1) {
111 if (!isxdigit(*tmp2)) { displayMessageAndWaitForKey("Invalid hexa string"); return; }
112 *tmp2 = hexCharToInt(*tmp2);
113 } else if (!hexStringToBinString(tmp2, &l2)) return;
115 tmp1 = malloc(l1);
116 for (i = 0; i < l1 - l2 + 1; i += l2) memcpy(tmp1 + i, tmp2, l2);
117 memcpy(tmp1 + i, tmp2, l1 - i);
118 addToEdited(mark_min, l1, tmp1);
119 readFile();
120 free(tmp1);
124 void updateMarked(void)
126 if (base + cursor > oldbase + oldcursor) {
128 if (mark_min == mark_max) {
129 mark_max = base + cursor;
130 } else if (oldbase + oldcursor == mark_min) {
131 if (base + cursor <= mark_max) {
132 mark_min = base + cursor;
133 unmarkRegion(oldbase + oldcursor, mark_min - 1);
134 } else {
135 unmarkRegion(oldbase + oldcursor, mark_max);
136 mark_min = mark_max;
137 mark_max = base + cursor;
139 } else {
140 mark_max = base + cursor;
143 } else if (base + cursor < oldbase + oldcursor){
144 if (mark_min == mark_max) {
145 mark_min = base + cursor;
146 } else if (oldbase + oldcursor == mark_max) {
147 if (base + cursor >= mark_min) {
148 mark_max = base + cursor;
149 unmarkRegion(mark_max + 1, oldbase + oldcursor);
150 } else {
151 unmarkRegion(mark_min, oldbase + oldcursor);
152 markRegion(base + cursor, mark_min - 1);
153 mark_max = mark_min;
154 mark_min = base + cursor;
156 } else {
157 mark_min = base + cursor;
160 if (mark_max >= getfilesize()) mark_max = getfilesize() - 1;
161 markSelectedRegion();