add a MemoryBuffer::getOpenFile method, which turns an open
[oota-llvm.git] / include / llvm / Support / MemoryBuffer.h
1 //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the MemoryBuffer interface.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_MEMORYBUFFER_H
15 #define LLVM_SUPPORT_MEMORYBUFFER_H
16
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/System/DataTypes.h"
19 #include <string>
20
21 namespace llvm {
22
23 /// MemoryBuffer - This interface provides simple read-only access to a block
24 /// of memory, and provides simple methods for reading files and standard input
25 /// into a memory buffer.  In addition to basic access to the characters in the
26 /// file, this interface guarantees you can read one character past the end of
27 /// the file, and that this character will read as '\0'.
28 ///
29 /// The '\0' guarantee is needed to support an optimization -- it's intended to
30 /// be more efficient for clients which are reading all the data to stop
31 /// reading when they encounter a '\0' than to continually check the file
32 /// position to see if it has reached the end of the file.
33 class MemoryBuffer {
34   const char *BufferStart; // Start of the buffer.
35   const char *BufferEnd;   // End of the buffer.
36
37   MemoryBuffer(const MemoryBuffer &); // DO NOT IMPLEMENT
38   MemoryBuffer &operator=(const MemoryBuffer &); // DO NOT IMPLEMENT
39 protected:
40   MemoryBuffer() {}
41   void init(const char *BufStart, const char *BufEnd);
42 public:
43   virtual ~MemoryBuffer();
44
45   const char *getBufferStart() const { return BufferStart; }
46   const char *getBufferEnd() const   { return BufferEnd; }
47   size_t getBufferSize() const { return BufferEnd-BufferStart; }
48
49   StringRef getBuffer() const { 
50     return StringRef(BufferStart, getBufferSize()); 
51   }
52
53   /// getBufferIdentifier - Return an identifier for this buffer, typically the
54   /// filename it was read from.
55   virtual const char *getBufferIdentifier() const {
56     return "Unknown buffer";
57   }
58
59   /// getFile - Open the specified file as a MemoryBuffer, returning a new
60   /// MemoryBuffer if successful, otherwise returning null.  If FileSize is
61   /// specified, this means that the client knows that the file exists and that
62   /// it has the specified size.
63   static MemoryBuffer *getFile(StringRef Filename, std::string *ErrStr = 0,
64                                int64_t FileSize = -1);
65   static MemoryBuffer *getFile(const char *Filename, std::string *ErrStr = 0,
66                                int64_t FileSize = -1);
67
68   /// getOpenFile - Given an already-open file descriptor, read the file and
69   /// return a MemoryBuffer.  This takes ownership of the descriptor,
70   /// immediately closing it after reading the file.
71   static MemoryBuffer *getOpenFile(int FD, const char *Filename,
72                                    std::string *ErrStr = 0,
73                                    int64_t FileSize = -1);
74   
75   /// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
76   /// that InputData must be null terminated.
77   static MemoryBuffer *getMemBuffer(StringRef InputData,
78                                     StringRef BufferName = "");
79
80   /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
81   /// copying the contents and taking ownership of it.  InputData does not
82   /// have to be null terminated.
83   static MemoryBuffer *getMemBufferCopy(StringRef InputData,
84                                         StringRef BufferName = "");
85
86   /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
87   /// is completely initialized to zeros.  Note that the caller should
88   /// initialize the memory allocated by this method.  The memory is owned by
89   /// the MemoryBuffer object.
90   static MemoryBuffer *getNewMemBuffer(size_t Size, StringRef BufferName = "");
91
92   /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
93   /// that is not initialized.  Note that the caller should initialize the
94   /// memory allocated by this method.  The memory is owned by the MemoryBuffer
95   /// object.
96   static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
97                                              StringRef BufferName = "");
98
99   /// getSTDIN - Read all of stdin into a file buffer, and return it.
100   /// If an error occurs, this returns null and fills in *ErrStr with a reason.
101   static MemoryBuffer *getSTDIN(std::string *ErrStr = 0);
102
103
104   /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
105   /// if the Filename is "-".  If an error occurs, this returns null and fills
106   /// in *ErrStr with a reason.
107   static MemoryBuffer *getFileOrSTDIN(StringRef Filename,
108                                       std::string *ErrStr = 0,
109                                       int64_t FileSize = -1);
110   static MemoryBuffer *getFileOrSTDIN(const char *Filename,
111                                       std::string *ErrStr = 0,
112                                       int64_t FileSize = -1);
113 };
114
115 } // end namespace llvm
116
117 #endif