File: skip.cc

package info (click to toggle)
gri 2.4.2-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 4,540 kB
  • ctags: 1,966
  • sloc: cpp: 32,542; lisp: 3,243; perl: 806; makefile: 548; sh: 253
file content (129 lines) | stat: -rw-r--r-- 3,558 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
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
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "gr.hh"
#include "private.hh"
#include "extern.hh"
#include "gr_coll.hh"
#include "DataFile.hh"

extern bool     _ignore_error;
bool            skip_forwardCmd(int n);
bool            skip_backwardCmd(int n);

// skipCmd() -- control skipping in data file
bool
skipCmd()
{
    double           tmp;
    bool            old_ignore_error = _ignore_error;
    if (_dataFILE.back().get_type() == DataFile::from_cmdfile) {
	err("`skip' ignored: no data file open");
	return false;
    }
    if (_dataFILE.back().get_type() == DataFile::bin_netcdf) {
	warning("`skip' ignored: cannot do this for netCDF file");
	return false;
    }
    if (_nword == 1)
	return skip_forwardCmd(1);
    _ignore_error = true;
    if (getdnum(_word[1], &tmp)) {
	skip_forwardCmd(int(floor(0.5 + tmp)));
	_ignore_error = old_ignore_error;
	return true;
    }
    if (!strcmp(_word[1], "forward")) {
	if (_nword == 2)
	    return skip_forwardCmd(1);
	else {
	    double           tmp;
	    if (!getdnum(_word[2], &tmp)) {
		err("invalid # to 'skip forward'");
		_ignore_error = old_ignore_error;
		return false;
	    }
	    return skip_forwardCmd(int(floor(0.5 + tmp)));
	}
    } else if (!strcmp(_word[1], "backward")) {
	if (_nword == 2)
	    return skip_backwardCmd(1);
	else {
	    if (!getdnum(_word[2], &tmp)) {
		err("invalid # to 'skip backward'");
		return false;
	    }
	    return skip_backwardCmd(int(floor(0.5 + tmp)));
	}
    } else {
	err("Unknown 'skip' parameter;\nvalid choices: forward/backward");
	_ignore_error = old_ignore_error;
	return true;
    }
}

bool
skip_forwardCmd(int n)
{
    Require(n >= 0,
	    err("Require positive (or zero) number of lines to skip"));
    if (_dataFILE.back().get_type() == DataFile::from_cmdfile) {
	warning("Cannot `skip' when data are embedded in command-file");
	return true;
    }
    while (n-- > 0) {
	if (_dataFILE.back().get_type() != DataFile::ascii) {
	    // type=0 ascii, others binary
	    unsigned char   tmpB;
	    if (1 != fread((char *) & tmpB, sizeof(tmpB), 1, _dataFILE.back().get_fp())) {
		warning("I/O error while skip forward in binary file.");
		return false;
	    }
	    if (feof(_dataFILE.back().get_fp())) {
		set_eof_flag_on_data_file();
		warning("`skip forward' at end-of-file on file `\\",
			_dataFILE.back().get_name(),  "'", "\\");
		break;
	    }
	} else {
	    // Ascii file
	    GriString inLine(128); // Start short
	    if (inLine.line_from_FILE(_dataFILE.back().get_fp())) {
		set_eof_flag_on_data_file();
		warning("`skip forward' at end-of-file on file `\\",
			_dataFILE.back().get_name(),  "'", "\\");
		return true;
	    }
	    _dataFILE.back().increment_line();
	}
    }
    return true;
}

bool
skip_backwardCmd(int n)
{
    Require(n >= 0,
	    err("Require positive (or zero) number of lines to skip"));
    if (_dataFILE.back().get_type() == DataFile::from_cmdfile) {
	warning("Cannot `skip' when data are embedded in command-file");
	return true;
    }
    if (_dataFILE.back().get_type() != DataFile::ascii) {
	warning("Can't `skip forward' in binary files");
	return false;
    }
    rewind(_dataFILE.back().get_fp());
    clearerr(_dataFILE.back().get_fp());
    GriString inLine(128); // Start short
    for (int l = 1; l < n; l++) {
	if (inLine.line_from_FILE(_dataFILE.back().get_fp())) {
	    set_eof_flag_on_data_file();
	    warning("`skip forward' at end-of-file on file `\\",
		    _dataFILE.back().get_name(), "'", "\\");
	    break;
	}
	_dataFILE.back().increment_line();
    }
    return true;
}