Garfield++ 3.0
A toolkit for the detailed simulation of particle detectors based on ionisation measurement in gases and semiconductors
Loading...
Searching...
No Matches
findmark.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <cstdio>
3
6
7/*
8Copyright (c) 2000 I. B. Smirnov
9
10Permission to use, copy, modify, distribute and sell this file for any purpose
11is hereby granted without fee, provided that the above copyright notice,
12this permission notice, and notices about any modifications of the original
13text appear in all copies and in supporting documentation.
14The file is provided "as is" without express or implied warranty.
15*/
16
17namespace Heed {
18
19int findmark(std::istream &file, const char *s) {
20 int ic;
21 int l = strlen(s); // length does not include end symbol
22 char *fs = new char[l + 1];
23 for (int n = 0; n < l; n++) {
24 if ((ic = file.get()) == EOF) {
25 delete[] fs;
26 return 0;
27 }
28 fs[n] = ic;
29 }
30 fs[l] = '\0';
31 while (strcmp(fs, s) != 0) {
32 for (int n = 1; n < l; n++) fs[n - 1] = fs[n];
33 if ((ic = file.get()) == EOF) {
34 delete[] fs;
35 return 0;
36 }
37 fs[l - 1] = ic;
38 fs[l] = '\0';
39 }
40 delete[] fs;
41 return 1;
42}
43
44int find1ofnmark(std::istream &file, int q, char *s[]) {
45 // mcout<<"find1ofnmark is started\n";
46 // char c;
47 int ic;
48 int *l = new int[q];
49 int *pos_fs = new int[q];
50 int s_init_pos_fs = 0;
51 int i;
52 int l_max = -1;
53 for (i = 0; i < q; i++) {
54 l[i] = strlen(s[i]); // length does not include end symbol
55 if (l[i] > l_max) l_max = l[i];
56 }
57 // l_max++;
58 // Iprintn(mcout, q);
59 // Iprintn(mcout, l_max);
60 char *fs = new char[l_max + 1];
61 // int qfs=0; // number of symbols in fs
62 for (i = 0; i < q; i++) {
63 pos_fs[i] = l_max;
64 }
65 fs[l_max] = '\0';
66 // Iprintn(mcout, file.good());
67 // Iprintn(mcout, file.eof());
68 // Iprintn(mcout, file.fail());
69 // Iprintn(mcout, file.bad());
70 // mcout<<"State:"<< file.rdstate() <<'\n';
71 while ((ic = file.get()) != EOF) {
72 // Iprintn(mcout, ic);
73 for (i = 1; i < l_max; i++) {
74 fs[i - 1] = fs[i];
75 }
76 fs[l_max - 1] = ic; // new symbol
77 if (s_init_pos_fs == 0) { // shift positions
78 int ss = 1;
79 for (i = 0; i < q; i++) {
80 if (l_max - pos_fs[i] < l[i]) {
81 pos_fs[i]--;
82 ss = 0;
83 // mcout<<"s[i]="<<s[i]<<'\n';
84 // mcout<<"i="<<i<<" l_max="<<l_max<<" pos_fs[i]="<<pos_fs[i]
85 // <<" l[i]="<<l[i]<<" "<< &(fs[ pos_fs[i] ])<<'\n';
86 }
87 }
88 if (ss == 1) s_init_pos_fs = 1;
89 }
90 for (i = 0; i < q; i++) {
91 if (strcmp(&(fs[pos_fs[i]]), s[i]) == 0) {
92 delete[] l;
93 delete[] fs;
94 delete[] pos_fs;
95 return i;
96 }
97 }
98 }
99 // Iprintn(mcout, ic);
100 delete[] l;
101 delete[] fs;
102 delete[] pos_fs;
103 return -1;
104}
105
106int find1ofnmark(std::istream &file, int q, // number of strings
107 const std::string str[]) // addresses
108{
109 char **s = new char *[q];
110 for (int i = 0; i < q; i++) {
111 s[i] = new char[strlen(str[i].c_str()) + 1];
112 strcpy(s[i], str[i].c_str());
113 }
114 int iret = find1ofnmark(file, q, s);
115 for (int i = 0; i < q; i++) {
116 delete[] s[i];
117 }
118 delete[] s;
119 return iret;
120}
121
122}
Definition: BGMesh.cpp:6
int findmark(std::istream &file, const char *s)
Definition: findmark.cpp:19
int find1ofnmark(std::istream &file, int q, char *s[])
Definition: findmark.cpp:44