/*
 * nextFile
 *
 * open the next source file, if any.
 *
 * return 1 if could not open the next file.
 * return 0 otherwise.
 */
static int nextFile(fstream_t*fs)
{
	int response_code;
	const char	*response_string;
	struct gpfxdist_t* transform = fs->options.transform;

	fs->compressed_position += gfile_get_compressed_size(&fs->fd);
	gfile_close(&fs->fd);
	fs->foff = 0;
	fs->line_number = 1;
	fs->fidx++;

	if (fs->fidx < fs->glob.gl_pathc)
	{
		fs->skip_header_line = fs->options.header;

		if (gfile_open(&fs->fd, fs->glob.gl_pathv[fs->fidx], GFILE_OPEN_FOR_READ, 
					   &response_code, &response_string, transform))
		{
			gfile_printf_then_putc_newline("fstream unable to open file %s",
											fs->glob.gl_pathv[fs->fidx]);
			fs->ferror = "unable to open file";
			return 1;
		}
	}

	return 0;
}
/*
 * fstream_rewind
 *
 * close the currently open file. open the first file in the file stream
 * chain, reset state and start from scratch.
 */
int fstream_rewind(fstream_t *fs)
{
	int 		response_code;
	const char*	response_string;
	struct gpfxdist_t* transform = fs->options.transform;

	fs->fidx = 0;
	fs->foff = 0;
	fs->line_number = 1;
	fs->ferror = 0;
	fs->skip_header_line = fs->options.header;
	fs->buffer_cur_size = 0;
	fs->compressed_position = 0;

	gfile_close(&fs->fd);

	if (gfile_open(&fs->fd, fs->glob.gl_pathv[0], GFILE_OPEN_FOR_READ,
				   &response_code, &response_string, transform))
	{
		gfile_printf_then_putc_newline("fstream unable to open file %s",
				fs->glob.gl_pathv[0]);
		fs->ferror = "unable to open file";
		return -1;
	}
	return 0;
}
/* close the file stream */
void fstream_close(fstream_t* fs)
{
#ifdef GPFXDIST
	/*
	 * remove temporary file we created to hold the file paths
	 */
	if (fs->options.transform && fs->options.transform->tempfilename)
	{
		apr_file_remove(fs->options.transform->tempfilename, fs->options.transform->mp);
		fs->options.transform->tempfilename = NULL;
	}
#endif

	if(fs->buffer)
		gfile_free(fs->buffer);

	glob_and_copyfree(&fs->glob);
	gfile_close(&fs->fd);
	gfile_free(fs);
}
Exemple #4
0
int
brot_main(const char *cmdline)
{
   struct brot_args_info brot_args;
   SeqMatrix* sm               = NULL;
   int retval                  = 0;
   Scmf_Rna_Opt_data* sim_data = NULL;
   GFile* entropy_file = NULL;

   /* command line parsing */
   brot_cmdline_parser_init (&brot_args);

   retval = brot_cmdline_parser_string (cmdline, &brot_args, get_progname());

   if (retval == 0)
   {
      retval = brot_cmdline_parser_required (&brot_args, get_progname());
   }

   /* postprocess arguments */
   if (retval == 0)
   {
      retval = brot_cmdline_parser_postprocess (&brot_args);
   }

   /* init simulation data */
   if (retval == 0)
   {
      sim_data = SCMF_RNA_OPT_DATA_NEW_INIT(brot_args.inputs[1], 
                                            strlen (brot_args.inputs[1]),
                                            RNA_ALPHABET,
                                            strlen(RNA_ALPHABET)/2,
            ((-1) * ((logf (1 / 0.000001f)) / (strlen (brot_args.inputs[1])))));
      if (sim_data == NULL)
      {
         retval = 1;
      }
   }

   /* init matrix */
   if (retval == 0)
   {
      sm = SEQMATRIX_NEW;
      if (sm != NULL)
      {
         retval = SEQMATRIX_INIT (
            alphabet_size (scmf_rna_opt_data_get_alphabet (sim_data)),
                                  strlen (brot_args.inputs[1]),
                                  sm);
         /*seqmatrix_print_2_stdout (6, sm);*/
      }
      else
      {
         retval = 1;
      }
   }

   /* fix certain sites in the matrix */
   if (retval == 0)
   {
      retval = adopt_site_presettings (&brot_args,
                                      scmf_rna_opt_data_get_alphabet (sim_data),
                                       sm);
   }

   /* open entropy file if name given */
   if (brot_args.entropy_output_given)
   {
      entropy_file = GFILE_OPEN (brot_args.entropy_output_arg,
                                 strlen (brot_args.entropy_output_arg),
                                 GFILE_VOID, "a");
      if (entropy_file == NULL)
      {
         retval = 1;
      }
      else
      {
         if (gfile_printf (entropy_file, "# bROT settings:\n") < 0)
         {
            retval = 1;
         }
         else if (gfile_printf (entropy_file, "# steps: %lu\n",
                                brot_args.steps_arg) < 0)
         {
            retval = 1;
         }
         else if (gfile_printf (entropy_file, "# temp:              %f\n",
                                brot_args.temp_arg) < 0)
         {
            retval = 1;
         }
         else if (gfile_printf (entropy_file, "# beta-short:        %f\n",
                                brot_args.beta_short_arg) < 0)
         {
            retval = 1;
         }
         else if (gfile_printf (entropy_file, "# beta-long:         %f\n",
                                brot_args.beta_long_arg) < 0)
         {
            retval = 1;
         }
         else if (gfile_printf (entropy_file, "# speedup-threshold: %f\n",
                                brot_args.speedup_threshold_arg) < 0)
         {
            retval = 1;
         }
         else if (gfile_printf (entropy_file, "# min-cool:          %f\n",
                                brot_args.min_cool_arg) < 0)
         {
            retval = 1;
         }
         else if (gfile_printf (entropy_file, "# scale-cool:        %f\n",
                                brot_args.scale_cool_arg) < 0)
         {
            retval = 1;
         }
         else if (gfile_printf (entropy_file, "# lambda:            %f\n",
                                brot_args.lambda_arg) < 0)
         {
            retval = 1;
         }
         else if (gfile_printf (entropy_file, "# sm-entropy:        %f\n",
                                brot_args.sm_entropy_arg) < 0)
         {
            retval = 1;
         }
      }
   }

   if (retval == 0)
   {
      if (brot_args.scoring_arg == scoring_arg_simpleNN)
      {
         /* special to NN usage: structure has to be of size >= 2 */
         if (strlen (brot_args.inputs[1]) > 1)
         {
            retval = simulate_using_simplenn_scoring (&brot_args,
                                                      sm,
                                                      sim_data,
                                                      entropy_file);
         }
         else
         {
            THROW_ERROR_MSG (NN_2_SMALL_WARNING,
                             brot_args.inputs[1], 
                             (unsigned long) strlen (brot_args.inputs[1]));
            retval = 1;
         }
      }
      else if (brot_args.scoring_arg == scoring_arg_nussinov)
      {
         retval = simulate_using_nussinov_scoring (&brot_args,
                                                   sm,
                                                   sim_data,
                                                   entropy_file);
      }
      else if (brot_args.scoring_arg == scoring_arg_NN)
      {
         /* special to NN usage: structure has to be of size >= 2 */
         if (strlen (brot_args.inputs[1]) > 1)
         {
            retval = simulate_using_nn_scoring (&brot_args,
                                                sm,
                                                sim_data,
                                                entropy_file);
         }
         else
         {
            THROW_ERROR_MSG (NN_2_SMALL_WARNING,
                             brot_args.inputs[1], 
                             (unsigned long) strlen (brot_args.inputs[1]));
            retval = 1;
         }
      }
   }

   /* close entropy file */
   if (retval == 0)
   {
      retval = gfile_close (entropy_file);
   }
   else
   {
      gfile_close (entropy_file);
   }

   if (retval == 0)
   {
      /* seqmatrix_print_2_stdout (2, sm); */
      mprintf ("%s\n", scmf_rna_opt_data_get_seq(sim_data));
   }   

   /* finalise */
   brot_cmdline_parser_free (&brot_args);
   seqmatrix_delete (sm);
   scmf_rna_opt_data_delete (sim_data);

   if (retval == 0)
   {
      return EXIT_SUCCESS;
   }
   else
   {
      return EXIT_FAILURE;
   }
}
Exemple #5
0
void game_close(struct gfile *f)
{
	gfile_close(gfs, f);
}
Exemple #6
0
int
brot_main(const char *cmdline)
{
   struct brot_args_info brot_args;
   SeqMatrix* sm               = NULL;
   int retval                  = 0;
   Scmf_Rna_Opt_data* sim_data = NULL;
   GFile* entropy_file = NULL;
   GFile* simulation_file = NULL;

   /* command line parsing */
   brot_cmdline_parser_init (&brot_args);

   retval = brot_cmdline_parser_string (cmdline, &brot_args, get_progname());

   if (retval == 0)
   {
      retval = brot_cmdline_parser_required (&brot_args, get_progname());
   }

   /* postprocess arguments */
   if (retval == 0)
   {
      retval = brot_cmdline_parser_postprocess (&brot_args, cmdline);
   }

   /* init simulation data */
   if (retval == 0)
   {
      sim_data = SCMF_RNA_OPT_DATA_NEW_INIT(brot_args.inputs[1], 
                                            strlen (brot_args.inputs[1]),
                                            RNA_ALPHABET,
                                            strlen(RNA_ALPHABET)/2,
             ((-1) * ((logf (1 / 0.000001f)) / (strlen (brot_args.inputs[1])))),
                                            brot_args.file_given);
      if (sim_data == NULL)
      {
         retval = 1;
      }
   }

   /* init matrix */
   if (retval == 0)
   {
      sm = SEQMATRIX_NEW;
      if (sm != NULL)
      {
         retval = SEQMATRIX_INIT (
            alphabet_size (scmf_rna_opt_data_get_alphabet (sim_data)),
                                      scmf_rna_opt_data_get_rna_size(sim_data),
                                      /* strlen (brot_args.inputs[1]), */
                                  sm);
         /*seqmatrix_print_2_stdout (6, sm);*/
      }
      else
      {
         retval = 1;
      }
   }

   /* fix certain sites in the matrix */
   if (retval == 0)
   {
      retval = adopt_site_presettings (&brot_args,
                                      scmf_rna_opt_data_get_alphabet (sim_data),
                                       sim_data,
                                       sm);
   }

   /* open entropy file if name given */
   if (retval == 0)
   {
      print_verbose ("# Entropy file            (-p): ");
      if (brot_args.entropy_output_given)
      {
         print_verbose ("%s", brot_args.entropy_output_arg);
         entropy_file = GFILE_OPEN (brot_args.entropy_output_arg,
                                    strlen (brot_args.entropy_output_arg),
                                    GFILE_VOID, "a");
         if (entropy_file == NULL)
         {
            retval = 1;
         }
         else
         {
            retval = brot_settings_2_file (entropy_file, cmdline);
         }
      }
      /* open simulation/ matrix file if name given */
      print_verbose ("\n# Simulation/ Matrix file (-m): ");
      if (brot_args.simulation_output_given)
      {
         print_verbose ("%s", brot_args.simulation_output_arg);
         simulation_file = GFILE_OPEN (brot_args.simulation_output_arg,
                                       strlen (brot_args.simulation_output_arg),
                                       GFILE_VOID, "a");
         
         if (simulation_file == NULL)
         {
            retval = 1;
         }
         else
         {
            retval = brot_settings_2_file (simulation_file, cmdline);
            if (retval == 0)
            {
               if (gfile_printf (simulation_file, "START\n") < 0)
               {
                  retval = 1;
               }
            }
         }
      }
   }
   
   if (retval == 0)
   {
      print_verbose ("\n# Scoring scheme          (-c): ");

      if (brot_args.scoring_arg == scoring_arg_simpleNN)
      {
         print_verbose ("simpleNN\n");
         /* special to NN usage: structure has to be of size >= 2 */
         if (strlen (brot_args.inputs[1]) > 1)
         {
            retval = simulate_using_simplenn_scoring (&brot_args,
                                                      sm,
                                                      sim_data,
                                                      entropy_file,
                                                      simulation_file);
         }
         else
         {
            THROW_ERROR_MSG (NN_2_SMALL_WARNING,
                             brot_args.inputs[1], 
                             (unsigned long) strlen (brot_args.inputs[1]));
            retval = 1;
         }
      }
      else if (brot_args.scoring_arg == scoring_arg_nussinov)
      {
         print_verbose ("nussinov\n");
         retval = simulate_using_nussinov_scoring (&brot_args,
                                                   sm,
                                                   sim_data,
                                                   entropy_file,
                                                   simulation_file);
      }
      else if (brot_args.scoring_arg == scoring_arg_NN)
      {
         print_verbose ("NN\n");
         /* special to NN usage: structure has to be of size >= 2 */
         if (strlen (brot_args.inputs[1]) > 1)
         {
            retval = simulate_using_nn_scoring (&brot_args,
                                                sm,
                                                sim_data,
                                                entropy_file,
                                                simulation_file);
         }
         else
         {
            THROW_ERROR_MSG (NN_2_SMALL_WARNING,
                             brot_args.inputs[1], 
                             (unsigned long) strlen (brot_args.inputs[1]));
            retval = 1;
         }
      }
   }

   /* close files */
   if (retval == 0)
   {
      retval = gfile_close (entropy_file);
   }
   else
   {
      gfile_close (entropy_file);
   }

   if (simulation_file != NULL)
   {
      if (gfile_printf (simulation_file, "END\n") < 0)
      {
         retval = 1;
      }
   }

   if (retval == 0)
   {
      retval = gfile_close (simulation_file);
   }
   else
   {
      gfile_close (simulation_file);
   }

   if (retval == 0)
   {
      /*seqmatrix_print_2_stdout (2, sm);*/
      mprintf ("%s\n", scmf_rna_opt_data_get_seq(sim_data));
   }

   /* finalise */
   brot_cmdline_parser_free (&brot_args);
   seqmatrix_delete (sm);
   scmf_rna_opt_data_delete (sim_data);

   if (retval == 0)
   {
      return EXIT_SUCCESS;
   }
   else
   {
      return EXIT_FAILURE;
   }
}
/*
 * fstream_open
 *
 * Allocate a new file stream given a path (a url). in case of wildcards,
 * expand them here. we end up with a final list of files and include them
 * in our filestream that we return.
 *
 * In case of errors we set the proper http response code to send to the client.
 */
fstream_t*
fstream_open(const char *path, const struct fstream_options *options,
			 int *response_code, const char **response_string)
{
	int i;
	fstream_t* fs;

	*response_code = 500;
	*response_string = "Internal Server Error";

	if (0 == (fs = gfile_malloc(sizeof *fs)))
	{
		gfile_printf_then_putc_newline("fstream out of memory");
		return 0;
	}

	memset(fs, 0, sizeof *fs);
	fs->options = *options;
	fs->buffer = gfile_malloc(options->bufsize);
	
	/*
	 * get a list of all files that were requested to be read and include them
	 * in our fstream. This includes any wildcard pattern matching.
	 */
	if (glob_path(fs, path))
	{
		fstream_close(fs);
		return 0;
	}

	/*
	 * If the list of files in our filestrem includes a directory name, expand
	 * the directory and add all the files inside of it.
	 */
	if (fpath_all_directories(&fs->glob))
	{
		if (expand_directories(fs))
		{
			fstream_close(fs);
			return 0;
		}
	}

	/*
	 * check if we don't have any matching files
	 */
	if (fs->glob.gl_pathc == 0)
	{
		gfile_printf_then_putc_newline("fstream bad path: %s", path);
		fstream_close(fs);
		*response_code = 404;
		*response_string = "No matching file(s) found";
		return 0;
	}

	if (fs->glob.gl_pathc != 1 && options->forwrite)
	{
		gfile_printf_then_putc_newline("fstream open for write found more than one file (%d)",
										fs->glob.gl_pathc);
		*response_code = 404;
		*response_string = "More than 1 file found for writing. Unsupported operation.";

		fstream_close(fs);
		return 0;
	}


#ifdef GPFXDIST
	/*
	 * when the subprocess transformation wants to handle iteration over the files
	 * we write the paths to a temporary file and replace the fs->glob items with
	 * just a single entry referencing the path to the temporary file.
	 */
	if (options->transform && options->transform->pass_paths)
	{
		apr_pool_t*  mp = options->transform->mp;
		apr_file_t*  f = NULL;
		const char*  tempdir = NULL;
		char*        tempfilename = NULL;
		apr_status_t rv;

		if ((rv = apr_temp_dir_get(&tempdir, mp)) != APR_SUCCESS)
		{
			*response_code = 500;
			*response_string = "failed to get temporary directory for paths file";
			gfile_printf_then_putc_newline(*response_string);
			fstream_close(fs);
			return 0;
		}

	    tempfilename = apr_pstrcat(mp, tempdir, "/pathsXXXXXX", NULL);
		if ((rv = apr_file_mktemp(&f, tempfilename, APR_CREATE|APR_WRITE|APR_EXCL, mp)) != APR_SUCCESS)
		{
			*response_code = 500;
			*response_string = "failed to open temporary paths file";
			gfile_printf_then_putc_newline(*response_string);
			fstream_close(fs);
			return 0;
		}

		options->transform->tempfilename = tempfilename;

		for (i = 0; i<fs->glob.gl_pathc; i++)
		{
			char* filename      = fs->glob.gl_pathv[i];
			apr_size_t expected = strlen(filename) + 1;
			
			if (apr_file_printf(f, "%s\n", filename) < expected)
			{
				apr_file_close(f);

				*response_code = 500;
				*response_string = "failed to fully write path to temporary paths file";
				gfile_printf_then_putc_newline(*response_string);
				fstream_close(fs);
				return 0;
			}
		}

		apr_file_close(f);

		if (glob_adjust(fs, tempfilename, response_code, response_string)) 
		{
			fstream_close(fs);
			return 0;
		}
	}
#endif

	/*
	 * if writing - check write access rights for the one file.
	 * if reading - check read access right for all files, and 
	 * then close them, leaving the first file open.
	 * 
	 */
	for (i = fs->glob.gl_pathc; --i >= 0;)
	{
		/*
		 * CR-2173 - the fstream code allows the upper level logic to treat a
		 * collection of input sources as a single stream.  One problem it has
		 * to handle is the possibility that some of the underlying sources may
		 * not be readable.  Here we're trying to detect potential problems in
		 * advance by checking that we can open and close each source in our
		 * list in reverse order.
		 *
		 * However in the case of subprocess transformations, we don't want to 
		 * start and stop each transformation in this manner.  The check that 
		 * each transformation's underlying input source can be read is still 
		 * useful so we do those until we get to the first source, at which 
		 * point we proceed to just setup the tranformation for it.
		 */
		struct gpfxdist_t* transform = (i == 0) ? options->transform : NULL;

		gfile_close(&fs->fd);

		if (gfile_open(&fs->fd, fs->glob.gl_pathv[i], gfile_open_flags(options->forwrite, options->usesync),
					   response_code, response_string, transform))
		{
			gfile_printf_then_putc_newline("fstream unable to open file %s",
					fs->glob.gl_pathv[i]);
			fstream_close(fs);
			return 0;
		}

		fs->compressed_size += gfile_get_compressed_size(&fs->fd);
	}

	fs->line_number = 1;
	fs->skip_header_line = options->header;

	return fs;
}
Exemple #8
0
// Background part as separate thread
// Don't do any GUI things on this thread.
void print_gdi_thread(void *pdummy)
{
    PGDI_THREAD *pth= (PGDI_THREAD *)pdummy;
    CPrintDIB printdib;
    char buf[MAXSTR];
    int length;
    LPBYTE pLine;
    LPBYTE p;
    DWORD dwRead;
    int i;

    GFile *pFile = gfile_open_handle((int)pth->hPipeRd);

    int page = 0;
    BOOL print_it;
    printdib.debug = (debug & DEBUG_GDI);

    while (printdib.ReadHeader(pFile)) {
	page++;
	print_it = TRUE;
	if ((pth->oddeven == EVEN_PAGES) && ((page & 1) == 1))
		print_it = FALSE;
	else if ((pth->oddeven == ODD_PAGES) && ((page & 1) == 0))
		print_it = FALSE;
	if ((pth->from > 0) && (page < pth->from))
		print_it = FALSE;
	if ((pth->to > 0) && (page > pth->to))
		print_it = FALSE;
	
	sprintf(buf, "Page %d, %s\n", page, print_it ? "PRINT" : "ignore");
	gs_addmess(buf);
	if (print_it)
	    StartPage(pth->hdc);
	length = printdib.m_bytewidth;
	pLine = new BYTE[length];
	
	for (i=0; i < printdib.m_PageBmp.bmp2.biHeight; i++) {
	    // read a scan line
	    length = printdib.m_bytewidth;
	    p = pLine;
	    while (length && (dwRead = gfile_read(pFile, p, length)) != 0) {
		    length -= dwRead;
		    p += dwRead;
	    }
	    if (print_it)
		    printdib.AddPrintLine(pth->hdc, i, pLine);
	}
	if (print_it) {
		printdib.FlushPrintBitmap(pth->hdc);
		EndPage(pth->hdc);
	}
	delete pLine;
    }

    EndDoc(pth->hdc);
    DeleteDC(pth->hdc);
    pth->hdc = NULL;
    gfile_close(pFile);

    free(pth);

    print_count--;

    /* if printing from command line, close GSview */
    if (print_exit && (print_count==0) && !(debug & DEBUG_GDI))
	gsview_command(IDM_EXIT);

    gs_addmess("\nPrint GDI finished\n");
}