/*****************************************************
**
**  S-PRES Copyright (c) 2010
**
**  Joshua T Berryman
**
**  This file is part of S-PRES
**
**  S-PRES is Free software: you can redistribute it and/or modify
**  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation, either version 3 of the License, or
**  (at your option) any later version.
**
**  S-PRES is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program.  If not, see <http://www.gnu.org/licenses/>.
**
**  If you make use of this program for research 
**  resulting in a publication, please cite:
**
**  Joshua T Berryman and Tanja Schilling, 
**  Sampling rare events in nonequilibrium and nonstationary systems. 
**  
**  J Chem Phys, 133(24):244101, Dec 2010.
**
******************************************************/
#include <sys/types.h>
#include <dirent.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#include "../../spres/src/spres.h"




/** function to parse out the age of the final state of the shot */
long int spres::getFileAge( string fileName ) {

   long int age;

   age = atoi( extractShotnameField( fileName, 0 ).c_str() );
   
   return( age );
}

/** function to parse out the shot ID */
long int spres::getFileParentId( string fileName ) {
   return( atoi( extractShotnameField( fileName, 1 ).c_str() ) );
}

/** function to parse out the shot ID */
long int spres::getFileShotId( string fileName ) {
   return( atoi( extractShotnameField( fileName, 2 ).c_str() ) );
}

/** function to parse out the starting reaction coordinate of the shot */
int spres::getFileStartRcRow( string fileName, double *rc  ) {

   *rc =  atof( extractShotnameField( fileName, 3 ).c_str() );
   return(  assignRcBin( *rc ) );
}

/** function to parse out the ending reaction coordinate of the shot */
int spres::getFileEndRcRow( string fileName, double *rc ) {

   *rc =  atof( extractShotnameField( fileName, 4 ).c_str() );
   return(  assignRcBin( *rc ) );

}

/** validate that a file refers to a shot and is not eg '..' */
int spres::testShotFileName( string fileName ) {

   unsigned int    cutIndex, count;
   string           cutString, field;
   
   cutString.assign( fileName );

   count = 0;
   while( count < 5 ) {

    cutIndex     = cutString.find_first_of("_");
    cutString    = cutString.substr( cutIndex + 1, fileName.length() );
    count++;

    if( cutIndex > fileName.length() ){
        return( 0 );
    }
   
   }

   return( count );
}

/** extract a data field from the shot filename */
string spres::extractShotnameField( string fileName, int index ) {

    
   size_t          cutIndex;
   int             count;
   string           cutString, field;

//   cerr << "field " << index << " of " << fileName << " is: "; 

   count        = 0;
   cutIndex     = fileName.find_first_of("_");

   if( cutIndex == string::npos ) { //error condition
         cerr << "Rejecting file: " << fileName << endl;
         return( "-1" );
   }
   cutString    = fileName.substr( cutIndex + 1, fileName.length() );

   while( count <= index ){

        cutIndex     = cutString.find_first_of("_");

        field        = cutString.substr( 0, cutIndex );
        cutString    = cutString.substr( cutIndex + 1, fileName.length() );

        count++;

   }


   return( field );

}
























