1 /** @file
2   Implementation of various string and line routines
3 
4   Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved. <BR>
5   SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 #include "HexEditor.h"
10 
11 extern BOOLEAN  HEditorMouseAction;
12 
13 /**
14   Free a line and it's internal buffer.
15 
16   @param[in] Src    The line to be freed.
17 **/
18 VOID
HLineFree(IN HEFI_EDITOR_LINE * Src)19 HLineFree (
20   IN  HEFI_EDITOR_LINE *Src
21   )
22 {
23   if (Src == NULL) {
24     return ;
25   }
26 
27   SHELL_FREE_NON_NULL (Src);
28 
29 }
30 
31 /**
32   Advance to the next Count lines.
33 
34   @param[in] Count      The line number to advance.
35 
36   @retval NULL An error occurred.
37   @return A pointer to the line after advance.
38 **/
39 HEFI_EDITOR_LINE *
HLineAdvance(IN UINTN Count)40 HLineAdvance (
41   IN  UINTN Count
42   )
43 {
44   UINTN             Index;
45   HEFI_EDITOR_LINE  *Line;
46 
47   Line = HMainEditor.BufferImage->CurrentLine;
48   if (Line == NULL) {
49     return NULL;
50   }
51 
52   for (Index = 0; Index < Count; Index++) {
53     //
54     // if already last line
55     //
56     if (Line->Link.ForwardLink == HMainEditor.BufferImage->ListHead) {
57       return NULL;
58     }
59 
60     Line = CR (Line->Link.ForwardLink, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);
61   }
62 
63   return Line;
64 }
65 
66 /**
67   Retreat to the previous Count lines.
68 
69   @param[in] Count    The line number to retreat.
70 
71   @retval NULL An error occurred.
72   @return A pointer to the line after retreat.
73 **/
74 HEFI_EDITOR_LINE *
HLineRetreat(IN UINTN Count)75 HLineRetreat (
76   IN  UINTN Count
77   )
78 {
79   UINTN             Index;
80   HEFI_EDITOR_LINE  *Line;
81 
82   Line = HMainEditor.BufferImage->CurrentLine;
83   if (Line == NULL) {
84     return NULL;
85   }
86 
87   for (Index = 0; Index < Count; Index++) {
88     //
89     // already the first line
90     //
91     if (Line->Link.BackLink == HMainEditor.BufferImage->ListHead) {
92       return NULL;
93     }
94 
95     Line = CR (Line->Link.BackLink, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);
96   }
97 
98   return Line;
99 }
100 
101 /**
102   Advance/Retreat lines.
103 
104   @param[in] Count      The line number to advance/retreat.
105                             >0 : advance
106                             <0: retreat
107 
108   @retval NULL An error occurred.
109   @return A pointer to the line after move.
110 **/
111 HEFI_EDITOR_LINE *
HMoveLine(IN INTN Count)112 HMoveLine (
113   IN  INTN Count
114   )
115 {
116   HEFI_EDITOR_LINE  *Line;
117   UINTN             AbsCount;
118 
119   //
120   // difference with MoveCurrentLine
121   //     just return Line
122   //     do not set currentline to Line
123   //
124   if (Count <= 0) {
125     AbsCount  = (UINTN)ABS(Count);
126     Line      = HLineRetreat (AbsCount);
127   } else {
128     Line = HLineAdvance ((UINTN)Count);
129   }
130 
131   return Line;
132 }
133 
134 /**
135   Advance/Retreat lines and set CurrentLine in BufferImage to it.
136 
137   @param[in] Count    The line number to advance/retreat.
138                           >0 : advance
139                           <0: retreat
140 
141   @retval NULL An error occurred.
142   @return A pointer to the line after move.
143 **/
144 HEFI_EDITOR_LINE *
HMoveCurrentLine(IN INTN Count)145 HMoveCurrentLine (
146   IN  INTN Count
147   )
148 {
149   HEFI_EDITOR_LINE  *Line;
150   UINTN             AbsCount;
151 
152   //
153   // <0: retreat
154   // >0: advance
155   //
156   if (Count <= 0) {
157     AbsCount  = (UINTN)ABS(Count);
158     Line      = HLineRetreat (AbsCount);
159   } else {
160     Line = HLineAdvance ((UINTN)Count);
161   }
162 
163   if (Line == NULL) {
164     return NULL;
165   }
166 
167   HMainEditor.BufferImage->CurrentLine = Line;
168 
169   return Line;
170 }
171 
172 
173 /**
174   Free all the lines in HBufferImage.
175     Fields affected:
176     Lines
177     CurrentLine
178     NumLines
179     ListHead
180 
181   @param[in] ListHead     The list head.
182   @param[in] Lines        The lines.
183 
184   @retval EFI_SUCCESS     The operation was successful.
185 **/
186 EFI_STATUS
HFreeLines(IN LIST_ENTRY * ListHead,IN HEFI_EDITOR_LINE * Lines)187 HFreeLines (
188   IN LIST_ENTRY   *ListHead,
189   IN HEFI_EDITOR_LINE *Lines
190   )
191 {
192   LIST_ENTRY        *Link;
193   HEFI_EDITOR_LINE  *Line;
194 
195   //
196   // release all the lines
197   //
198   if (Lines != NULL) {
199 
200     Line  = Lines;
201     Link  = &(Line->Link);
202     do {
203       Line  = CR (Link, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);
204       Link  = Link->ForwardLink;
205       HLineFree (Line);
206     } while (Link != ListHead);
207   }
208 
209   ListHead->ForwardLink = ListHead;
210   ListHead->BackLink = ListHead;
211 
212   return EFI_SUCCESS;
213 }
214 
215 /**
216   Get the X information for the mouse.
217 
218   @param[in] GuidX      The change.
219 
220   @return the new information.
221 **/
222 INT32
HGetTextX(IN INT32 GuidX)223 HGetTextX (
224   IN INT32 GuidX
225   )
226 {
227   INT32 Gap;
228 
229   HMainEditor.MouseAccumulatorX += GuidX;
230   Gap = (HMainEditor.MouseAccumulatorX * (INT32) HMainEditor.ScreenSize.Column) / (INT32) (50 * (INT32) HMainEditor.MouseInterface->Mode->ResolutionX);
231   HMainEditor.MouseAccumulatorX = (HMainEditor.MouseAccumulatorX * (INT32) HMainEditor.ScreenSize.Column) % (INT32) (50 * (INT32) HMainEditor.MouseInterface->Mode->ResolutionX);
232   HMainEditor.MouseAccumulatorX = HMainEditor.MouseAccumulatorX / (INT32) HMainEditor.ScreenSize.Column;
233   return Gap;
234 }
235 
236 /**
237   Get the Y information for the mouse.
238 
239   @param[in] GuidY      The change.
240 
241   @return the new information.
242 **/
243 INT32
HGetTextY(IN INT32 GuidY)244 HGetTextY (
245   IN INT32 GuidY
246   )
247 {
248   INT32 Gap;
249 
250   HMainEditor.MouseAccumulatorY += GuidY;
251   Gap = (HMainEditor.MouseAccumulatorY * (INT32) HMainEditor.ScreenSize.Row) / (INT32) (50 * (INT32) HMainEditor.MouseInterface->Mode->ResolutionY);
252   HMainEditor.MouseAccumulatorY = (HMainEditor.MouseAccumulatorY * (INT32) HMainEditor.ScreenSize.Row) % (INT32) (50 * (INT32) HMainEditor.MouseInterface->Mode->ResolutionY);
253   HMainEditor.MouseAccumulatorY = HMainEditor.MouseAccumulatorY / (INT32) HMainEditor.ScreenSize.Row;
254 
255   return Gap;
256 }
257