eb4784cbf5808ed8a434e388b209e7440eaa62fd
[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/Support/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 /// @verbatim the file, and that this character will read as '\0'. @endverbatim
28 class MemoryBuffer {
29   const char *BufferStart; // Start of the buffer.
30   const char *BufferEnd;   // End of the buffer.
31
32   /// MustDeleteBuffer - True if we allocated this buffer.  If so, the
33   /// destructor must know the delete[] it.
34   bool MustDeleteBuffer;
35 protected:
36   MemoryBuffer() : MustDeleteBuffer(false) {}
37   void init(const char *BufStart, const char *BufEnd);
38   void initCopyOf(const char *BufStart, const char *BufEnd);
39 public:
40   virtual ~MemoryBuffer();
41
42   const char *getBufferStart() const { return BufferStart; }
43   const char *getBufferEnd() const   { return BufferEnd; }
44   size_t getBufferSize() const { return BufferEnd-BufferStart; }
45
46   StringRef getBuffer() const { 
47     return StringRef(BufferStart, getBufferSize()); 
48   }
49
50   /// getBufferIdentifier - Return an identifier for this buffer, typically the
51   /// filename it was read from.
52   virtual const char *getBufferIdentifier() const {
53     return "Unknown buffer";
54   }
55
56   /// getFile - Open the specified file as a MemoryBuffer, returning a new
57   /// MemoryBuffer if successful, otherwise returning null.  If FileSize is
58   /// specified, this means that the client knows that the file exists and that
59   /// it has the specified size.
60   static MemoryBuffer *getFile(const char *Filename,
61                                std::string *ErrStr = 0,
62                                int64_t FileSize = -1);
63
64   /// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
65   /// that EndPtr[0] must be a null byte and be accessible!
66   static MemoryBuffer *getMemBuffer(const char *StartPtr, const char *EndPtr,
67                                     const char *BufferName = "");
68
69   /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
70   /// copying the contents and taking ownership of it.  This has no requirements
71   /// on EndPtr[0].
72   static MemoryBuffer *getMemBufferCopy(const char *StartPtr,const char *EndPtr,
73                                         const char *BufferName = "");
74
75   /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
76   /// is completely initialized to zeros.  Note that the caller should
77   /// initialize the memory allocated by this method.  The memory is owned by
78   /// the MemoryBuffer object.
79   static MemoryBuffer *getNewMemBuffer(size_t Size,
80                                        const char *BufferName = "");
81
82   /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
83   /// that is not initialized.  Note that the caller should initialize the
84   /// memory allocated by this method.  The memory is owned by the MemoryBuffer
85   /// object.
86   static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
87                                              const char *BufferName = "");
88
89   /// getSTDIN - Read all of stdin into a file buffer, and return it.  This
90   /// returns null if stdin is empty.
91   static MemoryBuffer *getSTDIN();
92
93
94   /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
95   /// if the Filename is "-".  If an error occurs, this returns null and fills
96   /// in *ErrStr with a reason.  If stdin is empty, this API (unlike getSTDIN)
97   /// returns an empty buffer.
98   static MemoryBuffer *getFileOrSTDIN(const char *Filename,
99                                       std::string *ErrStr = 0,
100                                       int64_t FileSize = -1);
101
102   /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
103   /// if the Filename is "-".  If an error occurs, this returns null and fills
104   /// in *ErrStr with a reason.
105   static MemoryBuffer *getFileOrSTDIN(const std::string &FN,
106                                       std::string *ErrStr = 0,
107                                       int64_t FileSize = -1) {
108     return getFileOrSTDIN(FN.c_str(), ErrStr, FileSize);
109   }
110 };
111
112 } // end namespace llvm
113
114 #endif