Home | History | Annotate | Download | only in base
      1 /*
      2  *  Copyright 2014 The WebRTC Project Authors. All rights reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #include "webrtc/base/platform_file.h"
     12 
     13 #if defined(WEBRTC_WIN)
     14 #include <io.h>
     15 #else
     16 #include <unistd.h>
     17 #endif
     18 
     19 namespace rtc {
     20 
     21 #if defined(WEBRTC_WIN)
     22 const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE;
     23 
     24 FILE* FdopenPlatformFileForWriting(PlatformFile file) {
     25   if (file == kInvalidPlatformFileValue)
     26     return NULL;
     27   int fd = _open_osfhandle(reinterpret_cast<intptr_t>(file), 0);
     28   if (fd < 0)
     29     return NULL;
     30 
     31   return _fdopen(fd, "w");
     32 }
     33 
     34 bool ClosePlatformFile(PlatformFile file) {
     35   return CloseHandle(file) != 0;
     36 }
     37 #else
     38 const PlatformFile kInvalidPlatformFileValue = -1;
     39 
     40 FILE* FdopenPlatformFileForWriting(PlatformFile file) {
     41   return fdopen(file, "w");
     42 }
     43 
     44 bool ClosePlatformFile(PlatformFile file) {
     45   return close(file);
     46 }
     47 #endif
     48 
     49 }  // namespace rtc
     50