summaryrefslogtreecommitdiff
path: root/lib/Input.cc
blob: 5109d2a99ccde433ee724e1569a3dea970ce9db0 (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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "Input.h"
#include "Exceptions.h"
#include "gettext.h"

Input::Input(const String & no) throw (GeneralException) :
    Handle(no.strlen() ? open(no.to_charp(), O_RDONLY) : dup(0)),
    n(no) {

#ifdef DEBUG    
    fprintf(stderr, "Opening file %s, Input at %p\n", no.to_charp(), this);
#endif

    if (GetHandle() < 0) {
	throw IOGeneral(String(_("Error opening file ")) + no + _(" for reading: ") + strerror(errno));
    }
    
    struct stat s;
    fstat(GetHandle(), &s);
    date_modif = s.st_mtime;

    if (S_ISREG(s.st_mode)) {
	size = seek(0, SEEK_END);
	seek(0, SEEK_SET);
    }
}

Input::Input(const Input & i) : Handle(i), n(i.n), size(i.size), date_modif(i.date_modif) {
}

bool Input::CanWrite() const {
    return 0;
}

bool Input::CanRead() const {
    return 1;
}

bool Input::CanSeek() const {
    struct stat s;
    
    fstat(GetHandle(), &s);

    return S_ISREG(s.st_mode);
}

String Input::GetName() const {
    return n;
}

ssize_t Input::GetSize() const {
    return size;
}

time_t Input::GetModif() const {
    return date_modif;
}

off_t Input::seek(off_t offset, int whence) throw (GeneralException) {
    if ((itell = lseek(GetHandle(), offset, whence)) < 0) {
	throw IOGeneral(String(_("Error seeking file ")) + GetName() + _(": ") + strerror(errno));
    }
#ifdef PARANOID_SEEK
    if (itell != lseek(GetHandle(), 0, SEEK_CUR)) {
	throw IOGeneral(String(_("Error seeking file ")) + GetName() + _(": the position does not match"));
    }
#endif
    return itell;
}

Stdin_t::Stdin_t() { }

bool Stdin_t::CanSeek() const {
    return 0;
}

String Stdin_t::GetName() const {
    return "Stdin";
}

Stdin_t Stdin;