Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
6.3 KiB

  1. /*++
  2. Copyright (c) 1999 Intel Corporation
  3. Module Name:
  4. libMisc.c
  5. Abstract:
  6. Implementation of various string and line routines
  7. --*/
  8. #include "libMisc.h"
  9. #ifndef _LIB_MISC
  10. #define _LIB_MISC
  11. UINTN StrInsert (CHAR16**,CHAR16,UINTN,UINTN);
  12. UINTN StrnCpy (CHAR16*,CHAR16*,UINTN,UINTN);
  13. INTN StrStr (CHAR16*,CHAR16*);
  14. VOID LineCat (EFI_EDITOR_LINE*,EFI_EDITOR_LINE*);
  15. VOID LineDeleteAt (EFI_EDITOR_LINE*,UINTN);
  16. VOID LineSplit (EFI_EDITOR_LINE*,UINTN,EFI_EDITOR_LINE*);
  17. VOID LineMerge (EFI_EDITOR_LINE*,UINTN,EFI_EDITOR_LINE*,UINTN);
  18. EFI_EDITOR_LINE* LineDup (EFI_EDITOR_LINE*);
  19. EFI_EDITOR_LINE* LineNext (VOID);
  20. EFI_EDITOR_LINE* LinePrevious (VOID);
  21. EFI_EDITOR_LINE* LineAdvance (UINTN Count);
  22. EFI_EDITOR_LINE* LineRetreat (UINTN Count);
  23. EFI_EDITOR_LINE* LineCurrent (VOID);
  24. EFI_EDITOR_LINE* LineFirst (VOID);
  25. EFI_EDITOR_LINE* LineLast (VOID);
  26. VOID
  27. EditorError (
  28. IN EFI_STATUS Status,
  29. IN CHAR16 *Msg
  30. )
  31. {
  32. CHAR16 *Str;
  33. CHAR16 *Error;
  34. Error = AllocatePool(255);
  35. StatusToString(Error,Status);
  36. Str = PoolPrint(L"%s: %s",Msg,Error);
  37. MainEditor.StatusBar->SetStatusString(Str);
  38. FreePool(Str);
  39. FreePool(Error);
  40. }
  41. UINTN
  42. StrInsert (
  43. IN OUT CHAR16 **Str,
  44. IN CHAR16 Char,
  45. IN UINTN Pos,
  46. IN UINTN StrSize
  47. )
  48. {
  49. UINTN i;
  50. CHAR16 *s;
  51. i = (StrSize)*2;
  52. *Str = ReallocatePool(*Str,i,i+2);
  53. s = *Str;
  54. for (i = StrSize-1; i > Pos; i--) {
  55. s[i] = s[i-1];
  56. }
  57. s[i] = Char;
  58. return (StrSize+1);
  59. }
  60. UINTN
  61. StrnCpy (
  62. OUT CHAR16 *Dest,
  63. IN CHAR16 *Src,
  64. IN UINTN Offset,
  65. IN UINTN Num
  66. )
  67. {
  68. UINTN i,j;
  69. j = 0;
  70. for ( i = Offset; (i < Num) && (Src[j] != 0); i++ ) {
  71. Dest[i] = Src[j];
  72. ++j;
  73. }
  74. return j;
  75. }
  76. INTN
  77. StrStr (
  78. IN CHAR16 *Str,
  79. IN CHAR16 *Pat
  80. )
  81. {
  82. INTN *Failure;
  83. INTN i,j;
  84. INTN Lenp;
  85. INTN Lens;
  86. Lenp = StrLen(Pat);
  87. Lens = StrLen(Str);
  88. Failure = AllocatePool(Lenp*sizeof(INTN));
  89. Failure[0] = -1;
  90. for (j=1; j< Lenp; j++ ) {
  91. i = Failure[j-1];
  92. while ( (Pat[j] != Pat[i+1]) && (i >= 0)) {
  93. i = Failure[i];
  94. }
  95. if ( Pat[i] == Pat[i+1]) {
  96. Failure[j] = i+1;
  97. } else {
  98. Failure[j] = -1;
  99. }
  100. }
  101. i = 0;
  102. j = 0;
  103. while (i < Lens && j < Lenp) {
  104. if (Str[i] == Pat[j]) {
  105. i++;
  106. j++;
  107. } else if (j == 0) {
  108. i++;
  109. } else {
  110. j = Failure[j-1] + 1;
  111. }
  112. }
  113. FreePool(Failure);
  114. return ((j == Lenp) ? (i - Lenp) : -1)+1;
  115. }
  116. VOID
  117. LineCat (
  118. IN OUT EFI_EDITOR_LINE* Dest,
  119. IN EFI_EDITOR_LINE* Src
  120. )
  121. {
  122. CHAR16 *Str;
  123. UINTN Size;
  124. Size = Dest->Size - 1;
  125. Dest->Buffer[Size] = 0;
  126. Str = PoolPrint (L"%s%s",Dest->Buffer,Src->Buffer);
  127. Dest->Size = Size + Src->Size;
  128. FreePool (Dest->Buffer);
  129. FreePool (Src->Buffer);
  130. Dest->Buffer = Str;
  131. }
  132. VOID
  133. LineDeleteAt (
  134. IN OUT EFI_EDITOR_LINE* Line,
  135. IN UINTN Pos
  136. )
  137. {
  138. UINTN i;
  139. for ( i = Pos; i < Line->Size; i++) {
  140. Line->Buffer[i] = Line->Buffer[i+1];
  141. }
  142. --Line->Size;
  143. }
  144. EFI_EDITOR_LINE*
  145. LineDup (
  146. IN EFI_EDITOR_LINE *Src
  147. )
  148. {
  149. EFI_EDITOR_LINE *Dest;
  150. Dest = AllocatePool(sizeof(EFI_EDITOR_LINE));
  151. Dest->Signature = EFI_EDITOR_LINE_LIST;
  152. Dest->Size = Src->Size;
  153. Dest->Buffer = PoolPrint(L"%s\0",Src->Buffer);
  154. Dest->Link = Src->Link;
  155. return Dest;
  156. }
  157. VOID
  158. LineSplit (
  159. IN EFI_EDITOR_LINE *Src,
  160. IN UINTN Pos,
  161. OUT EFI_EDITOR_LINE *Dest
  162. )
  163. {
  164. Dest->Size = Src->Size - Pos;
  165. Dest->Buffer = PoolPrint(L"%s\0",Src->Buffer+Pos);
  166. }
  167. VOID
  168. LineMerge (
  169. IN OUT EFI_EDITOR_LINE* Line1,
  170. IN UINTN Line1Pos,
  171. IN EFI_EDITOR_LINE* Line2,
  172. IN UINTN Line2Pos
  173. )
  174. {
  175. UINTN Size;
  176. CHAR16 *Buffer;
  177. Size = Line1Pos + Line2->Size - Line2Pos;
  178. Line1->Size = Size;
  179. Buffer = PoolPrint(L"%s%s\0",Line1->Buffer,Line2->Buffer+Line2Pos);
  180. FreePool(Line1->Buffer);
  181. Line1->Buffer = Buffer;
  182. }
  183. EFI_EDITOR_LINE*
  184. LineFirst (
  185. VOID
  186. )
  187. {
  188. MainEditor.FileBuffer->CurrentLine = MainEditor.FileImage->ListHead->Flink;
  189. return LineCurrent();
  190. }
  191. EFI_EDITOR_LINE*
  192. LineLast (
  193. VOID
  194. )
  195. {
  196. MainEditor.FileBuffer->CurrentLine = MainEditor.FileImage->ListHead->Blink;
  197. return LineCurrent();
  198. }
  199. EFI_EDITOR_LINE*
  200. LineNext (
  201. VOID
  202. )
  203. {
  204. LIST_ENTRY *Link;
  205. Link = MainEditor.FileBuffer->CurrentLine->Flink;
  206. if (Link == MainEditor.FileImage->ListHead) {
  207. Link = Link->Flink;
  208. }
  209. MainEditor.FileBuffer->CurrentLine = Link;
  210. return LineCurrent();
  211. }
  212. EFI_EDITOR_LINE*
  213. LinePrevious (
  214. VOID
  215. )
  216. {
  217. LIST_ENTRY *Link;
  218. Link = MainEditor.FileBuffer->CurrentLine->Blink;
  219. if (Link == MainEditor.FileImage->ListHead) {
  220. Link = Link->Blink;
  221. }
  222. MainEditor.FileBuffer->CurrentLine = Link;
  223. return LineCurrent();
  224. }
  225. EFI_EDITOR_LINE*
  226. LineAdvance (
  227. IN UINTN Count
  228. )
  229. {
  230. UINTN i;
  231. for (i = 0; i < Count && (MainEditor.FileBuffer->CurrentLine->Flink != MainEditor.FileImage->ListHead); i++ ) {
  232. MainEditor.FileBuffer->CurrentLine = MainEditor.FileBuffer->CurrentLine->Flink;
  233. }
  234. return LineCurrent();
  235. }
  236. EFI_EDITOR_LINE*
  237. LineRetreat (
  238. IN UINTN Count
  239. )
  240. {
  241. UINTN i;
  242. for (i = 0; i < Count && (MainEditor.FileBuffer->CurrentLine->Blink != MainEditor.FileImage->ListHead); i++ ) {
  243. MainEditor.FileBuffer->CurrentLine = MainEditor.FileBuffer->CurrentLine->Blink;
  244. }
  245. return LineCurrent();
  246. }
  247. EFI_EDITOR_LINE*
  248. LineCurrent (
  249. VOID
  250. )
  251. {
  252. return CR(MainEditor.FileBuffer->CurrentLine,EFI_EDITOR_LINE,Link,EFI_EDITOR_LINE_LIST);
  253. }
  254. UINTN
  255. UnicodeToAscii (
  256. IN CHAR16 *UStr,
  257. IN UINTN Length,
  258. OUT CHAR8 *AStr
  259. )
  260. {
  261. UINTN i;
  262. for (i = 0; i < Length; i++) {
  263. *AStr++ = (CHAR8)*UStr++;
  264. }
  265. return i;
  266. }
  267. #endif