rio.c

Go to the documentation of this file.
00001 /*
00002 %M This module contains the funtions to manipulate the input and output
00003    for the Relax program.
00004 %a Joao Luiz Elias Campos.
00005 %d September 2nd, 1998.
00006 %r $Id: rio.c,v 1.1 2004/06/22 05:29:59 joaoluiz Exp $
00007 %w (C) COPYRIGHT 1995-1996, Eduardo Nobre Lages.
00008    (C) COPYRIGHT 1997-1998, Joao Luiz Elias Campos.
00009    All Rights Reserved
00010    Duplication of this program or any part thereof without the express
00011    written consent of the author is prohibited.
00012  *
00013  *   Modified:    28-Apr-2005    Alexandre A. Del Savio
00014  *     Foram substituídas todas as alocações dinâmicas feitas com malloc 
00015  *     por calloc.
00016  *
00017  *   Modified:    13-Sep-2005    Alexandre A. Del Savio
00018  *     Criada a funcao IoReset.
00019  *
00020  *   Modified:    13-Jan-2006    Juan Pablo Ibañez
00021  *     Modificada a chamada da funcão SllRmvTop para RSllRmvTop, e
00022  *     SllAddEnd para RSllAddEnd. Modificado o tipo Sll para RSll
00023  *     nessas funcões.    
00024  *
00025  *   Modified:    22-Fev-2006    Juan Pablo Ibañez   
00026  *     Modificadas as funcões IoReadFile, IoOpenFiles, IoFindResultTag, 
00027  *     _IoCopyInputFile, IoWriteFile, que passam a retornar 0 ou 1 
00028  *     em caso de falha ou sucesso respectivamente.
00029  *
00030  *   Modified:    Jul-2007    André Luis Muller
00031  *     Modificado para BUFSIZ o "tamanho" dos arquivos.
00032  *
00033  */
00034 
00035 /*
00036 ** ------------------------------------------------------------------------
00037 ** Global variables and symbols:
00038 */
00039 #include <stdio.h>
00040 #include <stdlib.h>
00041 #include <string.h>
00042 #ifdef WIN32
00043 # include <sys\stat.h>
00044 #else
00045 # include <sys/stat.h>
00046 #endif
00047 
00048 #include "rio.h"
00049 #include "nfi.h"
00050 #include "sll.h"
00051 
00052 /*
00053 %V Handle to the input, output, config and temporary files
00054 */
00055 FILE *nf   = 0L;
00056 FILE *pos  = 0L;
00057 FILE *cfg  = 0L;
00058 FILE *ndlr = 0L;
00059 FILE *elmr = 0L;
00060 
00061 
00062 /*
00063 ** ------------------------------------------------------------------------
00064 ** Local variables and symbols:
00065 */
00066 
00067 /*
00068 %T Temp. file tag descriptor
00069 */
00070 typedef struct _tmptag
00071 {
00072  struct _tmptag *next;
00073  int  id;
00074  long ndlrpos;
00075  long elmrpos;
00076 } sTmpFileTags;
00077 
00078 
00079 /*
00080 %V Input, output, config and temporary files names
00081 */
00082 static char arqnf[BUFSIZ];
00083 static char arqpos[BUFSIZ];
00084 static char arqdat[BUFSIZ];
00085 static char arqcfg[BUFSIZ];
00086 static char arqndlr[BUFSIZ];
00087 static char arqelmr[BUFSIZ];
00088 
00089 
00090 /*
00091 %V Total number of result tags
00092 */
00093 static int _iNumFileTags = 0;
00094 
00095 
00096 /*
00097 %V Temp. file tags list
00098 */
00099 static sTmpFileTags *TmpFileTag = 0L;
00100 
00101 
00102 /*
00103 ** ------------------------------------------------------------------------
00104 ** Local functions:
00105 */
00106 static void _IoInitScreen     ( void );
00107 static int  _IoCopyInputFile  ( void );
00108 static void _IoRemoveTempFiles( void );
00109 
00110 
00111 /*
00112 %F This function display the intial screen.
00113 */
00114 static void _IoInitScreen( void )
00115 {
00116 
00117  printf( "\n\n\n\n\n\n\n\n" );
00118 #if 0
00119  printf( "\t====================================================\n\n" );
00120  printf( "\t PONTIFICIA UNIVERSIDADE CATOLICA DO RIO DE JANEIRO \n" );
00121  printf( "\t          DEPARTMENT OF CIVIL ENGINEERING           \n" );
00122  printf( "\t                GEOTECHNICAL DIVISION               \n" );
00123  printf( "\t                      R E L A X                     \n" );
00124  printf( "\t   FINITE ELEMENT METHOD - Dynamic relaxation alg.  \n\n" );
00125  printf( "\t                   Version: 2.9.94C - February/2001 \n" );
00126  printf( "\t                   Version: 3.0.00A - August/2005   \n" );
00127  printf( "\t====================================================\n\n" );
00128 #endif
00129  printf( "\tEnter the input file name [.nf|.dat]: " );
00130 
00131 } /* End of _IoInitScreem */
00132 
00133 
00134 /*
00135 %F This function make a copy of the input file in the output file.
00136 */
00137 static int _IoCopyInputFile( void )
00138 {
00139  char s[BUFSIZ+1], *c;
00140 
00141 /* Rewind input file
00142  */
00143  rewind( nf );
00144 
00145 /* Copy input file in output file
00146  */
00147  c = fgets( s, BUFSIZ+1, nf );
00148  while( (strncmp( s, "%END", 4 ) != 0) && (c != 0L) )
00149  {
00150   fprintf( pos, "%s", s );
00151   c = fgets( s, BUFSIZ+1, nf );
00152  }
00153 
00154  if( c == 0L )
00155  {
00156   printf( "\n\nNo END label was found in input file.\n\n" );
00157   return 0;
00158  }
00159 
00160  rewind( nf );
00161 
00162  return 1;
00163 
00164 } /* End of _IoCopyInputFile */
00165 
00166 
00167 /*
00168 %F
00169 */
00170 static void _IoRemoveTempFiles( void )
00171 {
00172  sTmpFileTags *item = 0L;
00173 
00174 /* Remove temporary files
00175  */
00176  remove( arqndlr );
00177  remove( arqelmr );
00178  
00179 /* Release temp. file tags list
00180  */
00181  while( TmpFileTag != 0L )
00182  {
00183   item = TmpFileTag;
00184   TmpFileTag = (sTmpFileTags *)RSllRmvTop( (RSll *)item );
00185   free( item );
00186  }
00187 
00188 } /* End of _IoRemoveTempFiles */
00189 
00190 
00191 /*
00192 ** ------------------------------------------------------------------------
00193 ** Public functions:
00194 */
00195 
00196 
00197 /*
00198 %F This function opens the input/output files that used by the Relax 
00199    program.
00200 %i Number of arguments in the command line.
00201 %i Arguments values in the command line.
00202 */
00203 int IoOpenFiles( int argc, char *argv[] )
00204 {
00205 /* Display the program initial screen
00206  */
00207  _IoInitScreen( );
00208 
00209 /* Get the input file name
00210  */
00211  if( argc > 1 )
00212  {
00213   strcpy( arqnf, argv[argc-1] );
00214   printf( "%s\n\n\n", arqnf );
00215  }
00216  else
00217  {
00218   fscanf( stdin, "%s", arqnf );
00219   printf( "\n\n" );
00220  }
00221  
00222 /* Define the output, the data and the temporary files
00223  */
00224  strcpy( arqpos, arqnf );
00225  strcpy( arqdat, arqnf );
00226  strcpy( arqndlr, arqnf );
00227  strcpy( arqelmr, arqnf );
00228 
00229 /* Set the defaults extensions
00230  */
00231  strcat( arqnf, ".nf" );
00232  strcat( arqdat, ".dat" );
00233  strcat( arqpos, ".pos" );
00234  strcat( arqndlr, "_1.tmp" );
00235  strcat( arqelmr, "_2.tmp" );
00236 
00237 /* Try to open a file with the Neutral File extension. If the file does not 
00238  * exists, try to open a file with the Data file extension.
00239  */
00240  if( (nf = fopen( arqnf, "r" )) == 0L )
00241  {
00242   if( (nf = fopen( arqdat, "r" )) == 0L )
00243   {
00244    printf( "\n\n\tCould not open the given input file.\n\n" );
00245    return 0;
00246   }
00247  }
00248 
00249 /* Open the output file
00250  */
00251  if( (pos = fopen( arqpos, "w" )) == 0L )
00252  {
00253   printf( "\n\n\tCould not open the given output file.\n\n" );
00254   return 0;
00255  }
00256 
00257 /* Make a copy of the input file
00258  */
00259  if( !_IoCopyInputFile( ) ) return 0;
00260  
00261  return 1;
00262 
00263 } /* End of IoOpenFiles */
00264 
00265 
00266 /*
00267 %F This function closes all opened files.
00268 */
00269 void IoCloseFiles( void )
00270 {
00271 /* Close input and output files
00272  */
00273  fclose( nf );
00274  fclose( pos );
00275 
00276 } /* End of IoCloseFiles */
00277 
00278 
00279 /*
00280 %F This function reads the input file. This function sets the current
00281    input file for the Neutral File interpreter module and call it to 
00282    interprete the input file.
00283 */
00284 int IoReadFile( void )
00285 {
00286 /* Show feedback
00287  */
00288  printf( "\tReading input file..................." );
00289  fflush( stdin );
00290 
00291 /* Set the input file
00292  */ 
00293  NfiSetFileHandle( nf );
00294 
00295 /* Interprete the input file
00296  */
00297  if ( !NfiInterpFile() ) return 0;
00298 
00299 /* Check to see if the configuration parametere was read from the 
00300  * Neutral File. If not, search for the relax config file.
00301  */
00302  if( !NfiCheckParameters() ) {
00303  /* Check to see if the config exists in the current directory.
00304   */
00305   struct stat statres;
00306   if( stat( "relax.cfg", &statres ) == -1 ) {
00307    char *apldir;
00308    if( (apldir = getenv( "TECTOSDIR" )) != 0L )
00309 #ifdef WIN32
00310     sprintf( arqcfg, "%s\\%s", apldir, "relax.cfg" );
00311 #else
00312     sprintf( arqcfg, "%s/%s", apldir, "relax.cfg" );
00313 #endif
00314    else
00315     strcpy( arqcfg, "relax.cfg" );
00316   }
00317   else {
00318    strcpy( arqcfg, "relax.cfg" );
00319   }
00320 
00321  /* Open the config file
00322   */
00323   if( (cfg = fopen( arqcfg, "r" )) == 0L ) {
00324    printf( "\n\n\tCould not open the given config file.\n\n" );
00325    return 0;
00326   }
00327   NfiSetFileHandle( cfg );
00328   if( !NfiReadConfigFile() ) return 0;
00329 
00330  /* Close config file
00331   */
00332   fclose( cfg );
00333  }
00334 
00335 /* Show feedback
00336  */
00337  printf( "\n\n\n" );
00338 
00339  return 1;
00340 
00341 } /* End of IoReadFile */
00342 
00343 
00344 /*
00345 %F This function writes the output file. This function sets the current 
00346    file handle for the Neutral File interpreter module and call it to 
00347    write the results.
00348 */
00349 int IoWriteFile( void )
00350 {
00351 /* Set the output file
00352  */
00353  NfiSetFileHandle( pos );
00354 
00355 /* Write the results
00356  */
00357    
00358  if( !NfiWriteResults( ) ) return 0;
00359 
00360 /* Close temp. files
00361  */
00362  _IoRemoveTempFiles( );
00363 
00364  return 1;
00365 
00366 } /* End of IoWriteFile */
00367 
00368 
00369 /*
00370 %F This function open a temp. file to save the results.
00371 */
00372 int IoStartSave( void )
00373 {
00374  sTmpFileTags *item = 0L;
00375 
00376 /* Create a temp. file tag item
00377  */
00378  item = (sTmpFileTags *)calloc(1, sizeof(sTmpFileTags));
00379  
00380 /* Create a temp. file tag object
00381  */
00382  item->id      = _iNumFileTags++;
00383  item->ndlrpos = 0L;
00384  item->elmrpos = 0L;
00385  item->next    = 0L;
00386  
00387 /* Open temp. files
00388  */
00389  if( (ndlr == 0L) && (elmr == 0L) )
00390  {
00391   ndlr = fopen( arqndlr, "w+b" );
00392   elmr = fopen( arqelmr, "w+b" );
00393   if( (ndlr == 0L) || (elmr == 0L) )
00394   {
00395    printf( "\n\n\tCould not open a temporary result file.\n\n" );
00396    return 0;
00397   }
00398  }
00399 
00400 /* Get current tag position
00401  */
00402  item->ndlrpos = ftell( ndlr );
00403  item->elmrpos = ftell( elmr );
00404 
00405 /* Add item on list
00406  */
00407  TmpFileTag = (sTmpFileTags *)RSllAddEnd( (RSll *)TmpFileTag, (RSll *)item );
00408 
00409    return 1;
00410 
00411 } /* End of IoStartSave */
00412 
00413 
00414 /*
00415 %F This function ends the temp. file save. The function close the opened temp.
00416    file.
00417 */
00418 void IoEndSave( void )
00419 {
00420 /* Close the opende temp. file
00421  */
00422  if( ndlr != 0L )
00423   fclose( ndlr );
00424 
00425  if( elmr != 0L )
00426   fclose( elmr );
00427 
00428 } /* End of IoEndSave */
00429 
00430 
00431 /*
00432 %F This funtion finds the result tag in the temp. file.
00433 */
00434 int IoFindResultTag( int id, long *ndlrpos, long *elmrpos )
00435 {
00436  sTmpFileTags *item = 0L;
00437 
00438 /* Find the tag by the id
00439  */
00440  item = TmpFileTag;
00441  while( (item != 0L) && (item->id != id) )
00442   item = item->next;
00443 
00444  if( item == 0L )
00445  {
00446   printf( "\n\n\tCould not find a result file tag.\n\n" );
00447   return 0;
00448  }
00449 
00450 /* Get tag and set its position
00451  */
00452  (*ndlrpos) = item->ndlrpos;
00453  (*elmrpos) = item->elmrpos;
00454 
00455  fseek( ndlr, item->ndlrpos, SEEK_SET );
00456  fseek( elmr, item->elmrpos, SEEK_SET );
00457  
00458  return 1;
00459  
00460 } /* End of IoFindResultTag */
00461 
00462 
00463 /*
00464 %F This function closes the temp. file
00465 */
00466 void IoCloseTempFile( void )
00467 {
00468 /* Close opened files
00469  */
00470  IoEndSave( );
00471 
00472 } /* End of IoCloseTempFile */
00473 
00474 
00475 /*
00476 %F This function reset this packtage.
00477 */
00478 void IoReset( void )
00479 {
00480   _iNumFileTags = 0;
00481   TmpFileTag    = 0L;
00482   nf   = 0L;
00483   pos  = 0L;
00484   cfg  = 0L;
00485   ndlr = 0L;
00486   elmr = 0L;
00487 
00488 } /* End of IoReset */
00489 
00490 
00491 /* =======================================================  End of File  */
00492 

Generated on Tue Oct 23 11:23:30 2007 for Relax by  doxygen 1.5.3