/** @file
Functions to deal with file buffer.
Copyright (c) 2005 - 2011, Intel Corporation. All rights reserved. <BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "HexEditor.h"
extern EFI_HANDLE HImageHandleBackup;
extern HEFI_EDITOR_BUFFER_IMAGE HBufferImage;
extern BOOLEAN HBufferImageNeedRefresh;
extern BOOLEAN HBufferImageMouseNeedRefresh;
extern HEFI_EDITOR_GLOBAL_EDITOR HMainEditor;
//
// for basic initialization of HFileImage
//
NULL,
0,
};
/**
Initialization function for HFileImage
@retval EFI_SUCCESS The operation was successful.
**/
)
{
//
// basically initialize the HFileImage
//
CopyMem (
sizeof (HFileImageBackupVar)
);
return EFI_SUCCESS;
}
/**
Backup function for HFileImage. Only a few fields need to be backup.
This is for making the file buffer refresh as few as possible.
@retval EFI_SUCCESS The operation was successful.
@retval EFI_OUT_OF_RESOURCES A memory allocation failed.
**/
)
{
return EFI_OUT_OF_RESOURCES;
}
return EFI_SUCCESS;
}
/**
Cleanup function for HFileImage.
@retval EFI_SUCCESS The operation was successful.
**/
)
{
return EFI_SUCCESS;
}
/**
Set FileName field in HFileImage
@param[in] Str File name to set.
@retval EFI_SUCCESS The operation was successful.
@retval EFI_OUT_OF_RESOURCES A memory allocation failed.
**/
)
{
//
// free the old file name
//
return EFI_OUT_OF_RESOURCES;
}
}
return EFI_SUCCESS;
}
/**
Read a file from disk into HBufferImage.
@param[in] FileName filename to read.
@param[in] Recover if is for recover, no information print.
@retval EFI_SUCCESS The operation was successful.
@retval EFI_OUT_OF_RESOURCES A memory allocation failed.
@retval EFI_LOAD_ERROR A load error occured.
**/
)
{
//
// variable initialization
//
//
// in this function, when you return error ( except EFI_OUT_OF_RESOURCES )
// you should set status string
// since this function maybe called before the editorhandleinput loop
// so any error will cause editor return
// so if you want to print the error status
// you should set the status string
//
//
// NULL pointer is only also a failure for a non-zero file size.
//
if (UnicodeBuffer == NULL) {
return EFI_OUT_OF_RESOURCES;
}
return EFI_OUT_OF_RESOURCES;
}
//
// free the old lines
//
HBufferImageFree ();
StatusBarSetStatusString (L"Error parsing file.");
return Status;
}
if (!Recover) {
if (UnicodeBuffer == NULL) {
return EFI_OUT_OF_RESOURCES;
}
HMainEditor.SelectStart = 0;
HMainEditor.SelectEnd = 0;
}
//
// has line
//
if (HBufferImage.Lines != 0) {
HBufferImage.CurrentLine = CR (HBufferImage.ListHead->ForwardLink, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);
} else {
//
// create a dummy line
//
Line = HBufferImageCreateLine ();
return EFI_OUT_OF_RESOURCES;
}
}
return EFI_SUCCESS;
}
/**
Save lines in HBufferImage to disk.
@param[in] FileName The file name.
@retval EFI_SUCCESS The operation was successful.
@retval EFI_OUT_OF_RESOURCES A memory allocation failed.
@retval EFI_LOAD_ERROR A load error occured.
**/
)
{
//
// if is the old file
//
if (HFileImage.FileName != NULL && FileName != NULL && StrCmp (FileName, HFileImage.FileName) == 0) {
//
// check whether file exists on disk
//
//
// current file exists on disk
// so if not modified, then not save
//
return EFI_SUCCESS;
}
//
// if file is read-only, set error
//
StatusBarSetStatusString (L"Read Only File Can Not Be Saved");
return EFI_SUCCESS;
}
}
}
StatusBarSetStatusString (L"Directory Can Not Be Saved");
return EFI_LOAD_ERROR;
}
//
// the file exits, delete it
//
StatusBarSetStatusString (L"Write File Failed");
return EFI_LOAD_ERROR;
}
}
//
// write all the lines back to disk
//
NumLines = 0;
TotalSize = 0;
for (Link = HBufferImage.ListHead->ForwardLink; Link != HBufferImage.ListHead; Link = Link->ForwardLink) {
}
//
// end of if Line -> Size != 0
//
NumLines++;
}
//
// end of for Link
//
return EFI_OUT_OF_RESOURCES;
}
for (Link = HBufferImage.ListHead->ForwardLink; Link != HBufferImage.ListHead; Link = Link->ForwardLink) {
}
//
// end of if Line -> Size != 0
//
}
Status = ShellOpenFileByName (FileName, &FileHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, 0);
StatusBarSetStatusString (L"Create File Failed");
return EFI_LOAD_ERROR;
}
return EFI_LOAD_ERROR;
}
//
// set status string
//
//
// now everything is ready , you can set the new file name to filebuffer
//
(FileName != NULL && HFileImage.FileName != NULL && StringNoCaseCompare (&FileName, &HFileImage.FileName) != 0)){
//
// not the same
//
return EFI_OUT_OF_RESOURCES;
}
}
return EFI_SUCCESS;
}