2 Functions to deal with file buffer.
4 Copyright (c) 2005 - 2011, Intel Corporation. All rights reserved. <BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "HexEditor.h"
17 extern EFI_HANDLE HImageHandleBackup
;
18 extern HEFI_EDITOR_BUFFER_IMAGE HBufferImage
;
20 extern BOOLEAN HBufferImageNeedRefresh
;
21 extern BOOLEAN HBufferImageOnlyLineNeedRefresh
;
22 extern BOOLEAN HBufferImageMouseNeedRefresh
;
24 extern HEFI_EDITOR_GLOBAL_EDITOR HMainEditor
;
26 HEFI_EDITOR_FILE_IMAGE HFileImage
;
27 HEFI_EDITOR_FILE_IMAGE HFileImageBackupVar
;
30 // for basic initialization of HFileImage
32 HEFI_EDITOR_BUFFER_IMAGE HFileImageConst
= {
39 Initialization function for HFileImage
41 @retval EFI_SUCCESS The operation was successful.
49 // basically initialize the HFileImage
51 CopyMem (&HFileImage
, &HFileImageConst
, sizeof (HFileImage
));
56 sizeof (HFileImageBackupVar
)
63 Backup function for HFileImage. Only a few fields need to be backup.
64 This is for making the file buffer refresh as few as possible.
66 @retval EFI_SUCCESS The operation was successful.
67 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
74 SHELL_FREE_NON_NULL (HFileImageBackupVar
.FileName
);
75 HFileImageBackupVar
.FileName
= CatSPrint(NULL
, L
"%s", HFileImage
.FileName
);
76 if (HFileImageBackupVar
.FileName
== NULL
) {
77 return EFI_OUT_OF_RESOURCES
;
84 Cleanup function for HFileImage.
86 @retval EFI_SUCCESS The operation was successful.
94 SHELL_FREE_NON_NULL (HFileImage
.FileName
);
95 SHELL_FREE_NON_NULL (HFileImageBackupVar
.FileName
);
101 Set FileName field in HFileImage
103 @param[in] Str File name to set.
105 @retval EFI_SUCCESS The operation was successful.
106 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
109 HFileImageSetFileName (
117 // free the old file name
119 SHELL_FREE_NON_NULL (HFileImage
.FileName
);
123 HFileImage
.FileName
= AllocateZeroPool (2 * (Size
+ 1));
124 if (HFileImage
.FileName
== NULL
) {
125 return EFI_OUT_OF_RESOURCES
;
128 for (Index
= 0; Index
< Size
; Index
++) {
129 HFileImage
.FileName
[Index
] = Str
[Index
];
132 HFileImage
.FileName
[Size
] = L
'\0';
138 Read a file from disk into HBufferImage.
140 @param[in] FileName filename to read.
141 @param[in] Recover if is for recover, no information print.
143 @retval EFI_SUCCESS The operation was successful.
144 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
145 @retval EFI_LOAD_ERROR A load error occured.
149 IN CONST CHAR16
*FileName
,
153 HEFI_EDITOR_LINE
*Line
;
155 CHAR16
*UnicodeBuffer
;
159 // variable initialization
164 // in this function, when you return error ( except EFI_OUT_OF_RESOURCES )
165 // you should set status string
166 // since this function maybe called before the editorhandleinput loop
167 // so any error will cause editor return
168 // so if you want to print the error status
169 // you should set the status string
171 Status
= ReadFileIntoBuffer (FileName
, (VOID
**)&Buffer
, &HFileImage
.Size
, &HFileImage
.ReadOnly
);
172 if (EFI_ERROR(Status
) || Buffer
== NULL
) {
173 UnicodeBuffer
= CatSPrint(NULL
, L
"Read error on file &s: %r", FileName
, Status
);
174 if (UnicodeBuffer
== NULL
) {
175 SHELL_FREE_NON_NULL(Buffer
);
176 return EFI_OUT_OF_RESOURCES
;
179 StatusBarSetStatusString (UnicodeBuffer
);
180 FreePool (UnicodeBuffer
);
181 return EFI_OUT_OF_RESOURCES
;
184 HFileImageSetFileName (FileName
);
187 // free the old lines
191 Status
= HBufferImageBufferToList (Buffer
, HFileImage
.Size
);
192 SHELL_FREE_NON_NULL (Buffer
);
193 if (EFI_ERROR (Status
)) {
194 StatusBarSetStatusString (L
"Error parsing file.");
198 HBufferImage
.DisplayPosition
.Row
= 2;
199 HBufferImage
.DisplayPosition
.Column
= 10;
200 HBufferImage
.MousePosition
.Row
= 2;
201 HBufferImage
.MousePosition
.Column
= 10;
202 HBufferImage
.LowVisibleRow
= 1;
203 HBufferImage
.HighBits
= TRUE
;
204 HBufferImage
.BufferPosition
.Row
= 1;
205 HBufferImage
.BufferPosition
.Column
= 1;
206 HBufferImage
.BufferType
= FileTypeFileBuffer
;
209 UnicodeBuffer
= CatSPrint(NULL
, L
"%d Lines Read", HBufferImage
.NumLines
);
210 if (UnicodeBuffer
== NULL
) {
211 SHELL_FREE_NON_NULL(Buffer
);
212 return EFI_OUT_OF_RESOURCES
;
215 StatusBarSetStatusString (UnicodeBuffer
);
216 FreePool (UnicodeBuffer
);
218 HMainEditor
.SelectStart
= 0;
219 HMainEditor
.SelectEnd
= 0;
225 if (HBufferImage
.Lines
!= 0) {
226 HBufferImage
.CurrentLine
= CR (HBufferImage
.ListHead
->ForwardLink
, HEFI_EDITOR_LINE
, Link
, EFI_EDITOR_LINE_LIST
);
229 // create a dummy line
231 Line
= HBufferImageCreateLine ();
233 SHELL_FREE_NON_NULL(Buffer
);
234 return EFI_OUT_OF_RESOURCES
;
237 HBufferImage
.CurrentLine
= Line
;
240 HBufferImage
.Modified
= FALSE
;
241 HBufferImageNeedRefresh
= TRUE
;
242 HBufferImageOnlyLineNeedRefresh
= FALSE
;
243 HBufferImageMouseNeedRefresh
= TRUE
;
249 Save lines in HBufferImage to disk.
251 @param[in] FileName The file name.
253 @retval EFI_SUCCESS The operation was successful.
254 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
255 @retval EFI_LOAD_ERROR A load error occured.
264 HEFI_EDITOR_LINE
*Line
;
268 SHELL_FILE_HANDLE FileHandle
;
272 EDIT_FILE_TYPE BufferTypeBackup
;
274 BufferTypeBackup
= HBufferImage
.BufferType
;
275 HBufferImage
.BufferType
= FileTypeFileBuffer
;
278 // if is the old file
280 if (HFileImage
.FileName
!= NULL
&& FileName
!= NULL
&& StrCmp (FileName
, HFileImage
.FileName
) == 0) {
282 // check whether file exists on disk
284 if (ShellIsFile(FileName
) == EFI_SUCCESS
) {
286 // current file exists on disk
287 // so if not modified, then not save
289 if (HBufferImage
.Modified
== FALSE
) {
293 // if file is read-only, set error
295 if (HFileImage
.ReadOnly
== TRUE
) {
296 StatusBarSetStatusString (L
"Read Only File Can Not Be Saved");
302 if (ShellIsDirectory(FileName
) == EFI_SUCCESS
) {
303 StatusBarSetStatusString (L
"Directory Can Not Be Saved");
304 return EFI_LOAD_ERROR
;
307 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
, 0);
309 if (!EFI_ERROR (Status
)) {
311 // the file exits, delete it
313 Status
= ShellDeleteFile (&FileHandle
);
314 if (EFI_ERROR (Status
) || Status
== EFI_WARN_DELETE_FAILURE
) {
315 StatusBarSetStatusString (L
"Write File Failed");
316 return EFI_LOAD_ERROR
;
321 // write all the lines back to disk
325 for (Link
= HBufferImage
.ListHead
->ForwardLink
; Link
!= HBufferImage
.ListHead
; Link
= Link
->ForwardLink
) {
326 Line
= CR (Link
, HEFI_EDITOR_LINE
, Link
, EFI_EDITOR_LINE_LIST
);
328 if (Line
->Size
!= 0) {
329 TotalSize
+= Line
->Size
;
332 // end of if Line -> Size != 0
339 Buffer
= AllocateZeroPool (TotalSize
);
340 if (Buffer
== NULL
) {
341 return EFI_OUT_OF_RESOURCES
;
345 for (Link
= HBufferImage
.ListHead
->ForwardLink
; Link
!= HBufferImage
.ListHead
; Link
= Link
->ForwardLink
) {
346 Line
= CR (Link
, HEFI_EDITOR_LINE
, Link
, EFI_EDITOR_LINE_LIST
);
348 if (Line
->Size
!= 0) {
349 CopyMem (Ptr
, Line
->Buffer
, Line
->Size
);
353 // end of if Line -> Size != 0
358 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, 0);
360 if (EFI_ERROR (Status
)) {
361 StatusBarSetStatusString (L
"Create File Failed");
362 return EFI_LOAD_ERROR
;
365 Status
= ShellWriteFile (FileHandle
, &TotalSize
, Buffer
);
367 if (EFI_ERROR (Status
)) {
368 ShellDeleteFile (&FileHandle
);
369 return EFI_LOAD_ERROR
;
372 ShellCloseFile(&FileHandle
);
374 HBufferImage
.Modified
= FALSE
;
379 Str
= CatSPrint(NULL
, L
"%d Lines Wrote", NumLines
);
380 StatusBarSetStatusString (Str
);
384 // now everything is ready , you can set the new file name to filebuffer
386 if ((BufferTypeBackup
!= FileTypeFileBuffer
&& FileName
!= NULL
) ||
387 (FileName
!= NULL
&& HFileImage
.FileName
!= NULL
&& StringNoCaseCompare (&FileName
, &HFileImage
.FileName
) != 0)){
391 HFileImageSetFileName (FileName
);
392 if (HFileImage
.FileName
== NULL
) {
393 return EFI_OUT_OF_RESOURCES
;
397 HFileImage
.ReadOnly
= FALSE
;