Garfield++ v1r0
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 <string.h>
3#include <cstdio>
4
7
8/*
9Copyright (c) 2000 I. B. Smirnov
10
11Permission to use, copy, modify, distribute and sell this file for any purpose
12is hereby granted without fee, provided that the above copyright notice,
13this permission notice, and notices about any modifications of the original
14text appear in all copies and in supporting documentation.
15The file is provided "as is" without express or implied warranty.
16*/
17
18int findmark(std::istream &file, const char *s) {
19 int ic;
20 int l = strlen(s); // length does not include end symbol
21 int n;
22 char *fs = new char[l + 1];
23 for (n = 0; n < l; n++) {
24 if ((ic = file.get()) == EOF) {
25 delete[] fs;
26 return 0;
27 } else {
28 fs[n] = ic;
29 }
30 }
31 fs[l] = '\0';
32 while (strcmp(fs, s) != 0) {
33 for (n = 1; n < l; n++)
34 fs[n - 1] = fs[n];
35 if ((ic = file.get()) == EOF) {
36 delete[] fs;
37 return 0;
38 }
39 fs[l - 1] = ic;
40 fs[l] = '\0';
41 }
42 delete[] fs;
43 return 1;
44}
45
46int findmark_other_repeat(std::istream &file, std::ostream &outfile,
47 const char *s) {
48 int ic;
49 int l = strlen(s); // length does not include end symbol
50 int n;
51 char *fs = new char[l + 1];
52 for (n = 0; n < l; n++) {
53 if ((ic = file.get()) == EOF) {
54 fs[n] = ic;
55 outfile.write(fs, n + 1);
56 delete[] fs;
57 return 0;
58 } else {
59 fs[n] = ic;
60 }
61 }
62 fs[l] = '\0';
63 int s_not_new_line = 0;
64 while (strcmp(fs, s) != 0) {
65 if (fs[0] != '\n') {
66 if ((fs[0] == '\t' || fs[0] == ' ') && s_not_new_line == 0) {
67 ;
68 } else {
69 s_not_new_line = 1;
70 outfile.put(fs[0]);
71 }
72 } else {
73 if (s_not_new_line == 1) outfile.put(fs[0]); // to finish valid line
74 s_not_new_line = 0; // to avoid switching to new line with empty old one
75 }
76 for (n = 1; n < l; n++)
77 fs[n - 1] = fs[n];
78 if ((ic = file.get()) == EOF) {
79 delete[] fs;
80 return 0;
81 }
82 fs[l - 1] = ic;
83 fs[l] = '\0';
84 }
85 delete[] fs;
86 return 1;
87}
88
89int find1ofnmark(std::istream &file, int q, char *s[]) {
90 //mcout<<"find1ofnmark is started\n";
91 //char c;
92 int ic;
93 int *l = new int[q];
94 int *pos_fs = new int[q];
95 int s_init_pos_fs = 0;
96 int i;
97 int l_max = -1;
98 for (i = 0; i < q; i++) {
99 l[i] = strlen(s[i]); // length does not include end symbol
100 if (l[i] > l_max) l_max = l[i];
101 }
102 //l_max++;
103 //Iprintn(mcout, q);
104 //Iprintn(mcout, l_max);
105 char *fs = new char[l_max + 1];
106 //int qfs=0; // number of symbols in fs
107 for (i = 0; i < q; i++) {
108 pos_fs[i] = l_max;
109 }
110 fs[l_max] = '\0';
111 //Iprintn(mcout, file.good());
112 //Iprintn(mcout, file.eof());
113 //Iprintn(mcout, file.fail());
114 //Iprintn(mcout, file.bad());
115 //mcout<<"State:"<< file.rdstate() <<'\n';
116 while ((ic = file.get()) != EOF) {
117 //Iprintn(mcout, ic);
118 for (i = 1; i < l_max; i++) {
119 fs[i - 1] = fs[i];
120 }
121 fs[l_max - 1] = ic; // new symbol
122 if (s_init_pos_fs == 0) { // shift positions
123 int ss = 1;
124 for (i = 0; i < q; i++) {
125 if (l_max - pos_fs[i] < l[i]) {
126 pos_fs[i]--;
127 ss = 0;
128 //mcout<<"s[i]="<<s[i]<<'\n';
129 //mcout<<"i="<<i<<" l_max="<<l_max<<" pos_fs[i]="<<pos_fs[i]
130 // <<" l[i]="<<l[i]<<" "<< &(fs[ pos_fs[i] ])<<'\n';
131 }
132 }
133 if (ss == 1) s_init_pos_fs = 1;
134 }
135 for (i = 0; i < q; i++) {
136 if (strcmp(&(fs[pos_fs[i]]), s[i]) == 0) {
137 delete[] l;
138 delete[] fs;
139 delete[] pos_fs;
140 return i;
141 }
142 }
143 }
144 //Iprintn(mcout, ic);
145 delete[] l;
146 delete[] fs;
147 delete[] pos_fs;
148 return -1;
149}
150
151int find1ofnmark(std::istream &file, int q, // number of strings
152 const String str[]) // addresses
153 {
154 char **s = new char *[q];
155 int i;
156 for (i = 0; i < q; i++) {
157#ifdef USE_STLSTRING
158 s[i] = new char[strlen(str[i].c_str()) + 1];
159 strcpy(s[i], str[i].c_str());
160//Iprint(mcout, s[i]);
161#else
162 s[i] = new char[strlen(str[i]) + 1];
163 strcpy(s[i], str[i]);
164#endif
165 }
166 int iret = find1ofnmark(file, q, s);
167 for (i = 0; i < q; i++) {
168 delete[] s[i];
169 }
170 delete[] s;
171 return iret;
172}
std::string String
Definition: String.h:75
int findmark_other_repeat(std::istream &file, std::ostream &outfile, const char *s)
Definition: findmark.cpp:46
int findmark(std::istream &file, const char *s)
Definition: findmark.cpp:18
int find1ofnmark(std::istream &file, int q, char *s[])
Definition: findmark.cpp:89