home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / write.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-30  |  3.2 KB  |  119 lines

  1. /*
  2.     GWAda Development Environment for 386/486 PCs   
  3.     Copyright (C) 1993, Arthur Vargas Lopes  & Michael Bliss Feldman
  4.                         vlopes@vortex.ufrgs.br mfeldman@seas.gwu.edu
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; version 2 of the License.    
  9.  
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20.  
  21. /* write.c */
  22.  
  23. #include "externs.h"
  24.  
  25.  
  26.  
  27. void AVL_WRITE_OUT()
  28. {
  29.     AVL_EDIT_WINDOW_PTR w;
  30.     AVL_WIN_PTR m1, m2;
  31.     int n1, n2, x, from_here = 0, till_here = 0;
  32.     char fname[80];
  33.     static char *msg = "GWAda - Put to which file? ";
  34.     char msg2[80];
  35.     AVL_LINE_PTR temp, temp2 = NULL;
  36.     int fp, i;
  37.     int line_no = 0;
  38.     long fsize;
  39.     char *buf;
  40.     char line[1024];
  41.     unsigned int size, n, j;
  42.     if (avl_block_first_line == NULL ||
  43.         avl_block_last_line == NULL) {
  44.         AVL_ERROR("Mark a block first.");
  45.         return;
  46.         }
  47.     w = &avl_windows[avl_window];
  48.     n1 = 62;
  49.     n2 = (80 - n1) / 2;
  50.     m1 = AVL_MAKE_WINDOW(msg,7,n2,9,n1+n2,avl_wnd_bk_color,avl_wnd_color);
  51.     sprintf(fname,"%s%cblock",avl_dir_sources,'\\');
  52.     AVL_PROMPT(1,1,fname,60);
  53.     sprintf(msg2,"Writing \'%s\'", fname);
  54.     n1 = strlen(msg2);
  55.     if (n1 < 17) n1 = 17;
  56.     n1 += 5;
  57.     n2 = (80 - n1) / 2;
  58.     m2 = AVL_MAKE_WINDOW("",23,1,25,strlen(msg2)+3,avl_wnd_bk_color,avl_wnd_color);
  59.     _setbkcolor(avl_msg_bk_color);
  60.     _settextcolor(avl_msg_color);
  61.     _settextposition(1,1);
  62.     _outtext(msg2);
  63.     unlink(fname);       
  64.     fp = open(fname,O_CREAT | O_RDWR,S_IREAD | S_IWRITE);
  65.     if (fp == -1)  {
  66.         sprintf(msg2,"Could not open \'%s\'\n", fname);
  67.         AVL_ERROR(msg2);
  68.         AVL_DEL_WINDOW(m2);
  69.         AVL_DEL_WINDOW(m1);
  70.         return;
  71.         }
  72.     size = 100000L;
  73.     if ((buf = malloc(size)) == NULL)  { 
  74.         AVL_ERROR("Out of memory!");
  75.         AVL_DEL_WINDOW(m2);
  76.         AVL_DEL_WINDOW(m1);
  77.         return;
  78.         }
  79.     /* FIll in buffer */
  80.     j = 0;
  81.     temp = avl_block_first_line;
  82.     from_here = avl_block_first_col;
  83.     while ( 1 )  {
  84.         if (j >= size)  {  /*  flush buffer to disk  */
  85.             n = write(fp,buf,size);
  86.             if (n != size) {
  87.                 sprintf(msg2,"Could not write \'%s\'\n", fname);
  88.                 AVL_ERROR(msg2);
  89.                 AVL_DEL_WINDOW(m2);
  90.                 AVL_DEL_WINDOW(m1);
  91.                 return;
  92.                 }
  93.             fsize -= n;
  94.             j = 0;
  95.             }
  96.         if (temp == avl_block_last_line)
  97.             till_here = avl_block_last_col + 1;
  98.         else
  99.             till_here = strlen(temp -> line);
  100.         for(i = 0 + from_here; i < till_here; ++i)
  101.             buf[j++] = temp -> line[i];
  102.         if (temp -> next != avl_block_first_line)
  103.             buf[j++] = '\n';
  104.         else
  105.             break;
  106.         temp = temp -> next;
  107.         } 
  108.     n = write(fp,buf,j);
  109.     if (n != j) {
  110.         sprintf(msg2,"Could not write \'%s\'\n", fname);
  111.         AVL_ERROR(msg2);
  112.         }
  113.     else {
  114.         close(fp);
  115.         free(buf);
  116.         }
  117.     AVL_DEL_WINDOW(m2);
  118.     AVL_DEL_WINDOW(m1);
  119. }
  120.