2 Implements filebuffer interface functions.
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 "TextEditor.h"
16 #include <Guid/FileSystemInfo.h>
17 #include <Library/FileHandleLib.h>
19 EFI_EDITOR_FILE_BUFFER FileBuffer
;
20 EFI_EDITOR_FILE_BUFFER FileBufferBackupVar
;
23 // for basic initialization of FileBuffer
25 EFI_EDITOR_FILE_BUFFER FileBufferConst
= {
54 // the whole edit area needs to be refreshed
56 BOOLEAN FileBufferNeedRefresh
;
59 // only the current line in edit area needs to be refresh
61 BOOLEAN FileBufferOnlyLineNeedRefresh
;
63 BOOLEAN FileBufferMouseNeedRefresh
;
65 extern BOOLEAN EditorMouseAction
;
68 Initialization function for FileBuffer.
70 @param EFI_SUCCESS The initialization was successful.
71 @param EFI_LOAD_ERROR A default name could not be created.
72 @param EFI_OUT_OF_RESOURCES A memory allocation failed.
81 // basically initialize the FileBuffer
83 CopyMem (&FileBuffer
, &FileBufferConst
, sizeof (EFI_EDITOR_FILE_BUFFER
));
84 CopyMem (&FileBufferBackupVar
, &FileBufferConst
, sizeof (EFI_EDITOR_FILE_BUFFER
));
87 // set default FileName
89 FileBuffer
.FileName
= EditGetDefaultFileName (L
"txt");
90 if (FileBuffer
.FileName
== NULL
) {
91 return EFI_LOAD_ERROR
;
94 FileBuffer
.ListHead
= AllocateZeroPool (sizeof (LIST_ENTRY
));
95 if (FileBuffer
.ListHead
== NULL
) {
96 return EFI_OUT_OF_RESOURCES
;
99 InitializeListHead (FileBuffer
.ListHead
);
101 FileBuffer
.DisplayPosition
.Row
= 2;
102 FileBuffer
.DisplayPosition
.Column
= 1;
103 FileBuffer
.LowVisibleRange
.Row
= 2;
104 FileBuffer
.LowVisibleRange
.Column
= 1;
106 FileBufferNeedRefresh
= FALSE
;
107 FileBufferMouseNeedRefresh
= FALSE
;
108 FileBufferOnlyLineNeedRefresh
= FALSE
;
114 Backup function for FileBuffer. Only backup the following items:
115 Mouse/Cursor position
116 File Name, Type, ReadOnly, Modified
119 This is for making the file buffer refresh as few as possible.
121 @retval EFI_SUCCESS The backup operation was successful.
129 FileBufferBackupVar
.MousePosition
= FileBuffer
.MousePosition
;
131 SHELL_FREE_NON_NULL (FileBufferBackupVar
.FileName
);
132 FileBufferBackupVar
.FileName
= NULL
;
133 FileBufferBackupVar
.FileName
= StrnCatGrow (&FileBufferBackupVar
.FileName
, NULL
, FileBuffer
.FileName
, 0);
135 FileBufferBackupVar
.ModeInsert
= FileBuffer
.ModeInsert
;
136 FileBufferBackupVar
.FileType
= FileBuffer
.FileType
;
138 FileBufferBackupVar
.FilePosition
= FileBuffer
.FilePosition
;
139 FileBufferBackupVar
.LowVisibleRange
= FileBuffer
.LowVisibleRange
;
141 FileBufferBackupVar
.FileModified
= FileBuffer
.FileModified
;
142 FileBufferBackupVar
.ReadOnly
= FileBuffer
.ReadOnly
;
148 Advance to the next Count lines
150 @param[in] Count The line number to advance by.
151 @param[in] CurrentLine The pointer to the current line structure.
152 @param[in] LineList The pointer to the linked list of lines.
154 @retval NULL There was an error.
155 @return The line structure after the advance.
159 InternalEditorMiscLineAdvance (
160 IN CONST UINTN Count
,
161 IN CONST EFI_EDITOR_LINE
*CurrentLine
,
162 IN CONST LIST_ENTRY
*LineList
167 CONST EFI_EDITOR_LINE
*Line
;
169 if (CurrentLine
== NULL
|| LineList
== NULL
) {
173 for (Line
= CurrentLine
, Index
= 0; Index
< Count
; Index
++) {
175 // if already last line
177 if (Line
->Link
.ForwardLink
== LineList
) {
181 Line
= CR (Line
->Link
.ForwardLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
184 return ((EFI_EDITOR_LINE
*)Line
);
188 Retreat to the previous Count lines.
190 @param[in] Count The line number to retreat by.
191 @param[in] CurrentLine The pointer to the current line structure.
192 @param[in] LineList The pointer to the linked list of lines.
194 @retval NULL There was an error.
195 @return The line structure after the retreat.
199 InternalEditorMiscLineRetreat (
200 IN CONST UINTN Count
,
201 IN CONST EFI_EDITOR_LINE
*CurrentLine
,
202 IN CONST LIST_ENTRY
*LineList
207 CONST EFI_EDITOR_LINE
*Line
;
209 if (CurrentLine
== NULL
|| LineList
== NULL
) {
213 for (Line
= CurrentLine
, Index
= 0; Index
< Count
; Index
++) {
215 // already the first line
217 if (Line
->Link
.BackLink
== LineList
) {
221 Line
= CR (Line
->Link
.BackLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
224 return ((EFI_EDITOR_LINE
*)Line
);
228 Advance/Retreat lines
230 @param[in] Count line number to advance/retreat
234 @retval NULL An error occured.
235 @return The line after advance/retreat.
242 EFI_EDITOR_LINE
*Line
;
246 // if < 0, then retreat
247 // if > 0, the advance
250 AbsCount
= (UINTN
)ABS(Count
);
251 Line
= InternalEditorMiscLineRetreat (AbsCount
,MainEditor
.FileBuffer
->CurrentLine
,MainEditor
.FileBuffer
->ListHead
);
253 Line
= InternalEditorMiscLineAdvance ((UINTN
)Count
,MainEditor
.FileBuffer
->CurrentLine
,MainEditor
.FileBuffer
->ListHead
);
260 Function to update the 'screen' to display the mouse position.
262 @retval EFI_SUCCESS The backup operation was successful.
266 FileBufferRestoreMousePosition (
270 EFI_EDITOR_COLOR_UNION Orig
;
271 EFI_EDITOR_COLOR_UNION New
;
274 BOOLEAN HasCharacter
;
275 EFI_EDITOR_LINE
*CurrentLine
;
276 EFI_EDITOR_LINE
*Line
;
280 // variable initialization
284 if (MainEditor
.MouseSupported
) {
286 if (FileBufferMouseNeedRefresh
) {
288 FileBufferMouseNeedRefresh
= FALSE
;
291 // if mouse position not moved and only mouse action
292 // so do not need to refresh mouse position
294 if ((FileBuffer
.MousePosition
.Row
== FileBufferBackupVar
.MousePosition
.Row
&&
295 FileBuffer
.MousePosition
.Column
== FileBufferBackupVar
.MousePosition
.Column
)
296 && EditorMouseAction
) {
300 // backup the old screen attributes
302 Orig
= MainEditor
.ColorAttributes
;
303 New
.Colors
.Foreground
= Orig
.Colors
.Background
;
304 New
.Colors
.Background
= Orig
.Colors
.Foreground
;
307 // clear the old mouse position
309 FRow
= FileBuffer
.LowVisibleRange
.Row
+ FileBufferBackupVar
.MousePosition
.Row
- 2;
311 FColumn
= FileBuffer
.LowVisibleRange
.Column
+ FileBufferBackupVar
.MousePosition
.Column
- 1;
314 if (FRow
> FileBuffer
.NumLines
) {
315 HasCharacter
= FALSE
;
317 CurrentLine
= FileBuffer
.CurrentLine
;
318 Line
= MoveLine (FRow
- FileBuffer
.FilePosition
.Row
);
320 if (Line
== NULL
|| FColumn
> Line
->Size
) {
321 HasCharacter
= FALSE
;
324 FileBuffer
.CurrentLine
= CurrentLine
;
328 (INT32
)FileBufferBackupVar
.MousePosition
.Column
- 1,
329 (INT32
)FileBufferBackupVar
.MousePosition
.Row
- 1,
334 Value
= (Line
->Buffer
[FColumn
- 1]);
336 (INT32
)FileBufferBackupVar
.MousePosition
.Column
- 1,
337 (INT32
)FileBufferBackupVar
.MousePosition
.Row
- 1,
343 // set the new mouse position
345 gST
->ConOut
->SetAttribute (gST
->ConOut
, New
.Data
);
348 // clear the old mouse position
350 FRow
= FileBuffer
.LowVisibleRange
.Row
+ FileBuffer
.MousePosition
.Row
- 2;
351 FColumn
= FileBuffer
.LowVisibleRange
.Column
+ FileBuffer
.MousePosition
.Column
- 1;
354 if (FRow
> FileBuffer
.NumLines
) {
355 HasCharacter
= FALSE
;
357 CurrentLine
= FileBuffer
.CurrentLine
;
358 Line
= MoveLine (FRow
- FileBuffer
.FilePosition
.Row
);
360 if (Line
== NULL
|| FColumn
> Line
->Size
) {
361 HasCharacter
= FALSE
;
364 FileBuffer
.CurrentLine
= CurrentLine
;
368 (INT32
)FileBuffer
.MousePosition
.Column
- 1,
369 (INT32
)FileBuffer
.MousePosition
.Row
- 1,
374 Value
= Line
->Buffer
[FColumn
- 1];
376 (INT32
)FileBuffer
.MousePosition
.Column
- 1,
377 (INT32
)FileBuffer
.MousePosition
.Row
- 1,
383 // end of HasCharacter
385 gST
->ConOut
->SetAttribute (gST
->ConOut
, Orig
.Data
);
388 // end of MouseNeedRefresh
392 // end of MouseSupported
398 Free all the lines in FileBuffer
405 @retval EFI_SUCCESS The operation was successful.
409 FileBufferFreeLines (
414 EFI_EDITOR_LINE
*Line
;
417 // free all the lines
419 if (FileBuffer
.Lines
!= NULL
) {
421 Line
= FileBuffer
.Lines
;
422 Link
= &(Line
->Link
);
424 Line
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
425 Link
= Link
->ForwardLink
;
428 // free line's buffer and line itself
431 } while (Link
!= FileBuffer
.ListHead
);
434 // clean the line list related structure
436 FileBuffer
.Lines
= NULL
;
437 FileBuffer
.CurrentLine
= NULL
;
438 FileBuffer
.NumLines
= 0;
440 FileBuffer
.ListHead
->ForwardLink
= FileBuffer
.ListHead
;
441 FileBuffer
.ListHead
->BackLink
= FileBuffer
.ListHead
;
447 Cleanup function for FileBuffer.
449 @retval EFI_SUCCESS The cleanup was successful.
459 SHELL_FREE_NON_NULL (FileBuffer
.FileName
);
462 // free all the lines
464 Status
= FileBufferFreeLines ();
466 SHELL_FREE_NON_NULL (FileBuffer
.ListHead
);
467 FileBuffer
.ListHead
= NULL
;
469 SHELL_FREE_NON_NULL (FileBufferBackupVar
.FileName
);
475 Print a line specified by Line on a row specified by Row of the screen.
477 @param[in] Line The line to print.
478 @param[in] Row The row on the screen to print onto (begin from 1).
480 @retval EFI_SUCCESS The printing was successful.
483 FileBufferPrintLine (
484 IN CONST EFI_EDITOR_LINE
*Line
,
491 CHAR16 PrintLine
[200];
494 // print start from correct character
496 Buffer
= Line
->Buffer
+ FileBuffer
.LowVisibleRange
.Column
- 1;
498 Limit
= Line
->Size
- FileBuffer
.LowVisibleRange
.Column
+ 1;
499 if (Limit
> Line
->Size
) {
503 StrnCpy (PrintLine
, Buffer
, MIN(MIN(Limit
,MainEditor
.ScreenSize
.Column
), 200));
504 for (; Limit
< MainEditor
.ScreenSize
.Column
; Limit
++) {
505 PrintLine
[Limit
] = L
' ';
508 PrintLine
[MainEditor
.ScreenSize
.Column
] = CHAR_NULL
;
521 Set the cursor position according to FileBuffer.DisplayPosition.
523 @retval EFI_SUCCESS The operation was successful.
527 FileBufferRestorePosition (
532 // set cursor position
534 return (gST
->ConOut
->SetCursorPosition (
536 FileBuffer
.DisplayPosition
.Column
- 1,
537 FileBuffer
.DisplayPosition
.Row
- 1
542 Refresh the screen with whats in the buffer.
544 @retval EFI_SUCCESS The refresh was successful.
545 @retval EFI_LOAD_ERROR There was an error finding what to write.
554 EFI_EDITOR_LINE
*Line
;
558 // if it's the first time after editor launch, so should refresh
562 // no definite required refresh
563 // and file position displayed on screen has not been changed
565 if (!FileBufferNeedRefresh
&&
566 !FileBufferOnlyLineNeedRefresh
&&
567 FileBufferBackupVar
.LowVisibleRange
.Row
== FileBuffer
.LowVisibleRange
.Row
&&
568 FileBufferBackupVar
.LowVisibleRange
.Column
== FileBuffer
.LowVisibleRange
.Column
571 FileBufferRestoreMousePosition ();
572 FileBufferRestorePosition ();
578 gST
->ConOut
->EnableCursor (gST
->ConOut
, FALSE
);
581 // only need to refresh current line
583 if (FileBufferOnlyLineNeedRefresh
&&
584 FileBufferBackupVar
.LowVisibleRange
.Row
== FileBuffer
.LowVisibleRange
.Row
&&
585 FileBufferBackupVar
.LowVisibleRange
.Column
== FileBuffer
.LowVisibleRange
.Column
588 EditorClearLine (FileBuffer
.DisplayPosition
.Row
, MainEditor
.ScreenSize
.Column
, MainEditor
.ScreenSize
.Row
);
589 FileBufferPrintLine (
590 FileBuffer
.CurrentLine
,
591 FileBuffer
.DisplayPosition
.Row
595 // the whole edit area need refresh
601 if (FileBuffer
.Lines
== NULL
) {
602 FileBufferRestoreMousePosition ();
603 FileBufferRestorePosition ();
604 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
609 // get the first line that will be displayed
611 Line
= MoveLine (FileBuffer
.LowVisibleRange
.Row
- FileBuffer
.FilePosition
.Row
);
613 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
615 return EFI_LOAD_ERROR
;
618 Link
= &(Line
->Link
);
621 Line
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
626 FileBufferPrintLine (Line
, Row
);
628 Link
= Link
->ForwardLink
;
630 } while (Link
!= FileBuffer
.ListHead
&& Row
<= (MainEditor
.ScreenSize
.Row
- 1));
632 // while not file end and not screen full
634 while (Row
<= (MainEditor
.ScreenSize
.Row
- 1)) {
635 EditorClearLine (Row
, MainEditor
.ScreenSize
.Column
, MainEditor
.ScreenSize
.Row
);
640 FileBufferRestoreMousePosition ();
641 FileBufferRestorePosition ();
643 FileBufferNeedRefresh
= FALSE
;
644 FileBufferOnlyLineNeedRefresh
= FALSE
;
646 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
651 Create a new line and append it to the line list.
656 @retval NULL The create line failed.
657 @return The line created.
661 FileBufferCreateLine (
665 EFI_EDITOR_LINE
*Line
;
668 // allocate a line structure
670 Line
= AllocateZeroPool (sizeof (EFI_EDITOR_LINE
));
675 // initialize the structure
677 Line
->Signature
= LINE_LIST_SIGNATURE
;
680 Line
->Type
= NewLineTypeDefault
;
683 // initial buffer of the line is "\0"
685 ASSERT(CHAR_NULL
== CHAR_NULL
);
686 Line
->Buffer
= CatSPrint (NULL
, L
"\0");
687 if (Line
->Buffer
== NULL
) {
691 FileBuffer
.NumLines
++;
694 // insert the line into line list
696 InsertTailList (FileBuffer
.ListHead
, &Line
->Link
);
698 if (FileBuffer
.Lines
== NULL
) {
699 FileBuffer
.Lines
= CR (FileBuffer
.ListHead
->ForwardLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
706 Set FileName field in FileBuffer.
708 @param Str The file name to set.
710 @retval EFI_SUCCESS The filename was successfully set.
711 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
712 @retval EFI_INVALID_PARAMETER Str is not a valid filename.
716 FileBufferSetFileName (
721 // Verify the parameters
723 if (!IsValidFileName(Str
)) {
724 return (EFI_INVALID_PARAMETER
);
727 // free the old file name
729 SHELL_FREE_NON_NULL (FileBuffer
.FileName
);
732 // Allocate and set the new name
734 FileBuffer
.FileName
= CatSPrint (NULL
, L
"%s", Str
);
735 if (FileBuffer
.FileName
== NULL
) {
736 return EFI_OUT_OF_RESOURCES
;
742 Free the existing file lines and reset the modified flag.
744 @retval EFI_SUCCESS The operation was successful.
753 // free all the lines
755 FileBufferFreeLines ();
756 FileBuffer
.FileModified
= FALSE
;
763 Read a file from disk into the FileBuffer.
765 @param[in] FileName The filename to read.
766 @param[in] Recover TRUE if is for recover mode, no information printouts.
768 @retval EFI_SUCCESS The load was successful.
769 @retval EFI_LOAD_ERROR The load failed.
770 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
771 @retval EFI_INVALID_PARAMETER FileName is a directory.
776 IN CONST CHAR16
*FileName
,
777 IN CONST BOOLEAN Recover
780 EFI_EDITOR_LINE
*Line
;
781 EE_NEWLINE_TYPE Type
;
786 CHAR16
*UnicodeBuffer
;
789 SHELL_FILE_HANDLE FileHandle
;
792 UINTN LineSizeBackup
;
798 UnicodeBuffer
= NULL
;
799 Type
= NewLineTypeDefault
;
804 // in this function, when you return error ( except EFI_OUT_OF_RESOURCES )
805 // you should set status string via StatusBarSetStatusString(L"blah")
806 // since this function maybe called before the editorhandleinput loop
807 // so any error will cause editor return
808 // so if you want to print the error status
809 // you should set the status string
813 // try to open the file
815 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
817 if (!EFI_ERROR(Status
)) {
819 if (FileHandle
== NULL
) {
820 StatusBarSetStatusString (L
"Disk Error");
821 return EFI_LOAD_ERROR
;
824 Info
= ShellGetFileInfo(FileHandle
);
826 if (Info
->Attribute
& EFI_FILE_DIRECTORY
) {
827 StatusBarSetStatusString (L
"Directory Can Not Be Edited");
829 return EFI_INVALID_PARAMETER
;
832 if (Info
->Attribute
& EFI_FILE_READ_ONLY
) {
833 FileBuffer
.ReadOnly
= TRUE
;
835 FileBuffer
.ReadOnly
= FALSE
;
840 FileSize
= (UINTN
) Info
->FileSize
;
843 } else if (Status
== EFI_NOT_FOUND
) {
845 // file not exists. add create and try again
847 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, 0);
848 if (EFI_ERROR (Status
)) {
849 if (Status
== EFI_WRITE_PROTECTED
||
850 Status
== EFI_ACCESS_DENIED
||
851 Status
== EFI_NO_MEDIA
||
852 Status
== EFI_MEDIA_CHANGED
854 StatusBarSetStatusString (L
"Access Denied");
855 } else if (Status
== EFI_DEVICE_ERROR
|| Status
== EFI_VOLUME_CORRUPTED
|| Status
== EFI_VOLUME_FULL
) {
856 StatusBarSetStatusString (L
"Disk Error");
858 StatusBarSetStatusString (L
"Invalid File Name or Current-working-directory");
864 // it worked. now delete it and move on with the name (now validated)
866 Status
= ShellDeleteFile (&FileHandle
);
867 if (Status
== EFI_WARN_DELETE_FAILURE
) {
868 Status
= EFI_ACCESS_DENIED
;
871 if (EFI_ERROR (Status
)) {
872 StatusBarSetStatusString (L
"Access Denied");
877 // file doesn't exist, so set CreateFile to TRUE
880 FileBuffer
.ReadOnly
= FALSE
;
883 // all the check ends
884 // so now begin to set file name, free lines
886 if (StrCmp (FileName
, FileBuffer
.FileName
) != 0) {
887 FileBufferSetFileName (FileName
);
890 // free the old lines
900 // allocate buffer to read file
902 Buffer
= AllocateZeroPool (FileSize
);
903 if (Buffer
== NULL
) {
904 return EFI_OUT_OF_RESOURCES
;
907 // read file into Buffer
909 Status
= ShellReadFile (FileHandle
, &FileSize
, Buffer
);
910 ShellCloseFile(&FileHandle
);
912 if (EFI_ERROR (Status
)) {
913 StatusBarSetStatusString (L
"Read File Failed");
914 SHELL_FREE_NON_NULL (Buffer
);
915 return EFI_LOAD_ERROR
;
918 // nothing in this file
921 SHELL_FREE_NON_NULL (Buffer
);
923 // since has no head, so only can be an ASCII file
925 FileBuffer
.FileType
= FileTypeAscii
;
930 AsciiBuffer
= Buffer
;
934 // size < Unicode file header, so only can be ASCII file
936 FileBuffer
.FileType
= FileTypeAscii
;
941 if (*(UINT16
*) Buffer
== EFI_UNICODE_BYTE_ORDER_MARK
) {
943 // Unicode file's size should be even
945 if ((FileSize
% 2) != 0) {
946 StatusBarSetStatusString (L
"File Format Wrong");
947 SHELL_FREE_NON_NULL (Buffer
);
948 return EFI_LOAD_ERROR
;
953 FileBuffer
.FileType
= FileTypeUnicode
;
954 UnicodeBuffer
= Buffer
;
957 // pass this 0xff and 0xfe
962 FileBuffer
.FileType
= FileTypeAscii
;
965 // end of AsciiBuffer ==
969 // end of FileSize < 2
970 // all the check ends
971 // so now begin to set file name, free lines
973 if (StrCmp (FileName
, FileBuffer
.FileName
) != 0) {
974 FileBufferSetFileName (FileName
);
978 // free the old lines
983 // parse file content line by line
985 for (LoopVar1
= 0; LoopVar1
< FileSize
; LoopVar1
++) {
986 Type
= NewLineTypeUnknown
;
988 for (LineSize
= LoopVar1
; LineSize
< FileSize
; LineSize
++) {
989 if (FileBuffer
.FileType
== FileTypeAscii
) {
990 if (AsciiBuffer
[LineSize
] == CHAR_CARRIAGE_RETURN
) {
991 Type
= NewLineTypeCarriageReturn
;
996 if (LineSize
< FileSize
- 1) {
997 if (AsciiBuffer
[LineSize
+ 1] == CHAR_LINEFEED
) {
998 Type
= NewLineTypeCarriageReturnLineFeed
;
1003 } else if (AsciiBuffer
[LineSize
] == CHAR_LINEFEED
) {
1004 Type
= NewLineTypeLineFeed
;
1009 if (LineSize
< FileSize
- 1) {
1010 if (AsciiBuffer
[LineSize
+ 1] == CHAR_CARRIAGE_RETURN
) {
1011 Type
= NewLineTypeLineFeedCarriageReturn
;
1018 if (UnicodeBuffer
[LineSize
] == CHAR_CARRIAGE_RETURN
) {
1019 Type
= NewLineTypeCarriageReturn
;
1024 if (LineSize
< FileSize
- 1) {
1025 if (UnicodeBuffer
[LineSize
+ 1] == CHAR_LINEFEED
) {
1026 Type
= NewLineTypeCarriageReturnLineFeed
;
1031 } else if (UnicodeBuffer
[LineSize
] == CHAR_LINEFEED
) {
1032 Type
= NewLineTypeLineFeed
;
1037 if (LineSize
< FileSize
- 1) {
1038 if (UnicodeBuffer
[LineSize
+ 1] == CHAR_CARRIAGE_RETURN
) {
1039 Type
= NewLineTypeLineFeedCarriageReturn
;
1051 // end of for LineSize
1053 // if the type is wrong, then exit
1055 if (Type
== NewLineTypeUnknown
) {
1057 // Now if Type is NewLineTypeUnknown, it should be file end
1059 Type
= NewLineTypeDefault
;
1062 LineSizeBackup
= LineSize
;
1065 // create a new line
1067 Line
= FileBufferCreateLine ();
1069 SHELL_FREE_NON_NULL (Buffer
);
1070 return EFI_OUT_OF_RESOURCES
;
1073 // calculate file length
1075 LineSize
-= LoopVar1
;
1078 // Unicode and one CHAR_NULL
1080 SHELL_FREE_NON_NULL (Line
->Buffer
);
1081 Line
->Buffer
= AllocateZeroPool (LineSize
* 2 + 2);
1083 if (Line
->Buffer
== NULL
) {
1084 RemoveEntryList (&Line
->Link
);
1085 return EFI_OUT_OF_RESOURCES
;
1088 // copy this line to Line->Buffer
1090 for (LoopVar2
= 0; LoopVar2
< LineSize
; LoopVar2
++) {
1091 if (FileBuffer
.FileType
== FileTypeAscii
) {
1092 Line
->Buffer
[LoopVar2
] = (CHAR16
) AsciiBuffer
[LoopVar1
];
1094 Line
->Buffer
[LoopVar2
] = UnicodeBuffer
[LoopVar1
];
1100 // LoopVar1 now points to where CHAR_CARRIAGE_RETURN or CHAR_LINEFEED;
1102 Line
->Buffer
[LineSize
] = 0;
1104 Line
->Size
= LineSize
;
1105 Line
->TotalSize
= LineSize
;
1108 if (Type
== NewLineTypeCarriageReturnLineFeed
|| Type
== NewLineTypeLineFeedCarriageReturn
) {
1113 // last character is a return, SO create a new line
1115 if (((Type
== NewLineTypeCarriageReturnLineFeed
|| Type
== NewLineTypeLineFeedCarriageReturn
) && LineSizeBackup
== FileSize
- 2) ||
1116 ((Type
== NewLineTypeLineFeed
|| Type
== NewLineTypeCarriageReturn
) && LineSizeBackup
== FileSize
- 1)
1118 Line
= FileBufferCreateLine ();
1120 SHELL_FREE_NON_NULL (Buffer
);
1121 return EFI_OUT_OF_RESOURCES
;
1131 SHELL_FREE_NON_NULL (Buffer
);
1135 // end of if CreateFile
1139 FileBuffer
.DisplayPosition
.Row
= 2;
1140 FileBuffer
.DisplayPosition
.Column
= 1;
1141 FileBuffer
.LowVisibleRange
.Row
= 1;
1142 FileBuffer
.LowVisibleRange
.Column
= 1;
1143 FileBuffer
.FilePosition
.Row
= 1;
1144 FileBuffer
.FilePosition
.Column
= 1;
1145 FileBuffer
.MousePosition
.Row
= 2;
1146 FileBuffer
.MousePosition
.Column
= 1;
1149 UnicodeBuffer
= CatSPrint (NULL
, L
"%d Lines Read", FileBuffer
.NumLines
);
1150 if (UnicodeBuffer
== NULL
) {
1151 return EFI_OUT_OF_RESOURCES
;
1154 StatusBarSetStatusString (UnicodeBuffer
);
1155 FreePool (UnicodeBuffer
);
1159 // check whether we have fs?: in filename
1162 FSMappingPtr = NULL;
1163 while (FileName[LoopVar1] != 0) {
1164 if (FileName[LoopVar1] == L':') {
1165 FSMappingPtr = &FileName[LoopVar1];
1172 if (FSMappingPtr == NULL) {
1173 CurDir = ShellGetCurrentDir (NULL);
1177 while (FileName[LoopVar1] != 0) {
1178 if (FileName[LoopVar1] == L':') {
1182 FSMapping[LoopVar2++] = FileName[LoopVar1];
1187 FSMapping[LoopVar2] = 0;
1188 CurDir = ShellGetCurrentDir (FSMapping);
1191 if (CurDir != NULL) {
1192 for (LoopVar1 = 0; LoopVar1 < StrLen (CurDir) && CurDir[LoopVar1] != ':'; LoopVar1++);
1194 CurDir[LoopVar1] = 0;
1195 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) ShellGetMap (CurDir);
1198 return EFI_LOAD_ERROR;
1201 Status = LibDevicePathToInterface (
1202 &gEfiSimpleFileSystemProtocolGuid,
1206 if (EFI_ERROR (Status)) {
1207 return EFI_LOAD_ERROR;
1210 Status = Vol->OpenVolume (Vol, &RootFs);
1211 if (EFI_ERROR (Status)) {
1212 return EFI_LOAD_ERROR;
1215 // Get volume information of file system
1217 Size = SIZE_OF_EFI_FILE_SYSTEM_INFO + 100;
1218 VolumeInfo = (EFI_FILE_SYSTEM_INFO *) AllocateZeroPool (Size);
1219 Status = RootFs->GetInfo (RootFs, &gEfiFileSystemInfoGuid, &Size, VolumeInfo);
1220 if (EFI_ERROR (Status)) {
1221 RootFs->Close (RootFs);
1222 return EFI_LOAD_ERROR;
1225 if (VolumeInfo->ReadOnly) {
1226 StatusBarSetStatusString (L"WARNING: Volume Read Only");
1229 FreePool (VolumeInfo);
1230 RootFs->Close (RootFs);
1237 if (FileBuffer
.Lines
!= 0) {
1238 FileBuffer
.CurrentLine
= CR (FileBuffer
.ListHead
->ForwardLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
1241 // create a dummy line
1243 Line
= FileBufferCreateLine ();
1245 return EFI_OUT_OF_RESOURCES
;
1248 FileBuffer
.CurrentLine
= Line
;
1251 FileBuffer
.FileModified
= FALSE
;
1252 FileBufferNeedRefresh
= TRUE
;
1253 FileBufferOnlyLineNeedRefresh
= FALSE
;
1254 FileBufferMouseNeedRefresh
= TRUE
;
1261 According to FileBuffer.NewLineType & FileBuffer.FileType,
1262 get the return buffer and size.
1264 @param[in] Type The type of line.
1265 @param[out] Buffer The buffer to fill.
1266 @param[out] Size The amount of the buffer used on return.
1271 IN CONST EE_NEWLINE_TYPE Type
,
1279 // give new line buffer,
1280 // and will judge unicode or ascii
1285 // not legal new line type
1287 if (Type
!= NewLineTypeLineFeed
&& Type
!= NewLineTypeCarriageReturn
&& Type
!= NewLineTypeCarriageReturnLineFeed
&& Type
!= NewLineTypeLineFeedCarriageReturn
) {
1292 // use_cr: give 0x0d
1294 if (Type
== NewLineTypeCarriageReturn
) {
1295 if (MainEditor
.FileBuffer
->FileType
== FileTypeUnicode
) {
1304 *Size
= NewLineSize
;
1308 // use_lf: give 0x0a
1310 if (Type
== NewLineTypeLineFeed
) {
1311 if (MainEditor
.FileBuffer
->FileType
== FileTypeUnicode
) {
1320 *Size
= NewLineSize
;
1324 // use_crlf: give 0x0d 0x0a
1326 if (Type
== NewLineTypeCarriageReturnLineFeed
) {
1327 if (MainEditor
.FileBuffer
->FileType
== FileTypeUnicode
) {
1340 *Size
= NewLineSize
;
1344 // use_lfcr: give 0x0a 0x0d
1346 if (Type
== NewLineTypeLineFeedCarriageReturn
) {
1347 if (MainEditor
.FileBuffer
->FileType
== FileTypeUnicode
) {
1360 *Size
= NewLineSize
;
1367 Change a Unicode string to an ASCII string.
1369 @param[in] UStr The Unicode string.
1370 @param[in] Length The maximum size of AStr.
1371 @param[out] AStr ASCII string to pass out.
1373 @return The actuall length.
1378 IN CONST CHAR16
*UStr
,
1379 IN CONST UINTN Length
,
1386 // just buffer copy, not character copy
1388 for (Index
= 0; Index
< Length
; Index
++) {
1389 *AStr
++ = (CHAR8
) *UStr
++;
1396 Save lines in FileBuffer to disk
1398 @param[in] FileName The file name for writing.
1400 @retval EFI_SUCCESS Data was written.
1401 @retval EFI_LOAD_ERROR
1402 @retval EFI_OUT_OF_RESOURCES There were not enough resources to write the file.
1407 IN CONST CHAR16
*FileName
1410 SHELL_FILE_HANDLE FileHandle
;
1412 EFI_EDITOR_LINE
*Line
;
1418 CHAR8 NewLineBuffer
[4];
1421 EFI_FILE_INFO
*Info
;
1425 EE_NEWLINE_TYPE Type
;
1440 TotalSize
= 0x200000;
1447 // if is the old file
1449 if (FileBuffer
.FileName
!= NULL
&& StrCmp (FileName
, FileBuffer
.FileName
) == 0) {
1451 // file has not been modified
1453 if (!FileBuffer
.FileModified
) {
1458 // if file is read-only, set error
1460 if (FileBuffer
.ReadOnly
) {
1461 StatusBarSetStatusString (L
"Read Only File Can Not Be Saved");
1466 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
, 0);
1468 if (!EFI_ERROR (Status
)) {
1469 Info
= ShellGetFileInfo(FileHandle
);
1471 if (Info
!= NULL
&& Info
->Attribute
& EFI_FILE_DIRECTORY
) {
1472 StatusBarSetStatusString (L
"Directory Can Not Be Saved");
1473 ShellCloseFile(FileHandle
);
1475 return EFI_LOAD_ERROR
;
1479 Attribute
= Info
->Attribute
& ~EFI_FILE_READ_ONLY
;
1484 // if file exits, so delete it
1486 Status
= ShellDeleteFile (&FileHandle
);
1487 if (EFI_ERROR (Status
) || Status
== EFI_WARN_DELETE_FAILURE
) {
1488 StatusBarSetStatusString (L
"Write File Failed");
1489 return EFI_LOAD_ERROR
;
1493 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, Attribute
);
1495 if (EFI_ERROR (Status
)) {
1496 StatusBarSetStatusString (L
"Create File Failed");
1497 return EFI_LOAD_ERROR
;
1501 // if file is Unicode file, write Unicode header to it.
1503 if (FileBuffer
.FileType
== FileTypeUnicode
) {
1505 Status
= ShellWriteFile (FileHandle
, &Length
, (VOID
*)&gUnicodeFileTag
);
1506 if (EFI_ERROR (Status
)) {
1507 ShellDeleteFile (&FileHandle
);
1508 return EFI_LOAD_ERROR
;
1512 Cache
= AllocateZeroPool (TotalSize
);
1513 if (Cache
== NULL
) {
1514 ShellDeleteFile (&FileHandle
);
1515 return EFI_OUT_OF_RESOURCES
;
1519 // write all the lines back to disk
1522 Type
= NewLineTypeCarriageReturnLineFeed
;
1525 LeftSize
= TotalSize
;
1527 for (Link
= FileBuffer
.ListHead
->ForwardLink
; Link
!= FileBuffer
.ListHead
; Link
= Link
->ForwardLink
) {
1528 Line
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
1530 if (Line
->Type
!= NewLineTypeDefault
) {
1534 // newline character is at most 4 bytes ( two Unicode characters )
1537 if (Line
->Buffer
!= NULL
&& Line
->Size
!= 0) {
1538 if (FileBuffer
.FileType
== FileTypeAscii
) {
1539 Length
+= Line
->Size
;
1541 Length
+= (Line
->Size
* 2);
1544 // end if FileTypeAscii
1549 // no cache room left, so write cache to disk
1551 if (LeftSize
< Length
) {
1552 Size
= TotalSize
- LeftSize
;
1553 Status
= ShellWriteFile (FileHandle
, &Size
, Cache
);
1554 if (EFI_ERROR (Status
)) {
1555 ShellDeleteFile (&FileHandle
);
1557 return EFI_LOAD_ERROR
;
1560 LeftSize
= TotalSize
;
1563 if (Line
->Buffer
!= NULL
&& Line
->Size
!= 0) {
1564 if (FileBuffer
.FileType
== FileTypeAscii
) {
1565 UnicodeToAscii (Line
->Buffer
, Line
->Size
, Ptr
);
1566 Length
= Line
->Size
;
1568 Length
= (Line
->Size
* 2);
1569 CopyMem (Ptr
, (CHAR8
*) Line
->Buffer
, Length
);
1572 // end if FileTypeAscii
1579 // end of if Line -> Buffer != NULL && Line -> Size != 0
1581 // if not the last line , write return buffer to disk
1583 if (Link
->ForwardLink
!= FileBuffer
.ListHead
) {
1584 GetNewLine (Type
, NewLineBuffer
, &NewLineSize
);
1585 CopyMem (Ptr
, (CHAR8
*) NewLineBuffer
, NewLineSize
);
1588 LeftSize
-= NewLineSize
;
1594 if (TotalSize
!= LeftSize
) {
1595 Size
= TotalSize
- LeftSize
;
1596 Status
= ShellWriteFile (FileHandle
, &Size
, Cache
);
1597 if (EFI_ERROR (Status
)) {
1598 ShellDeleteFile (&FileHandle
);
1600 return EFI_LOAD_ERROR
;
1606 ShellCloseFile(&FileHandle
);
1608 FileBuffer
.FileModified
= FALSE
;
1611 // set status string
1613 Str
= CatSPrint (NULL
, L
"%d Lines Wrote", NumLines
);
1615 return EFI_OUT_OF_RESOURCES
;
1618 StatusBarSetStatusString (Str
);
1619 SHELL_FREE_NON_NULL (Str
);
1622 // now everything is ready , you can set the new file name to filebuffer
1624 if (FileName
!= NULL
&& FileBuffer
.FileName
!= NULL
&& StrCmp (FileName
, FileBuffer
.FileName
) != 0) {
1628 FileBufferSetFileName (FileName
);
1629 if (FileBuffer
.FileName
== NULL
) {
1630 ShellDeleteFile (&FileHandle
);
1631 return EFI_OUT_OF_RESOURCES
;
1635 FileBuffer
.ReadOnly
= FALSE
;
1640 Scroll cursor to left 1 character position.
1642 @retval EFI_SUCCESS The operation was successful.
1646 FileBufferScrollLeft (
1650 EFI_EDITOR_LINE
*Line
;
1654 Line
= FileBuffer
.CurrentLine
;
1656 FRow
= FileBuffer
.FilePosition
.Row
;
1657 FCol
= FileBuffer
.FilePosition
.Column
;
1660 // if already at start of this line, so move to the end of previous line
1664 // has previous line
1666 if (Line
->Link
.BackLink
!= FileBuffer
.ListHead
) {
1668 Line
= CR (Line
->Link
.BackLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
1669 FCol
= Line
->Size
+ 1;
1675 // if not at start of this line, just move to previous column
1680 FileBufferMovePosition (FRow
, FCol
);
1686 Delete a char in line
1688 @param[in,out] Line The line to delete in.
1689 @param[in] Pos Position to delete the char at ( start from 0 ).
1694 IN OUT EFI_EDITOR_LINE
*Line
,
1701 // move the latter characters front
1703 for (Index
= Pos
- 1; Index
< Line
->Size
; Index
++) {
1704 Line
->Buffer
[Index
] = Line
->Buffer
[Index
+ 1];
1711 Concatenate Src into Dest.
1713 @param[in,out] Dest Destination string
1714 @param[in] Src Src String.
1719 IN OUT EFI_EDITOR_LINE
*Dest
,
1720 IN EFI_EDITOR_LINE
*Src
1728 Dest
->Buffer
[Size
] = 0;
1731 // concatenate the two strings
1733 Str
= CatSPrint (NULL
, L
"%s%s", Dest
->Buffer
, Src
->Buffer
);
1735 Dest
->Buffer
= NULL
;
1739 Dest
->Size
= Size
+ Src
->Size
;
1740 Dest
->TotalSize
= Dest
->Size
;
1742 FreePool (Dest
->Buffer
);
1743 FreePool (Src
->Buffer
);
1746 // put str to dest->buffer
1752 Delete the previous character.
1754 @retval EFI_SUCCESS The delete was successful.
1755 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1759 FileBufferDoBackspace (
1763 EFI_EDITOR_LINE
*Line
;
1764 EFI_EDITOR_LINE
*End
;
1768 FileColumn
= FileBuffer
.FilePosition
.Column
;
1770 Line
= FileBuffer
.CurrentLine
;
1775 if (FileColumn
== 1) {
1779 if (FileBuffer
.FilePosition
.Row
== 1) {
1783 FileBufferScrollLeft ();
1785 Line
= FileBuffer
.CurrentLine
;
1786 Link
= Line
->Link
.ForwardLink
;
1787 End
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
1790 // concatenate this line with previous line
1792 LineCat (Line
, End
);
1793 if (Line
->Buffer
== NULL
) {
1794 return EFI_OUT_OF_RESOURCES
;
1797 // remove End from line list
1799 RemoveEntryList (&End
->Link
);
1802 FileBuffer
.NumLines
--;
1804 FileBufferNeedRefresh
= TRUE
;
1805 FileBufferOnlyLineNeedRefresh
= FALSE
;
1809 // just delete the previous character
1811 LineDeleteAt (Line
, FileColumn
- 1);
1812 FileBufferScrollLeft ();
1813 FileBufferOnlyLineNeedRefresh
= TRUE
;
1816 if (!FileBuffer
.FileModified
) {
1817 FileBuffer
.FileModified
= TRUE
;
1824 Add a return into line at current position.
1826 @retval EFI_SUCCESS The insetrion of the character was successful.
1827 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1831 FileBufferDoReturn (
1835 EFI_EDITOR_LINE
*Line
;
1836 EFI_EDITOR_LINE
*NewLine
;
1843 FileBufferNeedRefresh
= TRUE
;
1844 FileBufferOnlyLineNeedRefresh
= FALSE
;
1846 Line
= FileBuffer
.CurrentLine
;
1848 FileColumn
= FileBuffer
.FilePosition
.Column
;
1850 NewLine
= AllocateZeroPool (sizeof (EFI_EDITOR_LINE
));
1851 if (NewLine
== NULL
) {
1852 return EFI_OUT_OF_RESOURCES
;
1855 NewLine
->Signature
= LINE_LIST_SIGNATURE
;
1856 NewLine
->Size
= Line
->Size
- FileColumn
+ 1;
1857 NewLine
->TotalSize
= NewLine
->Size
;
1858 NewLine
->Buffer
= CatSPrint (NULL
, L
"\0");
1859 if (NewLine
->Buffer
== NULL
) {
1860 return EFI_OUT_OF_RESOURCES
;
1863 NewLine
->Type
= NewLineTypeDefault
;
1865 if (NewLine
->Size
> 0) {
1867 // UNICODE + CHAR_NULL
1869 Buffer
= AllocateZeroPool (2 * (NewLine
->Size
+ 1));
1870 if (Buffer
== NULL
) {
1871 FreePool (NewLine
->Buffer
);
1873 return EFI_OUT_OF_RESOURCES
;
1876 FreePool (NewLine
->Buffer
);
1878 NewLine
->Buffer
= Buffer
;
1880 for (Index
= 0; Index
< NewLine
->Size
; Index
++) {
1881 NewLine
->Buffer
[Index
] = Line
->Buffer
[Index
+ FileColumn
- 1];
1884 NewLine
->Buffer
[NewLine
->Size
] = CHAR_NULL
;
1886 Line
->Buffer
[FileColumn
- 1] = CHAR_NULL
;
1887 Line
->Size
= FileColumn
- 1;
1890 // increase NumLines
1892 FileBuffer
.NumLines
++;
1895 // insert it into the correct position of line list
1897 NewLine
->Link
.BackLink
= &(Line
->Link
);
1898 NewLine
->Link
.ForwardLink
= Line
->Link
.ForwardLink
;
1899 Line
->Link
.ForwardLink
->BackLink
= &(NewLine
->Link
);
1900 Line
->Link
.ForwardLink
= &(NewLine
->Link
);
1903 // move cursor to the start of next line
1905 Row
= FileBuffer
.FilePosition
.Row
+ 1;
1908 FileBufferMovePosition (Row
, Col
);
1911 // set file is modified
1913 if (!FileBuffer
.FileModified
) {
1914 FileBuffer
.FileModified
= TRUE
;
1921 Delete current character from current line. This is the effect caused
1928 FileBufferDoDelete (
1932 EFI_EDITOR_LINE
*Line
;
1933 EFI_EDITOR_LINE
*Next
;
1937 Line
= FileBuffer
.CurrentLine
;
1938 FileColumn
= FileBuffer
.FilePosition
.Column
;
1943 if (FileColumn
>= Line
->Size
+ 1) {
1947 if (Line
->Link
.ForwardLink
== FileBuffer
.ListHead
) {
1951 // since last character,
1952 // so will add the next line to this line
1954 Link
= Line
->Link
.ForwardLink
;
1955 Next
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
1956 LineCat (Line
, Next
);
1957 if (Line
->Buffer
== NULL
) {
1958 return EFI_OUT_OF_RESOURCES
;
1961 RemoveEntryList (&Next
->Link
);
1964 FileBuffer
.NumLines
--;
1966 FileBufferNeedRefresh
= TRUE
;
1967 FileBufferOnlyLineNeedRefresh
= FALSE
;
1971 // just delete current character
1973 LineDeleteAt (Line
, FileColumn
);
1974 FileBufferOnlyLineNeedRefresh
= TRUE
;
1977 if (!FileBuffer
.FileModified
) {
1978 FileBuffer
.FileModified
= TRUE
;
1985 Scroll cursor to right 1 character.
1987 @retval EFI_SUCCESS The operation was successful.
1991 FileBufferScrollRight (
1995 EFI_EDITOR_LINE
*Line
;
1999 Line
= FileBuffer
.CurrentLine
;
2000 if (Line
->Buffer
== NULL
) {
2004 FRow
= FileBuffer
.FilePosition
.Row
;
2005 FCol
= FileBuffer
.FilePosition
.Column
;
2008 // if already at end of this line, scroll it to the start of next line
2010 if (FCol
> Line
->Size
) {
2014 if (Line
->Link
.ForwardLink
!= FileBuffer
.ListHead
) {
2022 // if not at end of this line, just move to next column
2027 FileBufferMovePosition (FRow
, FCol
);
2033 Insert a char into line
2036 @param[in] Line The line to insert into.
2037 @param[in] Char The char to insert.
2038 @param[in] Pos The position to insert the char at ( start from 0 ).
2039 @param[in] StrSize The current string size ( include CHAR_NULL ),unit is Unicode character.
2041 @return The new string size ( include CHAR_NULL ) ( unit is Unicode character ).
2046 IN EFI_EDITOR_LINE
*Line
,
2053 CHAR16
*TempStringPtr
;
2056 Index
= (StrSize
) * 2;
2061 // do not have free space
2063 if (Line
->TotalSize
<= Line
->Size
) {
2064 Str
= ReallocatePool (Index
, Index
+ 16, Str
);
2069 Line
->TotalSize
+= 8;
2072 // move the later part of the string one character right
2074 TempStringPtr
= Str
;
2075 for (Index
= StrSize
; Index
> Pos
; Index
--) {
2076 TempStringPtr
[Index
] = TempStringPtr
[Index
- 1];
2079 // insert char into it.
2081 TempStringPtr
[Index
] = Char
;
2090 Add a character to the current line.
2092 @param[in] Char The Character to input.
2094 @retval EFI_SUCCESS The input was succesful.
2102 EFI_EDITOR_LINE
*Line
;
2105 Line
= FileBuffer
.CurrentLine
;
2108 // only needs to refresh current line
2110 FileBufferOnlyLineNeedRefresh
= TRUE
;
2113 // when is insert mode, or cursor is at end of this line,
2114 // so insert this character
2115 // or replace the character.
2117 FilePos
= FileBuffer
.FilePosition
.Column
- 1;
2118 if (FileBuffer
.ModeInsert
|| FilePos
+ 1 > Line
->Size
) {
2119 LineStrInsert (Line
, Char
, FilePos
, Line
->Size
+ 1);
2121 Line
->Buffer
[FilePos
] = Char
;
2124 // move cursor to right
2126 FileBufferScrollRight ();
2128 if (!FileBuffer
.FileModified
) {
2129 FileBuffer
.FileModified
= TRUE
;
2136 Handles inputs from characters (ASCII key + Backspace + return)
2138 @param[in] Char The input character.
2140 @retval EFI_SUCCESS The operation was successful.
2141 @retval EFI_LOAD_ERROR There was an error.
2142 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2146 FileBufferDoCharInput (
2147 IN CONST CHAR16 Char
2152 Status
= EFI_SUCCESS
;
2158 case CHAR_BACKSPACE
:
2159 Status
= FileBufferDoBackspace ();
2169 case CHAR_CARRIAGE_RETURN
:
2170 Status
= FileBufferDoReturn ();
2175 // DEAL WITH ASCII CHAR, filter out thing like ctrl+f
2177 if (Char
> 127 || Char
< 32) {
2178 Status
= StatusBarSetStatusString (L
"Unknown Command");
2180 Status
= FileBufferAddChar (Char
);
2191 Scroll cursor to the next line.
2193 @retval EFI_SUCCESS The operation was successful.
2197 FileBufferScrollDown (
2201 EFI_EDITOR_LINE
*Line
;
2205 Line
= FileBuffer
.CurrentLine
;
2206 if (Line
->Buffer
== NULL
) {
2210 FRow
= FileBuffer
.FilePosition
.Row
;
2211 FCol
= FileBuffer
.FilePosition
.Column
;
2216 if (Line
->Link
.ForwardLink
!= FileBuffer
.ListHead
) {
2218 Line
= CR (Line
->Link
.ForwardLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
2221 // if the next line is not that long, so move to end of next line
2223 if (FCol
> Line
->Size
) {
2224 FCol
= Line
->Size
+ 1;
2231 FileBufferMovePosition (FRow
, FCol
);
2237 Scroll the cursor to previous line.
2239 @retval EFI_SUCCESS The operation was successful.
2243 FileBufferScrollUp (
2247 EFI_EDITOR_LINE
*Line
;
2251 Line
= FileBuffer
.CurrentLine
;
2253 FRow
= FileBuffer
.FilePosition
.Row
;
2254 FCol
= FileBuffer
.FilePosition
.Column
;
2257 // has previous line
2259 if (Line
->Link
.BackLink
!= FileBuffer
.ListHead
) {
2261 Line
= CR (Line
->Link
.BackLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
2264 // if previous line is not that long, so move to the end of previous line
2266 if (FCol
> Line
->Size
) {
2267 FCol
= Line
->Size
+ 1;
2274 FileBufferMovePosition (FRow
, FCol
);
2280 Scroll cursor to next page.
2282 @retval EFI_SUCCESS The operation wa successful.
2286 FileBufferPageDown (
2290 EFI_EDITOR_LINE
*Line
;
2295 Line
= FileBuffer
.CurrentLine
;
2297 FRow
= FileBuffer
.FilePosition
.Row
;
2298 FCol
= FileBuffer
.FilePosition
.Column
;
2303 if (FileBuffer
.NumLines
>= FRow
+ (MainEditor
.ScreenSize
.Row
- 2)) {
2304 Gap
= (MainEditor
.ScreenSize
.Row
- 2);
2307 // MOVE CURSOR TO LAST LINE
2309 Gap
= FileBuffer
.NumLines
- FRow
;
2314 Line
= MoveLine (Gap
);
2317 // if that line, is not that long, so move to the end of that line
2319 if (Line
!= NULL
&& FCol
> Line
->Size
) {
2320 FCol
= Line
->Size
+ 1;
2325 FileBufferMovePosition (FRow
, FCol
);
2331 Scroll cursor to previous screen.
2333 @retval EFI_SUCCESS The operation was successful.
2341 EFI_EDITOR_LINE
*Line
;
2347 Line
= FileBuffer
.CurrentLine
;
2349 FRow
= FileBuffer
.FilePosition
.Row
;
2350 FCol
= FileBuffer
.FilePosition
.Column
;
2353 // has previous page
2355 if (FRow
> (MainEditor
.ScreenSize
.Row
- 2)) {
2356 Gap
= (MainEditor
.ScreenSize
.Row
- 2);
2359 // the first line of file will displayed on the first line of screen
2370 Line
= MoveLine (Retreat
);
2373 // if that line is not that long, so move to the end of that line
2375 if (Line
!= NULL
&& FCol
> Line
->Size
) {
2376 FCol
= Line
->Size
+ 1;
2381 FileBufferMovePosition (FRow
, FCol
);
2387 Scroll cursor to end of the current line.
2389 @retval EFI_SUCCESS The operation was successful.
2397 EFI_EDITOR_LINE
*Line
;
2401 Line
= FileBuffer
.CurrentLine
;
2403 FRow
= FileBuffer
.FilePosition
.Row
;
2406 // goto the last column of the line
2408 FCol
= Line
->Size
+ 1;
2410 FileBufferMovePosition (FRow
, FCol
);
2416 Dispatch input to different handler
2417 @param[in] Key The input key. One of:
2421 Direction key: up/down/left/right/pgup/pgdn
2425 @retval EFI_SUCCESS The dispatch was done successfully.
2426 @retval EFI_LOAD_ERROR The dispatch was not successful.
2427 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2431 FileBufferHandleInput (
2432 IN CONST EFI_INPUT_KEY
*Key
2437 Status
= EFI_SUCCESS
;
2439 switch (Key
->ScanCode
) {
2441 // ordinary key input
2444 if (!FileBuffer
.ReadOnly
) {
2445 Status
= FileBufferDoCharInput (Key
->UnicodeChar
);
2447 Status
= StatusBarSetStatusString (L
"Read Only File Can Not Be Modified");
2456 Status
= FileBufferScrollUp ();
2463 Status
= FileBufferScrollDown ();
2470 Status
= FileBufferScrollRight ();
2477 Status
= FileBufferScrollLeft ();
2484 Status
= FileBufferPageUp ();
2490 case SCAN_PAGE_DOWN
:
2491 Status
= FileBufferPageDown ();
2498 if (!FileBuffer
.ReadOnly
) {
2499 Status
= FileBufferDoDelete ();
2501 Status
= StatusBarSetStatusString (L
"Read Only File Can Not Be Modified");
2510 FileBufferMovePosition (FileBuffer
.FilePosition
.Row
, 1);
2511 Status
= EFI_SUCCESS
;
2518 Status
= FileBufferEnd ();
2525 FileBuffer
.ModeInsert
= (BOOLEAN
)!FileBuffer
.ModeInsert
;
2526 Status
= EFI_SUCCESS
;
2530 Status
= StatusBarSetStatusString (L
"Unknown Command");
2538 Check user specified FileRow is above current screen.
2540 @param[in] FileRow The row of file position ( start from 1 ).
2542 @retval TRUE It is above the current screen.
2543 @retval FALSE It is not above the current screen.
2547 AboveCurrentScreen (
2552 // if is to the above of the screen
2554 if (FileRow
< FileBuffer
.LowVisibleRange
.Row
) {
2562 Check user specified FileRow is under current screen.
2564 @param[in] FileRow The row of file position ( start from 1 ).
2566 @retval TRUE It is under the current screen.
2567 @retval FALSE It is not under the current screen.
2571 UnderCurrentScreen (
2576 // if is to the under of the screen
2578 if (FileRow
> FileBuffer
.LowVisibleRange
.Row
+ (MainEditor
.ScreenSize
.Row
- 2) - 1) {
2586 Check user specified FileCol is left to current screen.
2588 @param[in] FileCol The column of file position ( start from 1 ).
2590 @retval TRUE It is to the left.
2591 @retval FALSE It is not to the left.
2600 // if is to the left of the screen
2602 if (FileCol
< FileBuffer
.LowVisibleRange
.Column
) {
2610 Check user specified FileCol is right to current screen.
2612 @param[in] FileCol The column of file position ( start from 1 ).
2614 @retval TRUE It is to the right.
2615 @retval FALSE It is not to the right.
2619 RightCurrentScreen (
2624 // if is to the right of the screen
2626 if (FileCol
> FileBuffer
.LowVisibleRange
.Column
+ MainEditor
.ScreenSize
.Column
- 1) {
2634 Advance/Retreat lines and set CurrentLine in FileBuffer to it
2636 @param[in] Count The line number to advance/retreat
2640 @retval NULL An error occured.
2641 @return The line after advance/retreat.
2649 EFI_EDITOR_LINE
*Line
;
2653 AbsCount
= (UINTN
)ABS(Count
);
2654 Line
= InternalEditorMiscLineRetreat (AbsCount
,MainEditor
.FileBuffer
->CurrentLine
,MainEditor
.FileBuffer
->ListHead
);
2656 Line
= InternalEditorMiscLineAdvance ((UINTN
)Count
,MainEditor
.FileBuffer
->CurrentLine
,MainEditor
.FileBuffer
->ListHead
);
2663 MainEditor
.FileBuffer
->CurrentLine
= Line
;
2669 According to cursor's file position, adjust screen display
2671 @param[in] NewFilePosRow The row of file position ( start from 1 ).
2672 @param[in] NewFilePosCol The column of file position ( start from 1 ).
2676 FileBufferMovePosition (
2677 IN CONST UINTN NewFilePosRow
,
2678 IN CONST UINTN NewFilePosCol
2690 // CALCULATE gap between current file position and new file position
2692 RowGap
= NewFilePosRow
- FileBuffer
.FilePosition
.Row
;
2693 ColGap
= NewFilePosCol
- FileBuffer
.FilePosition
.Column
;
2695 Under
= UnderCurrentScreen (NewFilePosRow
);
2696 Above
= AboveCurrentScreen (NewFilePosRow
);
2698 // if is below current screen
2702 // display row will be unchanged
2704 FileBuffer
.FilePosition
.Row
= NewFilePosRow
;
2708 // has enough above line, so display row unchanged
2709 // not has enough above lines, so the first line is at the
2710 // first display line
2712 if (NewFilePosRow
< (FileBuffer
.DisplayPosition
.Row
- 1)) {
2713 FileBuffer
.DisplayPosition
.Row
= NewFilePosRow
+ 1;
2716 FileBuffer
.FilePosition
.Row
= NewFilePosRow
;
2719 // in current screen
2721 FileBuffer
.FilePosition
.Row
= NewFilePosRow
;
2723 Abs
= (UINTN
)ABS(RowGap
);
2724 FileBuffer
.DisplayPosition
.Row
-= Abs
;
2726 FileBuffer
.DisplayPosition
.Row
+= RowGap
;
2731 FileBuffer
.LowVisibleRange
.Row
= FileBuffer
.FilePosition
.Row
- (FileBuffer
.DisplayPosition
.Row
- 2);
2733 Right
= RightCurrentScreen (NewFilePosCol
);
2734 Left
= LeftCurrentScreen (NewFilePosCol
);
2737 // if right to current screen
2741 // display column will be changed to end
2743 FileBuffer
.DisplayPosition
.Column
= MainEditor
.ScreenSize
.Column
;
2744 FileBuffer
.FilePosition
.Column
= NewFilePosCol
;
2748 // has enough left characters , so display row unchanged
2749 // not has enough left characters,
2750 // so the first character is at the first display column
2752 if (NewFilePosCol
< (FileBuffer
.DisplayPosition
.Column
)) {
2753 FileBuffer
.DisplayPosition
.Column
= NewFilePosCol
;
2756 FileBuffer
.FilePosition
.Column
= NewFilePosCol
;
2759 // in current screen
2761 FileBuffer
.FilePosition
.Column
= NewFilePosCol
;
2763 Abs
= (UINTN
)(-ColGap
);
2764 FileBuffer
.DisplayPosition
.Column
-= Abs
;
2766 FileBuffer
.DisplayPosition
.Column
+= ColGap
;
2771 FileBuffer
.LowVisibleRange
.Column
= FileBuffer
.FilePosition
.Column
- (FileBuffer
.DisplayPosition
.Column
- 1);
2774 // let CurrentLine point to correct line;
2776 FileBuffer
.CurrentLine
= MoveCurrentLine (RowGap
);
2781 Cut current line out and return a pointer to it.
2783 @param[out] CutLine Upon a successful return pointer to the pointer to
2784 the allocated cut line.
2786 @retval EFI_SUCCESS The cut was successful.
2787 @retval EFI_NOT_FOUND There was no selection to cut.
2788 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2793 OUT EFI_EDITOR_LINE
**CutLine
2796 EFI_EDITOR_LINE
*Line
;
2797 EFI_EDITOR_LINE
*NewLine
;
2801 if (FileBuffer
.ReadOnly
) {
2802 StatusBarSetStatusString (L
"Read Only File Can Not Be Modified");
2806 Line
= FileBuffer
.CurrentLine
;
2809 // if is the last dummy line, SO CAN not cut
2811 if (StrCmp (Line
->Buffer
, L
"\0") == 0 && Line
->Link
.ForwardLink
== FileBuffer
.ListHead
2817 // LAST LINE AND NOTHING ON THIS LINE, SO CUT NOTHING
2819 StatusBarSetStatusString (L
"Nothing to Cut");
2820 return EFI_NOT_FOUND
;
2823 // if is the last line, so create a dummy line
2825 if (Line
->Link
.ForwardLink
== FileBuffer
.ListHead
) {
2828 // create a new line
2830 NewLine
= FileBufferCreateLine ();
2831 if (NewLine
== NULL
) {
2832 return EFI_OUT_OF_RESOURCES
;
2836 FileBuffer
.NumLines
--;
2837 Row
= FileBuffer
.FilePosition
.Row
;
2842 FileBuffer
.CurrentLine
= CR (
2843 FileBuffer
.CurrentLine
->Link
.ForwardLink
,
2849 RemoveEntryList (&Line
->Link
);
2851 FileBuffer
.Lines
= CR (FileBuffer
.ListHead
->ForwardLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
2853 FileBufferMovePosition (Row
, Col
);
2855 FileBuffer
.FileModified
= TRUE
;
2856 FileBufferNeedRefresh
= TRUE
;
2857 FileBufferOnlyLineNeedRefresh
= FALSE
;
2865 Paste a line into line list.
2867 @retval EFI_SUCCESS The paste was successful.
2868 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2872 FileBufferPasteLine (
2876 EFI_EDITOR_LINE
*Line
;
2877 EFI_EDITOR_LINE
*NewLine
;
2882 // if nothing is on clip board
2885 if (MainEditor
.CutLine
== NULL
) {
2889 // read only file can not be pasted on
2891 if (FileBuffer
.ReadOnly
) {
2892 StatusBarSetStatusString (L
"Read Only File Can Not Be Modified");
2896 NewLine
= LineDup (MainEditor
.CutLine
);
2897 if (NewLine
== NULL
) {
2898 return EFI_OUT_OF_RESOURCES
;
2901 // insert it above current line
2903 Line
= FileBuffer
.CurrentLine
;
2904 NewLine
->Link
.BackLink
= Line
->Link
.BackLink
;
2905 NewLine
->Link
.ForwardLink
= &Line
->Link
;
2907 Line
->Link
.BackLink
->ForwardLink
= &NewLine
->Link
;
2908 Line
->Link
.BackLink
= &NewLine
->Link
;
2910 FileBuffer
.NumLines
++;
2911 FileBuffer
.CurrentLine
= NewLine
;
2913 FileBuffer
.Lines
= CR (FileBuffer
.ListHead
->ForwardLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
2919 Row
= FileBuffer
.FilePosition
.Row
;
2921 FileBufferMovePosition (Row
, Col
);
2924 // after paste, set some value so that refresh knows to do something
2926 FileBuffer
.FileModified
= TRUE
;
2927 FileBufferNeedRefresh
= TRUE
;
2928 FileBufferOnlyLineNeedRefresh
= FALSE
;
2934 Search string from current position on in file
2936 @param[in] Str The search string.
2937 @param[in] Offset The offset from current position.
2939 @retval EFI_SUCCESS The operation was successful.
2940 @retval EFI_NOT_FOUND The string Str was not found.
2945 IN CONST CHAR16
*Str
,
2946 IN CONST UINTN Offset
2953 EFI_EDITOR_LINE
*Line
;
2961 // search if in current line
2963 Current
= FileBuffer
.CurrentLine
->Buffer
+ FileBuffer
.FilePosition
.Column
- 1 + Offset
;
2965 if (Current
>= (FileBuffer
.CurrentLine
->Buffer
+ FileBuffer
.CurrentLine
->Size
)) {
2969 Current
= FileBuffer
.CurrentLine
->Buffer
+ FileBuffer
.CurrentLine
->Size
;
2972 CharPos
= StrStr (Current
, Str
);
2973 if (CharPos
!= NULL
) {
2974 Position
= CharPos
- Current
;
2982 if (Position
!= 0) {
2983 Column
= (Position
- 1) + FileBuffer
.FilePosition
.Column
+ Offset
;
2984 Row
= FileBuffer
.FilePosition
.Row
;
2988 // not found so find through next lines
2990 Link
= FileBuffer
.CurrentLine
->Link
.ForwardLink
;
2992 Row
= FileBuffer
.FilePosition
.Row
+ 1;
2993 while (Link
!= FileBuffer
.ListHead
) {
2994 Line
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
2995 // Position = StrStr (Line->Buffer, Str);
2996 CharPos
= StrStr (Line
->Buffer
, Str
);
2997 if (CharPos
!= NULL
) {
2998 Position
= CharPos
- Line
->Buffer
;
3000 if (Position
!= 0) {
3009 Link
= Link
->ForwardLink
;
3012 if (Link
== FileBuffer
.ListHead
) {
3020 return EFI_NOT_FOUND
;
3023 FileBufferMovePosition (Row
, Column
);
3026 // call refresh to fresh edit area,
3027 // because the outer may loop to find multiply occurrence of this string
3029 FileBufferRefresh ();
3035 Replace SearchLen characters from current position on with Replace.
3037 This will modify the current buffer at the current position.
3039 @param[in] Replace The string to replace.
3040 @param[in] SearchLen Search string's length.
3042 @retval EFI_SUCCESS The operation was successful.
3043 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
3048 IN CONST CHAR16
*Replace
,
3049 IN CONST UINTN SearchLen
3059 ReplaceLen
= StrLen (Replace
);
3061 OldSize
= FileBuffer
.CurrentLine
->Size
+ 1;
3063 // include CHAR_NULL
3065 NewSize
= OldSize
+ (ReplaceLen
- SearchLen
);
3067 if (ReplaceLen
> SearchLen
) {
3069 // do not have the enough space
3071 if (FileBuffer
.CurrentLine
->TotalSize
+ 1 <= NewSize
) {
3072 FileBuffer
.CurrentLine
->Buffer
= ReallocatePool (
3075 FileBuffer
.CurrentLine
->Buffer
3077 FileBuffer
.CurrentLine
->TotalSize
= NewSize
- 1;
3080 if (FileBuffer
.CurrentLine
->Buffer
== NULL
) {
3081 return EFI_OUT_OF_RESOURCES
;
3084 // the end CHAR_NULL character;
3086 Buffer
= FileBuffer
.CurrentLine
->Buffer
+ (NewSize
- 1);
3087 Gap
= ReplaceLen
- SearchLen
;
3090 // keep the latter part
3092 for (Index
= 0; Index
< (FileBuffer
.CurrentLine
->Size
- FileBuffer
.FilePosition
.Column
- SearchLen
+ 2); Index
++) {
3093 *Buffer
= *(Buffer
- Gap
);
3097 // set replace into it
3099 Buffer
= FileBuffer
.CurrentLine
->Buffer
+ FileBuffer
.FilePosition
.Column
;
3100 for (Index
= 0; Index
< ReplaceLen
; Index
++) {
3101 Buffer
[Index
] = Replace
[Index
];
3105 if (ReplaceLen
< SearchLen
) {
3106 Buffer
= FileBuffer
.CurrentLine
->Buffer
+ FileBuffer
.FilePosition
.Column
;
3108 for (Index
= 0; Index
< ReplaceLen
; Index
++) {
3109 Buffer
[Index
] = Replace
[Index
];
3112 Buffer
+= ReplaceLen
;
3113 Gap
= SearchLen
- ReplaceLen
;
3116 // set replace into it
3118 for (Index
= 0; Index
< (FileBuffer
.CurrentLine
->Size
- FileBuffer
.FilePosition
.Column
- ReplaceLen
+ 2); Index
++) {
3119 *Buffer
= *(Buffer
+ Gap
);
3124 if (ReplaceLen
== SearchLen
) {
3125 Buffer
= FileBuffer
.CurrentLine
->Buffer
+ FileBuffer
.FilePosition
.Column
;
3126 for (Index
= 0; Index
< ReplaceLen
; Index
++) {
3127 Buffer
[Index
] = Replace
[Index
];
3131 FileBuffer
.CurrentLine
->Size
+= (ReplaceLen
- SearchLen
);
3133 FileBufferOnlyLineNeedRefresh
= TRUE
;
3135 FileBuffer
.FileModified
= TRUE
;
3137 MainTitleBarRefresh (MainEditor
.FileBuffer
->FileName
, MainEditor
.FileBuffer
->FileType
, MainEditor
.FileBuffer
->ReadOnly
, MainEditor
.FileBuffer
->FileModified
, MainEditor
.ScreenSize
.Column
, MainEditor
.ScreenSize
.Row
, 0, 0);
3138 FileBufferRestorePosition ();
3139 FileBufferRefresh ();
3145 Move the mouse cursor position.
3147 @param[in] TextX The new x-coordinate.
3148 @param[in] TextY The new y-coordinate.
3152 FileBufferAdjustMousePosition (
3153 IN CONST INT32 TextX
,
3154 IN CONST INT32 TextY
3163 // TextX and TextY is mouse movement data returned by mouse driver
3164 // This function will change it to MousePosition
3167 // get absolute value
3173 CoordinateX
= FileBuffer
.MousePosition
.Column
;
3174 CoordinateY
= FileBuffer
.MousePosition
.Row
;
3177 CoordinateX
+= TextX
;
3179 if (CoordinateX
>= AbsX
) {
3180 CoordinateX
-= AbsX
;
3187 CoordinateY
+= TextY
;
3189 if (CoordinateY
>= AbsY
) {
3190 CoordinateY
-= AbsY
;
3196 // check whether new mouse column position is beyond screen
3197 // if not, adjust it
3199 if (CoordinateX
>= 1 && CoordinateX
<= MainEditor
.ScreenSize
.Column
) {
3200 FileBuffer
.MousePosition
.Column
= CoordinateX
;
3201 } else if (CoordinateX
< 1) {
3202 FileBuffer
.MousePosition
.Column
= 1;
3203 } else if (CoordinateX
> MainEditor
.ScreenSize
.Column
) {
3204 FileBuffer
.MousePosition
.Column
= MainEditor
.ScreenSize
.Column
;
3207 // check whether new mouse row position is beyond screen
3208 // if not, adjust it
3210 if (CoordinateY
>= 2 && CoordinateY
<= (MainEditor
.ScreenSize
.Row
- 1)) {
3211 FileBuffer
.MousePosition
.Row
= CoordinateY
;
3212 } else if (CoordinateY
< 2) {
3213 FileBuffer
.MousePosition
.Row
= 2;
3214 } else if (CoordinateY
> (MainEditor
.ScreenSize
.Row
- 1)) {
3215 FileBuffer
.MousePosition
.Row
= (MainEditor
.ScreenSize
.Row
- 1);
3221 Search and replace operation.
3223 @param[in] SearchStr The string to search for.
3224 @param[in] ReplaceStr The string to replace with.
3225 @param[in] Offset The column to start at.
3229 FileBufferReplaceAll (
3230 IN CHAR16
*SearchStr
,
3231 IN CHAR16
*ReplaceStr
,
3244 EFI_EDITOR_LINE
*Line
;
3248 SearchLen
= StrLen (SearchStr
);
3249 ReplaceLen
= StrLen (ReplaceStr
);
3251 Column
= FileBuffer
.FilePosition
.Column
+ Offset
- 1;
3253 if (Column
> FileBuffer
.CurrentLine
->Size
) {
3254 Column
= FileBuffer
.CurrentLine
->Size
;
3257 Link
= &(FileBuffer
.CurrentLine
->Link
);
3259 while (Link
!= FileBuffer
.ListHead
) {
3260 Line
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
3261 CharPos
= StrStr (Line
->Buffer
+ Column
, SearchStr
);
3262 if (CharPos
!= NULL
) {
3263 Position
= CharPos
- Line
->Buffer
;// + Column;
3267 if (ReplaceLen
> SearchLen
) {
3268 OldSize
= Line
->Size
+ 1;
3270 // include CHAR_NULL
3272 NewSize
= OldSize
+ (ReplaceLen
- SearchLen
);
3275 // do not have the enough space
3277 if (Line
->TotalSize
+ 1 <= NewSize
) {
3278 Line
->Buffer
= ReallocatePool (
3283 Line
->TotalSize
= NewSize
- 1;
3286 if (Line
->Buffer
== NULL
) {
3287 return EFI_OUT_OF_RESOURCES
;
3290 // the end CHAR_NULL character;
3292 Buffer
= Line
->Buffer
+ (NewSize
- 1);
3293 Gap
= ReplaceLen
- SearchLen
;
3296 // keep the latter part
3298 for (Index
= 0; Index
< (Line
->Size
- Position
- SearchLen
+ 1); Index
++) {
3299 *Buffer
= *(Buffer
- Gap
);
3303 } else if (ReplaceLen
< SearchLen
){
3304 Buffer
= Line
->Buffer
+ Position
+ ReplaceLen
;
3305 Gap
= SearchLen
- ReplaceLen
;
3307 for (Index
= 0; Index
< (Line
->Size
- Position
- ReplaceLen
+ 1); Index
++) {
3308 *Buffer
= *(Buffer
+ Gap
);
3312 ASSERT(ReplaceLen
== SearchLen
);
3315 // set replace into it
3317 Buffer
= Line
->Buffer
+ Position
;
3318 for (Index
= 0; Index
< ReplaceLen
; Index
++) {
3319 Buffer
[Index
] = ReplaceStr
[Index
];
3322 Line
->Size
+= (ReplaceLen
- SearchLen
);
3323 Column
+= ReplaceLen
;
3329 Link
= Link
->ForwardLink
;
3333 // call refresh to fresh edit area
3335 FileBuffer
.FileModified
= TRUE
;
3336 FileBufferNeedRefresh
= TRUE
;
3337 FileBufferRefresh ();
3343 Set the modified state to TRUE.
3347 FileBufferSetModified (
3351 FileBuffer
.FileModified
= TRUE
;