• Main Page
  • Classes
  • Files
  • File List
  • File Members

ath_files.c

Go to the documentation of this file.
00001 #include "copyright.h"
00002 /*============================================================================*/
00003 /*! \file ath_files.c
00004  *  \brief Function for creating descriptive output filenames.
00005  *
00006  * PURPOSE: Function for creating descriptive output filenames with form:
00007  *  -     [path]<basename>[-lev#][-dom#][.idump][.id].<ext>
00008  *
00009  *   where 
00010  *     -   path     = optional path
00011  *     -   basename = basename of file (usually problem name, e.g. "Sod")
00012  *     -   lev#     = level number of dump, only included when level > 0
00013  *     -   dom#     = domain number of dump, only included when domain != 0
00014  *     -   dlen     = number of digits to use for numeric extension (1..10)
00015  *     -   idump    = optional dump number (0,1,2,.....)
00016  *                    if(dlen > 0 and idump >= 0) we use the dump number
00017  *                    <idump> uses C-format descriptor "%0<dlen>d"
00018  *     -   id       = optional additional identifier set in <input> block
00019  *     -   ext      = file extension, e.g. ".tab", ".bin", ".dx", ".vtk"
00020  *
00021  * CONTAINS PUBLIC FUNCTIONS: 
00022  *   ath_fname()                                                              */
00023 /*============================================================================*/
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include "prototypes.h"
00028 
00029 /*----------------------------------------------------------------------------*/
00030 /*! \fn char *ath_fname(const char *path, const char *basename,
00031  *                      const char *levstr, const char *domstr, 
00032  *                      const int dlen, const int idump,
00033  *                      const char *id, const char *ext)
00034  *  \brief Creates descriptive output filenames.
00035  *
00036  *   Creates filenames with form 
00037  *    - [path]<basename>[-lev#][-dom#][.idump][.id].<ext>
00038  *
00039  *   Used by most of the dump_*() and output_*() functions.
00040  */
00041 char *ath_fname(const char *path, const char *basename,
00042                 const char *levstr, const char *domstr,
00043                 const int dlen, const int idump,
00044                 const char *id, const char *ext)
00045 {
00046   char fmt[80];
00047   size_t size,slen=0;
00048   char *cp, *fname;
00049 
00050 /* 2 = "." following the basename + NULL terminator */
00051   size = 2 + strlen(basename) + strlen(ext);
00052   if(path != NULL) size += 1 + strlen(path);        /* add 1 for possible '/' */
00053   if(levstr != NULL) size += 1 + strlen(levstr);    /* add 1 for the "-" */
00054   if(domstr != NULL) size += 1 + strlen(domstr);    /* add 1 for the "-" */
00055   if(dlen > 0) size += (dlen > 10 ? dlen : 10) + 1; /* add 1 for the "." */
00056   if(id != NULL) size += 1 + strlen(id);            /* add 1 for the "." */
00057 
00058   if((fname = (char*)malloc(size*sizeof(char))) == NULL){
00059     printf("[ath_fname]: malloc returned a NULL pointer\n");
00060     return NULL;
00061   }
00062 
00063 /* Build the filename. Start with the optional path */
00064 
00065   cp = fname;
00066   if(path != NULL){
00067     slen = strlen(path);
00068     strcpy(cp, path);
00069     cp = &(cp[slen]); /* point cp at the '\0' terminator */
00070 
00071     /* Append a '/' if necessary */
00072     if(cp[-1] != '/'){
00073       cp[0] = '/';
00074       cp[1] = '\0';
00075       cp = &(cp[1]);
00076     }
00077   }
00078 
00079 /* Append the basename of the file */
00080 
00081   slen = strlen(basename);
00082   strcpy(cp,basename);
00083   cp = &(cp[slen]); /* point cp at the '\0' terminator */
00084 
00085 /* Append the optional level number, preceded by '-' */
00086 
00087   if (levstr != NULL){
00088     slen = strlen(levstr);
00089     cp[0] = '-';
00090     cp[1] = '\0';
00091     cp = &(cp[1]);
00092     strcpy(cp,levstr);
00093     cp = &(cp[slen]); /* point cp at the '\0' terminator */
00094   }
00095 
00096 /* Append the optional domain number, preceded by '-' */
00097 
00098   if (domstr != NULL){
00099     slen = strlen(domstr);
00100     cp[0] = '-';
00101     cp[1] = '\0';
00102     cp = &(cp[1]);
00103     strcpy(cp,domstr);
00104     cp = &(cp[slen]); /* point cp at the '\0' terminator */
00105   }
00106 
00107 /* Append the optional dump number, id string, and extension */
00108 
00109 /* id not NULL */
00110   if(id){
00111     if(dlen > 0 && idump >= 0){
00112       sprintf(fmt,".%%0%dd.%%s.%%s",dlen);
00113       sprintf(cp,fmt,idump,id,ext);
00114     }
00115     else sprintf(cp,".%s.%s",id,ext);
00116   }
00117 /* id NULL */
00118   else{
00119     if(dlen > 0 && idump >= 0){
00120       sprintf(fmt,".%%0%dd.%%s",dlen);
00121       sprintf(cp,fmt,idump,ext);
00122     }
00123     else sprintf(cp,".%s",ext);
00124   }
00125 
00126   return fname;
00127 }

Generated on Mon Sep 27 2010 23:03:06 for Athena by  doxygen 1.7.1