PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/DICK.B1/IronPython.Modules/cStringIO.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 545 lines | 429 code | 100 blank | 16 comment | 48 complexity | a396fa2b930449463ab63ffc8b22e5bf MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Text;
  20. using Microsoft.Scripting.Runtime;
  21. using IronPython.Runtime;
  22. using IronPython.Runtime.Operations;
  23. using IronPython.Runtime.Types;
  24. [assembly: PythonModule("cStringIO", typeof(IronPython.Modules.PythonStringIO))]
  25. namespace IronPython.Modules {
  26. class StringStream {
  27. private StringBuilder _data; // builder used for reading/writing
  28. private string _lastValue; // a cached copy of the builder in string form
  29. private int _position; // our current position in the builder
  30. public StringStream(string data) {
  31. _data = new StringBuilder(_lastValue = data);
  32. _position = 0;
  33. }
  34. public bool EOF {
  35. get { return _position >= _data.Length; }
  36. }
  37. public int Position {
  38. get { return _position; }
  39. }
  40. public string Data {
  41. get {
  42. if (_lastValue == null) {
  43. _lastValue = _data.ToString();
  44. }
  45. return _lastValue;
  46. }
  47. }
  48. public string Prefix {
  49. get {
  50. return _data.ToString(0, _position);
  51. }
  52. }
  53. public string Read(int i) {
  54. if (_position + i > _data.Length) {
  55. i = _data.Length - _position;
  56. }
  57. string ret = _data.ToString(_position, i);
  58. _position += i;
  59. return ret;
  60. }
  61. public string ReadLine(int size) {
  62. if (size < 0) {
  63. size = Int32.MaxValue;
  64. }
  65. int i = _position;
  66. int count = 0;
  67. while (i < _data.Length && count < size) {
  68. char c = _data[i];
  69. if (c == '\n' || c == '\r') {
  70. i++;
  71. if (c == '\r' && _position < _data.Length && _data[i] == '\n') {
  72. i++;
  73. }
  74. // preserve newline character like StringIO
  75. string res = _data.ToString(_position, i - _position);
  76. _position = i;
  77. return res;
  78. }
  79. i++;
  80. count++;
  81. }
  82. if (i > _position) {
  83. string res = _data.ToString(_position, i - _position);
  84. _position = i;
  85. return res;
  86. }
  87. return "";
  88. }
  89. public string ReadToEnd() {
  90. if (_position < _data.Length) {
  91. string res = _data.ToString(_position, _data.Length - _position);
  92. _position = _data.Length;
  93. return res;
  94. }
  95. return String.Empty;
  96. }
  97. public void Reset() {
  98. _position = 0;
  99. }
  100. public int Seek(int offset, SeekOrigin origin) {
  101. switch (origin) {
  102. case SeekOrigin.Begin:
  103. _position = offset; break;
  104. case SeekOrigin.Current:
  105. _position = _position + offset; break;
  106. case SeekOrigin.End:
  107. _position = _data.Length + offset; break;
  108. default:
  109. throw new ArgumentException("origin");
  110. }
  111. return _position;
  112. }
  113. public void Truncate() {
  114. _lastValue = null;
  115. _data.Length = _position;
  116. }
  117. public void Truncate(int size) {
  118. _lastValue = null;
  119. if (size > _data.Length) {
  120. size = _data.Length;
  121. } else if (size < 0) {
  122. throw PythonOps.IOError("(22, 'Negative size not allowed')");
  123. }
  124. _data.Length = size;
  125. _position = size;
  126. }
  127. internal void Write(string s) {
  128. if (_data.Length < _position) {
  129. _data.Length = _position;
  130. }
  131. _lastValue = null;
  132. if (_position == _data.Length) {
  133. _data.Append(s);
  134. } else {
  135. // replace the existing text
  136. _data.Remove(_position, Math.Min(s.Length, _data.Length - _position));
  137. _data.Insert(_position, s);
  138. }
  139. _position += s.Length;
  140. }
  141. }
  142. public static class PythonStringIO {
  143. public static PythonType InputType = DynamicHelpers.GetPythonTypeFromType(typeof(StringI));
  144. public static PythonType OutputType = DynamicHelpers.GetPythonTypeFromType(typeof(StringO));
  145. public const string __doc__ = "Provides file like objects for reading and writing to strings.";
  146. [PythonType, PythonHidden]
  147. public class StringI : IEnumerator<string>, IEnumerator {
  148. private StringStream _sr;
  149. private string _enumValue;
  150. internal StringI(string data) {
  151. _sr = new StringStream(data);
  152. }
  153. public void close() {
  154. _sr = null;
  155. }
  156. public bool closed {
  157. get {
  158. return _sr == null;
  159. }
  160. }
  161. public void flush() {
  162. ThrowIfClosed();
  163. }
  164. public string getvalue() {
  165. ThrowIfClosed();
  166. return _sr.Data;
  167. }
  168. public string getvalue(bool usePos) {
  169. return _sr.Prefix;
  170. }
  171. public bool isatty() {
  172. ThrowIfClosed();
  173. return false;
  174. }
  175. public object __iter__() {
  176. return this;
  177. }
  178. public string next() {
  179. ThrowIfClosed();
  180. if (_sr.EOF) {
  181. throw PythonOps.StopIteration();
  182. }
  183. return readline();
  184. }
  185. public string read() {
  186. ThrowIfClosed();
  187. return _sr.ReadToEnd();
  188. }
  189. public string read(int s) {
  190. ThrowIfClosed();
  191. return (s < 0) ? _sr.ReadToEnd() : _sr.Read(s);
  192. }
  193. public string readline() {
  194. ThrowIfClosed();
  195. return _sr.ReadLine(-1);
  196. }
  197. public string readline(int size) {
  198. ThrowIfClosed();
  199. return _sr.ReadLine(size);
  200. }
  201. public List readlines() {
  202. ThrowIfClosed();
  203. List list = PythonOps.MakeList();
  204. while (!_sr.EOF) {
  205. list.AddNoLock(readline());
  206. }
  207. return list;
  208. }
  209. public List readlines(int size) {
  210. ThrowIfClosed();
  211. List list = PythonOps.MakeList();
  212. while (!_sr.EOF) {
  213. string line = readline();
  214. list.AddNoLock(line);
  215. if (line.Length >= size) break;
  216. size -= line.Length;
  217. }
  218. return list;
  219. }
  220. public void reset() {
  221. ThrowIfClosed();
  222. _sr.Reset();
  223. }
  224. public void seek(int position) {
  225. seek(position, 0);
  226. }
  227. public void seek(int position, int mode) {
  228. ThrowIfClosed();
  229. SeekOrigin so;
  230. switch (mode) {
  231. case 1: so = SeekOrigin.Current; break;
  232. case 2: so = SeekOrigin.End; break;
  233. default: so = SeekOrigin.Begin; break;
  234. }
  235. _sr.Seek(position, so);
  236. }
  237. public int tell() {
  238. ThrowIfClosed();
  239. return _sr.Position;
  240. }
  241. public void truncate() {
  242. ThrowIfClosed();
  243. _sr.Truncate();
  244. }
  245. public void truncate(int size) {
  246. ThrowIfClosed();
  247. _sr.Truncate(size);
  248. }
  249. private void ThrowIfClosed() {
  250. if (closed) {
  251. throw PythonOps.ValueError("I/O operation on closed file");
  252. }
  253. }
  254. #region IEnumerator Members
  255. object IEnumerator.Current {
  256. get { return _enumValue; }
  257. }
  258. bool IEnumerator.MoveNext() {
  259. if (!_sr.EOF) {
  260. _enumValue = readline();
  261. return true;
  262. }
  263. _enumValue = null;
  264. return false;
  265. }
  266. void IEnumerator.Reset() {
  267. throw new NotImplementedException();
  268. }
  269. #endregion
  270. #region IEnumerator<string> Members
  271. string IEnumerator<string>.Current {
  272. get { return _enumValue; }
  273. }
  274. #endregion
  275. #region IDisposable Members
  276. void IDisposable.Dispose() {
  277. }
  278. #endregion
  279. }
  280. [PythonType, PythonHidden, DontMapIEnumerableToContains]
  281. public class StringO : IEnumerator<string>, IEnumerator {
  282. private StringStream _sr = new StringStream("");
  283. private int _softspace;
  284. private string _enumValue;
  285. internal StringO() {
  286. }
  287. public object __iter__() {
  288. return this;
  289. }
  290. public void close() {
  291. if (_sr != null) { _sr = null; }
  292. }
  293. public bool closed {
  294. get {
  295. return _sr == null;
  296. }
  297. }
  298. public void flush() {
  299. }
  300. public string getvalue() {
  301. ThrowIfClosed();
  302. return _sr.Data;
  303. }
  304. public string getvalue(bool usePos) {
  305. ThrowIfClosed();
  306. return _sr.Prefix;
  307. }
  308. public bool isatty() {
  309. ThrowIfClosed();
  310. return false;
  311. }
  312. public string next() {
  313. ThrowIfClosed();
  314. if (_sr.EOF) {
  315. throw PythonOps.StopIteration();
  316. }
  317. return readline();
  318. }
  319. public string read() {
  320. ThrowIfClosed();
  321. return _sr.ReadToEnd();
  322. }
  323. public string read(int i) {
  324. ThrowIfClosed();
  325. return (i < 0) ? _sr.ReadToEnd() : _sr.Read(i);
  326. }
  327. public string readline() {
  328. ThrowIfClosed();
  329. return _sr.ReadLine(-1);
  330. }
  331. public string readline(int size) {
  332. ThrowIfClosed();
  333. return _sr.ReadLine(size);
  334. }
  335. public List readlines() {
  336. ThrowIfClosed();
  337. List list = PythonOps.MakeList();
  338. while (!_sr.EOF) {
  339. list.AddNoLock(readline());
  340. }
  341. return list;
  342. }
  343. public List readlines(int size) {
  344. ThrowIfClosed();
  345. List list = PythonOps.MakeList();
  346. while (!_sr.EOF) {
  347. string line = readline();
  348. list.AddNoLock(line);
  349. if (line.Length >= size) break;
  350. size -= line.Length;
  351. }
  352. return list;
  353. }
  354. public void reset() {
  355. ThrowIfClosed();
  356. _sr.Reset();
  357. }
  358. public void seek(int position) {
  359. seek(position, 0);
  360. }
  361. public void seek(int offset, int origin) {
  362. ThrowIfClosed();
  363. SeekOrigin so;
  364. switch (origin) {
  365. case 1: so = SeekOrigin.Current; break;
  366. case 2: so = SeekOrigin.End; break;
  367. default: so = SeekOrigin.Begin; break;
  368. }
  369. _sr.Seek(offset, so);
  370. }
  371. public int softspace {
  372. get { return _softspace; }
  373. set { _softspace = value; }
  374. }
  375. public int tell() {
  376. ThrowIfClosed();
  377. return _sr.Position;
  378. }
  379. public void truncate() {
  380. ThrowIfClosed();
  381. _sr.Truncate();
  382. }
  383. public void truncate(int size) {
  384. ThrowIfClosed();
  385. _sr.Truncate(size);
  386. }
  387. public void write(string s) {
  388. if (s == null) {
  389. throw PythonOps.TypeError("write argument must be a string or read-only character buffer, not None");
  390. }
  391. ThrowIfClosed();
  392. _sr.Write(s);
  393. }
  394. public void write([NotNull]PythonBuffer buffer) {
  395. _sr.Write(buffer.ToString());
  396. }
  397. public void writelines(object o) {
  398. IEnumerator e = PythonOps.GetEnumerator(o);
  399. while (e.MoveNext()) {
  400. string s = e.Current as string;
  401. if (s == null) {
  402. throw PythonOps.TypeError("string expected");
  403. }
  404. write(s);
  405. }
  406. }
  407. private void ThrowIfClosed() {
  408. if (closed) {
  409. throw PythonOps.ValueError("I/O operation on closed file");
  410. }
  411. }
  412. #region IEnumerator Members
  413. object IEnumerator.Current {
  414. get { return _enumValue; }
  415. }
  416. bool IEnumerator.MoveNext() {
  417. if (!_sr.EOF) {
  418. _enumValue = readline();
  419. return true;
  420. }
  421. _enumValue = null;
  422. return false;
  423. }
  424. void IEnumerator.Reset() {
  425. throw new NotImplementedException();
  426. }
  427. #endregion
  428. #region IEnumerator<string> Members
  429. string IEnumerator<string>.Current {
  430. get { return _enumValue; }
  431. }
  432. #endregion
  433. #region IDisposable Members
  434. void IDisposable.Dispose() {
  435. }
  436. #endregion
  437. }
  438. public static object StringIO() {
  439. return new StringO();
  440. }
  441. public static object StringIO(string data) {
  442. return new StringI(data);
  443. }
  444. }
  445. }