summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvisvgm/dvisvgm-src/src/SourceInput.cpp
blob: 96efb50a875bb2e9f6735d72223917397aa7934e (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
/*************************************************************************
** SourceInput.cpp                                                      **
**                                                                      **
** This file is part of dvisvgm -- a fast DVI to SVG converter          **
** Copyright (C) 2005-2018 Martin Gieseking <martin.gieseking@uos.de>   **
**                                                                      **
** This program is free software; you can redistribute it and/or        **
** modify it under the terms of the GNU General Public License as       **
** published by the Free Software Foundation; either version 3 of       **
** the License, or (at your option) any later version.                  **
**                                                                      **
** This program is distributed in the hope that it will be useful, but  **
** WITHOUT ANY WARRANTY; without even the implied warranty of           **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         **
** GNU General Public License for more details.                         **
**                                                                      **
** You should have received a copy of the GNU General Public License    **
** along with this program; if not, see <http://www.gnu.org/licenses/>. **
*************************************************************************/

#include <fstream>
#include <iostream>
#include "FileSystem.hpp"
#include "Message.hpp"
#include "MessageException.hpp"
#include "SourceInput.hpp"
#include "utility.hpp"

#ifdef _WIN32
#include <fcntl.h>
#include <sys/stat.h>
#include <windows.h>
#endif

#ifdef _MSC_VER
#include <io.h>
#else
#include <cstdlib>
#include <unistd.h>
#endif

using namespace std;

#ifdef _WIN32
static int fdwrite (int fd, const char *buf, size_t len) {return _write(fd, buf, len);}
static int fdclose (int fd) {return _close(fd);}
#else
static int fdwrite (int fd, const char *buf, size_t len) {return write(fd, buf, len);}
static int fdclose (int fd) {return close(fd);}
#endif


SourceInput::~SourceInput () {
	// remove temporary file created for reading from stdin
	if (!_tmpfilepath.empty())
		FileSystem::remove(_tmpfilepath);
}


/** Creates a temporary file in the configured tmp folder.
 *  @param[out] path path of the created file
 *  @return file descriptor (>= 0 on success) */
static int create_tmp_file (string &path) {
	path = FileSystem::tmpdir();
#ifndef _WIN32
	path += "stdinXXXXXX";
	int fd = mkstemp(&path[0]);
#else
	int fd = -1;
	char fname[MAX_PATH];
	std::replace(path.begin(), path.end(), '/', '\\');
	if (GetTempFileName(path.c_str(), "stdin", 0, fname)) {
		fd = _open(fname, _O_CREAT | _O_WRONLY | _O_BINARY, _S_IWRITE);
		path = fname;
	}
#endif
	return fd;
}


istream& SourceInput::getInputStream (bool showMessages) {
	if (!_ifs.is_open()) {
		if (!_fname.empty())
			_ifs.open(_fname, ios::binary);
		else {
			int fd = create_tmp_file(_tmpfilepath);
			if (fd < 0)
				throw MessageException("can't create temporary file for writing");
#ifdef _WIN32
			if (_setmode(_fileno(stdin), _O_BINARY) == -1)
				throw MessageException("can't open stdin in binary mode");
#endif
			if (showMessages)
				Message::mstream() << "reading from " << getMessageFileName() << '\n';
			char buf[1024];
			while (cin) {
				cin.read(buf, 1024);
				size_t count = cin.gcount();
				if (fdwrite(fd, buf, count) < 0)
					throw MessageException("failed to write data to temporary file");
			}
			if (fdclose(fd) < 0)
				throw MessageException("failed to close temporary file");
			_ifs.open(_tmpfilepath, ios::binary);
		}
	}
	return _ifs;
}


string SourceInput::getFileName () const {
	return _fname.empty() ? "stdin" : _fname;
}


string SourceInput::getMessageFileName () const {
	return _fname.empty() ? "<stdin>" : _fname;
}


string SourceInput::getFilePath () const {
	return _tmpfilepath.empty() ? _fname : _tmpfilepath;
}