File: search.c

package info (click to toggle)
hexedit 1.0.0-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 232 kB
  • ctags: 194
  • sloc: ansic: 1,406; sh: 152; makefile: 68
file content (101 lines) | stat: -rw-r--r-- 3,211 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* hexedit -- Hexadecimal Editor for Binary Files
   Copyright (C) 1998 Pixel (Pascal Rigaux)

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/
#include "hexedit.h"

static int searchA(char **string, int *sizea, char *tmp);
static void searchB(int quit, char *string);

/*******************************************************************************/
/* Search functions */
/*******************************************************************************/
static int searchA(char **string, int *sizea, char *tmp)
{
  char *msg = hexOrAscii ? "Hexa string to search: (" : "Ascii string to search: (";
  char **last = hexOrAscii ? &lastAskHexString : &lastAskAsciiString;

  if (!ask_about_save_and_redisplay()) return FALSE;
  if (!displayMessageAndGetString(msg, last, tmp)) return FALSE;

  *sizea = strlen(tmp);
  if (hexOrAscii) if (!hexStringToBinString(tmp, sizea)) return FALSE;

  *string = strdup(tmp);
  *sizea = strlen(*string);

  nodelay(stdscr, TRUE);
  displayTwoLineMessage("searching...", "(press any key to cancel)");
  return TRUE;
}

static void searchB(int loc, char *string)
{
  nodelay(stdscr, FALSE);
  free(string);

  if (loc >= 0) set_cursor(loc);
  else {
    if (loc == -3) displayMessageAndWaitForKey("not found");
  }
}

void search_forward(void)
{
  char *p, *string, tmp[BLOCK_SEARCH_SIZE];
  int quit, sizea, sizeb;
  int blockstart;

  if (!searchA(&string, &sizea, tmp)) return;
  quit = -1;
  blockstart = base + cursor - BLOCK_SEARCH_SIZE + sizea;
  do {
    blockstart += BLOCK_SEARCH_SIZE - sizea + 1;
    lseek(fd, blockstart, SEEK_SET);
    if ((sizeb = read(fd, tmp, BLOCK_SEARCH_SIZE)) < sizea) quit = -3;
    else if (getch() != ERR) quit = -2;
    else if ((p = mymemmem(tmp, sizeb, string, sizea))) quit = p - tmp;
  } while (quit == -1);

  searchB(quit + (quit >= 0 ? blockstart : 0), string);
}

void search_backward(void)
{
  char *p, *string, tmp[BLOCK_SEARCH_SIZE];
  int quit, sizea, sizeb;
  int blockstart;

  if (!searchA(&string, &sizea, tmp)) return;
  quit = -1;
  blockstart = base + cursor - sizea + 1;
  do {
    blockstart -= BLOCK_SEARCH_SIZE - sizea + 1;
    sizeb = BLOCK_SEARCH_SIZE;
    if (blockstart < 0) { sizeb -= -blockstart; blockstart = 0; }

    if (sizeb < sizea) quit = -3;
    else {
      lseek(fd, blockstart, SEEK_SET);
      if (sizeb != read(fd, tmp, sizeb)) quit = -3;
      else if (getch() != ERR) quit = -2;
      else if ((p = mymemrmem(tmp, sizeb, string, sizea))) quit = p - tmp;
    }
  } while (quit == -1);

  searchB(quit + (quit >= 0 ? blockstart : 0), string);
}