File: TLFILEStream.m

package info (click to toggle)
tom 1.1.1-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 6,340 kB
  • ctags: 2,244
  • sloc: objc: 27,863; ansic: 9,804; sh: 7,411; yacc: 3,377; lex: 966; asm: 208; makefile: 62; cpp: 10
file content (179 lines) | stat: -rw-r--r-- 4,051 bytes parent folder | download
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/* Implementation of TLFILEStream class.
   This file is part of TL, Tiggr's Library.
   Written by Tiggr <tiggr@es.ele.tue.nl>
   Copyright (C) 1995, 1996 Pieter J. Schoenmakers
   TL is distributed WITHOUT ANY WARRANTY.
   See the file LICENSE in the TL distribution for details.

   $Id: TLFILEStream.m,v 1.1 1998/01/08 16:11:35 tiggr Exp $  */

#import "tl/support.h"
#import "tl/TLFILEStream.h"
#import "tl/TLString.h"
#import "tl/TLPatchedRoots.h"
#import <string.h>

id <TLInputStream> V_stdin_;
id <TLOutputStream> V_stdout_;
id <TLInputOutputStream> V_stderr_;

@implementation TLFILEStream

+init
{
  if (!V_stdin_)
    {
      V_stdin_ = [self mutableStreamWithFILE: stdin];
      V_stdout_ = [self mutableStreamWithFILE: stdout];
      V_stderr_ = [self mutableStreamWithFILE: stderr];
    }
  return (self);
} /* +init */

+(id <TLStream>) streamWithFILE: (FILE *) an_f
{
  return ([[self gcAlloc] initWithFILE: an_f]);
} /* -streamWithFILE: */

+(id <TLMutableStream>) mutableStreamWithFILE: (FILE *) an_f
{
  return ([[self gcAlloc] initWithFILE: an_f]);
} /* -mutableStreamWithFILE: */

+(id <TLStream>) streamWithFileNamed: (id <TLString>) name
{
  FILE *af = fopen ([name cString], "r");
  if (!af)
    {
      [self warning: "open %#: %s", name, ERRMSG];
      return (nil);
    }
  return ([self streamWithFILE: af]);
} /* +streamWithFileNamed: */

+(id <TLStream>) streamWithFileNamed: (id <TLString>) name
 alongPath: (TLCons *) in_path
{
  TLCons *dir, *path = in_path;
  TLString *filename;
  FILE *af;

  if (!path || [[name string] fileExistsP])
    return ([self streamWithFileNamed: name]);

  while (path)
    {
      DECONS (path, dir, path);
      filename = [CO_TLString stringWithFormat: @"%@/%@", dir, name];
      af = fopen ([filename cString], "r");
      if (af)
	return ([self streamWithFILE: af]);
    }
  [self warning: "file %# not found along %#", name, in_path];
  return (nil);
} /* +streamWithFileNamed:alongPath: */

+(id <TLMutableStream>) mutableStreamWithFileNamed: (id <TLString>) name
{
  const char *s = [name cString];
  FILE *af = fopen (s, "w+");

  if (!af)
    {
      [self warning: "open %#: %s", name, ERRMSG];
      return (nil);
    }
  return ([self mutableStreamWithFILE: af]);
} /* +mutableStreamWithFileNamed: */

-initWithFILE: (FILE *) an_f
{
  f = an_f;
  return (self);
} /* -initWithFILE: */

-(void) print: (id <TLMutableStream>) s quoted: (BOOL) qp
{
  formac (s, @"#<%s %p on %p fd=%d eof=%d error=%d>",
	  class_get_class_name (isa), self, f,
	  f ? fileno (f) : 0, f ? feof (f) : 0, f ? ferror(f) : 0);
} /* -print:quoted: */

/******************** TLStream ********************/

-close
{
  FILE *file = f;
  f = NULL;
  return (fclose (file) ? nil : self);
} /* -fclose */

-(int) fileDescriptor
{
  return (f ? fileno (f) : -1);
} /* -fileDescriptor */

-streamp
{
  return (self);
} /* -streamp */

/******************** TLInputStream ********************/

-flushInput
{
  /* We don't do flush input, because of the horrible semantics of
     read/write FILEs.  */
  return (self);
} /* -flushInput */

-(int) readByte
{
  return (getc (f));
} /* -readByte */

-(int) readBytes: (int) n intoBuffer: (char *) b
{
  return (fread (b, 1, n, f));
} /* -readBytes:intoBuffer: */

/******************** TLOutputStream ********************/

-flushOutput
{
  return (fflush (f) ? nil : self);
} /* -flushOutput */

-(int) writeByte: (char) c
{
  return (putc (c, f));
} /* -writeByte: */

-(int) writeBytes: (int) n fromBuffer: (const char *) b
{
  return (fwrite (b, 1, n, f));
} /* -writeBytes:fromBuffer: */

/******************** TLMutableStream ********************/

-(long) _tell
{
  return (ftell (f));
} /* -_tell */

-(long) _seek: (long) offset from: (int) pos
{
  int r;

  switch (pos)
    {
    case TLSEEK_ABSOLUTE: r = SEEK_SET; break;
    case TLSEEK_FROM_END: r = SEEK_END; break;
    case TLSEEK_RELATIVE: r = SEEK_CUR; break;
    default: [self error: "bad TLSEEK %d", pos]; r = 0; break;
    }

  return (fseek (f, offset, r));
} /* -_seek:from: */

@end