summaryrefslogtreecommitdiff
path: root/ShellPkg/Library/BaseFileHandleLib/BaseFileHandleLib.c
diff options
context:
space:
mode:
Diffstat (limited to 'ShellPkg/Library/BaseFileHandleLib/BaseFileHandleLib.c')
-rw-r--r--ShellPkg/Library/BaseFileHandleLib/BaseFileHandleLib.c190
1 files changed, 178 insertions, 12 deletions
diff --git a/ShellPkg/Library/BaseFileHandleLib/BaseFileHandleLib.c b/ShellPkg/Library/BaseFileHandleLib/BaseFileHandleLib.c
index ed068243d6..4b2ffb9d95 100644
--- a/ShellPkg/Library/BaseFileHandleLib/BaseFileHandleLib.c
+++ b/ShellPkg/Library/BaseFileHandleLib/BaseFileHandleLib.c
@@ -22,6 +22,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include <Library/MemoryAllocationLib.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
+#include <Library/FileHandleLib.h>
+#include <Library/PcdLib.h>
+#include <Library/PrintLib.h>
#define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)
#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)
@@ -801,19 +804,63 @@ FileHandleGetFileName (
}
/**
+ Function to read a single line from a file. The \n is not included in the returned
+ buffer. The returned buffer must be callee freed.
+
+ If the position upon start is 0, then the Ascii Boolean will be set. This should be
+ maintained and not changed for all operations with the same file.
+
+ @param[in] Handle FileHandle to read from.
+ @param[in,out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);
+
+ @return The line of text from the file.
+
+ @sa FileHandleReadLine
+**/
+CHAR16*
+EFIAPI
+FileHandleReturnLine(
+ IN EFI_FILE_HANDLE Handle,
+ IN OUT BOOLEAN *Ascii
+ )
+{
+ CHAR16 *RetVal;
+ UINTN Size;
+ EFI_STATUS Status;
+
+ Size = 0;
+ RetVal = NULL;
+
+ Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
+ if (Status == EFI_BUFFER_TOO_SMALL) {
+ RetVal = AllocatePool(Size);
+ Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);
+ }
+ ASSERT_EFI_ERROR(Status);
+ if (EFI_ERROR(Status) && (RetVal != NULL)) {
+ FreePool(RetVal);
+ RetVal = NULL;
+ }
+ return (RetVal);
+}
+
+/**
Function to read a single line (up to but not including the \n) from a file.
+ If the position upon start is 0, then the Ascii Boolean will be set. This should be
+ maintained and not changed for all operations with the same file.
+
@param[in] Handle FileHandle to read from
@param[in,out] Buffer pointer to buffer to read into
@param[in,out] Size pointer to number of bytes in buffer
- @param[in[ Truncate if TRUE then allows for truncation of the line to fit.
+ @param[in] Truncate if TRUE then allows for truncation of the line to fit.
if FALSE will reset the position to the begining of the
line if the buffer is not large enough.
+ @param[in,out] Ascii Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);
@retval EFI_SUCCESS the operation was sucessful. the line is stored in
Buffer.
@retval EFI_INVALID_PARAMETER Handle was NULL.
- @retval EFI_INVALID_PARAMETER Buffer was NULL.
@retval EFI_INVALID_PARAMETER Size was NULL.
@retval EFI_BUFFER_TOO_SMALL Size was not enough space to store the line.
Size was updated to minimum space required.
@@ -823,31 +870,54 @@ EFI_STATUS
EFIAPI
FileHandleReadLine(
IN EFI_FILE_HANDLE Handle,
- IN OUT VOID *Buffer,
+ IN OUT CHAR16 *Buffer,
IN OUT UINTN *Size,
- IN BOOLEAN Truncate
+ IN BOOLEAN Truncate,
+ IN OUT BOOLEAN *Ascii
){
EFI_STATUS Status;
CHAR16 CharBuffer;
UINTN CharSize;
UINTN CountSoFar;
- UINT64 Position;
+ UINT64 OriginalFilePosition;
if (Handle == NULL
- ||Buffer == NULL
||Size == NULL
){
return (EFI_INVALID_PARAMETER);
}
- FileHandleGetPosition(Handle, &Position);
+ FileHandleGetPosition(Handle, &OriginalFilePosition);
+ if (OriginalFilePosition == 0) {
+ CharSize = sizeof(CHAR16);
+ Status = FileHandleRead(Handle, &CharSize, &CharBuffer);
+ ASSERT_EFI_ERROR(Status);
+ if (CharBuffer == UnicodeFileTag) {
+ *Ascii = FALSE;
+ } else {
+ *Ascii = TRUE;
+ FileHandleSetPosition(Handle, OriginalFilePosition);
+ }
+ }
for (CountSoFar = 0;;CountSoFar++){
- CharSize = sizeof(CharBuffer);
+ CharBuffer = 0;
+ if (*Ascii) {
+ CharSize = sizeof(CHAR8);
+ } else {
+ CharSize = sizeof(CHAR16);
+ }
Status = FileHandleRead(Handle, &CharSize, &CharBuffer);
+ if (OriginalFilePosition == 0 && *Ascii == FALSE && CountSoFar == 0) {
+ //
+ // we need to skip the unicode tag
+ //
+ continue;
+ }
if ( EFI_ERROR(Status)
|| CharSize == 0
- || CharBuffer == '\n'
+ || (CharBuffer == L'\n' && *Ascii == FALSE)
+ || (CharBuffer == '\n' && *Ascii != FALSE )
){
break;
}
@@ -855,6 +925,7 @@ FileHandleReadLine(
// if we have space save it...
//
if ((CountSoFar+1)*sizeof(CHAR16) < *Size){
+ ASSERT(Buffer != NULL);
((CHAR16*)Buffer)[CountSoFar] = CharBuffer;
((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;
}
@@ -866,13 +937,16 @@ FileHandleReadLine(
if ((CountSoFar+1)*sizeof(CHAR16) > *Size){
*Size = (CountSoFar+1)*sizeof(CHAR16);
if (Truncate == FALSE) {
- FileHandleSetPosition(Handle, Position);
+ FileHandleSetPosition(Handle, OriginalFilePosition);
} else {
- DEBUG((DEBUG_WARN, "The line was truncated in ReadLine"));
+ DEBUG((DEBUG_WARN, "The line was truncated in FileHandleReadLine"));
}
return (EFI_BUFFER_TOO_SMALL);
}
- *Size = (CountSoFar+1)*sizeof(CHAR16);
+ while(Buffer[StrLen(Buffer)-1] == L'\r') {
+ Buffer[StrLen(Buffer)-1] = CHAR_NULL;
+ }
+
return (Status);
}
@@ -913,3 +987,95 @@ FileHandleWriteLine(
Size = StrLen(L"\r\n");
return FileHandleWrite(Handle, &Size, L"\r\n");
}
+
+/**
+ function to take a formatted argument and print it to a file.
+
+ @param[in] Handle the file handle for the file to write to
+ @param[in] Format the format argument (see printlib for format specifier)
+ @param[in] ... the variable arguments for the format
+
+ @retval EFI_SUCCESS the operation was sucessful
+ @return other a return value from FileHandleWriteLine
+
+ @sa FileHandleWriteLine
+**/
+EFI_STATUS
+EFIAPI
+FileHandlePrintLine(
+ IN EFI_FILE_HANDLE Handle,
+ IN CONST CHAR16 *Format,
+ ...
+ )
+{
+ VA_LIST Marker;
+ CHAR16 *Buffer;
+ EFI_STATUS Status;
+
+ VA_START (Marker, Format);
+
+ //
+ // Get a buffer to print into
+ //
+ Buffer = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));
+ ASSERT (Buffer != NULL);
+
+ //
+ // Print into our buffer
+ //
+ UnicodeVSPrint (Buffer, PcdGet16 (PcdShellPrintBufferSize), Format, Marker);
+
+ //
+ // Print buffer into file
+ //
+ Status = FileHandleWriteLine(Handle, Buffer);
+
+ //
+ // Cleanup and return
+ //
+ FreePool(Buffer);
+ return (Status);
+}
+
+/**
+ Function to determine if a FILE_HANDLE is at the end of the file.
+
+ This will NOT work on directories.
+
+ If Handle is NULL, then ASSERT.
+
+ @param[in] Handle the file handle
+
+ @retval TRUE the position is at the end of the file
+ @retval FALSE the position is not at the end of the file
+**/
+BOOLEAN
+EFIAPI
+FileHandleEof(
+ IN EFI_FILE_HANDLE Handle
+ )
+{
+ EFI_FILE_INFO *Info;
+ UINT64 Pos;
+ BOOLEAN RetVal;
+
+ //
+ // ASSERT if Handle is NULL
+ //
+ ASSERT(Handle != NULL);
+
+ FileHandleGetPosition(Handle, &Pos);
+ Info = FileHandleGetInfo (Handle);
+ ASSERT(Info != NULL);
+ FileHandleSetPosition(Handle, Pos);
+
+ if (Info == NULL) {
+ return (FALSE);
+ }
+
+ RetVal = (Pos == Info->FileSize)?TRUE:FALSE;
+
+ FreePool (Info);
+
+ return (RetVal);
+} \ No newline at end of file