File: posixSystem.cpp

package info (click to toggle)
contextfree 2.2%2Bdfsg1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,292 kB
  • ctags: 4,316
  • sloc: cpp: 20,388; yacc: 507; objc: 494; ansic: 270; makefile: 114; lex: 92; sh: 35; xml: 24
file content (148 lines) | stat: -rw-r--r-- 3,742 bytes parent folder | download | duplicates (2)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// posixSystem.cpp
// Context Free
// ---------------------
// Copyright (C) 2005-2007 Mark Lentczner - markl@glyphic.com
// Copyright (C) 2007-2008 John Horigan - john@glyphic.com
//
// 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 2
// 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, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// 
// Mark Lentczner can be contacted at markl@glyphic.com or at
// Mark Lentczner, 1209 Villa St., Mountain View, CA 94041-1123, USA
//
// John Horigan can be contacted at john@glyphic.com or at
// John Horigan, 1209 Villa St., Mountain View, CA 94041-1123, USA
//
//

#include "posixSystem.h"

#include <cstdlib>
#include "stdarg.h"

#include <string>
#include <fstream>
#include <iostream>
#include <string.h>
#include <stdlib.h>

using namespace std;


void
PosixSystem::message(bool error, const char* fmt, ...)
{
    mError = mError || error;
    
	if (mQuiet && !error) return;

	char buf[256];
	{
		va_list args;
		va_start(args, fmt);
		vsnprintf(buf, sizeof(buf), fmt, args);
		buf[sizeof(buf)-1] = '\0';
		va_end(args);
	}
	
	ostream& out = error ? cerr : cout;
	out << buf << "                            " << endl;
}

void
PosixSystem::syntaxError(const std::string&, int)
{
  // this version doesn't high-light the line in error
  // the parser will have already called message() with
  // the syntax error information
}

istream*
PosixSystem::openFileForRead(const string& path)
{
    if (path == "-") return &cin;
	return new ifstream(path.c_str(), ios::binary);
}

istream*
PosixSystem::tempFileForRead(const string& path)
{
	return new ifstream(path.c_str(), ios::binary);
}

ostream*
PosixSystem::tempFileForWrite(string& prefixInNameOut)
{
#ifdef WIN32
    char* path = _tempnam(0, prefixInNameOut.c_str());
    prefixInNameOut = path;
    ofstream* f = new ofstream(path, ios::binary | ios::out | ios::trunc);
    free((void*)path);
    return f;
#else
	string t = "/tmp/" + prefixInNameOut + "XXXXXX";
	char* b = new char[t.size() + 1];
	strcpy(b, t.c_str());
	
	ofstream* f = 0;
	
	// Please ignore the warning about mktemp() being unsafe. There is no 
	// standard way to turn the descriptor returned by mkstemp() into an
	// ostream object. We could add the boost library to the project (NO!!!!)
	// or roll our own file descriptor ostream class (code exists on the
	// internet). But it just doesn't seem worth the bother. mktemp() is 
	// unsafe for some types of programs, but who cares if an attacker takes
	// down your Context Free.
	if (mktemp(b)) {
		f = new ofstream;
		f->open(b, ios::binary);
		prefixInNameOut = b;
	}
	delete b;
	
	return f;
#endif
}
		
string
PosixSystem::relativeFilePath(const string& base, const string& rel)
{
	string s = base;
	
	string::size_type i = s.rfind('/');
	if (i == string::npos) {
		return rel;
	}
	i += 1;
	s.replace(i, s.length() - i, rel);	
	return s;
}

void
PosixSystem::stats(const Stats& s)
{
	if (mQuiet || mError) return;

    cout << "    " << s.shapeCount << " shapes";

    if (s.toDoCount > 0)
        cout << " - " << s.toDoCount << " expansions to do";
    else
        cout << "                            ";

    cout << "        \r";
	cout.flush();
}