YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
cstdio.cpp
浏览该文件的文档.
1 /*
2  © 2009-2014 FrankHB.
3 
4  This file is part of the YSLib project, and may only be used,
5  modified, and distributed under the terms of the YSLib project
6  license, LICENSE.TXT. By continuing to use, modify, or distribute
7  this file you indicate that you have read the license and
8  understand and accept it fully.
9 */
10 
28 #include "ystdex/cstdio.h"
29 #include "ystdex/cassert.h"
30 #include <string> // for std::char_traits<char>::length;
31 
32 namespace ystdex
33 {
34 
35 bool
36 fexists(const char* path) ynothrow
37 {
39  if(const auto fp = std::fopen(path, "rb"))
40  {
41  std::fclose(fp);
42  return true;
43  }
44  return false;
45 }
46 
47 
48 const char*
49 openmode_conv(std::ios_base::openmode mode) ynothrow
50 {
51  using namespace std;
52 
53  switch(unsigned((mode &= ~ios_base::ate) & ~ios_base::binary))
54  {
55  case ios_base::out:
56  case ios_base::out | ios_base::trunc:
57  return mode & ios_base::binary ? "wb" : "w";
58  case ios_base::out | ios_base::app:
59  case ios_base::app:
60  return mode & ios_base::binary ? "ab" : "a";
61  case ios_base::in:
62  return mode & ios_base::binary ? "rb" : "r";
63  case ios_base::in | ios_base::out:
64  return mode & ios_base::binary ? "r+b" : "r+";
65  case ios_base::in | ios_base::out | ios_base::trunc:
66  return mode & ios_base::binary ? "w+b" : "w+";
67  case ios_base::in | ios_base::out | ios_base::app:
68  case ios_base::in | ios_base::app:
69  return mode & ios_base::binary ? "a+b" : "a+";
70  default:
71  break;
72  }
73  return nullptr;
74 }
75 std::ios_base::openmode
76 openmode_conv(const char* str) ynothrow
77 {
78  using namespace std;
79 
80  if(!str)
81  {
82  ios_base::openmode mode;
83 
84  switch(*str)
85  {
86  case 'w':
87  mode = ios_base::out | ios_base::trunc;
88  break;
89  case 'r':
90  mode = ios_base::in;
91  break;
92  case 'a':
93  mode = ios_base::app;
94  break;
95  default:
96  goto invalid;
97  }
98  if(str[1] != char())
99  {
100  auto l(char_traits<char>::length(str));
101 
102  if(str[l - 1] == 'x')
103  {
104  if(mode & ios_base::out)
105  mode &= ~ios_base::out;
106  else
107  goto invalid;
108  --l;
109  }
110 
111  bool b(str[1] == 'b'), p(str[1] == '+');
112 
113  switch(l)
114  {
115  case 2:
116  if(b != p)
117  break;
118  goto invalid;
119  case 3:
120  yunseq(b = b != (str[2] == 'b'), p = p != (str[2] == '+'));
121  if(b && p)
122  break;
123  default:
124  goto invalid;
125  }
126  if(p)
127  mode |= *str == 'r' ? ios::out : ios::in;
128  if(b)
129  mode |= ios::binary;
130  }
131  return mode;
132  }
133 invalid:
134  return ios_base::openmode();
135 }
136 
137 
138 ifile_iterator&
140 {
141  yassume(stream);
142 
143  const auto val(std::fgetc(stream));
144 
145  if(YB_UNLIKELY(val == EOF))
146  stream = {};
147  else
148  {
149  yassume(byte(val) == val);
150  value = byte(val);
151  }
152  return *this;
153 }
154 
155 } // namespace ystdex;
156 
std::FILE ConversionState fp
Definition: chrproc.h:88
ISO C 标准输入/输出扩展。
ifile_iterator & operator++()
Definition: cstdio.cpp:139
std::FILE * stream
流指针。
Definition: cstdio.h:88
一般路径模板。
Definition: path.hpp:149
unsigned char byte
字节类型。
Definition: ydef.h:555
ISO C 断言/调试跟踪扩展。
const char * openmode_conv(std::ios_base::openmode)
Definition: cstdio.cpp:49
#define YB_UNLIKELY(expr)
分支预测提示。
Definition: ydef.h:298
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:748
#define yassume
假定:环境语义。
Definition: cassert.h:58
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
#define yconstraint
约束:接口语义。
Definition: cassert.h:47
char_type value
Definition: cstdio.h:89
bool fexists(const char *)
判断指定路径的文件是否存在。
Definition: cstdio.cpp:36