home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume8 / graph+ / part03 / saveload.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-03-01  |  2.1 KB  |  98 lines

  1. /*
  2.  * Copyright (C) 1986   Alan Kent
  3.  *
  4.  * Permission is granted to freely distribute part or
  5.  * all of this code as long as it is not for profit
  6.  * and this message is retained in the code.
  7.  *
  8.  * No resposibility is taken for any damage or incorect
  9.  * results this program generates.
  10.  * 
  11.  */
  12.  
  13.  
  14. #include <stdio.h>
  15. #include "graph.h"
  16.  
  17.  
  18. extern struct table_st *new_table ();
  19.  
  20.  
  21. save_table ( filename , table )
  22. char *filename;
  23. table_st *table;
  24. {
  25.     table_st *p;
  26.     int i;
  27.     int cols;
  28.     FILE *fp;
  29.  
  30.  
  31.     if ( filename == NULL ) {
  32.     fp = stdout;
  33.     filename = "stdout";
  34.     }
  35.     else {
  36.     fp = fopen ( filename , "w" );
  37.     if ( fp == NULL )
  38.         abort ( "failed to open save file '%s'" , filename );
  39.     }
  40.     if ( table == NULL ) {
  41.     outint ( fp , 0 , filename );    /* cols */
  42.     outint ( fp , 0 , filename );    /* rows */
  43.     }
  44.     else {
  45.     cols = num_cols ( table );
  46.     outint ( fp , cols );
  47.     outint ( fp , table->size );
  48.     for ( p = table; p != NULL; p = p->next )
  49.         if ( fwrite ( p->data , sizeof ( double ) , p->size , fp ) != p->size )
  50.         abort ( "Write error on '%s'" , filename );
  51.     }
  52.     if ( fp != stdout )
  53.     fclose ( fp );
  54. }
  55.  
  56.  
  57. outint ( fp , val , filename )
  58. FILE *fp;
  59. int val;
  60. char *filename;
  61. {
  62.     if ( fwrite ( &val , sizeof ( val ) , 1 , fp ) != 1 )
  63.     abort ( "Error when writing to '%s'" , filename );
  64. }
  65.  
  66.  
  67.  
  68. struct table_st *
  69. load_table ( filename )
  70. char *filename;
  71. {
  72.     FILE *fp;
  73.     struct table_st *p , *tab;
  74.     int cols , rows;
  75.  
  76.     if ( filename == NULL ) {
  77.     fp = stdin;
  78.     filename = "stdin";
  79.     }
  80.     else {
  81.     fp = fopen ( filename , "r" );
  82.     if ( fp == NULL )
  83.         abort ( "failed to open file '%s' to load it" , filename );
  84.     }
  85.     if ( fread ( &cols , sizeof ( int ) , 1 , fp ) != 1 )
  86.     abort ( "Error when reading '%s'" , filename );
  87.     if ( fread ( &rows , sizeof ( int ) , 1 , fp ) != 1 )
  88.     abort ( "Error when reading '%s'" , filename );
  89.     tab = new_table ( cols , rows );
  90.     for ( p = tab; p != NULL; p = p->next ) {
  91.     if ( fread ( p->data , sizeof ( double ) , p->size , fp ) != p->size )
  92.         abort ( "Read error on '%s' when loading file" , filename );
  93.     }
  94.     if ( fp != stdin )
  95.     fclose ( fp );
  96.     return ( tab );
  97. }
  98.