temp_file.h 2.55 KB
Newer Older
gejun's avatar
gejun committed
1
// Copyright (c) 2010 Baidu, Inc.
gejun's avatar
gejun committed
2
// 
gejun's avatar
gejun committed
3 4 5
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
gejun's avatar
gejun committed
6
// 
gejun's avatar
gejun committed
7 8 9 10 11 12 13 14 15 16
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Author: Yan,Lin (yanlin@baidu.com)
//         Ge,Jun (gejun@baidu.com)
gejun's avatar
gejun committed
17 18
// Date: Thu Oct 28 15:23:09 2010

19 20
#ifndef BUTIL_FILES_TEMP_FILE_H
#define BUTIL_FILES_TEMP_FILE_H
gejun's avatar
gejun committed
21

22
#include "butil/macros.h"
gejun's avatar
gejun committed
23

24
namespace butil {
gejun's avatar
gejun committed
25 26 27 28 29 30 31 32 33 34 35

// Create a temporary file in current directory, which will be deleted when 
// corresponding TempFile object destructs, typically for unit testing.
// 
// Usage:
//   { 
//      TempFile tmpfile;           // A temporay file shall be created
//      tmpfile.save("some text");  // Write into the temporary file
//   }
//   // The temporary file shall be removed due to destruction of tmpfile
 
gejun's avatar
gejun committed
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
class TempFile {
public:
    // Create a temporary file in current directory. If |ext| is given,
    // filename will be temp_file_XXXXXX.|ext|, temp_file_XXXXXX otherwise.
    // If temporary file cannot be created, all save*() functions will
    // return -1. If |ext| is too long, filename will be truncated.
    TempFile();
    explicit TempFile(const char* ext);

    // The temporary file is removed in destructor.
    ~TempFile();

    // Save |content| to file, overwriting existing file.
    // Returns 0 when successful, -1 otherwise.
    int save(const char *content);

    // Save |fmt| and associated values to file, overwriting existing file.
    // Returns 0 when successful, -1 otherwise.
    int save_format(const char *fmt, ...) __attribute__((format (printf, 2, 3) ));

    // Save binary data |buf| (|count| bytes) to file, overwriting existing file.
    // Returns 0 when successful, -1 otherwise.
    int save_bin(const void *buf, size_t count);
    
    // Get name of the temporary file.
    const char *fname() const { return _fname; }

private:
    // TempFile is associated with file, copying makes no sense.
    DISALLOW_COPY_AND_ASSIGN(TempFile);
    
    int _reopen_if_necessary();
    
    int _fd;                // file descriptor
    int _ever_opened;
    char _fname[24];        // name of the file
};

74
} // namespace butil
gejun's avatar
gejun committed
75

76
#endif  // BUTIL_FILES_TEMP_FILE_H