summaryrefslogtreecommitdiff
path: root/generic/Input.cpp
blob: a51ee6580e30aec3b906ea9e4184319d31172e1d (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
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "Input.h"
#include "Exceptions.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define _(x) x
#endif

Input::Input(String no) throw (GeneralException) :
    Handle(no.strlen() ? open(no.to_charp(), O_RDONLY) : dup(0)),
    n(no) {
    if (GetHandle() < 0) {
	throw IOGeneral(String(_("Error opening file ")) + no + _(" for reading: ") + strerror(errno));
    }
    
    size = lseek(GetHandle(), 0, SEEK_END);
    lseek(GetHandle(), 0, SEEK_SET);
    
    struct stat s;
    
    fstat(GetHandle(), &s);
    
    date_modif = s.st_mtime;
}

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

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

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

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

    return S_ISREG(s.st_mode);
}

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

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

time_t Input::GetModif() {
    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 ")) + n + _(": ") + strerror(errno));
    }
#ifdef PARANOID_SEEK
    if (itell != lseek(GetHandle(), 0, SEEK_CUR)) {
	throw IOGeneral(String(_("Error seeking file ")) + n + _(": the position does not match"));
    }
#endif
    return itell;
}

Stdin_t::Stdin_t() { }

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

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

Stdin_t Stdin;