home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / ftes46b5.zip / ftes46b5 / src / e_print.cpp < prev    next >
C/C++ Source or Header  |  1997-08-25  |  5KB  |  173 lines

  1. /*    e_print.cpp
  2.  *
  3.  *    Copyright (c) 1994-1996, Marko Macek
  4.  *
  5.  *    You may distribute under the terms of either the GNU General Public
  6.  *    License or the Artistic License, as specified in the README file.
  7.  *
  8.  */
  9.  
  10. #include "fte.h"
  11.  
  12. int EBuffer::BlockPrint() {
  13.     static char cr = 13;
  14.     static char lf = 10;
  15.     EPoint B, E;
  16.     int L;
  17.     int A, Z;
  18.     PELine LL;
  19.     FILE *fp;
  20.     int bc = 0, lc = 0;
  21.     
  22.     AutoExtend = 0;
  23.     if (CheckBlock() == 0) return 0;
  24.     if (RCount == 0) return 0;
  25.     B = BB;
  26.     E = BE;
  27.     Msg(INFO, "Printing to %s...", PrintDevice);
  28.     if (PrintDevice[0] == '|')
  29.         fp = popen(PrintDevice + 1, "w");
  30.     else
  31.         fp = fopen(PrintDevice, "w");
  32.     if (fp == NULL) goto erroropen;
  33.     for (L = B.Row; L <= E.Row; L++) {
  34.         A = -1;
  35.         Z = -1;
  36.         LL = RLine(L);
  37.         switch (BlockMode) {
  38.           case bmLine:
  39.             if (L < E.Row) {
  40.                 A = 0;
  41.                 Z = LL->Count;
  42.             }
  43.             break;
  44.           case bmColumn:
  45.             if (L < E.Row) {
  46.                 A = CharOffset(LL, B.Col);
  47.                 Z = CharOffset(LL, E.Col);
  48.             }
  49.             break;
  50.           case bmStream:
  51.             if (B.Row == E.Row) {
  52.                 A = CharOffset(LL, B.Col);
  53.                 Z = CharOffset(LL, E.Col);
  54.             } else if (L == B.Row) {
  55.                 A = CharOffset(LL, B.Col);
  56.                 Z = LL->Count;
  57.             } else if (L < E.Row) {
  58.                 A = 0;
  59.                 Z = LL->Count;
  60.             } else if (L == E.Row) {
  61.                 A = 0;
  62.                 Z = CharOffset(LL, E.Col);
  63.             }
  64.             break;
  65.         }
  66.         if (A != -1 && Z != -1) {
  67.             if (A < LL->Count) {
  68.                 if (Z > LL->Count)
  69.                     Z = LL->Count;
  70.                 if (Z > A) {
  71.                     if (fwrite(LL->Chars + A, 1, Z - A, fp) != Z - A) {
  72.                         goto error;
  73.                     } else
  74.                         bc += Z - A;
  75.                 }
  76.             }
  77.             if (BFI(this, BFI_AddCR) == 1)
  78.                 if (fwrite(&cr, 1, 1, fp) != 1)
  79.                     goto error;
  80.                 else
  81.                     bc++;
  82.             if (BFI(this, BFI_AddLF) == 1)
  83.                 if (fwrite(&lf, 1, 1, fp) != 1)
  84.                     goto error;
  85.                 else {
  86.                     bc++;
  87.                     lc++;
  88.                 }
  89.             if ((lc % 200) == 0)
  90.                 Msg(INFO, "Printing, %d lines, %d bytes.", lc, bc);
  91.  
  92.         }
  93.     }
  94.     fwrite("\f\n", 2, 1, fp);
  95.     if (PrintDevice[0] == '|')
  96.         pclose(fp);
  97.     else
  98.         fclose(fp);
  99.     Msg(INFO, "Printing %d lines, %d bytes.", lc, bc);
  100.     return 1;
  101. error:
  102.     if (PrintDevice[0] == '|')
  103.         pclose(fp);
  104.     else
  105.         fclose(fp);
  106. erroropen:
  107.     Msg(INFO, "Failed to write to %s", PrintDevice);
  108.     return 0;
  109. }
  110.  
  111.  
  112. int EBuffer::FilePrint() {
  113.     static char cr = 13;
  114.     static char lf = 10;
  115.     int l;
  116.     FILE *fp;
  117.     unsigned long ByteCount = 0;
  118.     int BChars;
  119.   
  120.     Msg(INFO, "Printing %s to %s...", FileName, PrintDevice);
  121.     if (PrintDevice[0] == '|')
  122.         fp = popen(PrintDevice + 1, "w");
  123.     else
  124.         fp = fopen(PrintDevice, "w");
  125.     if (fp == NULL) goto erroropen;
  126.     BChars = 0;
  127.     for (l = 0; l < RCount; l++) {
  128.         if ((int) sizeof(FileBuffer) - (BChars + 2) < RLine(l)->Count) {
  129.             if (BChars) {
  130.                 ByteCount += BChars;
  131.                 Msg(INFO, "Printing: %d lines, %d bytes.", l, ByteCount);
  132.                 if (fwrite(FileBuffer, 1, BChars, fp) != BChars) goto fail;
  133.                 BChars = 0;
  134.             }
  135.         }
  136.         if (RLine(l)->Count > sizeof(FileBuffer) - 2) {
  137.             assert(BChars == 0);
  138.             ByteCount += RLine(l)->Count;
  139.             Msg(INFO, "Printing: %d lines, %d bytes.", l, ByteCount);
  140.             if (fwrite(RLine(l)->Chars, 1, RLine(l)->Count, fp) != RLine(l)->Count) goto fail;
  141.         } else {
  142.             memcpy(FileBuffer + BChars, RLine(l)->Chars, RLine(l)->Count);
  143.             BChars += RLine(l)->Count;
  144.         }
  145.         if ((l < RCount - 1) || BFI(this, BFI_ForceNewLine)) {
  146.             assert(sizeof(FileBuffer) >= BChars + 2);
  147.             if (BFI(this, BFI_AddCR) == 1) FileBuffer[BChars++] = cr;
  148.             if (BFI(this, BFI_AddLF) == 1) FileBuffer[BChars++] = lf;
  149.         }
  150.     }
  151.     if (BChars) {
  152.         ByteCount += BChars;
  153.         Msg(INFO, "Printing: %d lines, %d bytes.", l, ByteCount);
  154.         if (fwrite(FileBuffer, 1, BChars, fp) != BChars) goto fail;
  155.     }
  156.     BChars = 0;
  157.     if (PrintDevice[0] == '|')
  158.         pclose(fp);
  159.     else
  160.         fclose(fp);
  161.     Msg(INFO, "Printed %s.", FileName);
  162.     return 1;
  163. fail:
  164.     if (PrintDevice[0] == '|')
  165.         pclose(fp);
  166.     else
  167.         fclose(fp);
  168.     return 0;
  169. erroropen:
  170.     Msg(ERROR, "Error printing %s to %s.", FileName, PrintDevice);
  171.     return 0;
  172. }
  173.