nanotex5.mod 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. MODULE nanotext;
  2. FROM Storage IMPORT ALLOCATE , DEALLOCATE;
  3. IMPORT Lib,Str,IO,Window,FIO;
  4. TYPE t_ptr_t_element = POINTER TO t_element;
  5. t_element = CLASS
  6. ptr_suivant : t_ptr_t_element;
  7. ptr_precedent : t_ptr_t_element;
  8. PROCEDURE affiche_element () ; VIRTUAL;
  9. BEGIN
  10. (* … impl‚menter par les objets fils *)
  11. END affiche_element;
  12. PROCEDURE detruit_element () ; VIRTUAL;
  13. BEGIN
  14. (* … impl‚menter par les objets fils *)
  15. END detruit_element;
  16. END; (* CLASS t_element *)
  17. TYPE t_liste = CLASS
  18. debut : t_ptr_t_element;
  19. ptr_courant : t_ptr_t_element;
  20. PROCEDURE creer() ;
  21. BEGIN
  22. debut := NIL;
  23. ptr_courant := NIL;
  24. END creer;
  25. PROCEDURE premier () : t_ptr_t_element ;
  26. BEGIN
  27. RETURN debut;
  28. END premier;
  29. PROCEDURE au_debut () ;
  30. (* d‚place le pointeur courant au d‚but de la liste *)
  31. BEGIN
  32. ptr_courant := debut;
  33. END au_debut;
  34. PROCEDURE suivant () : t_ptr_t_element;
  35. BEGIN
  36. RETURN ptr_courant^.ptr_suivant;
  37. END suivant;
  38. PROCEDURE a_la_fin ();
  39. (* d‚place le pointeur courant en queue de liste *)
  40. BEGIN
  41. WHILE suivant() <> NIL DO
  42. ptr_courant := ptr_courant^.ptr_suivant;
  43. END;
  44. END a_la_fin;
  45. PROCEDURE au_suivant ();
  46. (* d‚place le pointeur courant au paragraphe suivant si possible *)
  47. BEGIN
  48. IF suivant() <> NIL THEN
  49. ptr_courant := suivant();
  50. END;
  51. END au_suivant;
  52. PROCEDURE precedent () : t_ptr_t_element;
  53. BEGIN
  54. RETURN ptr_courant^.ptr_precedent;
  55. END precedent;
  56. PROCEDURE au_precedent ();
  57. (* d‚place le pointeur courant au paragraphe precedent si possible *)
  58. BEGIN
  59. IF precedent() <> NIL THEN
  60. ptr_courant := precedent();
  61. END;
  62. END au_precedent;
  63. PROCEDURE dernier ():t_ptr_t_element;
  64. BEGIN
  65. au_debut();
  66. WHILE suivant() <> NIL DO
  67. au_suivant();
  68. END;
  69. RETURN ptr_courant;
  70. END dernier;
  71. PROCEDURE liste_vide():BOOLEAN ;
  72. BEGIN
  73. RETURN debut = NIL;
  74. END liste_vide;
  75. PROCEDURE ajouter ( VAR y : t_element);
  76. VAR l_ptr_element : t_ptr_t_element;
  77. (* on ajoute en fin de liste :
  78. 2 cas : liste vide
  79. : liste non vide
  80. *)
  81. BEGIN
  82. ALLOCATE(l_ptr_element,SIZE(y));
  83. Lib.Move(ADR(y),l_ptr_element,SIZE(y));
  84. IF liste_vide() THEN (* cas du debut de liste vide *)
  85. debut := l_ptr_element;
  86. ptr_courant := l_ptr_element ;
  87. ptr_courant^.ptr_precedent := NIL;
  88. ptr_courant^.ptr_suivant := NIL;
  89. ELSE (* ajout en fin de liste *)
  90. l_ptr_element^.ptr_suivant := NIL;
  91. l_ptr_element^.ptr_precedent := ptr_courant;
  92. ptr_courant^.ptr_suivant := l_ptr_element;
  93. ptr_courant := l_ptr_element;
  94. END;
  95. END ajouter;
  96. PROCEDURE inserer (VAR y : t_element);
  97. (* On insŠre AVANT le pointeur courant !!!!!!!!! *)
  98. (* 2 cas : insertion en d‚but de liste
  99. insertion au milieu de liste
  100. *)
  101. VAR l_ptr_element : t_ptr_t_element;
  102. BEGIN
  103. ALLOCATE(l_ptr_element,SIZE(y));
  104. Lib.Move(ADR(y),l_ptr_element,SIZE(y));
  105. IF ptr_courant^.ptr_precedent = NIL THEN (* cas du d‚but de liste non vide *)
  106. debut := l_ptr_element;
  107. l_ptr_element^.ptr_precedent := NIL;
  108. l_ptr_element^.ptr_suivant := ptr_courant;
  109. ptr_courant^.ptr_precedent := l_ptr_element;
  110. ptr_courant := debut^.ptr_suivant;
  111. ELSE (* cas du milieu de liste *)
  112. ptr_courant^.ptr_precedent^.ptr_suivant := l_ptr_element;
  113. l_ptr_element^.ptr_suivant := ptr_courant;
  114. l_ptr_element^.ptr_precedent := ptr_courant^.ptr_precedent;
  115. ptr_courant^.ptr_precedent := l_ptr_element;
  116. ptr_courant := l_ptr_element^.ptr_suivant;
  117. END; (* IF *)
  118. END inserer;
  119. PROCEDURE inserer_liste ( VAR x : t_liste);
  120. BEGIN
  121. END inserer_liste;
  122. PROCEDURE detruire ();
  123. (* on detruit l'element du pointeur courant *)
  124. VAR l_ptr_element,ll_ptr_element : t_ptr_t_element;
  125. BEGIN
  126. ptr_courant^.detruit_element;
  127. IF ptr_courant^.ptr_precedent = NIL THEN
  128. (* cas du premier element de la liste **)
  129. (* 2 cas : liste compos‚e d'un element
  130. : liste composee de plusieurs elements *)
  131. IF ptr_courant^.ptr_suivant = NIL THEN
  132. DEALLOCATE(ptr_courant,SIZE(ptr_courant^));
  133. debut := NIL;
  134. ptr_courant := NIL;
  135. ELSE
  136. ptr_courant := ptr_courant^.ptr_suivant;
  137. ptr_courant^.ptr_precedent := NIL;
  138. DEALLOCATE(debut,SIZE(debut^));
  139. debut := ptr_courant;
  140. END; (* IF *)
  141. ELSIF ptr_courant^.ptr_suivant = NIL THEN
  142. (* cas d'un ‚l‚ment en fin de liste *)
  143. ptr_courant := ptr_courant^.ptr_precedent;
  144. DEALLOCATE(ptr_courant^.ptr_suivant,SIZE(ptr_courant^.ptr_suivant^));
  145. ptr_courant^.ptr_suivant := NIL;
  146. ELSE
  147. (* cas g‚n‚ral d'un ‚l‚ment au milieu de liste *)
  148. l_ptr_element := ptr_courant^.ptr_suivant;
  149. l_ptr_element^.ptr_precedent := ptr_courant^.ptr_precedent;
  150. ptr_courant^.ptr_precedent^.ptr_suivant := l_ptr_element;
  151. DISPOSE(ptr_courant);
  152. ptr_courant := l_ptr_element;
  153. END ; (* IF *)
  154. END detruire;
  155. END; (* CLASS t_liste *)
  156. TYPE t_ptr_t_paragraphe = POINTER TO t_paragraphe;
  157. t_paragraphe = CLASS (t_element)
  158. longueur : CARDINAL;
  159. ptr_chaine : ADDRESS;
  160. PROCEDURE initialise_element ( VAR x : ARRAY OF CHAR);VIRTUAL;
  161. BEGIN
  162. longueur := Str.Length(x);
  163. ALLOCATE(ptr_chaine,longueur);
  164. Lib.Move(ADR(x),ptr_chaine,longueur);
  165. END initialise_element;
  166. PROCEDURE affiche_element (); VIRTUAL;
  167. VAR l_tampon : ARRAY[0..1023] OF CHAR;
  168. BEGIN
  169. Lib.Move(ptr_chaine,ADR(l_tampon),longueur);
  170. l_tampon[longueur] := CHR(0);
  171. IO.WrStr(l_tampon);
  172. IO.WrLn;
  173. END affiche_element;
  174. PROCEDURE detruit_element () ; VIRTUAL;
  175. BEGIN
  176. DEALLOCATE(ptr_chaine,SIZE(ptr_chaine^));
  177. END detruit_element;
  178. END; (* CLASS t_paragraphe *)
  179. (* le tampon est notre zone de travail : c'est dans lui que seront
  180. faites toutes les insertions et destructions , recherches ;
  181. donc le test des touches frapp‚es au clavier vont provoquer
  182. le transfert dans le tampon de notre paragraphe, et toutes les
  183. modifications se feront dans celui-ci; ceci nous procure en
  184. plus une fonction UNDO facile car le texte original n'est pas
  185. modifi‚ tant que le transfert inverse n'est pas r‚alis‚.
  186. Nous pourons sauvegarder toutes nos modifications dans une
  187. liste que nous pourrons ‚diter ou sauvegarder … souhait *)
  188. TYPE (* t_ptr_t_tampon = POINTER TO t_tampon;*)
  189. t_tampon = CLASS (t_paragraphe)
  190. position : CARDINAL;
  191. sauve_ptr_suivant : t_ptr_t_element;
  192. suve_ptr_precedent : t_ptr_t_element;
  193. PROCEDURE initialise_element ( VAR x : ARRAY OF CHAR ) ; VIRTUAL ;
  194. BEGIN
  195. (* notre tampon est un paragraphe particulier d'une taille
  196. de 32 Kilo-octets *)
  197. longueur := Str.Length(x);
  198. ALLOCATE(ptr_chaine,32*1024-1);
  199. Lib.Fill(ptr_chaine,32*1024-1,0);
  200. Lib.Move(ADR(x),ptr_chaine,longueur);
  201. END initialise_element;
  202. PROCEDURE insere ( x : ARRAY OF CHAR );
  203. VAR adresse1,adresse2 : ADDRESS;
  204. BEGIN
  205. adresse1 := Lib.AddAddr(ptr_chaine,position-1);
  206. IF position <= longueur THEN
  207. adresse2 := Lib.AddAddr(adresse1,Str.Length(x));
  208. Lib.Move(adresse1,adresse2,longueur-position+1);
  209. INC(longueur,Str.Length(x));
  210. Lib.Move(ADR(x),adresse1,Str.Length(x));
  211. ELSE
  212. adresse2 := Lib.AddAddr(ptr_chaine,longueur);
  213. Lib.Move(ADR(x),adresse1,Str.Length(x));
  214. Lib.Fill(adresse2,position-longueur-1,020H);
  215. longueur := position + Str.Length(x);
  216. END;
  217. END insere;
  218. PROCEDURE remplace ( x : ARRAY OF CHAR );
  219. VAR adresse : ADDRESS;
  220. BEGIN
  221. adresse := Lib.AddAddr(ptr_chaine,position-1);
  222. Lib.Move(ADR(x),adresse,Str.Length(x));
  223. END remplace;
  224. PROCEDURE detruit ( x : CARDINAL );
  225. VAR adresse1,adresse2 : ADDRESS;
  226. BEGIN
  227. adresse1 := Lib.AddAddr(ptr_chaine,position + x - 1);
  228. adresse2 := Lib.AddAddr(ptr_chaine,position-1);
  229. Lib.Move(adresse1,adresse2,longueur-position);
  230. adresse1 := Lib.AddAddr(ptr_chaine,longueur);
  231. Lib.Fill(adresse1,x,0);
  232. DEC(longueur,x);
  233. END detruit;
  234. END; (* CLASS t_tampon *)
  235. TYPE t_fenetre = RECORD
  236. x1,y1,x2,y2 : CARDINAL;
  237. posx,posy : CARDINAL;
  238. END;
  239. t_parametres = RECORD
  240. longueur_page : CARDINAL;
  241. largeur_ligne : CARDINAL;
  242. marge_haute : CARDINAL;
  243. marge_basse : CARDINAL;
  244. marge_droite : CARDINAL;
  245. marge_gauche : CARDINAL;
  246. largeur_tabulation : CARDINAL;
  247. entete : BOOLEAN;
  248. bas_de_page : BOOLEAN;
  249. END;
  250. TYPE t_ptr_t_paragraf= POINTER TO t_paragraf;
  251. t_paragraf = CLASS (t_paragraphe)
  252. PROCEDURE affiche_element(); VIRTUAL ;
  253. (*le texte contenu dans notre paragraphe est d‚j… justifi‚ *)
  254. TYPE l_ptr_tampon = POINTER TO l_t_tampon;
  255. l_t_tampon = ARRAY[0..32*1024-1] OF CHAR;
  256. VAR l_tampon : l_ptr_tampon;
  257. l_chaine : ARRAY[0..255] OF CHAR;
  258. i : CARDINAL;
  259. BEGIN
  260. NEW(l_tampon);
  261. Lib.Move(ptr_chaine,l_tampon,longueur);
  262. l_tampon^[longueur] := CHR(0);
  263. i := 0;
  264. LOOP
  265. Str.Slice(l_chaine,l_tampon^,i,30);
  266. IO.WrStr(l_chaine);
  267. IO.WrLn;
  268. IF Str.Length(l_chaine)< 30 THEN EXIT
  269. END;
  270. INC(i,30);
  271. END;
  272. DISPOSE(l_tampon);
  273. END affiche_element;
  274. END; (* class t_paragraf *)
  275. TYPE t_ptr_t_texte = POINTER TO t_texte;
  276. t_texte = CLASS (t_liste)
  277. tampon : t_tampon;
  278. fenetre : t_fenetre;
  279. parametres : t_parametres;
  280. PROCEDURE initialise_texte( VAR x : t_fenetre;
  281. VAR y : t_parametres);
  282. BEGIN
  283. creer;
  284. END initialise_texte;
  285. PROCEDURE affiche_element();VIRTUAL;
  286. BEGIN
  287. END affiche_element;
  288. PROCEDURE transfert_avant(); VIRTUAL;
  289. BEGIN
  290. END transfert_avant;
  291. PROCEDURE transfert_arriere(); VIRTUAL;
  292. BEGIN
  293. END transfert_arriere;
  294. PROCEDURE page_up(); VIRTUAL;
  295. BEGIN
  296. END page_up;
  297. PROCEDURE page_down(); VIRTUAL;
  298. BEGIN
  299. END page_down;
  300. PROCEDURE affiche_page(); VIRTUAL;
  301. BEGIN
  302. END affiche_page;
  303. END; (* CLASS t_texte *)
  304. PROCEDURE attente;
  305. BEGIN
  306. REPEAT UNTIL IO.KeyPressed();
  307. END attente;
  308. VAR ma_liste : t_liste;
  309. el : t_paragraf;
  310. VAR fichier : FIO.File;
  311. buffer : ARRAY[0..10000] OF CHAR;
  312. car : CHAR;
  313. PROCEDURE affiche;
  314. VAR g : t_ptr_t_paragraphe;
  315. BEGIN
  316. ma_liste.au_debut();
  317. REPEAT
  318. ma_liste.ptr_courant^.affiche_element();
  319. ma_liste.au_suivant();
  320. UNTIL ma_liste.suivant() = NIL;
  321. ma_liste.ptr_courant^.affiche_element();
  322. END affiche;
  323. PROCEDURE message (x : ARRAY OF CHAR);
  324. BEGIN
  325. Lib.Delay(5000);
  326. Window.Clear;
  327. IO.WrStr(x);
  328. IO.WrLn;
  329. END message;
  330. BEGIN (* principal *)
  331. (************** CREATION DE LA LISTE ****************)
  332. ma_liste.creer();
  333. (************** on remplit la liste *****************)
  334. message(" ON REMPLIT LA LISTE ");
  335. fichier := FIO.Open("c:\mii\mod.mod\texte1.MOD");
  336. FIO.EOF := FALSE;
  337. FIO.RdStr(fichier,buffer);
  338. WHILE NOT FIO.EOF DO
  339. el.initialise_element(buffer);
  340. ma_liste.ajouter(el);
  341. FIO.RdStr(fichier,buffer);
  342. END;
  343. (************ on parcoure la liste en avant **********)
  344. message(" ON parcoure LA LISTE en avant");
  345. affiche;
  346. attente;
  347. message(" Fin du programme");
  348. FIO.Close(fichier);
  349. END nanotext.