TextUtilities.SymU 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. MODULE TextUtilities;
  2. IMPORT SYSTEM, Configuration, Commands, Codecs, FP1616, KernelLog, Texts, Streams, Files, UTF8Strings, XML, XMLScanner, XMLParser, XMLObjects, Repositories, Strings, WMGraphics, UnicodeProperties;
  3. CONST
  4. Ok* = 0;
  5. FileNotFound* = Files.FileNotFound;
  6. FileCreationError* = 2;
  7. CodecNotFound* = 3;
  8. CR = 0DX;
  9. LF = 0AX;
  10. TAB = 9X;
  11. LoadUnicode* = 0;
  12. StoreUnicode* = 1;
  13. LoadFormated* = 2;
  14. StoreFormatted* = 3;
  15. BufferedAttributes = 256;
  16. TYPE
  17. Char32 = Texts.Char32;
  18. Text = Texts.Text;
  19. LoaderProc* = PROCEDURE {DELEGATE}(text: Text; filename: ARRAY OF CHAR; VAR res: WORD);
  20. FormatDescriptor = OBJECT
  21. VAR
  22. name: Strings.String;
  23. loadProc, storeProc: Strings.String;
  24. END FormatDescriptor;
  25. AttributesBuf* = RECORD
  26. attributes: POINTER TO ARRAY OF Texts.Attributes;
  27. positions: POINTER TO ARRAY OF LONGINT;
  28. length: LONGINT;
  29. END;
  30. TextWriter* = OBJECT (Streams.Writer)
  31. VAR
  32. text: Texts.Text;
  33. ucs32buf: POINTER TO ARRAY OF LONGINT;
  34. fontName: ARRAY 32 OF CHAR;
  35. fontSize, fontColor, fontBgColor, fontVOff: LONGINT;
  36. fontStyle: SET;
  37. currentAttributes-: Texts.Attributes;
  38. oldBytes: ARRAY 7 OF CHAR;
  39. nofOldBytes: LONGINT;
  40. attributesBuf: AttributesBuf;
  41. PROCEDURE ^ & Init*(text: Texts.Text);
  42. PROCEDURE ^ Add(CONST buf: ARRAY OF CHAR; ofs, len: LONGINT; propagate: BOOLEAN; VAR res: WORD);
  43. PROCEDURE ^ Ln*;
  44. PROCEDURE ^ SetAttributes*(attributes: Texts.Attributes);
  45. PROCEDURE ^ NewAttributes(): Texts.Attributes;
  46. PROCEDURE ^ SetFontName*(CONST name: ARRAY OF CHAR);
  47. PROCEDURE ^ SetFontSize*(size: LONGINT);
  48. PROCEDURE ^ SetFontStyle*(style: SET);
  49. PROCEDURE ^ SetFontColor*(color: LONGINT);
  50. PROCEDURE ^ SetBgColor*(bgColor: LONGINT);
  51. PROCEDURE ^ SetVerticalOffset*(voff: LONGINT);
  52. PROCEDURE ^ AddObject*(obj: ANY);
  53. END TextWriter;
  54. TextReader* = OBJECT (Streams.Reader)
  55. VAR
  56. reader: Texts.TextReader;
  57. remainder: LONGINT;
  58. PROCEDURE ^ & Init*(text: Texts.Text);
  59. PROCEDURE ^ Receive(VAR buf: ARRAY OF CHAR; ofs, size, min: LONGINT; VAR len, res: LONGINT);
  60. PROCEDURE ^ CanSetPos*(): BOOLEAN;
  61. PROCEDURE ^ SetPos*(pos: Streams.Position);
  62. END TextReader;
  63. LongintArray = POINTER TO ARRAY OF LONGINT;
  64. Operation = RECORD
  65. op, pos, len: LONGINT;
  66. END;
  67. Operations = POINTER TO ARRAY OF Operation;
  68. TextSelection* = OBJECT
  69. VAR
  70. text*: Texts.Text;
  71. from*, to*: Texts.TextPosition;
  72. END TextSelection;
  73. TextPositionKeeper* = OBJECT (Texts.TextPosition)
  74. VAR
  75. positions: LongintArray;
  76. nofPositions: LONGINT;
  77. operations: Operations;
  78. nofOperations, nofDeleted: LONGINT;
  79. PROCEDURE ^ & New*(t: Texts.Text);
  80. PROCEDURE ^ GrowOperations;
  81. PROCEDURE ^ Cleanup;
  82. PROCEDURE ^ Changed*(op, pos, len: LONGINT);
  83. PROCEDURE ^ GrowPositions;
  84. PROCEDURE ^ DeletePos*(index: LONGINT);
  85. PROCEDURE ^ AddPos*(pos: LONGINT): LONGINT;
  86. PROCEDURE ^ Clear*;
  87. PROCEDURE ^ GetPos*(index: LONGINT): LONGINT;
  88. PROCEDURE ^ SetPos*(index, pos: LONGINT);
  89. END TextPositionKeeper;
  90. OberonDecoder = OBJECT (Codecs.TextDecoder)
  91. VAR
  92. errors: BOOLEAN;
  93. in: Streams.Reader;
  94. text: Texts.Text;
  95. buffer: Strings.Buffer;
  96. string: Strings.String;
  97. reader, sreader: Streams.StringReader;
  98. PROCEDURE ^ Error(CONST x: ARRAY OF CHAR);
  99. PROCEDURE ^ LoadLibrary(buf: Strings.Buffer; pos: LONGINT; VARflen: LONGINT);
  100. PROCEDURE ^ IndexToColor(index: LONGINT): LONGINT;
  101. PROCEDURE ^ InsertPiece(ofs, len: LONGINT; attr: Texts.Attributes);
  102. PROCEDURE ^ Open*(in: Streams.Reader; VAR res: WORD);
  103. PROCEDURE ^ GetText*(): Texts.Text;
  104. PROCEDURE ^ OberonToUni(ch: LONGINT): LONGINT;
  105. END OberonDecoder;
  106. OberonEncoder = OBJECT (Codecs.TextEncoder)
  107. VAR
  108. out, w: Streams.Writer;
  109. w2: Streams.StringWriter;
  110. string: Strings.String;
  111. buffer: Strings.Buffer;
  112. oberonColors: ARRAY 16 OF LONGINT;
  113. fonts: ARRAY 256 OF Texts.FontInfo;
  114. font: Texts.FontInfo;
  115. nofFonts, hLen: LONGINT;
  116. firstPiece: BOOLEAN;
  117. voff: LONGINT;
  118. color: LONGINT;
  119. PROCEDURE ^ Open*(out: Streams.Writer);
  120. PROCEDURE ^ ColorToIndex(col: LONGINT): LONGINT;
  121. PROCEDURE ^ GetOberonColor(color: LONGINT): LONGINT;
  122. PROCEDURE ^ WritePiece(len: LONGINT);
  123. PROCEDURE ^ WriteText*(text: Texts.Text; VAR res: WORD);
  124. PROCEDURE ^ UniToOberon(ch: LONGINT): LONGINT;
  125. END OberonEncoder;
  126. BluebottleDecoder* = OBJECT (Codecs.TextDecoder)
  127. VAR
  128. errors: BOOLEAN;
  129. text: Texts.Text;
  130. doc: XML.Document;
  131. cont, tc, tc2: XMLObjects.Enumerator;
  132. ptr: ANY;
  133. root: XML.Element;
  134. str: Strings.String;
  135. o: Texts.ObjectPiece;
  136. attr: Texts.Attributes;
  137. fi: Texts.FontInfo;
  138. stylename, pstylename: ARRAY 64 OF CHAR;
  139. link: Texts.Link;
  140. PROCEDURE ^ Error(CONST x: ARRAY OF CHAR);
  141. PROCEDURE ^ GetUTF8Char(r: Streams.Reader; VAR u: Texts.Char32; VAR pos: LONGINT): BOOLEAN;
  142. PROCEDURE ^ InsertPiece(charContent: XML.CDataSect);
  143. PROCEDURE ^ InsertChar(pos: LONGINT; ch: Texts.Char32);
  144. PROCEDURE ^ MalformedXML(pos, line, row: LONGINT; CONST msg: ARRAY OF CHAR);
  145. PROCEDURE ^ Open*(in: Streams.Reader; VAR res: WORD);
  146. PROCEDURE ^ OpenXML*(d: XML.Document);
  147. PROCEDURE ^ GetText*(): Texts.Text;
  148. END BluebottleDecoder;
  149. BluebottleEncoder = OBJECT (Codecs.TextEncoder)
  150. VAR
  151. out: Streams.Writer;
  152. ch: Texts.Char32;
  153. r: Texts.TextReader;
  154. changed, pchanged, pOpen: BOOLEAN;
  155. stylename, pstylename: ARRAY 256 OF CHAR;
  156. cStyle: Texts.CharacterStyle;
  157. pStyle: Texts.ParagraphStyle;
  158. link: Texts.Link;
  159. family, dfFamily: ARRAY 64 OF CHAR;
  160. size, dfSize: LONGINT;
  161. style, dfStyle: LONGINT;
  162. voff, dfVoff: LONGINT;
  163. color, dfColor: LONGINT;
  164. bgcolor, dfBgcolor: LONGINT;
  165. PROCEDURE ^ Init;
  166. PROCEDURE ^ RetrieveAttributes;
  167. PROCEDURE ^ PrintAttributes;
  168. PROCEDURE ^ CompareAttributes(): BOOLEAN;
  169. PROCEDURE ^ CompareParagraphs(): BOOLEAN;
  170. PROCEDURE ^ WriteParagraph(CONST name: ARRAY OF CHAR);
  171. PROCEDURE ^ CloseParagraph;
  172. PROCEDURE ^ WriteSpan(CONST name: ARRAY OF CHAR);
  173. PROCEDURE ^ CloseSpan;
  174. PROCEDURE ^ WriteObject(o: ANY);
  175. PROCEDURE ^ WriteLabel(CONST label: ARRAY OF CHAR);
  176. PROCEDURE ^ Open*(out: Streams.Writer);
  177. PROCEDURE ^ WriteText*(text: Texts.Text; VAR res: WORD);
  178. END BluebottleEncoder;
  179. UTF8Decoder = OBJECT (Codecs.TextDecoder)
  180. VAR
  181. errors: BOOLEAN;
  182. in: Streams.Reader;
  183. text: Texts.Text;
  184. PROCEDURE ^ Error(CONST x: ARRAY OF CHAR);
  185. PROCEDURE ^ Open*(in: Streams.Reader; VAR res: WORD);
  186. PROCEDURE ^ GetText*(): Texts.Text;
  187. END UTF8Decoder;
  188. UTF8Encoder = OBJECT (Codecs.TextEncoder)
  189. VAR
  190. out: Streams.Writer;
  191. PROCEDURE ^ Open*(out: Streams.Writer);
  192. PROCEDURE ^ WriteText*(text: Texts.Text; VAR res: WORD);
  193. END UTF8Encoder;
  194. ISO88591Decoder = OBJECT (Codecs.TextDecoder)
  195. VAR
  196. errors: BOOLEAN;
  197. in: Streams.Reader;
  198. text: Texts.Text;
  199. PROCEDURE ^ Error(CONST x: ARRAY OF CHAR);
  200. PROCEDURE ^ Open*(in: Streams.Reader; VAR res: WORD);
  201. PROCEDURE ^ GetText*(): Texts.Text;
  202. END ISO88591Decoder;
  203. ISO88591Encoder = OBJECT (Codecs.TextEncoder)
  204. VAR
  205. out: Streams.Writer;
  206. PROCEDURE ^ Open*(out: Streams.Writer);
  207. PROCEDURE ^ WriteText*(text: Texts.Text; VAR res: WORD);
  208. END ISO88591Encoder;
  209. HEXDecoder = OBJECT (Codecs.TextDecoder)
  210. VAR
  211. errors: BOOLEAN;
  212. in: Streams.Reader;
  213. text: Texts.Text;
  214. PROCEDURE ^ Error(CONST x: ARRAY OF CHAR);
  215. PROCEDURE ^ Open*(in: Streams.Reader; VAR res: WORD);
  216. PROCEDURE ^ GetText*(): Texts.Text;
  217. END HEXDecoder;
  218. HEXEncoder = OBJECT (Codecs.TextEncoder)
  219. VAR
  220. out: Streams.Writer;
  221. PROCEDURE ^ Open*(out: Streams.Writer);
  222. PROCEDURE ^ WriteText*(text: Texts.Text; VAR res: WORD);
  223. END HEXEncoder;
  224. VAR
  225. unicodePropertyReader: UnicodeProperties.UnicodeTxtReader;
  226. oberonFontAllocatable*: PROCEDURE (CONST name: ARRAY OF CHAR):BOOLEAN;
  227. PROCEDURE ^ IsWhiteSpace*(x: Char32; utf: BOOLEAN): BOOLEAN;
  228. PROCEDURE ^ IsAlphaNum*(x: Char32): BOOLEAN;
  229. PROCEDURE ^ FindPosWordLeft*(utilreader: Texts.TextReader; pos: LONGINT): LONGINT;
  230. PROCEDURE ^ FindPosWordRight*(utilreader: Texts.TextReader; pos: LONGINT): LONGINT;
  231. PROCEDURE ^ FindPosLineStart*(utilreader: Texts.TextReader; pos: LONGINT): LONGINT;
  232. PROCEDURE ^ CountWhitespace*(utilreader: Texts.TextReader; pos: LONGINT): LONGINT;
  233. PROCEDURE ^ LoadAuto*(text: Text; CONST fileName: ARRAY OF CHAR; VAR format, res: LONGINT);
  234. PROCEDURE ^ DecodeAuto*(CONST fileName: ARRAY OF CHAR; VAR format: ARRAY OF CHAR): Codecs.TextDecoder;
  235. PROCEDURE ^ Load*(text: Text; CONST filename, format: ARRAY OF CHAR; VAR res: WORD);
  236. PROCEDURE ^ LoadAscii*(text: Text; CONST filename: ARRAY OF CHAR; VAR res: WORD);
  237. PROCEDURE ^ LoadUTF8*(text: Text; CONST filename: ARRAY OF CHAR; VAR res: WORD);
  238. PROCEDURE ^ LoadUCS16*(text: Text; CONST filename: ARRAY OF CHAR; VAR res: WORD);
  239. PROCEDURE ^ LoadOberonText*(text: Text; CONST fileName: ARRAY OF CHAR; VAR res: WORD);
  240. PROCEDURE ^ LoadText*(text: Texts.Text; CONST filename: ARRAY OF CHAR; VAR res: WORD);
  241. PROCEDURE ^ Store*(text: Text; CONST filename, format: ARRAY OF CHAR; VAR res: WORD);
  242. PROCEDURE ^ ExportAscii*(text: Text; CONST fileName: ARRAY OF CHAR; VAR res: WORD);
  243. PROCEDURE ^ ExportUTF8*(text: Text; CONST fileName: ARRAY OF CHAR; VAR res: WORD);
  244. PROCEDURE ^ StoreOberonText*(text: Text; CONST fileName: ARRAY OF CHAR; VAR res: WORD);
  245. PROCEDURE ^ StoreText*(text: Texts.Text; CONST fileName: ARRAY OF CHAR; VAR res: WORD);
  246. PROCEDURE ^ TextToStr*(text: Text; VAR string: ARRAY OF CHAR);
  247. PROCEDURE ^ SubTextToStream*(text: Text; start, length: LONGINT; w: Streams.Writer);
  248. PROCEDURE ^ TextToStream*(text: Text; w: Streams.Writer);
  249. PROCEDURE ^ SubTextToStrAt*(text: Text; startPos, len: LONGINT; VAR index: LONGINT; VAR string: ARRAY OF CHAR);
  250. PROCEDURE ^ SubTextToStr*(text: Text; startPos, len: LONGINT; VAR string: ARRAY OF CHAR);
  251. PROCEDURE ^ StrToText*(text: Text; pos: LONGINT; CONST string: ARRAY OF CHAR);
  252. PROCEDURE ^ IsDigit(ch: CHAR): BOOLEAN;
  253. PROCEDURE ^ DecodeOberonFontName(CONST name: ARRAY OF CHAR; VAR fn: ARRAY OF CHAR; VAR size: LONGINT; VAR style: SET);
  254. PROCEDURE ^ ToOberonFont(CONST name: ARRAY OF CHAR; size: LONGINT; style: SET; VAR oname: ARRAY OF CHAR): BOOLEAN;
  255. PROCEDURE ^ GetUTF8Char*(r: Streams.Reader; VAR u: Texts.Char32): BOOLEAN;
  256. PROCEDURE ^ WriteUTF8Char*(w: Streams.Writer; ch: Char32);
  257. PROCEDURE ^ StyleToAttribute*(style: Texts.CharacterStyle): Texts.Attributes;
  258. PROCEDURE ^ AttributeToStyle*(CONST name: ARRAY OF CHAR; attr: Texts.Attributes): Texts.CharacterStyle;
  259. PROCEDURE ^ Convert*(context: Commands.Context);
  260. PROCEDURE ^ ConvertAll*(context: Commands.Context);
  261. PROCEDURE ^ ConvertFile(CONST file: ARRAY OF CHAR; context: Commands.Context);
  262. PROCEDURE ^ SkipLine(utilreader: Texts.TextReader; pos: LONGINT): LONGINT;
  263. PROCEDURE ^ IndentText*(text: Texts.Text; from, to: LONGINT; minus: BOOLEAN);
  264. PROCEDURE ^ UCS32StrLength*(CONST string: ARRAY OF Char32): LONGINT;
  265. PROCEDURE ^ Pos*(CONST pattern: ARRAY OF Char32; from: LONGINT; text: Text): LONGINT;
  266. PROCEDURE ^ UpperCaseChar32*(VAR ch: Texts.Char32);
  267. PROCEDURE ^ Equals(CONST pattern: ARRAY OF Char32; r: Texts.TextReader; length: LONGINT; ignoreCase: BOOLEAN): BOOLEAN;
  268. PROCEDURE ^ GenericPos*(CONST pattern: ARRAY OF Char32; from: LONGINT; text: Text; ignoreCase, backwards: BOOLEAN): LONGINT;
  269. PROCEDURE ^ Replace*(CONST string, by: Texts.UCS32String; text: Texts.Text; VAR nofReplacements: LONGINT);
  270. PROCEDURE ^ AddFontFormat*(x: FormatDescriptor);
  271. PROCEDURE ^ GetConfig;
  272. PROCEDURE ^ OberonDecoderFactory*(): Codecs.TextDecoder;
  273. PROCEDURE ^ OberonEncoderFactory*(): Codecs.TextEncoder;
  274. PROCEDURE ^ BluebottleDecoderFactory*(): Codecs.TextDecoder;
  275. PROCEDURE ^ BluebottleEncoderFactory*(): Codecs.TextEncoder;
  276. PROCEDURE ^ UTF8DecoderFactory*(): Codecs.TextDecoder;
  277. PROCEDURE ^ UTF8EncoderFactory*(): Codecs.TextEncoder;
  278. PROCEDURE ^ ISO88591DecoderFactory*(): Codecs.TextDecoder;
  279. PROCEDURE ^ ISO88591EncoderFactory*(): Codecs.TextEncoder;
  280. PROCEDURE ^ HEXDecoderFactory*(): Codecs.TextDecoder;
  281. PROCEDURE ^ HEXEncoderFactory*(): Codecs.TextEncoder;
  282. PROCEDURE ^ GetClipboard*(context: Commands.Context);
  283. PROCEDURE ^ SetClipboard*(context: Commands.Context);
  284. PROCEDURE ^ GetTextReader*(CONST filename: ARRAY OF CHAR): Streams.Reader;
  285. PROCEDURE ^ GetDefaultAttributes*(): Texts.Attributes;
  286. BEGIN
  287. END TextUtilities.