NEURON
nrnbinstr.cpp
Go to the documentation of this file.
1 // prints the first "x:.../activate.bat" in the first 10000 bytes of
2 // the binary file (second arg). Characters may or may not be 16 bit
3 // integers (ignore '\0')
4 // (return 0 if found, 1 if not found or error)
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <ctype.h>
10 
11 int main(int argc, const char** argv) {
12  if (argc != 3) {
13  fprintf(stderr, "Usage: nrnbinstr \"pattern\" \"binaryfile\"\n");
14  return 1;
15  }
16  const char* pat = argv[1];
17  const char* fname = argv[2];
18 
19  FILE* f = fopen(fname, "rb");
20  if (!f) {
21  fprintf(stderr, "Could not open %s\n", fname);
22  return 1;
23  }
24 
25  char* buf = new char[10000];
26  int size = fread(buf, sizeof(char), 10000, f);
27  fclose(f);
28 
29  int c;
30  char* result = new char[10000];
31  int lenpat = strlen(pat);
32  int ifirst=0, ipat=0, iresult=0;
33  int icolon=0;
34  for (int i=0; i < size-1; ++i) {
35  c = buf[i];
36  if (c == 0 && buf[i+1] != 0) { continue; } // ignore single '\0'
37  if (isprint(c)) {
38  if (c == ':' && iresult > 0) {
39  icolon = iresult - 1; // must be single character drive letter
40  }
41  result[iresult++] = c;
42  if (c == pat[ipat]) {
43  ipat++;
44  if (ipat == lenpat) {
45  result[iresult] = '\0';
46  printf("%s\n", result + icolon);
47  return 0;
48  }
49  }else if (ipat > 0) {
50  ipat = 0;
51  }
52  }else{
53  icolon = 0;
54  iresult = 0;
55  ipat = 0;
56  ifirst = i+1;
57  }
58  }
59  return 1;
60 }
#define printf
Definition: mwprefix.h:26
static const char * fname(const char *name)
Definition: nrnbbs.cpp:108
fprintf(stderr, "Don't know the location of params at %p\, pp)
int main(int argc, const char **argv)
Definition: nrnbinstr.cpp:11
#define i
Definition: md1redef.h:12
#define c
char buf[512]
Definition: init.cpp:13
static int argc
Definition: inithoc.cpp:53
FILE * fopen()
static char ** argv
Definition: inithoc.cpp:54