Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

491 lines
10 KiB

  1. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. Copyright (c) 1989-1999 Microsoft Corporation
  3. Module Name:
  4. stream.hxx
  5. Abstract:
  6. This module provides class definitions for the stream class.
  7. Notes:
  8. The stream class simulates either a memory buffer stream or a
  9. file stream, but provides the caller with a consistent interface.
  10. Author:
  11. VibhasC Jun-11-1993 Created.
  12. Notes:
  13. NOTE !! NO MEMORY STREAM YET.
  14. ----------------------------------------------------------------------------*/
  15. #ifndef __STREAM_HXX__
  16. #define __STREAM_HXX__
  17. /****************************************************************************
  18. * include files
  19. ***************************************************************************/
  20. #include "nulldefs.h"
  21. extern "C"
  22. {
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <stddef.h>
  26. #include <string.h>
  27. }
  28. #include "common.hxx"
  29. /****************************************************************************
  30. * local data
  31. ***************************************************************************/
  32. /****************************************************************************
  33. * externs
  34. ***************************************************************************/
  35. /****************************************************************************
  36. * definitions
  37. ***************************************************************************/
  38. //
  39. // definitions related to streams.
  40. //
  41. typedef unsigned char STREAM_TYPE;
  42. #define STREAM_MEMORY 0
  43. #define STREAM_FILE 1
  44. #define STREAM_NULL 2
  45. typedef unsigned char STREAM_MODE;
  46. #define STREAM_TEXT 0
  47. #define STREAM_BINARY 1
  48. #define DEFAULT_MEM_SIZE_FOR_STREAM 1024
  49. #define DEFAULT_MEM_INCR_FOR_STREAM 1024
  50. // the requested kind of file protection
  51. #define FILE_STREAM_OVERWRITE 0
  52. #define FILE_STREAM_REWRITE 1
  53. #define FILE_STREAM_WRITE_ONCE 2
  54. // the result of the above protection
  55. #define FILE_STATUS_OK 0 // the file was opened ok
  56. #define FILE_STATUS_TEMP 1 // a temp file was opened instead
  57. #define FILE_STATUS_NO_WRITE 2 // no file at all
  58. //
  59. // The stream class itself.
  60. //
  61. class STREAM
  62. {
  63. protected:
  64. //
  65. // this field identifies the stream to be either a file stream or a
  66. // memory stream. The memory stream is the default stream.
  67. //
  68. unsigned char StreamType;
  69. unsigned char StreamMode;
  70. unsigned char fEnd;
  71. unsigned char fError;
  72. unsigned char fIgnore;
  73. unsigned char fConsoleStream;
  74. unsigned char StreamOpenStatus;
  75. char * pSpecialCommentString; // set if not // or /* */
  76. union
  77. {
  78. struct
  79. {
  80. FILE * pHandle;
  81. } F;
  82. struct
  83. {
  84. int InitialSize;
  85. int Increment;
  86. char * pCurrent;
  87. char * pMem;
  88. char * pEnd;
  89. } M;
  90. } S;
  91. public:
  92. //
  93. // construct a class either as a file stream class or a memory buffer class
  94. //
  95. STREAM( char * pFileName, unsigned char SProt = FILE_STREAM_OVERWRITE );
  96. //
  97. // The user could encapulate an existing file into the stream and use it
  98. // from then on.
  99. //
  100. STREAM( FILE * pFile );
  101. //
  102. // construct a memory stream. By default the memory stream is constructed
  103. // as a buffer of 1024 bytes, incremented by 1024 bytes, unless specified
  104. // by the creator.
  105. STREAM();
  106. //
  107. // The user could specify an initial size and increment factor. If either
  108. // are 0, then the user leaves that decision to the constructor, which
  109. // chooses the default values.
  110. STREAM( int, int );
  111. //
  112. // The destructor. If it is a file stream, close it. If it is a memory
  113. // stream, release the memory.
  114. //
  115. ~STREAM();
  116. //
  117. // queries.
  118. //
  119. int SetInitialSize( int InitSize );
  120. int GetInitialSize();
  121. int SetInitialIncr( int Incr );
  122. int GetInitialIncr();
  123. char * Expand();
  124. char * ExpandBy( short Amt );
  125. char * SetCurrentPtr( char * pCur );
  126. char * GetCurrentPtr();
  127. long GetCurrentPosition();
  128. void SetCurrentPosition( long Position );
  129. char * SetStart( char * pCur );
  130. char * GetStart();
  131. char * SetMemStreamEnd( char * p )
  132. {
  133. return (S.M.pEnd = p);
  134. }
  135. void SetConsoleStream()
  136. {
  137. fConsoleStream = 1;
  138. }
  139. void ResetConsoleStream()
  140. {
  141. fConsoleStream = 0;
  142. }
  143. BOOL IsConsoleStream()
  144. {
  145. return (fConsoleStream != 0 );
  146. }
  147. STREAM_TYPE GetStreamType()
  148. {
  149. return StreamType;
  150. }
  151. char * NewCopy();
  152. void SetStreamType( STREAM_TYPE S )
  153. {
  154. StreamType = S;
  155. }
  156. STREAM_MODE GetStreamMode()
  157. {
  158. return StreamMode;
  159. }
  160. void SetStreamMode( STREAM_MODE mode );
  161. BOOL IsEnd()
  162. {
  163. return (BOOL) (fEnd != 0);
  164. }
  165. void SetEnd()
  166. {
  167. fEnd = 1;
  168. }
  169. void ResetEnd()
  170. {
  171. fEnd = 0;
  172. }
  173. BOOL IsError()
  174. {
  175. return (BOOL) ( fError != 0 );
  176. }
  177. void ResetError()
  178. {
  179. fError = 0;
  180. }
  181. void SetError()
  182. {
  183. fError = 1;
  184. }
  185. void SetIgnore()
  186. {
  187. fIgnore = 1;
  188. }
  189. BOOL IsIgnore()
  190. {
  191. return fIgnore;
  192. }
  193. void ResetIgnore()
  194. {
  195. fIgnore = 0;
  196. }
  197. BOOL IsTempStream()
  198. {
  199. return (BOOL) ( StreamOpenStatus == FILE_STATUS_TEMP );
  200. }
  201. BOOL IsBlockedStream()
  202. {
  203. return (BOOL) ( StreamOpenStatus == FILE_STATUS_NO_WRITE );
  204. }
  205. //
  206. // Write into the stream. There are character or string based writes or
  207. // writes from other streams.
  208. //
  209. //
  210. // Write a character into the stream.
  211. //
  212. void Write( char C )
  213. {
  214. if ( ( GetStreamType() == STREAM_NULL ) || IsError() || IsIgnore() )
  215. return;
  216. if( (GetStreamType() == STREAM_FILE ) )
  217. putc( C, S.F.pHandle );
  218. else
  219. {
  220. if( S.M.pCurrent >= S.M.pEnd )
  221. {
  222. Expand();
  223. }
  224. *(S.M.pCurrent)++ = C;
  225. }
  226. }
  227. //
  228. // write a memory buffer into the stream.
  229. //
  230. void Write( const void * const p, int length );
  231. //
  232. // write a string into the stream.
  233. //
  234. void Write( const char * const pC );
  235. //
  236. // write a number into the stream, with a printf-style format.
  237. //
  238. void WriteNumber( const char * pFmt, const unsigned long ul );
  239. void WriteFormat( const char * pFmt, ... );
  240. //
  241. // flush the stream. This is ignored by memory buffer streams.
  242. //
  243. void Flush();
  244. //
  245. // close the stream.
  246. //
  247. void Close();
  248. char * SetSpecialCommentString( char * P )
  249. {
  250. return (pSpecialCommentString = P);
  251. }
  252. char * GetSpecialCommentString()
  253. {
  254. return pSpecialCommentString;
  255. }
  256. void EmitSpecialCommentString()
  257. {
  258. if ( pSpecialCommentString )
  259. Write( pSpecialCommentString );
  260. }
  261. };
  262. /////////////////////////////////////////////////////////////////////////////
  263. // The indentation aware stream
  264. /////////////////////////////////////////////////////////////////////////////
  265. //
  266. // The need for this class stems mainly from the need of the output routines
  267. // of the midl20 code generator. We could have used an intendation manager class
  268. // but that would mean dealing with 2 classes instead of just one and hence
  269. // the choice to implement an indentation stream.
  270. // We will however support ONLY the file based stream here. This is ensured
  271. // by implementing only those constructors that have signatures suitable for
  272. // file streams.
  273. //
  274. class ISTREAM : public STREAM
  275. {
  276. private:
  277. short CurrentIndent;
  278. short PreferredIndent;
  279. public:
  280. //
  281. // The constructors. Suitable ONLY for file streams.
  282. //
  283. ISTREAM( char * pFileName,
  284. short PrefIndent, unsigned char SProt = FILE_STREAM_OVERWRITE )
  285. : STREAM( pFileName, SProt )
  286. {
  287. CurrentIndent = 0;
  288. PreferredIndent = PrefIndent;
  289. }
  290. ISTREAM() : STREAM(1024,1024)
  291. {
  292. }
  293. ~ISTREAM()
  294. {
  295. }
  296. //
  297. // Get and set functions.
  298. //
  299. short SetIndent( short I )
  300. {
  301. return (CurrentIndent = I);
  302. }
  303. short GetIndent()
  304. {
  305. return CurrentIndent;
  306. }
  307. short SetPreferredIndent( short P )
  308. {
  309. return (PreferredIndent = P);
  310. }
  311. short GetPreferredIndent()
  312. {
  313. return PreferredIndent;
  314. }
  315. short IndentInc()
  316. {
  317. return CurrentIndent = (short) (CurrentIndent + PreferredIndent);
  318. }
  319. short IndentDec()
  320. {
  321. if((CurrentIndent - PreferredIndent) < 0 )
  322. return SetIndent(0);
  323. else
  324. return SetIndent( ( short ) ( CurrentIndent - PreferredIndent ) );
  325. }
  326. //
  327. // This writes a newline and readies the stream for the next string to
  328. // go to the current indent.
  329. //
  330. void NewLine();
  331. void NewLine( unsigned short count );
  332. //
  333. // This method writes a given number of spaces.
  334. //
  335. void Spaces( unsigned short NoOfSpaces );
  336. //
  337. // This method writes the string after printing a new line.
  338. //
  339. void WriteOnNewLine( const char * pS )
  340. {
  341. NewLine();
  342. Write( pS );
  343. }
  344. void WriteOnNewLine( char ch )
  345. {
  346. NewLine();
  347. Write( ch );
  348. }
  349. //
  350. // write a series of memory buffers into the stream.
  351. // the last string should be NULL (not "" )
  352. //
  353. void WriteBlock( const char * const * pC )
  354. {
  355. while ( *pC )
  356. {
  357. NewLine();
  358. Write( *pC );
  359. pC++;
  360. }
  361. }
  362. };
  363. // a handy type for the WriteBlock above
  364. typedef const char * const STRING_BLOCK[];
  365. #endif // __STREAM_HXX__