File: xgetline.awk

package info (click to toggle)
runawk 1.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 712 kB
  • sloc: awk: 1,127; ansic: 736; sh: 420; makefile: 103
file content (59 lines) | stat: -rw-r--r-- 1,420 bytes parent folder | download | duplicates (4)
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
# Written by Aleksey Cheusov <vle@gmx.net>, public domain
#
# This awk module is a part of RunAWK distribution,
#        http://sourceforge.net/projects/runawk
#    
############################################################

# =head2 xgetline.awk
#
# =over 2
#
# =item I<xgetline0([FILE])>
#
# Safe analog to 'getline < FILE' or 'getline' (if no FILE is specified).
# 0 at the end means that input line is assigned to $0.
#
# =item I<xgetline([FILE])>
#
# Safe analog to 'getline __input < FILE' and 'getline __input'
# (if no FILE is specified)
#
# =back
#
# In both cases "safe" means that returned value is analysed and
# if it is less than zero (file reading error happens) program will
# be terminated emmidiately with appropriate error message sent to stderr.
# Both functions return zero if end of file is reached or non-zero otherwise.
#
# Example:
#       while (xgetline("/etc/passwd")){
#           print "user: " __input
#       }
#

#use "alt_assert.awk"

function xgetline0 (fn,                 ret){
	if (fn == ""){
		ret = getline
		assert(ret >= 0, "getline failed")
	}else{
		ret = (getline < fn)
		assert(ret >= 0, "getline < " fn " failed")
	}

	return (ret > 0)
}

function xgetline (fn,                 ret){
	if (fn == ""){
		ret = getline __input
		assert(ret >= 0, "getline failed")
	}else{
		ret = (getline __input < fn)
		assert(ret >= 0, "getline < " fn " failed")
	}

	return (ret > 0)
}