Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Reader.cpp
Go to the documentation of this file.
1 /* Project STARS
2  John DiCamillo Software Consulting
3  Copyright (C) 1997-2000. All Rights Reserved.
4 
5  SUBSYSTEM: obelisk
6  FILE: reader.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Implementation of the Reader class
13 */
14 
15 #include "MemDebug.h"
16 #include "reader.h"
17 #include <stdio.h>
18 #include <fstream>
19 #include <ctype.h>
20 
21 
22 void Print(const char* fmt, ...);
23 
24 // +-------------------------------------------------------------------+
25 
26 Text
28 {
29  // loop until the user types something
30  do {
33  } while (! *p);
34 
35  return Text(p);
36 }
37 
38 void
40 {
41  printf("- ");
42 }
43 
44 void
46 {
47  fgets(buffer, 980, stdin);
48  p = buffer;
49  while (isspace(*p)) p++;
50 }
51 
52 // +-------------------------------------------------------------------+
53 
54 FileReader::FileReader(const char* fname)
55  : filename(fname), done(0)
56 { }
57 
58 Text
60 {
61  if (done) return Text();
62 
63  std::fstream fin(filename, std::fstream::in);
64 
65  if (!fin) {
66  Print("ERROR(Parse): Could not open file '%s'\n", filename.data());
67  return Text();
68  }
69 
70  Text result;
71  char buf[1000], newline;
72 
73  while (fin.get(buf, 1000)) {
74  result.append(buf);
75  fin.get(newline);
76  result.append(newline);
77  }
78 
79  done = 1;
80  return result;
81 }
82 
83 // +-------------------------------------------------------------------+
84 
85 BlockReader::BlockReader(const char* block)
86  : data((char*) block), done(0), length(0)
87 { }
88 
89 BlockReader::BlockReader(const char* block, int len)
90  : data((char*) block), done(0), length(len)
91 { }
92 
93 Text
95 {
96  if (done) return Text();
97 
98  if (length) {
99  Text result(data, length);
100  done = 1;
101  return result;
102  }
103  else if (data) {
104  Text result(data);
105  done = 1;
106  return result;
107  }
108 
109  done = 1;
110  return Text();
111 }
112 
113