Mercurial > ~dholland > hg > ag > index.cgi
comparison examples/dsl/redirect.cpp @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children | 607e3be6bad8 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:13d2b8934445 |
---|---|
1 /***** | |
2 | |
3 AnaGram Programming Examples | |
4 | |
5 A Dos Script Language | |
6 Redirection Class Module | |
7 | |
8 Copyright 1993 Parsifal Software. All Rights Reserved. | |
9 | |
10 This software is provided 'as-is', without any express or implied | |
11 warranty. In no event will the authors be held liable for any damages | |
12 arising from the use of this software. | |
13 | |
14 Permission is granted to anyone to use this software for any purpose, | |
15 including commercial applications, and to alter it and redistribute it | |
16 freely, subject to the following restrictions: | |
17 | |
18 1. The origin of this software must not be misrepresented; you must not | |
19 claim that you wrote the original software. If you use this software | |
20 in a product, an acknowledgment in the product documentation would be | |
21 appreciated but is not required. | |
22 2. Altered source versions must be plainly marked as such, and must not be | |
23 misrepresented as being the original software. | |
24 3. This notice may not be removed or altered from any source distribution. | |
25 | |
26 *****/ | |
27 | |
28 #include "redirect.h" | |
29 #include "assert.h" | |
30 #include "util.h" | |
31 | |
32 // Constructors | |
33 | |
34 redirect::redirect(int h) { | |
35 int temp; | |
36 handle = h; | |
37 #if defined(__MSDOS__) || defined(__WINDOWS__) | |
38 tmpnam(name); | |
39 temp = open(name, O_CREAT | O_RDWR | O_TEXT, S_IREAD | S_IWRITE); | |
40 #else | |
41 /* tmpnam() is unsafe, and causes a warning from many Unix linkers */ | |
42 strcpy(name, ".dsl.XXXXXX"); | |
43 temp = mkstemp(name); | |
44 #endif | |
45 assert(temp >= 0); | |
46 old_handle = dup(handle); | |
47 dup2(temp, handle); | |
48 close(temp); | |
49 copy_flag = 0; // live | |
50 } | |
51 | |
52 redirect::redirect(int h, char *file_name, int append_flag) { | |
53 int temp; | |
54 int flag; | |
55 handle = h; | |
56 temp = open(file_name, O_CREAT | O_RDWR | O_TEXT, S_IREAD | S_IWRITE); | |
57 if (append_flag) lseek(temp, 0L, SEEK_END); | |
58 name[0] = 0; | |
59 assert(temp >= 0); | |
60 old_handle = dup(handle); | |
61 flag = dup2(temp, handle); | |
62 assert(flag == 0); | |
63 close(temp); | |
64 copy_flag = 0; // live | |
65 } | |
66 | |
67 redirect::redirect(redirect &r) { | |
68 *this = r; | |
69 copy_flag++; // memorex | |
70 } | |
71 | |
72 | |
73 // Destructor | |
74 | |
75 redirect::~redirect() { | |
76 if (copy_flag) return; | |
77 dup2(old_handle, handle); | |
78 close(old_handle); | |
79 if (name[0]) unlink(name); | |
80 } | |
81 | |
82 | |
83 // Save temporary file | |
84 | |
85 char *save_file(redirect &s) { | |
86 assert(s.copy_flag == 0); | |
87 char *result = memdup(s.name, 1+strlen(s.name)); | |
88 s.name[0] = 0; | |
89 return result; | |
90 } | |
91 | |
92 | |
93 // Extract content of file | |
94 | |
95 char *content(redirect &s) { | |
96 long length = filelength(s.handle); | |
97 long where = tell(s.handle); // Current file position | |
98 unsigned n; | |
99 char *result; | |
100 | |
101 assert(length < 65520L); | |
102 n = (unsigned) (length + 1); | |
103 result = new char[n]; | |
104 assert(result != NULL); | |
105 lseek(s.handle,0,0); | |
106 n = read(s.handle, result, (unsigned) length); | |
107 lseek(s.handle,where,0); // Restore file | |
108 result[n] = 0; // Terminate string | |
109 return result; | |
110 } | |
111 | |
112 |