2 Implements filebuffer interface functions.
4 Copyright (c) 2005 - 2016, 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
;
304 New
.Colors
.Foreground
= Orig
.Colors
.Background
& 0xF;
305 New
.Colors
.Background
= Orig
.Colors
.Foreground
& 0x7;
308 // clear the old mouse position
310 FRow
= FileBuffer
.LowVisibleRange
.Row
+ FileBufferBackupVar
.MousePosition
.Row
- 2;
312 FColumn
= FileBuffer
.LowVisibleRange
.Column
+ FileBufferBackupVar
.MousePosition
.Column
- 1;
315 if (FRow
> FileBuffer
.NumLines
) {
316 HasCharacter
= FALSE
;
318 CurrentLine
= FileBuffer
.CurrentLine
;
319 Line
= MoveLine (FRow
- FileBuffer
.FilePosition
.Row
);
321 if (Line
== NULL
|| FColumn
> Line
->Size
) {
322 HasCharacter
= FALSE
;
325 FileBuffer
.CurrentLine
= CurrentLine
;
329 (INT32
)FileBufferBackupVar
.MousePosition
.Column
- 1,
330 (INT32
)FileBufferBackupVar
.MousePosition
.Row
- 1,
335 Value
= (Line
->Buffer
[FColumn
- 1]);
337 (INT32
)FileBufferBackupVar
.MousePosition
.Column
- 1,
338 (INT32
)FileBufferBackupVar
.MousePosition
.Row
- 1,
344 // set the new mouse position
346 gST
->ConOut
->SetAttribute (gST
->ConOut
, New
.Data
& 0x7F);
349 // clear the old mouse position
351 FRow
= FileBuffer
.LowVisibleRange
.Row
+ FileBuffer
.MousePosition
.Row
- 2;
352 FColumn
= FileBuffer
.LowVisibleRange
.Column
+ FileBuffer
.MousePosition
.Column
- 1;
355 if (FRow
> FileBuffer
.NumLines
) {
356 HasCharacter
= FALSE
;
358 CurrentLine
= FileBuffer
.CurrentLine
;
359 Line
= MoveLine (FRow
- FileBuffer
.FilePosition
.Row
);
361 if (Line
== NULL
|| FColumn
> Line
->Size
) {
362 HasCharacter
= FALSE
;
365 FileBuffer
.CurrentLine
= CurrentLine
;
369 (INT32
)FileBuffer
.MousePosition
.Column
- 1,
370 (INT32
)FileBuffer
.MousePosition
.Row
- 1,
375 Value
= Line
->Buffer
[FColumn
- 1];
377 (INT32
)FileBuffer
.MousePosition
.Column
- 1,
378 (INT32
)FileBuffer
.MousePosition
.Row
- 1,
384 // end of HasCharacter
386 gST
->ConOut
->SetAttribute (gST
->ConOut
, Orig
.Data
);
389 // end of MouseNeedRefresh
393 // end of MouseSupported
399 Free all the lines in FileBuffer
406 @retval EFI_SUCCESS The operation was successful.
410 FileBufferFreeLines (
415 EFI_EDITOR_LINE
*Line
;
418 // free all the lines
420 if (FileBuffer
.Lines
!= NULL
) {
422 Line
= FileBuffer
.Lines
;
423 Link
= &(Line
->Link
);
425 Line
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
426 Link
= Link
->ForwardLink
;
429 // free line's buffer and line itself
432 } while (Link
!= FileBuffer
.ListHead
);
435 // clean the line list related structure
437 FileBuffer
.Lines
= NULL
;
438 FileBuffer
.CurrentLine
= NULL
;
439 FileBuffer
.NumLines
= 0;
441 FileBuffer
.ListHead
->ForwardLink
= FileBuffer
.ListHead
;
442 FileBuffer
.ListHead
->BackLink
= FileBuffer
.ListHead
;
448 Cleanup function for FileBuffer.
450 @retval EFI_SUCCESS The cleanup was successful.
460 SHELL_FREE_NON_NULL (FileBuffer
.FileName
);
463 // free all the lines
465 Status
= FileBufferFreeLines ();
467 SHELL_FREE_NON_NULL (FileBuffer
.ListHead
);
468 FileBuffer
.ListHead
= NULL
;
470 SHELL_FREE_NON_NULL (FileBufferBackupVar
.FileName
);
476 Print a line specified by Line on a row specified by Row of the screen.
478 @param[in] Line The line to print.
479 @param[in] Row The row on the screen to print onto (begin from 1).
481 @retval EFI_SUCCESS The printing was successful.
484 FileBufferPrintLine (
485 IN CONST EFI_EDITOR_LINE
*Line
,
497 // print start from correct character
499 Buffer
= Line
->Buffer
+ FileBuffer
.LowVisibleRange
.Column
- 1;
501 Limit
= Line
->Size
- FileBuffer
.LowVisibleRange
.Column
+ 1;
502 if (Limit
> Line
->Size
) {
506 BufLen
= (MainEditor
.ScreenSize
.Column
+ 1) * sizeof (CHAR16
);
507 PrintLine
= AllocatePool (BufLen
);
508 if (PrintLine
!= NULL
) {
509 StrnCpyS (PrintLine
, BufLen
/sizeof(CHAR16
), Buffer
, MIN(Limit
, MainEditor
.ScreenSize
.Column
));
510 for (; Limit
< MainEditor
.ScreenSize
.Column
; Limit
++) {
511 PrintLine
[Limit
] = L
' ';
514 PrintLine
[MainEditor
.ScreenSize
.Column
] = CHAR_NULL
;
516 PrintLine2
= AllocatePool (BufLen
* 2);
517 if (PrintLine2
!= NULL
) {
518 ShellCopySearchAndReplace(PrintLine
, PrintLine2
, BufLen
* 2, L
"%", L
"^%", FALSE
, FALSE
);
526 FreePool (PrintLine2
);
528 FreePool (PrintLine
);
535 Set the cursor position according to FileBuffer.DisplayPosition.
537 @retval EFI_SUCCESS The operation was successful.
541 FileBufferRestorePosition (
546 // set cursor position
548 return (gST
->ConOut
->SetCursorPosition (
550 FileBuffer
.DisplayPosition
.Column
- 1,
551 FileBuffer
.DisplayPosition
.Row
- 1
556 Refresh the screen with whats in the buffer.
558 @retval EFI_SUCCESS The refresh was successful.
559 @retval EFI_LOAD_ERROR There was an error finding what to write.
568 EFI_EDITOR_LINE
*Line
;
572 // if it's the first time after editor launch, so should refresh
576 // no definite required refresh
577 // and file position displayed on screen has not been changed
579 if (!FileBufferNeedRefresh
&&
580 !FileBufferOnlyLineNeedRefresh
&&
581 FileBufferBackupVar
.LowVisibleRange
.Row
== FileBuffer
.LowVisibleRange
.Row
&&
582 FileBufferBackupVar
.LowVisibleRange
.Column
== FileBuffer
.LowVisibleRange
.Column
585 FileBufferRestoreMousePosition ();
586 FileBufferRestorePosition ();
592 gST
->ConOut
->EnableCursor (gST
->ConOut
, FALSE
);
595 // only need to refresh current line
597 if (FileBufferOnlyLineNeedRefresh
&&
598 FileBufferBackupVar
.LowVisibleRange
.Row
== FileBuffer
.LowVisibleRange
.Row
&&
599 FileBufferBackupVar
.LowVisibleRange
.Column
== FileBuffer
.LowVisibleRange
.Column
602 EditorClearLine (FileBuffer
.DisplayPosition
.Row
, MainEditor
.ScreenSize
.Column
, MainEditor
.ScreenSize
.Row
);
603 FileBufferPrintLine (
604 FileBuffer
.CurrentLine
,
605 FileBuffer
.DisplayPosition
.Row
609 // the whole edit area need refresh
615 if (FileBuffer
.Lines
== NULL
) {
616 FileBufferRestoreMousePosition ();
617 FileBufferRestorePosition ();
618 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
623 // get the first line that will be displayed
625 Line
= MoveLine (FileBuffer
.LowVisibleRange
.Row
- FileBuffer
.FilePosition
.Row
);
627 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
629 return EFI_LOAD_ERROR
;
632 Link
= &(Line
->Link
);
635 Line
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
640 FileBufferPrintLine (Line
, Row
);
642 Link
= Link
->ForwardLink
;
644 } while (Link
!= FileBuffer
.ListHead
&& Row
<= (MainEditor
.ScreenSize
.Row
- 1));
646 // while not file end and not screen full
648 while (Row
<= (MainEditor
.ScreenSize
.Row
- 1)) {
649 EditorClearLine (Row
, MainEditor
.ScreenSize
.Column
, MainEditor
.ScreenSize
.Row
);
654 FileBufferRestoreMousePosition ();
655 FileBufferRestorePosition ();
657 FileBufferNeedRefresh
= FALSE
;
658 FileBufferOnlyLineNeedRefresh
= FALSE
;
660 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
665 Create a new line and append it to the line list.
670 @retval NULL The create line failed.
671 @return The line created.
675 FileBufferCreateLine (
679 EFI_EDITOR_LINE
*Line
;
682 // allocate a line structure
684 Line
= AllocateZeroPool (sizeof (EFI_EDITOR_LINE
));
689 // initialize the structure
691 Line
->Signature
= LINE_LIST_SIGNATURE
;
694 Line
->Type
= NewLineTypeDefault
;
697 // initial buffer of the line is "\0"
699 ASSERT(CHAR_NULL
== CHAR_NULL
);
700 Line
->Buffer
= CatSPrint (NULL
, L
"\0");
701 if (Line
->Buffer
== NULL
) {
705 FileBuffer
.NumLines
++;
708 // insert the line into line list
710 InsertTailList (FileBuffer
.ListHead
, &Line
->Link
);
712 if (FileBuffer
.Lines
== NULL
) {
713 FileBuffer
.Lines
= CR (FileBuffer
.ListHead
->ForwardLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
720 Set FileName field in FileBuffer.
722 @param Str The file name to set.
724 @retval EFI_SUCCESS The filename was successfully set.
725 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
726 @retval EFI_INVALID_PARAMETER Str is not a valid filename.
730 FileBufferSetFileName (
735 // Verify the parameters
737 if (!IsValidFileName(Str
)) {
738 return (EFI_INVALID_PARAMETER
);
741 // free the old file name
743 SHELL_FREE_NON_NULL (FileBuffer
.FileName
);
746 // Allocate and set the new name
748 FileBuffer
.FileName
= CatSPrint (NULL
, L
"%s", Str
);
749 if (FileBuffer
.FileName
== NULL
) {
750 return EFI_OUT_OF_RESOURCES
;
756 Free the existing file lines and reset the modified flag.
758 @retval EFI_SUCCESS The operation was successful.
767 // free all the lines
769 FileBufferFreeLines ();
770 FileBuffer
.FileModified
= FALSE
;
777 Read a file from disk into the FileBuffer.
779 @param[in] FileName The filename to read.
780 @param[in] Recover TRUE if is for recover mode, no information printouts.
782 @retval EFI_SUCCESS The load was successful.
783 @retval EFI_LOAD_ERROR The load failed.
784 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
785 @retval EFI_INVALID_PARAMETER FileName is a directory.
790 IN CONST CHAR16
*FileName
,
791 IN CONST BOOLEAN Recover
794 EFI_EDITOR_LINE
*Line
;
795 EE_NEWLINE_TYPE Type
;
800 CHAR16
*UnicodeBuffer
;
803 SHELL_FILE_HANDLE FileHandle
;
806 UINTN LineSizeBackup
;
812 UnicodeBuffer
= NULL
;
813 Type
= NewLineTypeDefault
;
818 // in this function, when you return error ( except EFI_OUT_OF_RESOURCES )
819 // you should set status string via StatusBarSetStatusString(L"blah")
820 // since this function maybe called before the editorhandleinput loop
821 // so any error will cause editor return
822 // so if you want to print the error status
823 // you should set the status string
827 // try to open the file
829 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
831 if (!EFI_ERROR(Status
)) {
833 if (FileHandle
== NULL
) {
834 StatusBarSetStatusString (L
"Disk Error");
835 return EFI_LOAD_ERROR
;
838 Info
= ShellGetFileInfo(FileHandle
);
840 if (Info
->Attribute
& EFI_FILE_DIRECTORY
) {
841 StatusBarSetStatusString (L
"Directory Can Not Be Edited");
843 return EFI_INVALID_PARAMETER
;
846 if (Info
->Attribute
& EFI_FILE_READ_ONLY
) {
847 FileBuffer
.ReadOnly
= TRUE
;
849 FileBuffer
.ReadOnly
= FALSE
;
854 FileSize
= (UINTN
) Info
->FileSize
;
857 } else if (Status
== EFI_NOT_FOUND
) {
859 // file not exists. add create and try again
861 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, 0);
862 if (EFI_ERROR (Status
)) {
863 if (Status
== EFI_WRITE_PROTECTED
||
864 Status
== EFI_ACCESS_DENIED
||
865 Status
== EFI_NO_MEDIA
||
866 Status
== EFI_MEDIA_CHANGED
868 StatusBarSetStatusString (L
"Access Denied");
869 } else if (Status
== EFI_DEVICE_ERROR
|| Status
== EFI_VOLUME_CORRUPTED
|| Status
== EFI_VOLUME_FULL
) {
870 StatusBarSetStatusString (L
"Disk Error");
872 StatusBarSetStatusString (L
"Invalid File Name or Current-working-directory");
878 // it worked. now delete it and move on with the name (now validated)
880 Status
= ShellDeleteFile (&FileHandle
);
881 if (Status
== EFI_WARN_DELETE_FAILURE
) {
882 Status
= EFI_ACCESS_DENIED
;
885 if (EFI_ERROR (Status
)) {
886 StatusBarSetStatusString (L
"Access Denied");
891 // file doesn't exist, so set CreateFile to TRUE
894 FileBuffer
.ReadOnly
= FALSE
;
897 // all the check ends
898 // so now begin to set file name, free lines
900 if (StrCmp (FileName
, FileBuffer
.FileName
) != 0) {
901 FileBufferSetFileName (FileName
);
904 // free the old lines
914 // allocate buffer to read file
916 Buffer
= AllocateZeroPool (FileSize
);
917 if (Buffer
== NULL
) {
918 return EFI_OUT_OF_RESOURCES
;
921 // read file into Buffer
923 Status
= ShellReadFile (FileHandle
, &FileSize
, Buffer
);
924 ShellCloseFile(&FileHandle
);
926 if (EFI_ERROR (Status
)) {
927 StatusBarSetStatusString (L
"Read File Failed");
928 SHELL_FREE_NON_NULL (Buffer
);
929 return EFI_LOAD_ERROR
;
932 // nothing in this file
935 SHELL_FREE_NON_NULL (Buffer
);
937 // since has no head, so only can be an ASCII file
939 FileBuffer
.FileType
= FileTypeAscii
;
944 AsciiBuffer
= Buffer
;
948 // size < Unicode file header, so only can be ASCII file
950 FileBuffer
.FileType
= FileTypeAscii
;
955 if (*(UINT16
*) Buffer
== EFI_UNICODE_BYTE_ORDER_MARK
) {
957 // Unicode file's size should be even
959 if ((FileSize
% 2) != 0) {
960 StatusBarSetStatusString (L
"File Format Wrong");
961 SHELL_FREE_NON_NULL (Buffer
);
962 return EFI_LOAD_ERROR
;
967 FileBuffer
.FileType
= FileTypeUnicode
;
968 UnicodeBuffer
= Buffer
;
971 // pass this 0xff and 0xfe
976 FileBuffer
.FileType
= FileTypeAscii
;
979 // end of AsciiBuffer ==
983 // end of FileSize < 2
984 // all the check ends
985 // so now begin to set file name, free lines
987 if (StrCmp (FileName
, FileBuffer
.FileName
) != 0) {
988 FileBufferSetFileName (FileName
);
992 // free the old lines
997 // parse file content line by line
999 for (LoopVar1
= 0; LoopVar1
< FileSize
; LoopVar1
++) {
1000 Type
= NewLineTypeUnknown
;
1002 for (LineSize
= LoopVar1
; LineSize
< FileSize
; LineSize
++) {
1003 if (FileBuffer
.FileType
== FileTypeAscii
) {
1004 if (AsciiBuffer
[LineSize
] == CHAR_CARRIAGE_RETURN
) {
1005 Type
= NewLineTypeCarriageReturn
;
1010 if (LineSize
< FileSize
- 1) {
1011 if (AsciiBuffer
[LineSize
+ 1] == CHAR_LINEFEED
) {
1012 Type
= NewLineTypeCarriageReturnLineFeed
;
1017 } else if (AsciiBuffer
[LineSize
] == CHAR_LINEFEED
) {
1018 Type
= NewLineTypeLineFeed
;
1023 if (LineSize
< FileSize
- 1) {
1024 if (AsciiBuffer
[LineSize
+ 1] == CHAR_CARRIAGE_RETURN
) {
1025 Type
= NewLineTypeLineFeedCarriageReturn
;
1032 if (UnicodeBuffer
[LineSize
] == CHAR_CARRIAGE_RETURN
) {
1033 Type
= NewLineTypeCarriageReturn
;
1038 if (LineSize
< FileSize
- 1) {
1039 if (UnicodeBuffer
[LineSize
+ 1] == CHAR_LINEFEED
) {
1040 Type
= NewLineTypeCarriageReturnLineFeed
;
1045 } else if (UnicodeBuffer
[LineSize
] == CHAR_LINEFEED
) {
1046 Type
= NewLineTypeLineFeed
;
1051 if (LineSize
< FileSize
- 1) {
1052 if (UnicodeBuffer
[LineSize
+ 1] == CHAR_CARRIAGE_RETURN
) {
1053 Type
= NewLineTypeLineFeedCarriageReturn
;
1065 // end of for LineSize
1067 // if the type is wrong, then exit
1069 if (Type
== NewLineTypeUnknown
) {
1071 // Now if Type is NewLineTypeUnknown, it should be file end
1073 Type
= NewLineTypeDefault
;
1076 LineSizeBackup
= LineSize
;
1079 // create a new line
1081 Line
= FileBufferCreateLine ();
1083 SHELL_FREE_NON_NULL (Buffer
);
1084 return EFI_OUT_OF_RESOURCES
;
1087 // calculate file length
1089 LineSize
-= LoopVar1
;
1092 // Unicode and one CHAR_NULL
1094 SHELL_FREE_NON_NULL (Line
->Buffer
);
1095 Line
->Buffer
= AllocateZeroPool (LineSize
* 2 + 2);
1097 if (Line
->Buffer
== NULL
) {
1098 RemoveEntryList (&Line
->Link
);
1099 return EFI_OUT_OF_RESOURCES
;
1102 // copy this line to Line->Buffer
1104 for (LoopVar2
= 0; LoopVar2
< LineSize
; LoopVar2
++) {
1105 if (FileBuffer
.FileType
== FileTypeAscii
) {
1106 Line
->Buffer
[LoopVar2
] = (CHAR16
) AsciiBuffer
[LoopVar1
];
1108 Line
->Buffer
[LoopVar2
] = UnicodeBuffer
[LoopVar1
];
1114 // LoopVar1 now points to where CHAR_CARRIAGE_RETURN or CHAR_LINEFEED;
1116 Line
->Buffer
[LineSize
] = 0;
1118 Line
->Size
= LineSize
;
1119 Line
->TotalSize
= LineSize
;
1122 if (Type
== NewLineTypeCarriageReturnLineFeed
|| Type
== NewLineTypeLineFeedCarriageReturn
) {
1127 // last character is a return, SO create a new line
1129 if (((Type
== NewLineTypeCarriageReturnLineFeed
|| Type
== NewLineTypeLineFeedCarriageReturn
) && LineSizeBackup
== FileSize
- 2) ||
1130 ((Type
== NewLineTypeLineFeed
|| Type
== NewLineTypeCarriageReturn
) && LineSizeBackup
== FileSize
- 1)
1132 Line
= FileBufferCreateLine ();
1134 SHELL_FREE_NON_NULL (Buffer
);
1135 return EFI_OUT_OF_RESOURCES
;
1145 SHELL_FREE_NON_NULL (Buffer
);
1149 // end of if CreateFile
1153 FileBuffer
.DisplayPosition
.Row
= 2;
1154 FileBuffer
.DisplayPosition
.Column
= 1;
1155 FileBuffer
.LowVisibleRange
.Row
= 1;
1156 FileBuffer
.LowVisibleRange
.Column
= 1;
1157 FileBuffer
.FilePosition
.Row
= 1;
1158 FileBuffer
.FilePosition
.Column
= 1;
1159 FileBuffer
.MousePosition
.Row
= 2;
1160 FileBuffer
.MousePosition
.Column
= 1;
1163 UnicodeBuffer
= CatSPrint (NULL
, L
"%d Lines Read", FileBuffer
.NumLines
);
1164 if (UnicodeBuffer
== NULL
) {
1165 return EFI_OUT_OF_RESOURCES
;
1168 StatusBarSetStatusString (UnicodeBuffer
);
1169 FreePool (UnicodeBuffer
);
1173 // check whether we have fs?: in filename
1176 FSMappingPtr = NULL;
1177 while (FileName[LoopVar1] != 0) {
1178 if (FileName[LoopVar1] == L':') {
1179 FSMappingPtr = &FileName[LoopVar1];
1186 if (FSMappingPtr == NULL) {
1187 CurDir = ShellGetCurrentDir (NULL);
1191 while (FileName[LoopVar1] != 0) {
1192 if (FileName[LoopVar1] == L':') {
1196 FSMapping[LoopVar2++] = FileName[LoopVar1];
1201 FSMapping[LoopVar2] = 0;
1202 CurDir = ShellGetCurrentDir (FSMapping);
1205 if (CurDir != NULL) {
1206 for (LoopVar1 = 0; LoopVar1 < StrLen (CurDir) && CurDir[LoopVar1] != ':'; LoopVar1++);
1208 CurDir[LoopVar1] = 0;
1209 DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) ShellGetMap (CurDir);
1212 return EFI_LOAD_ERROR;
1215 Status = LibDevicePathToInterface (
1216 &gEfiSimpleFileSystemProtocolGuid,
1220 if (EFI_ERROR (Status)) {
1221 return EFI_LOAD_ERROR;
1224 Status = Vol->OpenVolume (Vol, &RootFs);
1225 if (EFI_ERROR (Status)) {
1226 return EFI_LOAD_ERROR;
1229 // Get volume information of file system
1231 Size = SIZE_OF_EFI_FILE_SYSTEM_INFO + 100;
1232 VolumeInfo = (EFI_FILE_SYSTEM_INFO *) AllocateZeroPool (Size);
1233 Status = RootFs->GetInfo (RootFs, &gEfiFileSystemInfoGuid, &Size, VolumeInfo);
1234 if (EFI_ERROR (Status)) {
1235 RootFs->Close (RootFs);
1236 return EFI_LOAD_ERROR;
1239 if (VolumeInfo->ReadOnly) {
1240 StatusBarSetStatusString (L"WARNING: Volume Read Only");
1243 FreePool (VolumeInfo);
1244 RootFs->Close (RootFs);
1251 if (FileBuffer
.Lines
!= 0) {
1252 FileBuffer
.CurrentLine
= CR (FileBuffer
.ListHead
->ForwardLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
1255 // create a dummy line
1257 Line
= FileBufferCreateLine ();
1259 return EFI_OUT_OF_RESOURCES
;
1262 FileBuffer
.CurrentLine
= Line
;
1265 FileBuffer
.FileModified
= FALSE
;
1266 FileBufferNeedRefresh
= TRUE
;
1267 FileBufferOnlyLineNeedRefresh
= FALSE
;
1268 FileBufferMouseNeedRefresh
= TRUE
;
1275 According to FileBuffer.NewLineType & FileBuffer.FileType,
1276 get the return buffer and size.
1278 @param[in] Type The type of line.
1279 @param[out] Buffer The buffer to fill.
1280 @param[out] Size The amount of the buffer used on return.
1285 IN CONST EE_NEWLINE_TYPE Type
,
1293 // give new line buffer,
1294 // and will judge unicode or ascii
1299 // not legal new line type
1301 if (Type
!= NewLineTypeLineFeed
&& Type
!= NewLineTypeCarriageReturn
&& Type
!= NewLineTypeCarriageReturnLineFeed
&& Type
!= NewLineTypeLineFeedCarriageReturn
) {
1306 // use_cr: give 0x0d
1308 if (Type
== NewLineTypeCarriageReturn
) {
1309 if (MainEditor
.FileBuffer
->FileType
== FileTypeUnicode
) {
1318 *Size
= NewLineSize
;
1322 // use_lf: give 0x0a
1324 if (Type
== NewLineTypeLineFeed
) {
1325 if (MainEditor
.FileBuffer
->FileType
== FileTypeUnicode
) {
1334 *Size
= NewLineSize
;
1338 // use_crlf: give 0x0d 0x0a
1340 if (Type
== NewLineTypeCarriageReturnLineFeed
) {
1341 if (MainEditor
.FileBuffer
->FileType
== FileTypeUnicode
) {
1354 *Size
= NewLineSize
;
1358 // use_lfcr: give 0x0a 0x0d
1360 if (Type
== NewLineTypeLineFeedCarriageReturn
) {
1361 if (MainEditor
.FileBuffer
->FileType
== FileTypeUnicode
) {
1374 *Size
= NewLineSize
;
1381 Change a Unicode string to an ASCII string.
1383 @param[in] UStr The Unicode string.
1384 @param[in] Length The maximum size of AStr.
1385 @param[out] AStr ASCII string to pass out.
1387 @return The actuall length.
1392 IN CONST CHAR16
*UStr
,
1393 IN CONST UINTN Length
,
1400 // just buffer copy, not character copy
1402 for (Index
= 0; Index
< Length
; Index
++) {
1403 *AStr
++ = (CHAR8
) *UStr
++;
1410 Save lines in FileBuffer to disk
1412 @param[in] FileName The file name for writing.
1414 @retval EFI_SUCCESS Data was written.
1415 @retval EFI_LOAD_ERROR
1416 @retval EFI_OUT_OF_RESOURCES There were not enough resources to write the file.
1421 IN CONST CHAR16
*FileName
1424 SHELL_FILE_HANDLE FileHandle
;
1426 EFI_EDITOR_LINE
*Line
;
1432 CHAR8 NewLineBuffer
[4];
1435 EFI_FILE_INFO
*Info
;
1439 EE_NEWLINE_TYPE Type
;
1454 TotalSize
= 0x200000;
1461 // if is the old file
1463 if (FileBuffer
.FileName
!= NULL
&& StrCmp (FileName
, FileBuffer
.FileName
) == 0) {
1465 // file has not been modified
1467 if (!FileBuffer
.FileModified
) {
1472 // if file is read-only, set error
1474 if (FileBuffer
.ReadOnly
) {
1475 StatusBarSetStatusString (L
"Read Only File Can Not Be Saved");
1480 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
, 0);
1482 if (!EFI_ERROR (Status
)) {
1483 Info
= ShellGetFileInfo(FileHandle
);
1485 if (Info
!= NULL
&& Info
->Attribute
& EFI_FILE_DIRECTORY
) {
1486 StatusBarSetStatusString (L
"Directory Can Not Be Saved");
1487 ShellCloseFile(FileHandle
);
1489 return EFI_LOAD_ERROR
;
1493 Attribute
= Info
->Attribute
& ~EFI_FILE_READ_ONLY
;
1498 // if file exits, so delete it
1500 Status
= ShellDeleteFile (&FileHandle
);
1501 if (EFI_ERROR (Status
) || Status
== EFI_WARN_DELETE_FAILURE
) {
1502 StatusBarSetStatusString (L
"Write File Failed");
1503 return EFI_LOAD_ERROR
;
1507 Status
= ShellOpenFileByName (FileName
, &FileHandle
, EFI_FILE_MODE_READ
|EFI_FILE_MODE_WRITE
|EFI_FILE_MODE_CREATE
, Attribute
);
1509 if (EFI_ERROR (Status
)) {
1510 StatusBarSetStatusString (L
"Create File Failed");
1511 return EFI_LOAD_ERROR
;
1515 // if file is Unicode file, write Unicode header to it.
1517 if (FileBuffer
.FileType
== FileTypeUnicode
) {
1519 Status
= ShellWriteFile (FileHandle
, &Length
, (VOID
*)&gUnicodeFileTag
);
1520 if (EFI_ERROR (Status
)) {
1521 ShellDeleteFile (&FileHandle
);
1522 return EFI_LOAD_ERROR
;
1526 Cache
= AllocateZeroPool (TotalSize
);
1527 if (Cache
== NULL
) {
1528 ShellDeleteFile (&FileHandle
);
1529 return EFI_OUT_OF_RESOURCES
;
1533 // write all the lines back to disk
1536 Type
= NewLineTypeCarriageReturnLineFeed
;
1539 LeftSize
= TotalSize
;
1541 for (Link
= FileBuffer
.ListHead
->ForwardLink
; Link
!= FileBuffer
.ListHead
; Link
= Link
->ForwardLink
) {
1542 Line
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
1544 if (Line
->Type
!= NewLineTypeDefault
) {
1548 // newline character is at most 4 bytes ( two Unicode characters )
1551 if (Line
->Buffer
!= NULL
&& Line
->Size
!= 0) {
1552 if (FileBuffer
.FileType
== FileTypeAscii
) {
1553 Length
+= Line
->Size
;
1555 Length
+= (Line
->Size
* 2);
1558 // end if FileTypeAscii
1563 // no cache room left, so write cache to disk
1565 if (LeftSize
< Length
) {
1566 Size
= TotalSize
- LeftSize
;
1567 Status
= ShellWriteFile (FileHandle
, &Size
, Cache
);
1568 if (EFI_ERROR (Status
)) {
1569 ShellDeleteFile (&FileHandle
);
1571 return EFI_LOAD_ERROR
;
1574 LeftSize
= TotalSize
;
1577 if (Line
->Buffer
!= NULL
&& Line
->Size
!= 0) {
1578 if (FileBuffer
.FileType
== FileTypeAscii
) {
1579 UnicodeToAscii (Line
->Buffer
, Line
->Size
, Ptr
);
1580 Length
= Line
->Size
;
1582 Length
= (Line
->Size
* 2);
1583 CopyMem (Ptr
, (CHAR8
*) Line
->Buffer
, Length
);
1586 // end if FileTypeAscii
1593 // end of if Line -> Buffer != NULL && Line -> Size != 0
1595 // if not the last line , write return buffer to disk
1597 if (Link
->ForwardLink
!= FileBuffer
.ListHead
) {
1598 GetNewLine (Type
, NewLineBuffer
, &NewLineSize
);
1599 CopyMem (Ptr
, (CHAR8
*) NewLineBuffer
, NewLineSize
);
1602 LeftSize
-= NewLineSize
;
1608 if (TotalSize
!= LeftSize
) {
1609 Size
= TotalSize
- LeftSize
;
1610 Status
= ShellWriteFile (FileHandle
, &Size
, Cache
);
1611 if (EFI_ERROR (Status
)) {
1612 ShellDeleteFile (&FileHandle
);
1614 return EFI_LOAD_ERROR
;
1620 ShellCloseFile(&FileHandle
);
1622 FileBuffer
.FileModified
= FALSE
;
1625 // set status string
1627 Str
= CatSPrint (NULL
, L
"%d Lines Wrote", NumLines
);
1629 return EFI_OUT_OF_RESOURCES
;
1632 StatusBarSetStatusString (Str
);
1633 SHELL_FREE_NON_NULL (Str
);
1636 // now everything is ready , you can set the new file name to filebuffer
1638 if (FileName
!= NULL
&& FileBuffer
.FileName
!= NULL
&& StrCmp (FileName
, FileBuffer
.FileName
) != 0) {
1642 FileBufferSetFileName (FileName
);
1643 if (FileBuffer
.FileName
== NULL
) {
1644 ShellDeleteFile (&FileHandle
);
1645 return EFI_OUT_OF_RESOURCES
;
1649 FileBuffer
.ReadOnly
= FALSE
;
1654 Scroll cursor to left 1 character position.
1656 @retval EFI_SUCCESS The operation was successful.
1660 FileBufferScrollLeft (
1664 EFI_EDITOR_LINE
*Line
;
1668 Line
= FileBuffer
.CurrentLine
;
1670 FRow
= FileBuffer
.FilePosition
.Row
;
1671 FCol
= FileBuffer
.FilePosition
.Column
;
1674 // if already at start of this line, so move to the end of previous line
1678 // has previous line
1680 if (Line
->Link
.BackLink
!= FileBuffer
.ListHead
) {
1682 Line
= CR (Line
->Link
.BackLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
1683 FCol
= Line
->Size
+ 1;
1689 // if not at start of this line, just move to previous column
1694 FileBufferMovePosition (FRow
, FCol
);
1700 Delete a char in line
1702 @param[in, out] Line The line to delete in.
1703 @param[in] Pos Position to delete the char at ( start from 0 ).
1708 IN OUT EFI_EDITOR_LINE
*Line
,
1715 // move the latter characters front
1717 for (Index
= Pos
- 1; Index
< Line
->Size
; Index
++) {
1718 Line
->Buffer
[Index
] = Line
->Buffer
[Index
+ 1];
1725 Concatenate Src into Dest.
1727 @param[in, out] Dest Destination string
1728 @param[in] Src Src String.
1733 IN OUT EFI_EDITOR_LINE
*Dest
,
1734 IN EFI_EDITOR_LINE
*Src
1742 Dest
->Buffer
[Size
] = 0;
1745 // concatenate the two strings
1747 Str
= CatSPrint (NULL
, L
"%s%s", Dest
->Buffer
, Src
->Buffer
);
1749 Dest
->Buffer
= NULL
;
1753 Dest
->Size
= Size
+ Src
->Size
;
1754 Dest
->TotalSize
= Dest
->Size
;
1756 FreePool (Dest
->Buffer
);
1757 FreePool (Src
->Buffer
);
1760 // put str to dest->buffer
1766 Delete the previous character.
1768 @retval EFI_SUCCESS The delete was successful.
1769 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1773 FileBufferDoBackspace (
1777 EFI_EDITOR_LINE
*Line
;
1778 EFI_EDITOR_LINE
*End
;
1782 FileColumn
= FileBuffer
.FilePosition
.Column
;
1784 Line
= FileBuffer
.CurrentLine
;
1789 if (FileColumn
== 1) {
1793 if (FileBuffer
.FilePosition
.Row
== 1) {
1797 FileBufferScrollLeft ();
1799 Line
= FileBuffer
.CurrentLine
;
1800 Link
= Line
->Link
.ForwardLink
;
1801 End
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
1804 // concatenate this line with previous line
1806 LineCat (Line
, End
);
1807 if (Line
->Buffer
== NULL
) {
1808 return EFI_OUT_OF_RESOURCES
;
1811 // remove End from line list
1813 RemoveEntryList (&End
->Link
);
1816 FileBuffer
.NumLines
--;
1818 FileBufferNeedRefresh
= TRUE
;
1819 FileBufferOnlyLineNeedRefresh
= FALSE
;
1823 // just delete the previous character
1825 LineDeleteAt (Line
, FileColumn
- 1);
1826 FileBufferScrollLeft ();
1827 FileBufferOnlyLineNeedRefresh
= TRUE
;
1830 if (!FileBuffer
.FileModified
) {
1831 FileBuffer
.FileModified
= TRUE
;
1838 Add a return into line at current position.
1840 @retval EFI_SUCCESS The insetrion of the character was successful.
1841 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1845 FileBufferDoReturn (
1849 EFI_EDITOR_LINE
*Line
;
1850 EFI_EDITOR_LINE
*NewLine
;
1857 FileBufferNeedRefresh
= TRUE
;
1858 FileBufferOnlyLineNeedRefresh
= FALSE
;
1860 Line
= FileBuffer
.CurrentLine
;
1862 FileColumn
= FileBuffer
.FilePosition
.Column
;
1864 NewLine
= AllocateZeroPool (sizeof (EFI_EDITOR_LINE
));
1865 if (NewLine
== NULL
) {
1866 return EFI_OUT_OF_RESOURCES
;
1869 NewLine
->Signature
= LINE_LIST_SIGNATURE
;
1870 NewLine
->Size
= Line
->Size
- FileColumn
+ 1;
1871 NewLine
->TotalSize
= NewLine
->Size
;
1872 NewLine
->Buffer
= CatSPrint (NULL
, L
"\0");
1873 if (NewLine
->Buffer
== NULL
) {
1874 return EFI_OUT_OF_RESOURCES
;
1877 NewLine
->Type
= NewLineTypeDefault
;
1879 if (NewLine
->Size
> 0) {
1881 // UNICODE + CHAR_NULL
1883 Buffer
= AllocateZeroPool (2 * (NewLine
->Size
+ 1));
1884 if (Buffer
== NULL
) {
1885 FreePool (NewLine
->Buffer
);
1887 return EFI_OUT_OF_RESOURCES
;
1890 FreePool (NewLine
->Buffer
);
1892 NewLine
->Buffer
= Buffer
;
1894 for (Index
= 0; Index
< NewLine
->Size
; Index
++) {
1895 NewLine
->Buffer
[Index
] = Line
->Buffer
[Index
+ FileColumn
- 1];
1898 NewLine
->Buffer
[NewLine
->Size
] = CHAR_NULL
;
1900 Line
->Buffer
[FileColumn
- 1] = CHAR_NULL
;
1901 Line
->Size
= FileColumn
- 1;
1904 // increase NumLines
1906 FileBuffer
.NumLines
++;
1909 // insert it into the correct position of line list
1911 NewLine
->Link
.BackLink
= &(Line
->Link
);
1912 NewLine
->Link
.ForwardLink
= Line
->Link
.ForwardLink
;
1913 Line
->Link
.ForwardLink
->BackLink
= &(NewLine
->Link
);
1914 Line
->Link
.ForwardLink
= &(NewLine
->Link
);
1917 // move cursor to the start of next line
1919 Row
= FileBuffer
.FilePosition
.Row
+ 1;
1922 FileBufferMovePosition (Row
, Col
);
1925 // set file is modified
1927 if (!FileBuffer
.FileModified
) {
1928 FileBuffer
.FileModified
= TRUE
;
1935 Delete current character from current line. This is the effect caused
1942 FileBufferDoDelete (
1946 EFI_EDITOR_LINE
*Line
;
1947 EFI_EDITOR_LINE
*Next
;
1951 Line
= FileBuffer
.CurrentLine
;
1952 FileColumn
= FileBuffer
.FilePosition
.Column
;
1957 if (FileColumn
>= Line
->Size
+ 1) {
1961 if (Line
->Link
.ForwardLink
== FileBuffer
.ListHead
) {
1965 // since last character,
1966 // so will add the next line to this line
1968 Link
= Line
->Link
.ForwardLink
;
1969 Next
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
1970 LineCat (Line
, Next
);
1971 if (Line
->Buffer
== NULL
) {
1972 return EFI_OUT_OF_RESOURCES
;
1975 RemoveEntryList (&Next
->Link
);
1978 FileBuffer
.NumLines
--;
1980 FileBufferNeedRefresh
= TRUE
;
1981 FileBufferOnlyLineNeedRefresh
= FALSE
;
1985 // just delete current character
1987 LineDeleteAt (Line
, FileColumn
);
1988 FileBufferOnlyLineNeedRefresh
= TRUE
;
1991 if (!FileBuffer
.FileModified
) {
1992 FileBuffer
.FileModified
= TRUE
;
1999 Scroll cursor to right 1 character.
2001 @retval EFI_SUCCESS The operation was successful.
2005 FileBufferScrollRight (
2009 EFI_EDITOR_LINE
*Line
;
2013 Line
= FileBuffer
.CurrentLine
;
2014 if (Line
->Buffer
== NULL
) {
2018 FRow
= FileBuffer
.FilePosition
.Row
;
2019 FCol
= FileBuffer
.FilePosition
.Column
;
2022 // if already at end of this line, scroll it to the start of next line
2024 if (FCol
> Line
->Size
) {
2028 if (Line
->Link
.ForwardLink
!= FileBuffer
.ListHead
) {
2036 // if not at end of this line, just move to next column
2041 FileBufferMovePosition (FRow
, FCol
);
2047 Insert a char into line
2050 @param[in] Line The line to insert into.
2051 @param[in] Char The char to insert.
2052 @param[in] Pos The position to insert the char at ( start from 0 ).
2053 @param[in] StrSize The current string size ( include CHAR_NULL ),unit is Unicode character.
2055 @return The new string size ( include CHAR_NULL ) ( unit is Unicode character ).
2060 IN EFI_EDITOR_LINE
*Line
,
2067 CHAR16
*TempStringPtr
;
2070 Index
= (StrSize
) * 2;
2075 // do not have free space
2077 if (Line
->TotalSize
<= Line
->Size
) {
2078 Str
= ReallocatePool (Index
, Index
+ 16, Str
);
2083 Line
->TotalSize
+= 8;
2086 // move the later part of the string one character right
2088 TempStringPtr
= Str
;
2089 for (Index
= StrSize
; Index
> Pos
; Index
--) {
2090 TempStringPtr
[Index
] = TempStringPtr
[Index
- 1];
2093 // insert char into it.
2095 TempStringPtr
[Index
] = Char
;
2104 Add a character to the current line.
2106 @param[in] Char The Character to input.
2108 @retval EFI_SUCCESS The input was succesful.
2116 EFI_EDITOR_LINE
*Line
;
2119 Line
= FileBuffer
.CurrentLine
;
2122 // only needs to refresh current line
2124 FileBufferOnlyLineNeedRefresh
= TRUE
;
2127 // when is insert mode, or cursor is at end of this line,
2128 // so insert this character
2129 // or replace the character.
2131 FilePos
= FileBuffer
.FilePosition
.Column
- 1;
2132 if (FileBuffer
.ModeInsert
|| FilePos
+ 1 > Line
->Size
) {
2133 LineStrInsert (Line
, Char
, FilePos
, Line
->Size
+ 1);
2135 Line
->Buffer
[FilePos
] = Char
;
2138 // move cursor to right
2140 FileBufferScrollRight ();
2142 if (!FileBuffer
.FileModified
) {
2143 FileBuffer
.FileModified
= TRUE
;
2150 Handles inputs from characters (ASCII key + Backspace + return)
2152 @param[in] Char The input character.
2154 @retval EFI_SUCCESS The operation was successful.
2155 @retval EFI_LOAD_ERROR There was an error.
2156 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2160 FileBufferDoCharInput (
2161 IN CONST CHAR16 Char
2166 Status
= EFI_SUCCESS
;
2172 case CHAR_BACKSPACE
:
2173 Status
= FileBufferDoBackspace ();
2183 case CHAR_CARRIAGE_RETURN
:
2184 Status
= FileBufferDoReturn ();
2189 // DEAL WITH ASCII CHAR, filter out thing like ctrl+f
2191 if (Char
> 127 || Char
< 32) {
2192 Status
= StatusBarSetStatusString (L
"Unknown Command");
2194 Status
= FileBufferAddChar (Char
);
2205 Scroll cursor to the next line.
2207 @retval EFI_SUCCESS The operation was successful.
2211 FileBufferScrollDown (
2215 EFI_EDITOR_LINE
*Line
;
2219 Line
= FileBuffer
.CurrentLine
;
2220 if (Line
->Buffer
== NULL
) {
2224 FRow
= FileBuffer
.FilePosition
.Row
;
2225 FCol
= FileBuffer
.FilePosition
.Column
;
2230 if (Line
->Link
.ForwardLink
!= FileBuffer
.ListHead
) {
2232 Line
= CR (Line
->Link
.ForwardLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
2235 // if the next line is not that long, so move to end of next line
2237 if (FCol
> Line
->Size
) {
2238 FCol
= Line
->Size
+ 1;
2245 FileBufferMovePosition (FRow
, FCol
);
2251 Scroll the cursor to previous line.
2253 @retval EFI_SUCCESS The operation was successful.
2257 FileBufferScrollUp (
2261 EFI_EDITOR_LINE
*Line
;
2265 Line
= FileBuffer
.CurrentLine
;
2267 FRow
= FileBuffer
.FilePosition
.Row
;
2268 FCol
= FileBuffer
.FilePosition
.Column
;
2271 // has previous line
2273 if (Line
->Link
.BackLink
!= FileBuffer
.ListHead
) {
2275 Line
= CR (Line
->Link
.BackLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
2278 // if previous line is not that long, so move to the end of previous line
2280 if (FCol
> Line
->Size
) {
2281 FCol
= Line
->Size
+ 1;
2288 FileBufferMovePosition (FRow
, FCol
);
2294 Scroll cursor to next page.
2296 @retval EFI_SUCCESS The operation wa successful.
2300 FileBufferPageDown (
2304 EFI_EDITOR_LINE
*Line
;
2309 Line
= FileBuffer
.CurrentLine
;
2311 FRow
= FileBuffer
.FilePosition
.Row
;
2312 FCol
= FileBuffer
.FilePosition
.Column
;
2317 if (FileBuffer
.NumLines
>= FRow
+ (MainEditor
.ScreenSize
.Row
- 2)) {
2318 Gap
= (MainEditor
.ScreenSize
.Row
- 2);
2321 // MOVE CURSOR TO LAST LINE
2323 Gap
= FileBuffer
.NumLines
- FRow
;
2328 Line
= MoveLine (Gap
);
2331 // if that line, is not that long, so move to the end of that line
2333 if (Line
!= NULL
&& FCol
> Line
->Size
) {
2334 FCol
= Line
->Size
+ 1;
2339 FileBufferMovePosition (FRow
, FCol
);
2345 Scroll cursor to previous screen.
2347 @retval EFI_SUCCESS The operation was successful.
2355 EFI_EDITOR_LINE
*Line
;
2361 Line
= FileBuffer
.CurrentLine
;
2363 FRow
= FileBuffer
.FilePosition
.Row
;
2364 FCol
= FileBuffer
.FilePosition
.Column
;
2367 // has previous page
2369 if (FRow
> (MainEditor
.ScreenSize
.Row
- 2)) {
2370 Gap
= (MainEditor
.ScreenSize
.Row
- 2);
2373 // the first line of file will displayed on the first line of screen
2384 Line
= MoveLine (Retreat
);
2387 // if that line is not that long, so move to the end of that line
2389 if (Line
!= NULL
&& FCol
> Line
->Size
) {
2390 FCol
= Line
->Size
+ 1;
2395 FileBufferMovePosition (FRow
, FCol
);
2401 Scroll cursor to end of the current line.
2403 @retval EFI_SUCCESS The operation was successful.
2411 EFI_EDITOR_LINE
*Line
;
2415 Line
= FileBuffer
.CurrentLine
;
2417 FRow
= FileBuffer
.FilePosition
.Row
;
2420 // goto the last column of the line
2422 FCol
= Line
->Size
+ 1;
2424 FileBufferMovePosition (FRow
, FCol
);
2430 Dispatch input to different handler
2431 @param[in] Key The input key. One of:
2435 Direction key: up/down/left/right/pgup/pgdn
2439 @retval EFI_SUCCESS The dispatch was done successfully.
2440 @retval EFI_LOAD_ERROR The dispatch was not successful.
2441 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2445 FileBufferHandleInput (
2446 IN CONST EFI_INPUT_KEY
*Key
2451 Status
= EFI_SUCCESS
;
2453 switch (Key
->ScanCode
) {
2455 // ordinary key input
2458 if (!FileBuffer
.ReadOnly
) {
2459 Status
= FileBufferDoCharInput (Key
->UnicodeChar
);
2461 Status
= StatusBarSetStatusString (L
"Read Only File Can Not Be Modified");
2470 Status
= FileBufferScrollUp ();
2477 Status
= FileBufferScrollDown ();
2484 Status
= FileBufferScrollRight ();
2491 Status
= FileBufferScrollLeft ();
2498 Status
= FileBufferPageUp ();
2504 case SCAN_PAGE_DOWN
:
2505 Status
= FileBufferPageDown ();
2512 if (!FileBuffer
.ReadOnly
) {
2513 Status
= FileBufferDoDelete ();
2515 Status
= StatusBarSetStatusString (L
"Read Only File Can Not Be Modified");
2524 FileBufferMovePosition (FileBuffer
.FilePosition
.Row
, 1);
2525 Status
= EFI_SUCCESS
;
2532 Status
= FileBufferEnd ();
2539 FileBuffer
.ModeInsert
= (BOOLEAN
)!FileBuffer
.ModeInsert
;
2540 Status
= EFI_SUCCESS
;
2544 Status
= StatusBarSetStatusString (L
"Unknown Command");
2552 Check user specified FileRow is above current screen.
2554 @param[in] FileRow The row of file position ( start from 1 ).
2556 @retval TRUE It is above the current screen.
2557 @retval FALSE It is not above the current screen.
2561 AboveCurrentScreen (
2566 // if is to the above of the screen
2568 if (FileRow
< FileBuffer
.LowVisibleRange
.Row
) {
2576 Check user specified FileRow is under current screen.
2578 @param[in] FileRow The row of file position ( start from 1 ).
2580 @retval TRUE It is under the current screen.
2581 @retval FALSE It is not under the current screen.
2585 UnderCurrentScreen (
2590 // if is to the under of the screen
2592 if (FileRow
> FileBuffer
.LowVisibleRange
.Row
+ (MainEditor
.ScreenSize
.Row
- 2) - 1) {
2600 Check user specified FileCol is left to current screen.
2602 @param[in] FileCol The column of file position ( start from 1 ).
2604 @retval TRUE It is to the left.
2605 @retval FALSE It is not to the left.
2614 // if is to the left of the screen
2616 if (FileCol
< FileBuffer
.LowVisibleRange
.Column
) {
2624 Check user specified FileCol is right to current screen.
2626 @param[in] FileCol The column of file position ( start from 1 ).
2628 @retval TRUE It is to the right.
2629 @retval FALSE It is not to the right.
2633 RightCurrentScreen (
2638 // if is to the right of the screen
2640 if (FileCol
> FileBuffer
.LowVisibleRange
.Column
+ MainEditor
.ScreenSize
.Column
- 1) {
2648 Advance/Retreat lines and set CurrentLine in FileBuffer to it
2650 @param[in] Count The line number to advance/retreat
2654 @retval NULL An error occured.
2655 @return The line after advance/retreat.
2663 EFI_EDITOR_LINE
*Line
;
2667 AbsCount
= (UINTN
)ABS(Count
);
2668 Line
= InternalEditorMiscLineRetreat (AbsCount
,MainEditor
.FileBuffer
->CurrentLine
,MainEditor
.FileBuffer
->ListHead
);
2670 Line
= InternalEditorMiscLineAdvance ((UINTN
)Count
,MainEditor
.FileBuffer
->CurrentLine
,MainEditor
.FileBuffer
->ListHead
);
2677 MainEditor
.FileBuffer
->CurrentLine
= Line
;
2683 According to cursor's file position, adjust screen display
2685 @param[in] NewFilePosRow The row of file position ( start from 1 ).
2686 @param[in] NewFilePosCol The column of file position ( start from 1 ).
2690 FileBufferMovePosition (
2691 IN CONST UINTN NewFilePosRow
,
2692 IN CONST UINTN NewFilePosCol
2704 // CALCULATE gap between current file position and new file position
2706 RowGap
= NewFilePosRow
- FileBuffer
.FilePosition
.Row
;
2707 ColGap
= NewFilePosCol
- FileBuffer
.FilePosition
.Column
;
2709 Under
= UnderCurrentScreen (NewFilePosRow
);
2710 Above
= AboveCurrentScreen (NewFilePosRow
);
2712 // if is below current screen
2716 // display row will be unchanged
2718 FileBuffer
.FilePosition
.Row
= NewFilePosRow
;
2722 // has enough above line, so display row unchanged
2723 // not has enough above lines, so the first line is at the
2724 // first display line
2726 if (NewFilePosRow
< (FileBuffer
.DisplayPosition
.Row
- 1)) {
2727 FileBuffer
.DisplayPosition
.Row
= NewFilePosRow
+ 1;
2730 FileBuffer
.FilePosition
.Row
= NewFilePosRow
;
2733 // in current screen
2735 FileBuffer
.FilePosition
.Row
= NewFilePosRow
;
2737 Abs
= (UINTN
)ABS(RowGap
);
2738 FileBuffer
.DisplayPosition
.Row
-= Abs
;
2740 FileBuffer
.DisplayPosition
.Row
+= RowGap
;
2745 FileBuffer
.LowVisibleRange
.Row
= FileBuffer
.FilePosition
.Row
- (FileBuffer
.DisplayPosition
.Row
- 2);
2747 Right
= RightCurrentScreen (NewFilePosCol
);
2748 Left
= LeftCurrentScreen (NewFilePosCol
);
2751 // if right to current screen
2755 // display column will be changed to end
2757 FileBuffer
.DisplayPosition
.Column
= MainEditor
.ScreenSize
.Column
;
2758 FileBuffer
.FilePosition
.Column
= NewFilePosCol
;
2762 // has enough left characters , so display row unchanged
2763 // not has enough left characters,
2764 // so the first character is at the first display column
2766 if (NewFilePosCol
< (FileBuffer
.DisplayPosition
.Column
)) {
2767 FileBuffer
.DisplayPosition
.Column
= NewFilePosCol
;
2770 FileBuffer
.FilePosition
.Column
= NewFilePosCol
;
2773 // in current screen
2775 FileBuffer
.FilePosition
.Column
= NewFilePosCol
;
2777 Abs
= (UINTN
)(-ColGap
);
2778 FileBuffer
.DisplayPosition
.Column
-= Abs
;
2780 FileBuffer
.DisplayPosition
.Column
+= ColGap
;
2785 FileBuffer
.LowVisibleRange
.Column
= FileBuffer
.FilePosition
.Column
- (FileBuffer
.DisplayPosition
.Column
- 1);
2788 // let CurrentLine point to correct line;
2790 FileBuffer
.CurrentLine
= MoveCurrentLine (RowGap
);
2795 Cut current line out and return a pointer to it.
2797 @param[out] CutLine Upon a successful return pointer to the pointer to
2798 the allocated cut line.
2800 @retval EFI_SUCCESS The cut was successful.
2801 @retval EFI_NOT_FOUND There was no selection to cut.
2802 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2807 OUT EFI_EDITOR_LINE
**CutLine
2810 EFI_EDITOR_LINE
*Line
;
2811 EFI_EDITOR_LINE
*NewLine
;
2815 if (FileBuffer
.ReadOnly
) {
2816 StatusBarSetStatusString (L
"Read Only File Can Not Be Modified");
2820 Line
= FileBuffer
.CurrentLine
;
2823 // if is the last dummy line, SO CAN not cut
2825 if (StrCmp (Line
->Buffer
, L
"\0") == 0 && Line
->Link
.ForwardLink
== FileBuffer
.ListHead
2831 // LAST LINE AND NOTHING ON THIS LINE, SO CUT NOTHING
2833 StatusBarSetStatusString (L
"Nothing to Cut");
2834 return EFI_NOT_FOUND
;
2837 // if is the last line, so create a dummy line
2839 if (Line
->Link
.ForwardLink
== FileBuffer
.ListHead
) {
2842 // create a new line
2844 NewLine
= FileBufferCreateLine ();
2845 if (NewLine
== NULL
) {
2846 return EFI_OUT_OF_RESOURCES
;
2850 FileBuffer
.NumLines
--;
2851 Row
= FileBuffer
.FilePosition
.Row
;
2856 FileBuffer
.CurrentLine
= CR (
2857 FileBuffer
.CurrentLine
->Link
.ForwardLink
,
2863 RemoveEntryList (&Line
->Link
);
2865 FileBuffer
.Lines
= CR (FileBuffer
.ListHead
->ForwardLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
2867 FileBufferMovePosition (Row
, Col
);
2869 FileBuffer
.FileModified
= TRUE
;
2870 FileBufferNeedRefresh
= TRUE
;
2871 FileBufferOnlyLineNeedRefresh
= FALSE
;
2879 Paste a line into line list.
2881 @retval EFI_SUCCESS The paste was successful.
2882 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2886 FileBufferPasteLine (
2890 EFI_EDITOR_LINE
*Line
;
2891 EFI_EDITOR_LINE
*NewLine
;
2896 // if nothing is on clip board
2899 if (MainEditor
.CutLine
== NULL
) {
2903 // read only file can not be pasted on
2905 if (FileBuffer
.ReadOnly
) {
2906 StatusBarSetStatusString (L
"Read Only File Can Not Be Modified");
2910 NewLine
= LineDup (MainEditor
.CutLine
);
2911 if (NewLine
== NULL
) {
2912 return EFI_OUT_OF_RESOURCES
;
2915 // insert it above current line
2917 Line
= FileBuffer
.CurrentLine
;
2918 NewLine
->Link
.BackLink
= Line
->Link
.BackLink
;
2919 NewLine
->Link
.ForwardLink
= &Line
->Link
;
2921 Line
->Link
.BackLink
->ForwardLink
= &NewLine
->Link
;
2922 Line
->Link
.BackLink
= &NewLine
->Link
;
2924 FileBuffer
.NumLines
++;
2925 FileBuffer
.CurrentLine
= NewLine
;
2927 FileBuffer
.Lines
= CR (FileBuffer
.ListHead
->ForwardLink
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
2933 Row
= FileBuffer
.FilePosition
.Row
;
2935 FileBufferMovePosition (Row
, Col
);
2938 // after paste, set some value so that refresh knows to do something
2940 FileBuffer
.FileModified
= TRUE
;
2941 FileBufferNeedRefresh
= TRUE
;
2942 FileBufferOnlyLineNeedRefresh
= FALSE
;
2948 Search string from current position on in file
2950 @param[in] Str The search string.
2951 @param[in] Offset The offset from current position.
2953 @retval EFI_SUCCESS The operation was successful.
2954 @retval EFI_NOT_FOUND The string Str was not found.
2959 IN CONST CHAR16
*Str
,
2960 IN CONST UINTN Offset
2967 EFI_EDITOR_LINE
*Line
;
2976 // search if in current line
2978 Current
= FileBuffer
.CurrentLine
->Buffer
+ FileBuffer
.FilePosition
.Column
- 1 + Offset
;
2980 if (Current
>= (FileBuffer
.CurrentLine
->Buffer
+ FileBuffer
.CurrentLine
->Size
)) {
2984 Current
= FileBuffer
.CurrentLine
->Buffer
+ FileBuffer
.CurrentLine
->Size
;
2989 CharPos
= StrStr (Current
, Str
);
2990 if (CharPos
!= NULL
) {
2991 Position
= CharPos
- Current
+ 1;
2999 Column
= (Position
- 1) + FileBuffer
.FilePosition
.Column
+ Offset
;
3000 Row
= FileBuffer
.FilePosition
.Row
;
3003 // not found so find through next lines
3005 Link
= FileBuffer
.CurrentLine
->Link
.ForwardLink
;
3007 Row
= FileBuffer
.FilePosition
.Row
+ 1;
3008 while (Link
!= FileBuffer
.ListHead
) {
3009 Line
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
3010 // Position = StrStr (Line->Buffer, Str);
3011 CharPos
= StrStr (Line
->Buffer
, Str
);
3012 if (CharPos
!= NULL
) {
3013 Position
= CharPos
- Line
->Buffer
+ 1;
3026 Link
= Link
->ForwardLink
;
3029 if (Link
== FileBuffer
.ListHead
) {
3037 return EFI_NOT_FOUND
;
3040 FileBufferMovePosition (Row
, Column
);
3043 // call refresh to fresh edit area,
3044 // because the outer may loop to find multiply occurrence of this string
3046 FileBufferRefresh ();
3052 Replace SearchLen characters from current position on with Replace.
3054 This will modify the current buffer at the current position.
3056 @param[in] Replace The string to replace.
3057 @param[in] SearchLen Search string's length.
3059 @retval EFI_SUCCESS The operation was successful.
3060 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
3065 IN CONST CHAR16
*Replace
,
3066 IN CONST UINTN SearchLen
3076 ReplaceLen
= StrLen (Replace
);
3078 OldSize
= FileBuffer
.CurrentLine
->Size
+ 1;
3080 // include CHAR_NULL
3082 NewSize
= OldSize
+ (ReplaceLen
- SearchLen
);
3084 if (ReplaceLen
> SearchLen
) {
3086 // do not have the enough space
3088 if (FileBuffer
.CurrentLine
->TotalSize
+ 1 <= NewSize
) {
3089 FileBuffer
.CurrentLine
->Buffer
= ReallocatePool (
3092 FileBuffer
.CurrentLine
->Buffer
3094 FileBuffer
.CurrentLine
->TotalSize
= NewSize
- 1;
3097 if (FileBuffer
.CurrentLine
->Buffer
== NULL
) {
3098 return EFI_OUT_OF_RESOURCES
;
3101 // the end CHAR_NULL character;
3103 Buffer
= FileBuffer
.CurrentLine
->Buffer
+ (NewSize
- 1);
3104 Gap
= ReplaceLen
- SearchLen
;
3107 // keep the latter part
3109 for (Index
= 0; Index
< (FileBuffer
.CurrentLine
->Size
- FileBuffer
.FilePosition
.Column
- SearchLen
+ 2); Index
++) {
3110 *Buffer
= *(Buffer
- Gap
);
3114 // set replace into it
3116 Buffer
= FileBuffer
.CurrentLine
->Buffer
+ FileBuffer
.FilePosition
.Column
- 1;
3117 for (Index
= 0; Index
< ReplaceLen
; Index
++) {
3118 Buffer
[Index
] = Replace
[Index
];
3122 if (ReplaceLen
< SearchLen
) {
3123 Buffer
= FileBuffer
.CurrentLine
->Buffer
+ FileBuffer
.FilePosition
.Column
- 1;
3125 for (Index
= 0; Index
< ReplaceLen
; Index
++) {
3126 Buffer
[Index
] = Replace
[Index
];
3129 Buffer
+= ReplaceLen
;
3130 Gap
= SearchLen
- ReplaceLen
;
3133 // set replace into it
3135 for (Index
= 0; Index
< (FileBuffer
.CurrentLine
->Size
- FileBuffer
.FilePosition
.Column
- ReplaceLen
+ 2); Index
++) {
3136 *Buffer
= *(Buffer
+ Gap
);
3141 if (ReplaceLen
== SearchLen
) {
3142 Buffer
= FileBuffer
.CurrentLine
->Buffer
+ FileBuffer
.FilePosition
.Column
- 1;
3143 for (Index
= 0; Index
< ReplaceLen
; Index
++) {
3144 Buffer
[Index
] = Replace
[Index
];
3148 FileBuffer
.CurrentLine
->Size
+= (ReplaceLen
- SearchLen
);
3150 FileBufferOnlyLineNeedRefresh
= TRUE
;
3152 FileBuffer
.FileModified
= TRUE
;
3154 MainTitleBarRefresh (MainEditor
.FileBuffer
->FileName
, MainEditor
.FileBuffer
->FileType
, MainEditor
.FileBuffer
->ReadOnly
, MainEditor
.FileBuffer
->FileModified
, MainEditor
.ScreenSize
.Column
, MainEditor
.ScreenSize
.Row
, 0, 0);
3155 FileBufferRestorePosition ();
3156 FileBufferRefresh ();
3162 Move the mouse cursor position.
3164 @param[in] TextX The new x-coordinate.
3165 @param[in] TextY The new y-coordinate.
3169 FileBufferAdjustMousePosition (
3170 IN CONST INT32 TextX
,
3171 IN CONST INT32 TextY
3180 // TextX and TextY is mouse movement data returned by mouse driver
3181 // This function will change it to MousePosition
3184 // get absolute value
3190 CoordinateX
= FileBuffer
.MousePosition
.Column
;
3191 CoordinateY
= FileBuffer
.MousePosition
.Row
;
3194 CoordinateX
+= TextX
;
3196 if (CoordinateX
>= AbsX
) {
3197 CoordinateX
-= AbsX
;
3204 CoordinateY
+= TextY
;
3206 if (CoordinateY
>= AbsY
) {
3207 CoordinateY
-= AbsY
;
3213 // check whether new mouse column position is beyond screen
3214 // if not, adjust it
3216 if (CoordinateX
>= 1 && CoordinateX
<= MainEditor
.ScreenSize
.Column
) {
3217 FileBuffer
.MousePosition
.Column
= CoordinateX
;
3218 } else if (CoordinateX
< 1) {
3219 FileBuffer
.MousePosition
.Column
= 1;
3220 } else if (CoordinateX
> MainEditor
.ScreenSize
.Column
) {
3221 FileBuffer
.MousePosition
.Column
= MainEditor
.ScreenSize
.Column
;
3224 // check whether new mouse row position is beyond screen
3225 // if not, adjust it
3227 if (CoordinateY
>= 2 && CoordinateY
<= (MainEditor
.ScreenSize
.Row
- 1)) {
3228 FileBuffer
.MousePosition
.Row
= CoordinateY
;
3229 } else if (CoordinateY
< 2) {
3230 FileBuffer
.MousePosition
.Row
= 2;
3231 } else if (CoordinateY
> (MainEditor
.ScreenSize
.Row
- 1)) {
3232 FileBuffer
.MousePosition
.Row
= (MainEditor
.ScreenSize
.Row
- 1);
3238 Search and replace operation.
3240 @param[in] SearchStr The string to search for.
3241 @param[in] ReplaceStr The string to replace with.
3242 @param[in] Offset The column to start at.
3246 FileBufferReplaceAll (
3247 IN CHAR16
*SearchStr
,
3248 IN CHAR16
*ReplaceStr
,
3261 EFI_EDITOR_LINE
*Line
;
3265 SearchLen
= StrLen (SearchStr
);
3266 ReplaceLen
= StrLen (ReplaceStr
);
3268 Column
= FileBuffer
.FilePosition
.Column
+ Offset
- 1;
3270 if (Column
> FileBuffer
.CurrentLine
->Size
) {
3271 Column
= FileBuffer
.CurrentLine
->Size
;
3274 Link
= &(FileBuffer
.CurrentLine
->Link
);
3276 while (Link
!= FileBuffer
.ListHead
) {
3277 Line
= CR (Link
, EFI_EDITOR_LINE
, Link
, LINE_LIST_SIGNATURE
);
3278 CharPos
= StrStr (Line
->Buffer
+ Column
, SearchStr
);
3279 if (CharPos
!= NULL
) {
3280 Position
= CharPos
- Line
->Buffer
;// + Column;
3284 if (ReplaceLen
> SearchLen
) {
3285 OldSize
= Line
->Size
+ 1;
3287 // include CHAR_NULL
3289 NewSize
= OldSize
+ (ReplaceLen
- SearchLen
);
3292 // do not have the enough space
3294 if (Line
->TotalSize
+ 1 <= NewSize
) {
3295 Line
->Buffer
= ReallocatePool (
3300 Line
->TotalSize
= NewSize
- 1;
3303 if (Line
->Buffer
== NULL
) {
3304 return EFI_OUT_OF_RESOURCES
;
3307 // the end CHAR_NULL character;
3309 Buffer
= Line
->Buffer
+ (NewSize
- 1);
3310 Gap
= ReplaceLen
- SearchLen
;
3313 // keep the latter part
3315 for (Index
= 0; Index
< (Line
->Size
- Position
- SearchLen
+ 1); Index
++) {
3316 *Buffer
= *(Buffer
- Gap
);
3320 } else if (ReplaceLen
< SearchLen
){
3321 Buffer
= Line
->Buffer
+ Position
+ ReplaceLen
;
3322 Gap
= SearchLen
- ReplaceLen
;
3324 for (Index
= 0; Index
< (Line
->Size
- Position
- ReplaceLen
+ 1); Index
++) {
3325 *Buffer
= *(Buffer
+ Gap
);
3329 ASSERT(ReplaceLen
== SearchLen
);
3332 // set replace into it
3334 Buffer
= Line
->Buffer
+ Position
;
3335 for (Index
= 0; Index
< ReplaceLen
; Index
++) {
3336 Buffer
[Index
] = ReplaceStr
[Index
];
3339 Line
->Size
+= (ReplaceLen
- SearchLen
);
3340 Column
+= ReplaceLen
;
3346 Link
= Link
->ForwardLink
;
3350 // call refresh to fresh edit area
3352 FileBuffer
.FileModified
= TRUE
;
3353 FileBufferNeedRefresh
= TRUE
;
3354 FileBufferRefresh ();
3360 Set the modified state to TRUE.
3364 FileBufferSetModified (
3368 FileBuffer
.FileModified
= TRUE
;