12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127 |
- /*++
- Copyright (c) 2013 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- wstream.c
- Abstract:
- This module implements support for wide character stream operations.
- Author:
- Evan Green 28-Aug-2013
- Environment:
- Kernel
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include "libcp.h"
- #include <assert.h>
- #include <errno.h>
- #include <limits.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- BOOL
- ClpFileFormatWriteWideCharacter (
- INT Character,
- PPRINT_FORMAT_CONTEXT Context
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // ------------------------------------------------------------------ Functions
- //
- LIBC_API
- wint_t
- fgetwc (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine retrieves the next wide character from the given file stream.
- Arguments:
- Stream - Supplies a pointer to the file stream.
- Return Value:
- Returns the next wide character in stream on success.
- WEOF on failure or if the end of the file was reached. The error or end of
- file indicators will be set on the stream.
- --*/
- {
- wint_t Result;
- ClpLockStream(Stream);
- Result = fgetwc_unlocked(Stream);
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- wint_t
- fgetwc_unlocked (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine retrieves the next wide character from the given file stream,
- without acquiring the stream lock.
- Arguments:
- Stream - Supplies a pointer to the file stream.
- Return Value:
- Returns the next wide character in stream on success.
- WEOF on failure or if the end of the file was reached. The error or end of
- file indicators will be set on the stream.
- --*/
- {
- int Character;
- CHAR MultibyteBuffer[MB_LEN_MAX];
- ULONG MultibyteSize;
- int Result;
- wchar_t WideCharacter;
- ORIENT_STREAM(Stream, FILE_FLAG_WIDE_ORIENTED);
- //
- // If there's an unget character, return that.
- //
- if ((Stream->Flags & FILE_FLAG_UNGET_VALID) != 0) {
- Stream->Flags &= ~FILE_FLAG_UNGET_VALID;
- return Stream->UngetCharacter;
- }
- //
- // Loop getting normal characters, adding them to the buffer, and then
- // attempting to convert to a wide character.
- //
- MultibyteSize = 0;
- while (MultibyteSize < MB_LEN_MAX) {
- Character = fgetc_unlocked(Stream);
- if (Character == EOF) {
- return EOF;
- }
- MultibyteBuffer[MultibyteSize] = (CHAR)Character;
- MultibyteSize += 1;
- Result = mbrtowc(&WideCharacter,
- MultibyteBuffer,
- MultibyteSize,
- &(Stream->ShiftState));
- if (Result == 0) {
- return L'\0';
- } else if (Result > 0) {
- assert(Result == MultibyteSize);
- return WideCharacter;
- } else if (Result == -1) {
- Stream->Flags |= FILE_FLAG_ERROR;
- return WEOF;
- }
- //
- // -2 means the conversion function needs more characters. Anything
- // else is unexpected.
- //
- assert(Result == -2);
- }
- //
- // It would be weird if the max weren't really enough to convert any
- // characters.
- //
- assert(FALSE);
- Stream->Flags |= FILE_FLAG_ERROR;
- errno = EILSEQ;
- return WEOF;
- }
- LIBC_API
- wint_t
- getwchar (
- void
- )
- /*++
- Routine Description:
- This routine reads one wide character from standard in.
- Arguments:
- None.
- Return Value:
- Returns the wide character from standard in on success.
- WEOF on failure or the end of the file, and errno will contain more
- information.
- --*/
- {
- return fgetwc(stdin);
- }
- LIBC_API
- wint_t
- getwc (
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine reads one wide character from the given file stream. It is
- equivalent to the fgetwc function.
- Arguments:
- Stream - Supplies a pointer to the open file stream.
- Return Value:
- Returns the wide character on success.
- WEOF on failure or the end of the file, and errno will contain more
- information.
- --*/
- {
- return fgetwc(Stream);
- }
- LIBC_API
- wchar_t *
- fgetws (
- wchar_t *Buffer,
- int ElementCount,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine reads wide characters out of the given stream until a newline
- or the maximum number of elements minus one is read. Then the string is
- null terminated.
- Arguments:
- Buffer - Supplies a pointer to the wide character array where the read
- characters will be returned.
- ElementCount - Supplies the maximum number of wide characters to return in
- the given buffer.
- Stream - Supplies a pointer to the file stream to read from.
- Return Value:
- Returns a pointer to the input buffer on success.
- NULL if a read error occurs or the end of the file is reached. If at the
- end of the file, the end of file indicator will be set on the stream. If an
- error occurs, he error indicator will be set for the stream, and the errno
- variable will be set to provide more information.
- --*/
- {
- wchar_t *Result;
- ClpLockStream(Stream);
- Result = fgetws_unlocked(Buffer, ElementCount, Stream);
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- wchar_t *
- fgetws_unlocked (
- wchar_t *Buffer,
- int ElementCount,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine reads wide characters out of the given stream until a newline
- or the maximum number of elements minus one is read. Then the string is
- null terminated. This routine does not acquire the stream lock.
- Arguments:
- Buffer - Supplies a pointer to the wide character array where the read
- characters will be returned.
- ElementCount - Supplies the maximum number of wide characters to return in
- the given buffer.
- Stream - Supplies a pointer to the file stream to read from.
- Return Value:
- Returns a pointer to the input buffer on success.
- NULL if a read error occurs or the end of the file is reached. If at the
- end of the file, the end of file indicator will be set on the stream. If an
- error occurs, he error indicator will be set for the stream, and the errno
- variable will be set to provide more information.
- --*/
- {
- wint_t Character;
- int Index;
- Character = WEOF;
- if ((Buffer == NULL) || (ElementCount < 1)) {
- return NULL;
- }
- //
- // Loop reading in characters until the buffer is full.
- //
- for (Index = 0; Index < ElementCount - 1; Index += 1) {
- Character = fgetwc_unlocked(Stream);
- if (Character == WEOF) {
- break;
- }
- Buffer[Index] = Character;
- Index += 1;
- if (Character == L'\n') {
- break;
- }
- }
- Buffer[Index] = L'\0';
- if (Character == WEOF) {
- return NULL;
- }
- return Buffer;
- }
- LIBC_API
- wint_t
- fputwc (
- wchar_t WideCharacter,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine writes the given wide character out to the given stream.
- Arguments:
- WideCharacter - Supplies the wide character to write.
- Stream - Supplies the stream to write to.
- Return Value:
- Returns the wide character on success.
- EOF on error. The error indicator for the stream will be set and errno will
- be set to contain more information.
- --*/
- {
- wint_t Result;
- ClpLockStream(Stream);
- Result = fputwc_unlocked(WideCharacter, Stream);
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- wint_t
- fputwc_unlocked (
- wchar_t WideCharacter,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine writes the given wide character out to the given stream
- without acquiring the stream lock.
- Arguments:
- WideCharacter - Supplies the wide character to write.
- Stream - Supplies the stream to write to.
- Return Value:
- Returns the wide character on success.
- WEOF on error. The error indicator for the stream will be set and errno will
- be set to contain more information.
- --*/
- {
- CHAR Buffer[MB_LEN_MAX];
- ULONG ByteIndex;
- size_t Length;
- size_t Result;
- ORIENT_STREAM(Stream, FILE_FLAG_WIDE_ORIENTED);
- //
- // Convert the wide character to a multibyte sequence.
- //
- Length = wcrtomb(Buffer, WideCharacter, &(Stream->ShiftState));
- if (Length == -1) {
- Stream->Flags |= FILE_FLAG_ERROR;
- return WEOF;
- }
- //
- // Write the bytes out.
- //
- for (ByteIndex = 0; ByteIndex < Length; ByteIndex += 1) {
- Result = fputc_unlocked(Buffer[ByteIndex], Stream);
- if (Result == EOF) {
- return WEOF;
- }
- }
- return WideCharacter;
- }
- LIBC_API
- wint_t
- putwc (
- wchar_t Character,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine writes a wide character to the given file stream. It is
- equivalent to the fputwc function.
- Arguments:
- Character - Supplies the character to write.
- Stream - Supplies the stream to write the character to.
- Return Value:
- Returns the character it has written on success.
- WEOF on failure, and errno will contain more information.
- --*/
- {
- return fputwc(Character, Stream);
- }
- LIBC_API
- wint_t
- putwchar (
- wchar_t Character
- )
- /*++
- Routine Description:
- This routine writes a wide character to standard out. This routine is
- equivalent to fputwc(Character, stdout).
- Arguments:
- Character - Supplies the character to write.
- Return Value:
- Returns the character it has written on success.
- WEOF on failure, and errno will contain more information.
- --*/
- {
- return fputwc(Character, stdout);
- }
- LIBC_API
- int
- fputws (
- const wchar_t *WideString,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine writes the given null-terminated wide character string to the
- given stream.
- Arguments:
- WideString - Supplies a pointer to the null terminated wide string to write.
- The null terminator itself will not be written.
- Stream - Supplies the stream to write to.
- Return Value:
- Returns a non-negative number on success.
- -1 on failure, and errno will be set to contain more information. The error
- indicator for the stream will also be set.
- --*/
- {
- int Result;
- ClpLockStream(Stream);
- Result = fputws_unlocked(WideString, Stream);
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- int
- fputws_unlocked (
- const wchar_t *WideString,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine writes the given null-terminated wide character string to the
- given stream. This routine does not acquire the stream lock.
- Arguments:
- WideString - Supplies a pointer to the null terminated wide string to write.
- The null terminator itself will not be written.
- Stream - Supplies the stream to write to.
- Return Value:
- Returns a non-negative number on success.
- -1 on failure, and errno will be set to contain more information. The error
- indicator for the stream will also be set.
- --*/
- {
- wint_t Result;
- if (WideString == NULL) {
- return 0;
- }
- while (*WideString != L'\0') {
- Result = fputwc_unlocked(*WideString, Stream);
- if (Result == WEOF) {
- return -1;
- }
- WideString += 1;
- }
- return 0;
- }
- LIBC_API
- wint_t
- ungetwc (
- wint_t Character,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine pushes the specified wide character back onto the input
- stream. The pushed back character shall be returned by subsequent reads on
- that stream in the reverse order of their pushing. A successful intervening
- call seek or flush will discard any pushed back bytes for the stream. One
- character of push back is provided.
- Arguments:
- Character - Supplies the character (converted to a wchar_t) to push back.
- Stream - Supplies the stream to push the character on to.
- Return Value:
- Returns the character pushed back on success.
- EOF on failure, and errno will contain more information.
- --*/
- {
- int Result;
- ClpLockStream(Stream);
- Result = ungetwc_unlocked(Character, Stream);
- ClpUnlockStream(Stream);
- return Result;
- }
- LIBC_API
- wint_t
- ungetwc_unlocked (
- wint_t Character,
- FILE *Stream
- )
- /*++
- Routine Description:
- This routine pushes the specified wide character back onto the input
- stream. The pushed back character shall be returned by subsequent reads on
- that stream in the reverse order of their pushing. A successful intervening
- call seek or flush will discard any pushed back bytes for the stream. One
- character of push back is provided. This routine does not acquire the
- internal stream lock.
- Arguments:
- Character - Supplies the character (converted to a wchar_t) to push back.
- Stream - Supplies the stream to push the character on to.
- Return Value:
- Returns the character pushed back on success.
- EOF on failure, and errno will contain more information.
- --*/
- {
- if (Character == WEOF) {
- return WEOF;
- }
- ORIENT_STREAM(Stream, FILE_FLAG_BYTE_ORIENTED);
- if ((Stream->Flags & FILE_FLAG_UNGET_VALID) == 0) {
- Stream->Flags |= FILE_FLAG_UNGET_VALID;
- Stream->Flags &= ~FILE_FLAG_END_OF_FILE;
- Stream->UngetCharacter = (wchar_t)Character;
- return (wchar_t)Character;
- }
- return WEOF;
- }
- LIBC_API
- int
- fwide (
- FILE *Stream,
- int Mode
- )
- /*++
- Routine Description:
- This routine determines and potentially sets the orientation of the given
- stream.
- Arguments:
- Stream - Supplies a pointer to the stream.
- Mode - Supplies an operation to perform. If this parameter is greater than
- zero, then this routine will attempt to make the stream wide-oriented.
- If this parameter is less than zero, this routine will attempt to make
- the stream byte oriented. If this parameter is 0, no change will be
- made to the stream's orientation.
- Return Value:
- >0 if after this call the stream is wide-oriented.
- <0 if after this call the stream is byte-oriented.
- 0 if the stream has no orientation.
- --*/
- {
- ULONG OrientationFlags;
- if (Mode > 0) {
- ORIENT_STREAM(Stream, FILE_FLAG_WIDE_ORIENTED);
- } else if (Mode < 0) {
- ORIENT_STREAM(Stream, FILE_FLAG_BYTE_ORIENTED);
- }
- OrientationFlags = Stream->Flags;
- if ((OrientationFlags & FILE_FLAG_WIDE_ORIENTED) != 0) {
- return 1;
- } else if ((OrientationFlags & FILE_FLAG_BYTE_ORIENTED) != 0) {
- return -1;
- }
- return 0;
- }
- LIBC_API
- int
- wprintf (
- const wchar_t *Format,
- ...
- )
- /*++
- Routine Description:
- This routine prints a formatted wide string to the standard output file
- stream.
- Arguments:
- Format - Supplies the printf wide format string.
- ... - Supplies a variable number of arguments, as required by the printf
- format string argument.
- Return Value:
- Returns the number of wide characters successfully converted, not
- including the null terminator.
- Returns a negative number if an error was encountered.
- --*/
- {
- va_list Arguments;
- int Result;
- va_start(Arguments, Format);
- Result = vfwprintf(stdout, Format, Arguments);
- va_end(Arguments);
- return Result;
- }
- LIBC_API
- int
- fwprintf (
- FILE *Stream,
- const wchar_t *Format,
- ...
- )
- /*++
- Routine Description:
- This routine prints a formatted wide string to the given file stream.
- Arguments:
- Stream - Supplies the file stream to print to.
- Format - Supplies the printf wide format string.
- ... - Supplies a variable number of arguments, as required by the printf
- format string argument.
- Return Value:
- Returns the number of wide characters successfully converted, not
- including the null terminator.
- Returns a negative number if an error was encountered.
- --*/
- {
- va_list Arguments;
- int Result;
- va_start(Arguments, Format);
- Result = vfwprintf(Stream, Format, Arguments);
- va_end(Arguments);
- return Result;
- }
- LIBC_API
- int
- vfwprintf (
- FILE *File,
- const wchar_t *Format,
- va_list Arguments
- )
- /*++
- Routine Description:
- This routine prints a formatted wide string to the given file pointer.
- Arguments:
- File - Supplies a pointer to the file stream to output to.
- Format - Supplies the printf wide format string.
- Arguments - Supplies the argument list to the format string. The va_end
- macro is not invoked on this list.
- Return Value:
- Returns the number of wide characters successfully converted, not
- including the null terminator.
- Returns a negative number if an error was encountered.
- --*/
- {
- int Result;
- ClpLockStream(File);
- Result = vfwprintf_unlocked(File, Format, Arguments);
- ClpUnlockStream(File);
- return Result;
- }
- LIBC_API
- int
- vfwprintf_unlocked (
- FILE *File,
- const wchar_t *Format,
- va_list Arguments
- )
- /*++
- Routine Description:
- This routine prints a formatted wide string to the given file pointer. This
- routine does not acquire the stream lock.
- Arguments:
- File - Supplies a pointer to the file stream to output to.
- Format - Supplies the printf wide format string.
- Arguments - Supplies the argument list to the format string. The va_end
- macro is not invoked on this list.
- Return Value:
- Returns the number of wide characters successfully converted, not
- including the null terminator.
- Returns a negative number if an error was encountered.
- --*/
- {
- PRINT_FORMAT_CONTEXT PrintContext;
- memset(&PrintContext, 0, sizeof(PRINT_FORMAT_CONTEXT));
- PrintContext.Context = File;
- PrintContext.WriteCharacter = ClpFileFormatWriteWideCharacter;
- RtlFormatWide(&PrintContext, (PWSTR)Format, Arguments);
- return PrintContext.CharactersWritten;
- }
- LIBC_API
- int
- vwprintf (
- const wchar_t *Format,
- va_list Arguments
- )
- /*++
- Routine Description:
- This routine prints a formatted wide string to the standard output file
- stream.
- Arguments:
- Format - Supplies the printf wide format string.
- Arguments - Supplies the argument list to the format string. The va_end
- macro is not invoked on this list.
- Return Value:
- Returns the number of wide characters successfully converted, not
- including the null terminator.
- Returns a negative number if an error was encountered.
- --*/
- {
- return vfwprintf(stdout, Format, Arguments);
- }
- LIBC_API
- int
- swprintf (
- wchar_t *OutputString,
- size_t OutputStringCount,
- const wchar_t *Format,
- ...
- )
- /*++
- Routine Description:
- This routine prints a formatted wide string to the given bounded buffer.
- Arguments:
- OutputString - Supplies the buffer where the formatted wide string will be
- returned.
- OutputStringCount - Supplies the number of wide characters that can fit in
- the output buffer.
- Format - Supplies the printf wide format string.
- ... - Supplies a variable number of arguments, as required by the printf
- format string argument.
- Return Value:
- Returns the number of wide characters successfully converted, not
- including the null terminator.
- Returns a negative number if OutputStringCount or more wide characters
- needed to be converted or if an error was encountered.
- --*/
- {
- va_list Arguments;
- int Result;
- va_start(Arguments, Format);
- Result = vswprintf(OutputString, OutputStringCount, Format, Arguments);
- va_end(Arguments);
- return Result;
- }
- LIBC_API
- int
- vswprintf (
- wchar_t *OutputString,
- size_t OutputStringSize,
- const wchar_t *Format,
- va_list Arguments
- )
- /*++
- Routine Description:
- This routine implements the core string print format function.
- Arguments:
- OutputString - Supplies a pointer to the buffer where the resulting string
- will be written.
- OutputStringSize - Supplies the size of the output string buffer, in bytes.
- If the format is too long for the output buffer, the resulting string
- will be truncated and the last byte will always be a null terminator.
- Format - Supplies the printf format string.
- Arguments - Supplies the argument list to the format string. The va_end
- macro is not invoked on this list.
- Return Value:
- Returns the number of wide characters successfully converted, not
- including the null terminator.
- Returns a negative number if OutputStringCount or more wide characters
- needed to be converted or if an error was encountered.
- --*/
- {
- ULONG Result;
- Result = RtlFormatStringWide(OutputString,
- OutputStringSize,
- CharacterEncodingDefault,
- (PWSTR)Format,
- Arguments);
- if (Result > OutputStringSize) {
- return -1;
- }
- return Result - 1;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- BOOL
- ClpFileFormatWriteWideCharacter (
- INT Character,
- PPRINT_FORMAT_CONTEXT Context
- )
- /*++
- Routine Description:
- This routine writes a wide character to the output during a printf-style
- formatting operation.
- Arguments:
- Character - Supplies the character to be written.
- Context - Supplies a pointer to the printf-context.
- Return Value:
- TRUE on success.
- FALSE on failure.
- --*/
- {
- if (fputwc_unlocked(Character, Context->Context) == -1) {
- return FALSE;
- }
- return TRUE;
- }
|