Exemple #1
0
/**
   Composes the name of an input file, based on target

   Takes a data directory and an executable name, and tries to open an input 
   file based on these variables.  If a file is found in neither of two 
   locattions derived from these variables, this method tries to fall back on 
   /dev/null.

   Source file locations:
     - [data_dir]/manual/in/[target].in
     - [data_dir]/tutorial/in/[target].in
     - /dev/null

   @param data_dir the path of the reference data directory
   @param target the name of executable being run
   @returns the name of the file
*/
char*
input_name (const char* data_dir, const char* target)
{
    char* fname = 0;
    int stat_result = 0;
    struct stat sb;

    assert (0 != target);

    if (data_dir && *data_dir) {

        /* Try data_dir/manual/in/target.in */
        fname       = reference_name (data_dir, "manual", "in");
        stat_result = stat (fname, &sb);
    
        if (0 == stat_result)
            return fname;

        free (fname);

        /* Try data_dir/tutorial/in/target.in */
        fname       = reference_name (data_dir, "tutorial", "in");
        stat_result = stat (fname, &sb);

        if (0 == stat_result)
            return fname;

        free (fname);
    }

    /* If we didn't find a source file, use /dev/null */
    fname = (char*)RW_MALLOC (sizeof DEV_NULL);
    strcpy (fname, DEV_NULL);
    return fname;
}
Exemple #2
0
std::string record_to_string(const bam_hdr_t* header, const bam1_t* record) {
    std::string reference_name(record->core.tid >= 0 ? header->target_name[record->core.tid] : "*");
    std::string mate_reference_name(record->core.mtid >= 0 ? header->target_name[record->core.mtid] : "*");

    std::stringstream s;
    s << get_qname(record)
      << "\t" << record->core.flag
      << "\t" << reference_name
      << "\t" << record->core.pos + 1
      << "\t" << mate_reference_name
      << "\t" << record->core.mpos + 1
      << "\t" << record->core.isize;
    return s.str();
}
Exemple #3
0
/**
   Parses output file out_name for the example target_name.
   
   This method tries to compare out_name against a reference output file and
   reports the result of the comparison.  If the comparison fails to result in
   a match, status->status is set to the appropriate error state.  The 
   reference file name is generated by the reference_name function.

   Reference file locations:
     - [data_dir]/manual/out/[target_name].out
     - [data_dir]/tutorial/out/[target_name].out

   @todo add logic to handle differing line endings (CR vs LF vs CRLF)

   @param out_name the name of the output file
   @param fout pointer to file structure for output file being parsed
   @param status status object to record results in.
   @see reference_name
*/
static void
check_example (const char* const data_dir, char* const out_name, FILE* fout, 
               struct target_status* status)
{
    struct stat file_info;
    char* ref_name;

    FILE* fref;   /* reference file (expected output) */

    assert (0 != out_name);
    assert (0 != fout);
    assert (0 != status);

    /* Mark as an example by setting assertions to -1 */
    status->assert = (unsigned)-1;

    /* Try data_dir/manual/out/target_name.out */
    ref_name = reference_name (data_dir, "manual", "out");

    if (0 > stat (ref_name, &file_info)) {
        if (ENOENT != errno) {
            warn ("stat (%s) error: %s\n", exe_name, ref_name, 
                  strerror (errno));
            status->status = ST_BAD_REF;
            free (ref_name);
            return;
        }
                        
        /* If that doesn't exist, try 
           data_dir/tutorial/out/target_name.out */
        free (ref_name);
        ref_name = reference_name (data_dir, "tutorial", "out");

        if (0 > stat (ref_name, &file_info)) {
            if (ENOENT != errno) {
                warn ("stat (%s) error: %s\n", exe_name, ref_name, 
                      strerror (errno));
                status->status = ST_BAD_REF;
            }
            else
                status->status = ST_NO_REF;

            free (ref_name);
            return;
        }
    }

    fref = fopen (ref_name, "r");

    if (0 == fref) {
        int cache = errno; /* caching errno, as puts could alter it */
        if (ENOENT != cache)
            warn ("Error opening %s: %s\n", ref_name, strerror (cache));
        status->status = ST_BAD_REF;
    }
    else {
        int match = 1;   /* do the two files match? */

        while (!feof (fref) && !feof (fout)) {

            char buf [2][DELTA_BUF_LEN];

            size_t nread [2];   /* bytes read from the output/ref file */

            /* First, read a block from the files into the buffer */
            nread [0] = fread (buf [0], 1, sizeof buf [0], fout);
            if (ferror (fout)) {
                warn ("Error reading %s: %s\n", out_name, strerror (errno));
                match = 0;
                break;
            }

            nread [1] = fread (buf [1], 1, sizeof buf [1], fref);
            if (ferror (fref)) {
                warn ("Error reading %s: %s\n", ref_name, strerror (errno));
                match = 0;
                break;
            }

            /* Then, check if the amount of data read or the state of the 
               files differs
            */
            if (nread [0] != nread [1] || feof (fref) != feof (fout)) {
                match = 0;
                break;
            }

            /* Finally, check if the contents of the buffers differ */
            if (0 != memcmp (buf [0], buf [1], nread [0])) {
                match = 0;
                break;
            }
        }

        if (!match)
            status->status = ST_BAD_OUTPUT;

        fclose (fref);
    }
    free (ref_name);
}