Laxkit  0.0.7.1
fileorstr.h
1 //
2 // The Laxkit, a windowing toolkit
3 // Please consult http://laxkit.sourceforge.net about where to send any
4 // correspondence about this software.
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Library General Public
8 // License as published by the Free Software Foundation; either
9 // version 2 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Library General Public License for more details.
15 //
16 // You should have received a copy of the GNU Library General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // Copyright (C) 2013 by Tom Lechner
21 //
22 #ifndef _LAX_FILEORSTR_H
23 #define _LAX_FILEORSTR_H
24 
25 
26 
27 ***** /* is this even useful? */ ******
28 
29 // Idea is to have a single interface for various streams.
30 // This might simplify various dump_out and att/css/json/xml conversion systems
31 // These could be:
32 // char[] buffers
33 // files on disk
34 
35 
36 
37 namespace Laxkit {
38 
39 class FileOrStr
40 {
41  protected:
42  char *str;
43  const char *cstr;
44  long n; //num of defined bytes in str or cstr
45  long max; //maximum space allocated
46 
47  char *filename;
48  FILE *f;
49 
50  int errorstate;
51  long curpos;
52 
53  public:
54  FileOrStr();
55  virtual ~FileOrStr();
56 
57  int printf(const char *fmt,...);
58  size_t write(const void *ptr, size_t size, size_t nmemb);
59  size_t fread(void *ptr, size_t size, size_t nmemb);
60  int getline(char **lineptr, size_t *n);
61 
62  int setpos(long offset, int whence=0); //fseek-whence: SEEK_SET, SEEK_CUR, or SEEK_END
63  long curpos();
64  void rewind();
65 
66  void clearerr();
67  int feof();
68 
69  const char *Filename();
70  int Open(const char *filename, const char *mode);
71  int Close();
72  int OpenTempFile();
73 
74  int OpenCString(const char *str); //reads only, does not allocate a new string
75  int OpenInString(char *str, long nn, long nmax); //can read and write within the string, does not allocate new. cannot shrink or grow string past allocation
76  int OpenNewString(const char *str); //copies to a new string, can read, write, and grow the string
77 
78  int SaveStrToFile(const char *filename);
79  int GetStrFromFile(const char *filename);
80 };
81 
82 } //namespace Laxkit;
83 
84 #endif
85 

Mon Feb 17 2014 11:52:56, Laxkit