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
);
183 HFileImageSetFileName (FileName
);
186 // free the old lines
190 Status
= HBufferImageBufferToList (Buffer
, HFileImage
.Size
);
191 SHELL_FREE_NON_NULL (Buffer
);
192 if (EFI_ERROR (Status
)) {
193 StatusBarSetStatusString (L
"Error parsing file.");
197 HBufferImage
.DisplayPosition
.Row
= 2;
198 HBufferImage
.DisplayPosition
.Column
= 10;
199 HBufferImage
.MousePosition
.Row
= 2;
200 HBufferImage
.MousePosition
.Column
= 10;
201 HBufferImage
.LowVisibleRow
= 1;
202 HBufferImage
.HighBits
= TRUE
;
203 HBufferImage
.BufferPosition
.Row
= 1;
204 HBufferImage
.BufferPosition
.Column
= 1;
205 HBufferImage
.BufferType
= FileTypeFileBuffer
;
208 UnicodeBuffer
= CatSPrint(NULL
, L
"%d Lines Read", HBufferImage
.NumLines
);
209 if (UnicodeBuffer
== NULL
) {
210 SHELL_FREE_NON_NULL(Buffer
);
211 return EFI_OUT_OF_RESOURCES
;
214 StatusBarSetStatusString (UnicodeBuffer
);
215 FreePool (UnicodeBuffer
);
217 HMainEditor
.SelectStart
= 0;
218 HMainEditor
.SelectEnd
= 0;
224 if (HBufferImage
.Lines
!= 0) {
225 HBufferImage
.CurrentLine
= CR (HBufferImage
.ListHead
->ForwardLink
, HEFI_EDITOR_LINE
, Link
, EFI_EDITOR_LINE_LIST
);
228 // create a dummy line
230 Line
= HBufferImageCreateLine ();
232 SHELL_FREE_NON_NULL(Buffer
);
233 return EFI_OUT_OF_RESOURCES
;
236 HBufferImage
.CurrentLine
= Line
;
239 HBufferImage
.Modified
= FALSE
;
240 HBufferImageNeedRefresh
= TRUE
;
241 HBufferImageOnlyLineNeedRefresh
= FALSE
;
242 HBufferImageMouseNeedRefresh
= TRUE
;
248 Save lines in HBufferImage to disk.
250 @param[in] FileName The file name.
252 @retval EFI_SUCCESS The operation was successful.
253 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
254 @retval EFI_LOAD_ERROR A load error occured.
263 HEFI_EDITOR_LINE
*Line
;
267 SHELL_FILE_HANDLE FileHandle
;
271 EDIT_FILE_TYPE BufferTypeBackup
;
273 BufferTypeBackup
= HBufferImage
.BufferType
;
274 HBufferImage
.BufferType
= FileTypeFileBuffer
;
277 // if is the old file
279 if (HFileImage
.FileName
!= NULL
&& FileName
!= NULL
&& StrCmp (FileName
, HFileImage
.FileName
) == 0) {
281 // check whether file exists on disk
283 if (ShellIsFile(FileName
) == EFI_SUCCESS
) {
285 // current file exists on disk
286 // so if not modified, then not save
288 if (HBufferImage
.Modified
== FALSE
) {
292 // if file is read-only, set error
294 if (HFileImage
.ReadOnly
== TRUE
) {
295 StatusBarSetStatusString (L
"Read Only File Can Not Be Saved");
301 if (ShellIsDirectory(FileName
) == EFI_SUCCESS
) {
302 StatusBarSetStatusString (L
"Directory Can Not Be Saved");
303 return EFI_LOAD_ERROR
;
306 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
, 0);
308 if (!EFI_ERROR (Status
)) {
310 // the file exits, delete it
312 Status
= ShellDeleteFile (&FileHandle
);
313 if (EFI_ERROR (Status
) || Status
== EFI_WARN_DELETE_FAILURE
) {
314 StatusBarSetStatusString (L
"Write File Failed");
315 return EFI_LOAD_ERROR
;
320 // write all the lines back to disk
324 for (Link
= HBufferImage
.ListHead
->ForwardLink
; Link
!= HBufferImage
.ListHead
; Link
= Link
->ForwardLink
) {
325 Line
= CR (Link
, HEFI_EDITOR_LINE
, Link
, EFI_EDITOR_LINE_LIST
);
327 if (Line
->Size
!= 0) {
328 TotalSize
+= Line
->Size
;
331 // end of if Line -> Size != 0
338 Buffer
= AllocateZeroPool (TotalSize
);
339 if (Buffer
== NULL
) {
340 return EFI_OUT_OF_RESOURCES
;
344 for (Link
= HBufferImage
.ListHead
->ForwardLink
; Link
!= HBufferImage
.ListHead
; Link
= Link
->ForwardLink
) {
345 Line
= CR (Link
, HEFI_EDITOR_LINE
, Link
, EFI_EDITOR_LINE_LIST
);
347 if (Line
->Size
!= 0) {
348 CopyMem (Ptr
, Line
->Buffer
, Line
->Size
);
352 // end of if Line -> Size != 0
357 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, 0);
359 if (EFI_ERROR (Status
)) {
360 StatusBarSetStatusString (L
"Create File Failed");
361 return EFI_LOAD_ERROR
;
364 Status
= ShellWriteFile (FileHandle
, &TotalSize
, Buffer
);
366 if (EFI_ERROR (Status
)) {
367 ShellDeleteFile (&FileHandle
);
368 return EFI_LOAD_ERROR
;
371 ShellCloseFile(&FileHandle
);
373 HBufferImage
.Modified
= FALSE
;
378 Str
= CatSPrint(NULL
, L
"%d Lines Wrote", NumLines
);
379 StatusBarSetStatusString (Str
);
383 // now everything is ready , you can set the new file name to filebuffer
385 if ((BufferTypeBackup
!= FileTypeFileBuffer
&& FileName
!= NULL
) ||
386 (FileName
!= NULL
&& HFileImage
.FileName
!= NULL
&& StringNoCaseCompare (&FileName
, &HFileImage
.FileName
) != 0)){
390 HFileImageSetFileName (FileName
);
391 if (HFileImage
.FileName
== NULL
) {
392 return EFI_OUT_OF_RESOURCES
;
396 HFileImage
.ReadOnly
= FALSE
;