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
);
173 // NULL pointer is only also a failure for a non-zero file size.
175 if ((EFI_ERROR(Status
)) || (Buffer
== NULL
&& HFileImage
.Size
!= 0)) {
176 UnicodeBuffer
= CatSPrint(NULL
, L
"Read error on file &s: %r", FileName
, Status
);
177 if (UnicodeBuffer
== NULL
) {
178 SHELL_FREE_NON_NULL(Buffer
);
179 return EFI_OUT_OF_RESOURCES
;
182 StatusBarSetStatusString (UnicodeBuffer
);
183 FreePool (UnicodeBuffer
);
184 return EFI_OUT_OF_RESOURCES
;
187 HFileImageSetFileName (FileName
);
190 // free the old lines
194 Status
= HBufferImageBufferToList (Buffer
, HFileImage
.Size
);
195 SHELL_FREE_NON_NULL (Buffer
);
196 if (EFI_ERROR (Status
)) {
197 StatusBarSetStatusString (L
"Error parsing file.");
201 HBufferImage
.DisplayPosition
.Row
= 2;
202 HBufferImage
.DisplayPosition
.Column
= 10;
203 HBufferImage
.MousePosition
.Row
= 2;
204 HBufferImage
.MousePosition
.Column
= 10;
205 HBufferImage
.LowVisibleRow
= 1;
206 HBufferImage
.HighBits
= TRUE
;
207 HBufferImage
.BufferPosition
.Row
= 1;
208 HBufferImage
.BufferPosition
.Column
= 1;
209 HBufferImage
.BufferType
= FileTypeFileBuffer
;
212 UnicodeBuffer
= CatSPrint(NULL
, L
"%d Lines Read", HBufferImage
.NumLines
);
213 if (UnicodeBuffer
== NULL
) {
214 SHELL_FREE_NON_NULL(Buffer
);
215 return EFI_OUT_OF_RESOURCES
;
218 StatusBarSetStatusString (UnicodeBuffer
);
219 FreePool (UnicodeBuffer
);
221 HMainEditor
.SelectStart
= 0;
222 HMainEditor
.SelectEnd
= 0;
228 if (HBufferImage
.Lines
!= 0) {
229 HBufferImage
.CurrentLine
= CR (HBufferImage
.ListHead
->ForwardLink
, HEFI_EDITOR_LINE
, Link
, EFI_EDITOR_LINE_LIST
);
232 // create a dummy line
234 Line
= HBufferImageCreateLine ();
236 SHELL_FREE_NON_NULL(Buffer
);
237 return EFI_OUT_OF_RESOURCES
;
240 HBufferImage
.CurrentLine
= Line
;
243 HBufferImage
.Modified
= FALSE
;
244 HBufferImageNeedRefresh
= TRUE
;
245 HBufferImageOnlyLineNeedRefresh
= FALSE
;
246 HBufferImageMouseNeedRefresh
= TRUE
;
252 Save lines in HBufferImage to disk.
254 @param[in] FileName The file name.
256 @retval EFI_SUCCESS The operation was successful.
257 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
258 @retval EFI_LOAD_ERROR A load error occured.
267 HEFI_EDITOR_LINE
*Line
;
271 SHELL_FILE_HANDLE FileHandle
;
275 EDIT_FILE_TYPE BufferTypeBackup
;
277 BufferTypeBackup
= HBufferImage
.BufferType
;
278 HBufferImage
.BufferType
= FileTypeFileBuffer
;
281 // if is the old file
283 if (HFileImage
.FileName
!= NULL
&& FileName
!= NULL
&& StrCmp (FileName
, HFileImage
.FileName
) == 0) {
285 // check whether file exists on disk
287 if (ShellIsFile(FileName
) == EFI_SUCCESS
) {
289 // current file exists on disk
290 // so if not modified, then not save
292 if (HBufferImage
.Modified
== FALSE
) {
296 // if file is read-only, set error
298 if (HFileImage
.ReadOnly
== TRUE
) {
299 StatusBarSetStatusString (L
"Read Only File Can Not Be Saved");
305 if (ShellIsDirectory(FileName
) == EFI_SUCCESS
) {
306 StatusBarSetStatusString (L
"Directory Can Not Be Saved");
307 return EFI_LOAD_ERROR
;
310 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
, 0);
312 if (!EFI_ERROR (Status
)) {
314 // the file exits, delete it
316 Status
= ShellDeleteFile (&FileHandle
);
317 if (EFI_ERROR (Status
) || Status
== EFI_WARN_DELETE_FAILURE
) {
318 StatusBarSetStatusString (L
"Write File Failed");
319 return EFI_LOAD_ERROR
;
324 // write all the lines back to disk
328 for (Link
= HBufferImage
.ListHead
->ForwardLink
; Link
!= HBufferImage
.ListHead
; Link
= Link
->ForwardLink
) {
329 Line
= CR (Link
, HEFI_EDITOR_LINE
, Link
, EFI_EDITOR_LINE_LIST
);
331 if (Line
->Size
!= 0) {
332 TotalSize
+= Line
->Size
;
335 // end of if Line -> Size != 0
342 Buffer
= AllocateZeroPool (TotalSize
);
343 if (Buffer
== NULL
) {
344 return EFI_OUT_OF_RESOURCES
;
348 for (Link
= HBufferImage
.ListHead
->ForwardLink
; Link
!= HBufferImage
.ListHead
; Link
= Link
->ForwardLink
) {
349 Line
= CR (Link
, HEFI_EDITOR_LINE
, Link
, EFI_EDITOR_LINE_LIST
);
351 if (Line
->Size
!= 0) {
352 CopyMem (Ptr
, Line
->Buffer
, Line
->Size
);
356 // end of if Line -> Size != 0
361 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, 0);
363 if (EFI_ERROR (Status
)) {
364 StatusBarSetStatusString (L
"Create File Failed");
365 return EFI_LOAD_ERROR
;
368 Status
= ShellWriteFile (FileHandle
, &TotalSize
, Buffer
);
370 if (EFI_ERROR (Status
)) {
371 ShellDeleteFile (&FileHandle
);
372 return EFI_LOAD_ERROR
;
375 ShellCloseFile(&FileHandle
);
377 HBufferImage
.Modified
= FALSE
;
382 Str
= CatSPrint(NULL
, L
"%d Lines Wrote", NumLines
);
383 StatusBarSetStatusString (Str
);
387 // now everything is ready , you can set the new file name to filebuffer
389 if ((BufferTypeBackup
!= FileTypeFileBuffer
&& FileName
!= NULL
) ||
390 (FileName
!= NULL
&& HFileImage
.FileName
!= NULL
&& StringNoCaseCompare (&FileName
, &HFileImage
.FileName
) != 0)){
394 HFileImageSetFileName (FileName
);
395 if (HFileImage
.FileName
== NULL
) {
396 return EFI_OUT_OF_RESOURCES
;
400 HFileImage
.ReadOnly
= FALSE
;