summaryrefslogtreecommitdiffstats
path: root/sources/utilities.cpp
blob: 82544a5c27eba2c77749d26c827da05cb168c83e (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//-*- mode: c++; indent-tabs-mode: t; coding: utf-8; show-trailing-whitespace: t -*-

// file utilities.cpp

#include "utilities.hpp"

//

namespace kwtcom {

  long int nKWTVAX;

}

namespace utilities
{

#ifdef USE_POSIX

  bool exists(const char *p)
  {
    struct stat sb;
    //
    return p && (0 == stat (p, &sb));
  }

#else

  bool exists(const char *p)
  {
    bool bReturnValue = false;
    std::fstream sFile;
    //
    if(p) {
      sFile.open(p, std::ios_base::in);
      bReturnValue = sFile.is_open();
      if(bReturnValue)
	sFile.close();
    }
    return bReturnValue;
  }

#endif

  std::string format(const std::string &format, ...)
  {
    va_list args;
    va_start(args, format);
    size_t nLength = std::vsnprintf(NULL, 0, format.c_str(), args);
    va_end (args);
    std::vector<char> sVector (nLength + 1);
    va_start (args, format);
    std::vsnprintf (&sVector[ 0 ], nLength + 1, format.c_str(), args);
    va_end (args);
    return &sVector[ 0 ];
  }

  void quiter(void)
  {
    ;
  }

  // subroutine window.
  void window(std::ostream &os, std::stringstream &ss)
  {
    // Module of interactive emtp usage only, which services "emtspy".
    // For non-interactive emtp, this module can be destroyed.
    // For character*132 spy display channel munit6, this serves
    // to actually output the line to the spy display device.
    // VAX-11  installation-dependent EMTP module.
    size_t j;
    size_t k;
    std::string sData;
    //
    sData = ss.str();
    if(abs(spycom::nKVerfy) != 34543) {
      for(j = 1; j <= 132; j++) {
	k = 133 - j;
	if(sData[ k - 1 ] != ' ')
	  break;
      }
      os << sData.substr(0, k) << std::endl;
      os.flush();
    }
  }

  // subroutine honker.
  void honker(const long int &nKLevel)
  {
    // Module of interactive EMTP only, which services "emtspy".
    // If no interactive use, this module can be deleted.
    // VAX-11 installation-dependent emtp module which issues
    // an audible alarm (via a terminal speaker) of intensity
    // controlled by argument klevel.  This is on a scale of
    // one to ten, with zero meaning no noise at all, and ten
    // corresponding to a disaster alert (e.g., air raid sirens).
    // This VAX-11 module is designed for DEC VT100 terminal.  The
    // idea is by Albert H. Schmidt, BPA route EOGA.  At the keyboard,
    // ctrl-g will ring the bell, and the ascii character 7 is
    // equivalent within a program.  Shortage of 1 bell is due to
    // carriage control, maybe (see n8 = j + 1, rather than j, below).
    int nd13;
    double d13;
    std::string pSpyTim[ 2 ], pSpDate[ 2 ];
    //
    d13 = 2.0;
    date_time::time44(pSpyTim);
    date_time::date44(pSpDate);
    spyf77::sBuff77 = std::string (10, '\7');
    FOREVER {
      (*reinterpret_cast<std::stringstream *>(spycom::pMFiles[ 5 ])) << " Audible alarm began at  ";
      (*reinterpret_cast<std::stringstream *>(spycom::pMFiles[ 5 ])) << std::setw(4) << pSpyTim[ 0 ] << pSpyTim[ 1 ];
      (*reinterpret_cast<std::stringstream *>(spycom::pMFiles[ 5 ])) << "  ";
      (*reinterpret_cast<std::stringstream *>(spycom::pMFiles[ 5 ])) << std::setw(4) << pSpDate[ 0 ] << pSpDate[ 1 ];
      (*reinterpret_cast<std::stringstream *>(spycom::pMFiles[ 5 ])) << std::string(5, ' ');
      (*reinterpret_cast<std::stringstream *>(spycom::pMFiles[ 5 ])) << std::setw(3) << spycom::nKLevel;
      (*reinterpret_cast<std::stringstream *>(spycom::pMFiles[ 5 ])) << " bells.";
      (*reinterpret_cast<std::stringstream *>(spycom::pMFiles[ 5 ])) << spyf77::sBuff77 << std::endl << std::flush;
      window(*reinterpret_cast<std::ostream*>(blkcom::pLFiles[ 5 ]),	\
	     *reinterpret_cast<std::stringstream *>(spycom::pMFiles[ 5 ]));
      if(spycom::nKLevel < 10)
	break;
      nd13 = (int) d13;
      date_time::tdelay(nd13);
      if(d13 == -7654)
	break;
      flager();
      if(spycom::nKWTSpy != 0) {
	spycom::nKWTSpy = 0;
	break;
      } else
	d13 = 1.5 * d13;
    }
  }

  // subroutine flager.
  void flager(void)
  {
    // Module of interactive EMTP only, which services "emtspy".
    // if no interactive use, convert to dummy module ("return").
    // VAX-11 installation-dependent emtp module which serves
    // to read spy command from munit5 if: 1) ctrl-c interrupt
    // has occurred, or 2) if  lockbr = 1  upon entry.
    long int pIDum[ 3 ];
    //
    if(spycom::nIprSpy >= 10) {
      (*reinterpret_cast<std::stringstream *>(spycom::pMFiles[ 5 ])) << " Top flager.  istep, kwtspy, itype, lastov =" << blkcom::nIStep << blkcom::nKWTSpy << blkcom::nIType << blkcom::nLastOV << std::endl;
      window(*reinterpret_cast<std::ostream *>(blkcom::pLFiles[ 5 ]), \
	     *reinterpret_cast<std::stringstream *>(spycom::pMFiles[ 5 ]));
    }
    if(blkcom::nLastOV == 9911) {
      spyf77::sBuff77 = "space";
      blkcom::nKWTSpy = 1;
      spycom::nKFile5 = 2;
      goto a3651;
    }
    if(kwtcom::nKWTVAX != 0) {
      spyf77::sProm80 = " spy:";
      prompt(*reinterpret_cast<std::ostream *>(blkcom::pLFiles[ 5 ]),	\
	     spyf77::sProm80);
      //
      if (spycom::nIprSpy < 1)
	(*reinterpret_cast<std::stringstream *>(spycom::pMFiles[ 5 ])) << " Enable VAX ctrl-c interception in \"flager\"." << std::endl;
      window(*reinterpret_cast<std::ostream *>(blkcom::pLFiles[ 5 ]),	\
	     *reinterpret_cast<std::stringstream *>(spycom::pMFiles[ 5 ]));

    a3491:
      goto a3643;
    }
    if((spycom::nLockBR != 1) || (spycom::nKFile5 == 1))
      goto a3651;

  a3643:
    reinterpret_cast<std::stringstream *>(spycom::pMFiles[ 4 ]) -> read(&spyf77::sBuff77[ 0 ], 80);
    blkcom::nKWTSpy = 1;
    spycom::nKFile5 = 2;
    if(spycom::nKSPSav == 0)
      goto a3650;
    if(strcom::toLower(spyf77::sBuff77.substr(0, 7)) == "cancel ")
      goto a3648;
    ++spycom::nKSPSav;
    goto a3643;

  a3648:
    --spycom::nKSPSav;
    if (spycom::nKSPSav > spycom::nNumCRD)
      goto a3649;
    std::cout << " Error stop; overflow in \"flager\"." << std::endl;
    emtp::stoptp();

  a3649:
    spyf77::sFile6[ spycom::nKSPSav - 1 ] = spyf77::sBuff77;

  a3650:
    goto a3651;

  a3651:
    if (spycom::nIprSpy >= 9) {
      *((std::stringstream *) spycom::pMFiles[ 5 ]) << " Exit \"flager\".  kwtspy =" << blkcom::nKWTSpy << "   buff77 = " << spyf77::sBuff77.substr(0, 80) << std::endl;
      window(*reinterpret_cast<std::ostream *>(blkcom::pLFiles[ 5 ]),	\
	     *reinterpret_cast<std::stringstream *>(spycom::pMFiles[ 5 ]));
    }
  }

  // subroutine prompt.
  void prompt(std::ostream &os, const std::string &sPrompt)
  {
    // VAX-11  installation-dependent emtp module used only
    // for interactive EMTP ("emtspy").  Input is program
    // prompt in character*80 variable prom80 of deck "dekspy".
    // The prompt must end with colon (":").  Then line feed
    // will be suppressed, so subsequent read is to right of ":".
    // For non-interactive EMTP, this module can be destroyed.
    long int j;
    long int n2;
    //
    n2 = 80;
    for(j = 1; j <= 80; j++) {
      if(sPrompt[ n2 - 1 ] == ' ')
	break;
      --n2;
    }
    os << sPrompt.substr(0, n2);
  }

  // subroutine openfile.
  bool openFile(std::fstream *pFile,				\
		std::string &sFileName,				\
		const std::ios::openmode &mode)
  {
    bool bReturnValue = false;
    //
    if(pFile) {
      pFile -> open(sFileName.c_str(), mode);
      bReturnValue = pFile -> good();
    }
    return bReturnValue;
  }

  // subroutine closefile.
  bool closeFile(std::fstream *pFile)
  {
    bool bReturnValue = false;
    //
    if(pFile) {
      pFile -> close();
      bReturnValue = true;
    }
    return bReturnValue;
  }

  // subroutine copyfile.
  bool copyFile(std::fstream *pInFile,					\
		std::fstream *pOutFile,					\
		const std::string &sFileName)
  {
    bool bReturnValue = false;
    //
    if(pInFile && pOutFile) {
      pOutFile -> close();
      pOutFile -> open(sFileName.c_str(), std::ios::out | std::ios::trunc);
      pInFile -> seekg(0, std::ios::beg);
      char pTempData[ 80 ];
      while(!pInFile -> eof()) {
	pInFile -> read(pTempData, 80);
	pOutFile -> write(pTempData, 80);
      }
      pOutFile -> close();
      pInFile -> seekg(0, std::ios::beg);
      bReturnValue = true;
    }
    return bReturnValue;
  }

  // subroutine deletefile.
  bool deleteFile(const std::string &sFileName)
  {
    ;
  }

  struct winsize getTerminalSize(void)
  {
    struct winsize sReturnValue;
    //
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &sReturnValue);
    if(blkcom::nNOutPR > 0) {
      (*reinterpret_cast<std::ostream *>(blkcom::pLFiles[ 5 ])) << " Terminal lines = ";
      (*reinterpret_cast<std::ostream *>(blkcom::pLFiles[ 5 ])) << std::setfill(' ') << std::setw(8) << std::dec << sReturnValue.ws_row;
      (*reinterpret_cast<std::ostream *>(blkcom::pLFiles[ 5 ])) << ", columns = ";
      (*reinterpret_cast<std::ostream *>(blkcom::pLFiles[ 5 ])) << std::setfill(' ') << std::setw(8) << std::dec << sReturnValue.ws_col << std::endl;
    }
    return sReturnValue;
  }

  void expchk(long int &n1,			\
	      long int &n2,			\
	      long int &n5)
  {
    const char cText1 = 'e', cTextN = '-', cTextP = '+';
    const char pTexNum[ 11 ] = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.' };
    char pX[ 80 ];
    //
    long int i;
    long int j;
    long int k;
    long int l;
    long int n3, n4;
    long int nKey;
    //
    blkcom::sABuff.read(pX, 80);
    for(i = n1; i <= n2; i += n5) {
      nKey = 0;
      for(j = 1; j <= n5; j++) {
	n3 = i + j - 1;
	if(pX[ 2 ] == cText1)
	  goto a2622;
	if(nKey > 0)
	  goto a2620;
	for(k = 1; k <= 11; k++) {

	a2619:
	  if(pX[ n3 - 1 ] == pTexNum[ k - 1 ])
	    nKey = k;
	  goto a2621;

	a2620:
	  if((pX[ n3 - 1 ] == cTextP) || (pX[ n3 - 1 ] == cTextN))
	    goto a2622;

	a2621:
	  ;
	}
	goto a2625;

      a2622:
	n4 = i + n5 - 1;
	for(l = 1; l<= 10; l++) {
	  if(pX[ n4 - 1 ] == pTexNum[ l - 1 ])
	    goto a2625;
	}

      a2624:
	blkcom::nKill = 97;
	blkcom::pSStat -> m_nWT = i;
	blkcom::pSStat -> m_nTails = n4;
	blkcom::pSStat -> m_nTACST = 2624;
	return;

      a2625:
	;
      }
    }
  }

}
// end of file utilities.cpp