• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_SCOPED_HANDLE_H_
6 #define BASE_SCOPED_HANDLE_H_
7 
8 #include <stdio.h>
9 
10 #include "base/basictypes.h"
11 
12 #if defined(OS_WIN)
13 #include "base/scoped_handle_win.h"
14 #endif
15 
16 class ScopedStdioHandle {
17  public:
ScopedStdioHandle()18   ScopedStdioHandle()
19       : handle_(NULL) { }
20 
ScopedStdioHandle(FILE * handle)21   explicit ScopedStdioHandle(FILE* handle)
22       : handle_(handle) { }
23 
~ScopedStdioHandle()24   ~ScopedStdioHandle() {
25     Close();
26   }
27 
Close()28   void Close() {
29     if (handle_) {
30       fclose(handle_);
31       handle_ = NULL;
32     }
33   }
34 
get()35   FILE* get() const { return handle_; }
36 
Take()37   FILE* Take() {
38     FILE* temp = handle_;
39     handle_ = NULL;
40     return temp;
41   }
42 
Set(FILE * newhandle)43   void Set(FILE* newhandle) {
44     Close();
45     handle_ = newhandle;
46   }
47 
48  private:
49   FILE* handle_;
50 
51   DISALLOW_EVIL_CONSTRUCTORS(ScopedStdioHandle);
52 };
53 
54 #endif  // BASE_SCOPED_HANDLE_H_
55