YSTest: cstdio.cpp 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
cstdio.cpp
浏览该文件的文档.
1 ﻿/*
2 Copyright by FrankHB 2009 - 2013.
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 <string> // for std::char_traits<char>::length;
30
31 namespace ystdex
32 {
33
34 bool
35 fexists(const char* path) ynothrow
36 {
37 yconstraint(path);
38
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 ^= str[2] == 'b', 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&
139 ifile_iterator::operator++()
140 {
141 yassume(stream);
142
143 const auto val(std::fgetc(stream));
144
145 if(YB_UNLIKELY(val == EOF))
146 stream = nullptr;
147 else
148 value = val;
149 return *this;
150 }
151
152 } // namespace ystdex;
153