Ejemplo n.º 1
0
static int handle_inotify_event(struct inotify_event *inev, struct file_struct *f)
{
	int ret = 0;

	if (inev->mask & IN_MODIFY) {
		char *fbuf;
		ssize_t rc;
		struct stat finfo;

		if (verbose)
			write_header(f->name);

		/* Seek to old file size */
		if (lseek(f->fd, f->size, SEEK_SET) == (off_t) -1) {
			fprintf(stderr, "Error: Could not seek in file '%s' (%s)\n", f->name, strerror(errno));
			ret = -1;
			goto ignore;
		}

		fbuf = emalloc(f->blksize);

		while ((rc = read(f->fd, fbuf, f->blksize)) != 0) {
			auparse_feed(au,fbuf, rc);
			}

		if (fstat(f->fd, &finfo) < 0) {
			fprintf(stderr, "Error: Could not stat file '%s' (%s)\n", f->name, strerror(errno));
			ret = -1;
			free(fbuf);
			goto ignore;
		}

		f->size = finfo.st_size;

		free(fbuf);
		return ret;
	} else if (inev->mask & IN_DELETE_SELF) {
		fprintf(stderr, "File '%s' deleted.\n", f->name);
	} else if (inev->mask & IN_MOVE_SELF) {
		fprintf(stderr, "File '%s' moved.\n", f->name);

		// SCOTT
		close(f->fd);

		free(files);
		struct file_struct *new_files = NULL;
		new_files = emalloc( sizeof(struct file_struct) );
		new_files[0].name = "/var/log/audit/audit.log";
		setup_file(&new_files[0]);

	    // Take a sec here to let the new file get
		//  set up by the OS - there are problems with this
		//  asking just a bit too soon.
		sleep(1);
		tail_file(new_files, DEFAULT_N_LINES, M_LINES, 1);
		watch_files(new_files, 1);
		return 0;
	} else if (inev->mask & IN_UNMOUNT) {
		fprintf(stderr, "Device containing file '%s' unmounted.\n", f->name);
	}

ignore:
	ignore_file(f);
	return ret;
}
Ejemplo n.º 2
0
/*============================================================================
 *                                  main
 *
 *==========================================================================*/
int main(int argc, char **argv)
{
    int i;
    char *infile = NULL;
    char *outfile = NULL;
    char *line;

    int dim=0;
    float *M = NULL;
    float *M0 = NULL;

    float *mean = NULL;

    float *eigen_values_r = NULL;
    float *eigen_values_i = NULL;
    float *eigen_vectors_r = NULL;
    float *eigen_vectors_l = NULL;

    eigen_t *eigen = NULL;

    int num_models  = 0;
    int models_size = 0;
    float **models  = NULL;
    
    int num_xs  = 0;
    int xs_size = 0;
    float *xs   = NULL;

    float start_time=0, end_time=0, total_time=0;

    int in_model = 0;
    int in_ensem = 0;
    int in_input = 0;

    int nev = 5;

    in  = stdin;
    out = stdout;
    err = stderr;

    static struct option long_options[] = {
        {"show", optional_argument, 0, 's'},
        {"scale", required_argument, 0, 0},
        {"sort", required_argument, 0, 0},
        {"nev", required_argument, 0, 0},
        {"mo", required_argument, 0, 0},
        {"proj", optional_argument, 0, 'p'},
        {"help", no_argument, 0, 'h'},
        {"format", no_argument, 0, 0},
        {"header", no_argument, 0, 0},
        {"interp", no_argument, 0, 0},
        {"add-back-mean", no_argument, 0, 0},
        {0, 0, 0, 0}
    };

    /*========================================================================
     * Capture commandline arguments for write_header()
     *======================================================================*/

    if (argc != 1)
    {
        int c=0;
        for (i=1; i < argc; i++) c += strlen(argv[i]) + 1;
        commandline = (char *)calloc(c + 1, sizeof(char));
        for (i=1; i < argc; i++)
        {
            strcat(commandline, argv[i]);
            strcat(commandline, " ");
        }
    }

    /*========================================================================
     * Process the command line flags
     *======================================================================*/
    while (1)
    {
        int option_index = 0;
        int c = getopt_long(argc, argv, "p::i:o:v::s::hc", 
                            long_options, &option_index);

        if (c == -1) break;

        switch (c)
        {
            case 0:
                if (!strcmp("mo", long_options[option_index].name))
                {
                    opt_multi_out = 1;
                    mo_prefix = optarg;
                }
                if (!strcmp("interp", long_options[option_index].name))
                {
                    opt_interp_proj = 1;
                }
                else if (!strcmp("scale", long_options[option_index].name))
                {
                    if (!strcmp("mult", optarg))
                        opt_scale = SCALE_MULT;
                    else if (!strcmp("diag", optarg))
                        opt_scale = SCALE_DIAG;
                    else if (!strcmp("none", optarg))
                        opt_scale = SCALE_NONE;
                    else
                        error("Unrecognized scale type.");
                }
                else if (!strcmp("sort", long_options[option_index].name))
                {
                    if (!strcmp("ev", optarg))
                        opt_sort = SORT_EV;
                    else if (!strcmp("lc", optarg))
                        opt_sort = SORT_LC;
                    else
                        error("Unrecognized sort type.");
                }
                else if (!strcmp("header", long_options[option_index].name))
                {
                    write_header(err);
                    exit(0);
                }
                else if (!strcmp("add-back-mean", long_options[option_index].name))
                {
                    opt_add_back_mean = 1;
                }
                else if (!strcmp("format", long_options[option_index].name))
                {
fprintf(err, 
"\n"
"#BEGIN INPUT\n"
"<PixeLens input text line 0>\n"
"<PixeLens input text line 1>\n"
"  ...\n"
"<PixeLens input text line n>\n"
"#END INPUT\n"
"#BEGIN ENSEM \n"
"#BEGIN MODEL\n"
"<point 0>\n"
"<point 1>\n"
"  ...\n"
"<point m>\n"
"#END MODEL\n"
"  ...\n"
"#END ENSEM\n"
"\n"
"Blank lines are allowed and any line beginning with a '#' that is\n"
"not mentioned above is interpretted as a comment. It may be the case\n"
"that there are no models. \n"
"\n"
);
                    exit(0);

                }
                break;

            case 's':
                opt_show = 0;
                if (optarg == NULL)
                {
                    opt_show = SHOW_DEFAULT;
                }
                else
                {
                    if (strchr(optarg, 'm')) opt_show |= SHOW_MATRIX_FLAT;
                    if (strchr(optarg, 'M')) opt_show |= SHOW_MATRIX;
                    if (strchr(optarg, 'e')) opt_show |= SHOW_EIGEN_VALUES;
                    if (strchr(optarg, 'r')) opt_show |= SHOW_EIGEN_VECTORS_R;
                    if (strchr(optarg, 'l')) opt_show |= SHOW_EIGEN_VECTORS_L;
                }
                break;

            case 'v':
                if (optarg == NULL) verbosity++;
                else                verbosity = atoi(optarg);
                break;

            case 'i': 
                fprintf(err, "%s\n", optarg);
            infile = optarg; break;
            case 'o': outfile = optarg; break;
            case 'p': 
                fprintf(err, "%s\n", optarg);
                opt_proj = 1;
                if (optarg != NULL) nev = atoi(optarg);
                break;

            case 'h': help(); break;
            case '?': exit(2);
        }
    }

    if (opt_multi_out && !opt_proj)
    {
        fprintf(err, "--mo needs -p\n");
        exit(2);
    }

    if (!opt_show && !opt_proj)
        opt_show = SHOW_DEFAULT;

    if (infile != NULL) 
    {
        in = fopen(infile, "r");
        if (in == NULL)
        {
            fprintf(err, "%s", strerror(errno));
            exit(EXIT_FAILURE);
        }
    }

    if (outfile != NULL) 
    {
        out = fopen(outfile, "w");
        if (out == NULL)
        {
            fprintf(err, "%s", strerror(errno));
            exit(EXIT_FAILURE);
        }
    }


    /*========================================================================
     * Read commands or numerical input from the command line and update
     * the matrix accordingly, until there is no more input. The file we 
     * expect to read has the following structure:
     *
     * #BEGIN INPUT
     * #END INPUT
     * #BEGIN ENSEM 
     * #BEGIN MODEL
     * <point 0>
     * <point 1>
     *   ...
     * <point dim-1>
     * #END MODEL
     *   ...
     * #END ENSEM
     *
     * Blank lines are allowed and any line beginning with a '#' that is
     * not mentioned above is interpretted as a comment. It may be the case
     * that there are no models. 
     *
     *======================================================================*/

    rl_instream = in;
    rl_outstream = out;
    while (1)
    {
        line = readline(NULL);
        if (line == NULL) break;            /* EOF */
        if (strlen(line) == 0) continue;    /* Blank line */

        if (strstr(line, "#BEGIN INPUT") == line)
        {
            if (num_input_lines == input_size)
            {
                input_size += 5;
                input = (char **)realloc(input, input_size * sizeof(char *));
            }
            input[num_input_lines++] = line;

            in_input = 1;

            line = NULL; // prevent deallocation of line by free()

        }
        else if (strstr(line, "#END INPUT") == line)
        {
            in_input = 0;
            num_input_lines++;

            if (num_input_lines-1 == input_size)
            {
                input_size += 5;
                input = (char **)realloc(input, input_size * sizeof(char *));
            }
            input[num_input_lines-1] = line;

            line = NULL; // prevent deallocation of line by free()
        }
        else if (strstr(line, "#BEGIN LENS") == line) /*Backward compatibility*/
        {
            int q;
            sscanf(line, "#BEGIN LENS dim %i omega %f lambda %f", 
                &q, &omega, &lambda);

            in_ensem = 1;
        }
        else if (strstr(line, "#BEGIN ENSEM") == line)
        {
            in_ensem = 1;
        }
        else if (strstr(line, "#END LENS")  == line 
             ||  strstr(line, "#END ENSEM") == line)
        {
            in_ensem = 0;
            in_model = 0;
        }
        else if (in_ensem && strstr(line, "#BEGIN MODEL") == line)
        {
            num_xs = 0;
            xs = NULL;

            start_time = CPUTIME;

            if (in_model)
            {
                error("File inconsistency. "
                      "#BEGIN MODEL found before #END MODEL.\n");
            }

            in_model = 1;
        }
        else if (in_model && strstr(line, "#END MODEL") == line)
        {
            /*================================================================
             * Now that the x's have been read, update the matrix.
             *==============================================================*/
            if (!in_model)
            {
                error("File inconsistency. "
                      "#END MODEL found before #BEGIN MODEL.\n");
            }

            if (num_models == models_size)
            {
                models_size += 100;
                models = (float **)realloc(models, 
                                           models_size * sizeof(float *));
            }
            models[num_models++] = xs;


            if (verbosity > 1)
            {
                end_time = CPUTIME;
                total_time += end_time - start_time;

                fprintf(err, "[%ix%i matrix; %i models; %fs; %fs]\n", 
                        dim, dim, num_models, 
                        end_time - start_time, total_time);
            }

            in_model = 0;
        }
        else if (line[0] == '#') 
        {
            continue;
        }
        else if (in_model)
        {
            /*================================================================
             * Read the input values.
             *==============================================================*/

            if (num_models == 0)
            {
                if (num_xs == xs_size)
                {
                    xs_size += 100;
                    xs = (float *)realloc(xs, xs_size * sizeof(float));
                }

                dim = num_xs+1;
            }
            else if (num_xs > dim)
            {
                error("Model sizes differ!");
            }
            else if (xs == NULL)
            {
                xs = (float *)malloc(dim * sizeof(float));
            }

            xs[num_xs++] = atof(line);
        }
        else if (in_input)
        {
            num_input_lines++;

            if (num_input_lines-1 == input_size)
            {
                input_size += 5;
                input = (char **)realloc(input, input_size * sizeof(char *));
            }
            input[num_input_lines-1] = line;

            line = NULL; // prevent deallocation of line by free()
        }


        free(line);
    }


    if (verbosity > 0)
        fprintf(err, "[%ix%i matrix; %i models]\n", dim, dim, num_models);

    if (!opt_multi_out) 
    {
        write_header(out);
        write_input(out);
    }

    /*========================================================================
     *------------------------------------------------------------------------
     *======================================================================*/

    if (dim > 0)
    {
        /*====================================================================
         * Center the data points to the origin.
         *==================================================================*/

        /* Find the average of the models. */
        mean = (float *)calloc(dim, sizeof(float));
        for (i=0; i < num_models; i++)
            vecvecadd(mean, models[i], dim, mean);
        vecdiv(mean, num_models, dim, mean);

        /* Subtract off the mean from each of the models. */
        if (verbosity > 0) fprintf(err, "Normalizing models...\n");
        for (i=0; i < num_models; i++)
            vecvecsub(models[i], mean, dim, models[i]);

        /*====================================================================
         * Optionally apply a general multiplicative scaling.
         *==================================================================*/
        if (opt_scale & SCALE_MULT)
        {
            if (verbosity > 0) fprintf(err, "Applying multiply scaling...\n");
            scale(models[i], dim, NULL);
        }

        /*====================================================================
         * Now with the normalized data, generate the inertia matrix.
         *==================================================================*/
        if (verbosity > 0) fprintf(err, "Creating matrix...\n");
        M = (float *)calloc(dim * dim, sizeof(float));
        if (M == NULL) error("Can't allocate memory for matrix.");

        for (i=0; i < num_models; i++)
            update_matrix(M, models[i], dim);

        /*====================================================================
         * Optionally apply a diagonalization scaling.
         *==================================================================*/
        if (opt_scale & SCALE_DIAG)
        {
            M0 = (float *)malloc(dim * dim * sizeof(float));
            float *t;

            int i, j;
            if (verbosity > 0) fprintf(err, "Applying diagonal scaling...\n");
            for (i=0; i < dim; i++)
                for (j=0; j < dim; j++)
                    M0[i*dim+j] = M[i*dim+j] / sqrt(M[i*dim+i] * M[j*dim+j]);

            for (i=0; i < num_models; i++)
                scale(models[i], dim, M);

            t = M;
            M = M0;
            M0 = t;
        }
        else
        {
            M0 = M;
        }

        /*====================================================================
         * Compute the eigen values (and vectors) of the matrix we built. 
         *==================================================================*/
        if (verbosity>0) fprintf(err, "Calculating eigenvalues(/vectors)...\n");
        eigen_values_r = (float *)malloc(dim * sizeof(float)); assert(eigen_values_r != NULL);
        eigen_values_i = (float *)malloc(dim * sizeof(float)); assert(eigen_values_i != NULL);

        if (opt_proj || (opt_show & SHOW_EIGEN_VECTORS_R))
        {
            eigen_vectors_r = (float *)malloc(dim * dim * sizeof(float)); assert(eigen_vectors_r != NULL);
        }

        if (opt_show & SHOW_EIGEN_VECTORS_L)
        {
            eigen_vectors_l = (float *)malloc(dim * dim * sizeof(float)); assert(eigen_vectors_r != NULL);
        }

        if (get_eigen_values(M, eigen_values_r, eigen_values_i, 
                                eigen_vectors_l, eigen_vectors_r, dim,
                                0))
        {
            error("Library call failed for given inputs.");
        }


        /*====================================================================
         * Now place the results in a nice array which we can sort. The array
         * is sorted by the real part of the eigenvalues, largest to smallest.
         *==================================================================*/
        eigen = (eigen_t *)calloc(dim, sizeof(eigen_t));

        for (i=0; i < dim; i++)
        {
            if (eigen_values_r[i] == -0) eigen_values_r[i] = 0;
            if (eigen_values_i[i] == -0) eigen_values_i[i] = 0;

            eigen[i].re   = eigen_values_r[i];
            eigen[i].im   = eigen_values_i[i];
            eigen[i].dim  = dim;

            if (eigen_vectors_l) eigen[i].lvec = &(eigen_vectors_l[i * dim]);
            if (eigen_vectors_r) eigen[i].rvec = &(eigen_vectors_r[i * dim]);
        }

        /*====================================================================
         * Optionally add back the mean model.
         *==================================================================*/
        if (opt_add_back_mean)
        {
            if (verbosity>0) fprintf(err, "Adding back mean...\n");
            /* add back mean */
            if (eigen_vectors_r)
                for (i=0; i < dim; i++)
                    vecvecadd(eigen[i].rvec, mean, dim, eigen[i].rvec);

            if (eigen_vectors_l)
                for (i=0; i < dim; i++)
                    vecvecadd(eigen[i].lvec, mean, dim, eigen[i].lvec);
        }

        /*====================================================================
         * Sort.
         *==================================================================*/
        if (!opt_sort || (opt_sort & SORT_EV))
        {
            qsort(eigen, dim, sizeof(eigen_t), eigen_compar_rev);
        }
        else if (opt_sort & SORT_LC)
        {
            qsort(eigen, dim, sizeof(eigen_t), largest_ev_last_component);
        }

        /*====================================================================
         *--------------------------------------------------------------------
         *==================================================================*/

        if (opt_proj)
            do_eigen_projections(nev, M0, models, mean, num_models, eigen, dim);

    }


    do_output(M, omega, lambda, eigen, dim);


    if (M == M0) 
    {
        free(M);
    }
    else
    {
        free(M);
        free(M0);
    }
    free(eigen_values_r);
    free(eigen_values_i);
    free(eigen_vectors_l);
    free(eigen_vectors_r);
    free(eigen);
    free(mean);
    free(commandline);

    for (i=0; i < num_input_lines; i++) free(input[i]);
    free(input);

    for (i=0; i < num_models; i++) free(models[i]); 
    free(models);

    if (in  != stdin)  fclose(in);
    if (out != stdout) fclose(out);

    return 0;
}
Ejemplo n.º 3
0
const char * Nes_File_Writer::write_block_header( nes_tag_t tag, long size )
{
	write_remain = size;
	return write_header( tag, size );
}
Ejemplo n.º 4
0
std::ostream& CPPCodeGenerator::operator ()(std::ostream &os) const
{
	begin_comment(os);
	write_header(os);
	end_comment(os);

	os << std::endl <<std::endl;
	

	std::set< shared_ptr<model::Class> > class_dependencies;
	m_Class->visit(bind(&CPPCodeGenerator::collect_class_dependencies, this, _1, ref(class_dependencies)));

	os << "#ifndef J2CPP_INCLUDE_IMPLEMENTATION" << std::endl << std::endl;

	std::string strIFNDEF=m_Class->get_cxx_include_path();
	algorithm::to_upper(strIFNDEF);
	algorithm::replace_all(strIFNDEF,"/","_");



	os << "#ifndef J2CPP_" << strIFNDEF << "_HPP_DECL" << std::endl;
	os << "#define J2CPP_" << strIFNDEF << "_HPP_DECL" << std::endl;

	os << std::endl << std::endl;



	BOOST_FOREACH(shared_ptr<model::Class> dependend_class, class_dependencies)
	{
		if(dependend_class==m_Class) continue;
		std::vector< shared_ptr<model::Entity> > parentEntities;
		if(shared_ptr<model::Entity> parentEntity=dependend_class->get_parent())
		{
			do
			{
				parentEntities.push_back(parentEntity);
				parentEntity=parentEntity->get_parent();
			}
			while(parentEntity && parentEntity!=m_RootNS);
		}
		os << "namespace j2cpp { ";
		for(std::size_t ns=0;ns<parentEntities.size();++ns)
		{
			if(shared_ptr<model::Namespace> enclosingNamespace=shared_ptr<model::Namespace>(parentEntities[parentEntities.size()-ns-1],detail::dynamic_cast_tag()))
			{
				os << "namespace " << enclosingNamespace->get_name() << " { ";
			}
			else
			if(shared_ptr<model::Class> enclosingClass=shared_ptr<model::Class>(parentEntities[parentEntities.size()-ns-1],detail::dynamic_cast_tag()))
			{
				os << "namespace " << enclosingClass->get_name() << "_ { ";
			}
		}
		os << "class " << dependend_class->get_name() << ";";
		for(std::size_t ns=0;ns<parentEntities.size();++ns)
			os << " }";		
		os << " }" << std::endl;
	}
	os << std::endl << std::endl;

	std::set<std::string> dependenciesIncludes;

	BOOST_FOREACH(shared_ptr<model::Class> dependend_class, class_dependencies)
	{
		if(dependend_class==m_Class) continue;
		dependenciesIncludes.insert(dependend_class->get_cxx_include_path());
	}

	BOOST_FOREACH(std::string incPath, dependenciesIncludes)
	{
		os << "#include <" << incPath << ".hpp>" << std::endl;
	}
Ejemplo n.º 5
0
int ordered_map_file_open(const char *path, OrderedMapFile **out_omf) {
    *out_omf = nullptr;
    OrderedMapFile *omf = create_zero<OrderedMapFile>();
    if (!omf) {
        ordered_map_file_close(omf);
        return GenesisErrorNoMem;
    }
    if (omf->queue.error() || omf->cond.error() || omf->mutex.error()) {
        ordered_map_file_close(omf);
        return omf->queue.error() || omf->cond.error() || omf->mutex.error();
    }
    omf->list = create_zero<List<OrderedMapFileEntry *>>();
    if (!omf->list) {
        ordered_map_file_close(omf);
        return GenesisErrorNoMem;
    }

    omf->map = create_zero<HashMap<ByteBuffer, OrderedMapFileEntry *, ByteBuffer::hash>>();
    if (!omf->map) {
        ordered_map_file_close(omf);
        return GenesisErrorNoMem;
    }

    omf->running = true;
    int err = omf->write_thread.start(run_write, omf);
    if (err) {
        ordered_map_file_close(omf);
        return err;
    }

    bool open_for_writing = false;
    omf->file = fopen(path, "rb+");
    if (omf->file) {
        int err = read_header(omf);
        if (err == GenesisErrorEmptyFile) {
            open_for_writing = true;
        } else if (err) {
            ordered_map_file_close(omf);
            return err;
        }
    } else {
        open_for_writing = true;
    }
    if (open_for_writing) {
        omf->file = fopen(path, "wb+");
        if (!omf->file) {
            ordered_map_file_close(omf);
            return GenesisErrorFileAccess;
        }
        int err = write_header(omf);
        if (err) {
            ordered_map_file_close(omf);
            return err;
        }
    }

    // read everything into list
    omf->write_buffer.resize(TRANSACTION_METADATA_SIZE);
    omf->transaction_offset = UUID_SIZE;
    for (;;) {
        size_t amt_read = fread(omf->write_buffer.raw(), 1, TRANSACTION_METADATA_SIZE, omf->file);
        if (amt_read != TRANSACTION_METADATA_SIZE) {
            // partial transaction. ignore it and we're done.
            break;
        }
        uint8_t *transaction_ptr = (uint8_t*)omf->write_buffer.raw();
        int transaction_size = read_uint32be(&transaction_ptr[4]);

        omf->write_buffer.resize(transaction_size);
        transaction_ptr = (uint8_t*)omf->write_buffer.raw();

        size_t amt_to_read = transaction_size - TRANSACTION_METADATA_SIZE;
        amt_read = fread(&transaction_ptr[TRANSACTION_METADATA_SIZE], 1, amt_to_read, omf->file);
        if (amt_read != amt_to_read) {
            // partial transaction. ignore it and we're done.
            break;
        }
        uint32_t computed_crc = crc32(0, &transaction_ptr[4], transaction_size - 4);
        uint32_t crc_from_file = read_uint32be(&transaction_ptr[0]);
        if (computed_crc != crc_from_file) {
            // crc check failed. ignore this transaction and we're done.
            break;
        }

        int put_count = read_uint32be(&transaction_ptr[8]);
        int del_count = read_uint32be(&transaction_ptr[12]);

        int offset = TRANSACTION_METADATA_SIZE;
        for (int i = 0; i < put_count; i += 1) {
            int key_size = read_uint32be(&transaction_ptr[offset]); offset += 4;
            int val_size = read_uint32be(&transaction_ptr[offset]); offset += 4;

            OrderedMapFileEntry *entry = create_zero<OrderedMapFileEntry>();
            if (!entry) {
                ordered_map_file_close(omf);
                return GenesisErrorNoMem;
            }

            entry->key = ByteBuffer((char*)&transaction_ptr[offset], key_size); offset += key_size;
            entry->offset = omf->transaction_offset + offset;
            entry->size = val_size;
            offset += val_size;

            auto old_hash_entry = omf->map->maybe_get(entry->key);
            if (old_hash_entry) {
                OrderedMapFileEntry *old_entry = old_hash_entry->value;
                destroy(old_entry, 1);
            }

            omf->map->put(entry->key, entry);
        }
        for (int i = 0; i < del_count; i += 1) {
            int key_size = read_uint32be(&transaction_ptr[offset]); offset += 4;
            ByteBuffer key((char*)&transaction_ptr[offset], key_size); offset += key_size;

            auto hash_entry = omf->map->maybe_get(key);
            if (hash_entry) {
                OrderedMapFileEntry *entry = hash_entry->value;
                omf->map->remove(key);
                destroy(entry, 1);
            }
        }

        omf->transaction_offset += transaction_size;

    }

    // transfer map to list and sort
    auto it = omf->map->entry_iterator();
    if (omf->list->ensure_capacity(omf->map->size())) {
        ordered_map_file_close(omf);
        return GenesisErrorNoMem;
    }
    for (;;) {
        auto *map_entry = it.next();
        if (!map_entry)
            break;

        ok_or_panic(omf->list->append(map_entry->value));
    }
    omf->map->clear();
    destroy_map(omf);

    omf->list->sort<compare_entries>();

    *out_omf = omf;
    return 0;
}
Ejemplo n.º 6
0
int main(int argc, char *argv[])
/**********************************************************************
	vicNl.c		Dag Lohmann		January 1996

  This program controls file I/O and variable initialization as well as
  being the primary driver for the model.

  For details about variables, input files and subroutines check:
	http://ce.washington.edu/~hydro/Lettenmaier/Models/VIC/VIC_home.html

  UNITS: unless otherwise marked:
         all water balance components are in mm
	 all energy balance components are in mks
	 depths, and lengths are in m

  modifications:
  1997-98 Model was updated from simple 2 layer water balance to 
          an extension of the full energy and water balance 3 layer
	  model.                                                  KAC
  02-27-01 added controls for lake model                          KAC
  11-18-02 Updated storage of lake water for water balance 
           calculations.                                          LCB
  03-12-03 Modifed to add AboveTreeLine to soil_con_struct so that
           the model can make use of the computed treeline.     KAC
  04-10-03 Modified to initialize storm parameters using the state
           file.                                                KAC
  04-10-03 Modified to start the model by skipping records until the
           state file date is found.  This replaces the previous method
           of modifying the global file start date, which can change 
           the interpolation of atmospheric forcing data.        KAC
  04-15-03 Modified to store wet and dry fractions when intializing 
           water balance storage.  This accounts for changes in model
           state initialization, which now stores wet and dry fractions
           rather than just averagedvalues.                      KAC
  29-Oct-03 Modified the version display banner to print the version
	    string defined in global.h.					TJB
  01-Nov-04 Updated arglist for make_dist_prcp(), as part of fix for
	    QUICK_FLUX state file compatibility.			TJB
  02-Nov-04 Updated arglist for read_lakeparam(), as part of fix for
	    lake fraction readjustment.					TJB
  2005-Apr-13 OUTPUT_FORCE option now calls close_files().		TJB
  2006-Sep-23 Implemented flexible output configuration; uses the new
              out_data, out_data_files, and save_data structures.	TJB
  2006-Oct-16 Merged infiles and outfiles structs into filep_struct;
	      This included merging builtnames into filenames.		TJB
  2006-Nov-07 Removed LAKE_MODEL option.				TJB
  2006-Nov-07 Changed statefile to init_state in call to
	      check_state_file().					TJB
  2007-Jan-15 Added PRT_HEADER option; added call to
	      write_header().						TJB
  2007-Apr-04 Added option to continue run after a cell fails. 		GCT/KAC
  2007-Apr-21 Added calls to free_dmy(), free_out_data_files(),
	      free_out_data(), and free_veglib().  Added closing of
	      all parameter files.					TJB
  2007-Aug-21 Return ErrorFlag from initialize_model_state.		JCA
  2007-Sep-14 Excluded calls to free_veglib() and closing of parameter
	      files other than the soil param file for the case
	      when OUTPUT_FORCE=TRUE.					TJB
  2007-Nov-06 Moved computation of cell_area from read_lakeparam() to
	      read_soilparam() and read_soilparam_arc().		TJB
  2008-May-05 Added prcp fraction (mu) to initial water storage
	      computation.  This solves water balance errors for the
	      case where DIST_PRCP is TRUE.				TJB
  2009-Jan-16 Added soil_con.avgJulyAirTemp to argument list of
	      initialize_atmos().					TJB
  2009-Jun-09 Modified to use extension of veg_lib structure to contain
	      bare soil information.					TJB
  2009-Jul-07 Added soil_con.BandElev[] to read_snowband() arg list.	TJB
  2009-Jul-31 Replaced references to N+1st veg tile with references
	      to index of lake/wetland tile.				TJB
  2009-Sep-28 Replaced initial water/energy storage computations and
	      calls to calc_water_balance_error/calc_energy_balance_error
	      with an initial call to put_data.  Modified the call to
	      read_snowband().						TJB
  2009-Dec-11 Removed save_data structure from argument list of 
	      initialize_model_state().					TJB
  2010-Mar-31 Added cell_area to initialize_atmos().			TJB
  2010-Apr-28 Removed individual soil_con variables from argument list
	      of initialize_atmos() and replaced with *soil_con.	TJB
  2010-Nov-10 Added closing of state files.				TJB
  2011-Jan-04 Made read_soilparam_arc() a sub-function of
	      read_soilparam().						TJB
  2012-Jan-16 Removed LINK_DEBUG code					BN
**********************************************************************/
{

  extern veg_lib_struct *veg_lib;
  extern option_struct options;
  extern Error_struct Error;
  extern global_param_struct global_param;

  /** Variable Declarations **/

  char                     NEWCELL;
  char                     LASTREC;
  char                     MODEL_DONE;
  char                     RUN_MODEL;
  char                    *init_STILL_STORM;
  char                     ErrStr[MAXSTRING];
  int                      rec, i, j;
  int                      veg;
  int                      dist;
  int                      band;
  int                      Ndist;
  int                      Nveg_type;
  int                      cellnum;
  int                      index;
  int                     *init_DRY_TIME;
  int                      Ncells;
  int                      cell_cnt;
  int                      startrec;
  int                      ErrorFlag;
  float                    mu;
  double                   storage;
  double                   veg_fract;
  double                   band_fract;
  double                   Clake;
  dmy_struct              *dmy;
  atmos_data_struct       *atmos;
  veg_con_struct          *veg_con;
  soil_con_struct          soil_con;
  dist_prcp_struct         prcp; /* stores information about distributed 
				    precipitation */
  filenames_struct         filenames;
  filep_struct             filep;
  lake_con_struct          lake_con;
  out_data_file_struct     *out_data_files;
  out_data_struct          *out_data;
  save_data_struct         save_data;
  
  /** Read Model Options **/
  initialize_global();
  filenames = cmd_proc(argc, argv);

#if VERBOSE
  display_current_settings(DISP_VERSION,(filenames_struct*)NULL,(global_param_struct*)NULL);
#endif

  /** Read Global Control File **/
  filep.globalparam = open_file(filenames.global,"r");
  global_param = get_global_param(&filenames, filep.globalparam);

  /** Set up output data structures **/
  out_data = create_output_list();
  out_data_files = set_output_defaults(out_data);
  fclose(filep.globalparam);
  filep.globalparam = open_file(filenames.global,"r");
  parse_output_info(&filenames, filep.globalparam, &out_data_files, out_data);

  /** Check and Open Files **/
  check_files(&filep, &filenames);

#if !OUTPUT_FORCE

  /** Read Vegetation Library File **/
  veg_lib = read_veglib(filep.veglib,&Nveg_type);

#endif // !OUTPUT_FORCE

  /** Initialize Parameters **/
  if(options.DIST_PRCP) Ndist = 2;
  else Ndist = 1;
  cellnum = -1;

  /** Make Date Data Structure **/
  dmy      = make_dmy(&global_param);

  /** allocate memory for the atmos_data_struct **/
  alloc_atmos(global_param.nrecs, &atmos);

  /** Initial state **/
  startrec = 0;
#if !OUTPUT_FORCE
  if ( options.INIT_STATE ) 
    filep.init_state = check_state_file(filenames.init_state, dmy, 
					 &global_param, options.Nlayer, 
					 options.Nnode, &startrec);

  /** open state file if model state is to be saved **/
  if ( options.SAVE_STATE && strcmp( filenames.statefile, "NONE" ) != 0 )
    filep.statefile = open_state_file(&global_param, filenames, options.Nlayer,
                                         options.Nnode);
  else filep.statefile = NULL;

#endif // !OUTPUT_FORCE

  /************************************
    Run Model for all Active Grid Cells
    ************************************/
  MODEL_DONE = FALSE;
  cell_cnt=0;
  while(!MODEL_DONE) {

    soil_con = read_soilparam(filep.soilparam, filenames.soil_dir, &cell_cnt, &RUN_MODEL, &MODEL_DONE);

    if(RUN_MODEL) {

#if QUICK_FS
      /** Allocate Unfrozen Water Content Table **/
      if(options.FROZEN_SOIL) {
	for(i=0;i<MAX_LAYERS;i++) {
	  soil_con.ufwc_table_layer[i] = (double **)malloc((QUICK_FS_TEMPS+1)*sizeof(double *));
	  for(j=0;j<QUICK_FS_TEMPS+1;j++) 
	    soil_con.ufwc_table_layer[i][j] = (double *)malloc(2*sizeof(double));
	}
	for(i=0;i<MAX_NODES;i++) {
	  soil_con.ufwc_table_node[i] = (double **)malloc((QUICK_FS_TEMPS+1)*sizeof(double *));

	  for(j=0;j<QUICK_FS_TEMPS+1;j++) 
	    soil_con.ufwc_table_node[i][j] = (double *)malloc(2*sizeof(double));
	}
      }
#endif /* QUICK_FS */

      NEWCELL=TRUE;
      cellnum++;

#if !OUTPUT_FORCE

      /** Read Grid Cell Vegetation Parameters **/
      veg_con = read_vegparam(filep.vegparam, soil_con.gridcel,
                              Nveg_type);
      calc_root_fractions(veg_con, &soil_con);

      if ( options.LAKES ) 
	lake_con = read_lakeparam(filep.lakeparam, soil_con, veg_con);

#endif // !OUTPUT_FORCE

      /** Build Gridded Filenames, and Open **/
      make_in_and_outfiles(&filep, &filenames, &soil_con, out_data_files);

      if (options.PRT_HEADER) {
        /** Write output file headers **/
        write_header(out_data_files, out_data, dmy, global_param);
      }

#if !OUTPUT_FORCE

      /** Read Elevation Band Data if Used **/
      read_snowband(filep.snowband, &soil_con);

      /** Make Precipitation Distribution Control Structure **/
      prcp     = make_dist_prcp(veg_con[0].vegetat_type_num);

#endif // !OUTPUT_FORCE

      /**************************************************
         Initialize Meteological Forcing Values That
         Have not Been Specifically Set
       **************************************************/

#if VERBOSE
      fprintf(stderr,"Initializing Forcing Data\n");
#endif /* VERBOSE */

      initialize_atmos(atmos, dmy, filep.forcing,
#if OUTPUT_FORCE
		       &soil_con, out_data_files, out_data); 
#else /* OUTPUT_FORCE */
                       &soil_con); 
#endif /* OUTPUT_FORCE */

#if !OUTPUT_FORCE

      /**************************************************
        Initialize Energy Balance and Snow Variables 
      **************************************************/

#if VERBOSE
      fprintf(stderr,"Model State Initialization\n");
#endif /* VERBOSE */
      ErrorFlag = initialize_model_state(&prcp, dmy[0], &global_param, filep, 
			     soil_con.gridcel, veg_con[0].vegetat_type_num,
			     options.Nnode, Ndist, 
			     atmos[0].air_temp[NR],
			     &soil_con, veg_con, lake_con,
			     &init_STILL_STORM, &init_DRY_TIME);
      if ( ErrorFlag == ERROR ) {
	if ( options.CONTINUEONERROR == TRUE ) {
	  // Handle grid cell solution error
	  fprintf(stderr, "ERROR: Grid cell %i failed in record %i so the simulation has not finished.  An incomplete output file has been generated, check your inputs before rerunning the simulation.\n", soil_con.gridcel, rec);
	  break;
	} else {
	  // Else exit program on cell solution error as in previous versions
	  sprintf(ErrStr, "ERROR: Grid cell %i failed in record %i so the simulation has ended. Check your inputs before rerunning the simulation.\n", soil_con.gridcel, rec);
	  vicerror(ErrStr);
	}
      }
      
#if VERBOSE
      fprintf(stderr,"Running Model\n");
#endif /* VERBOSE */

      /** Update Error Handling Structure **/
      Error.filep = filep;
      Error.out_data_files = out_data_files;

      /** Initialize the storage terms in the water and energy balances **/
      /** Sending a negative record number (-global_param.nrecs) to dist_prec() will accomplish this **/
      ErrorFlag = dist_prec(&atmos[0], &prcp, &soil_con, veg_con,
		  &lake_con, dmy, &global_param, &filep, out_data_files,
		  out_data, &save_data, -global_param.nrecs, cellnum,
                  NEWCELL, LASTREC, init_STILL_STORM, init_DRY_TIME);

      /******************************************
	Run Model in Grid Cell for all Time Steps
	******************************************/

      for ( rec = startrec ; rec < global_param.nrecs; rec++ ) {

        if ( rec == global_param.nrecs - 1 ) LASTREC = TRUE;
        else LASTREC = FALSE;

        ErrorFlag = dist_prec(&atmos[rec], &prcp, &soil_con, veg_con,
		  &lake_con, dmy, &global_param, &filep,
		  out_data_files, out_data, &save_data, rec, cellnum,
                  NEWCELL, LASTREC, init_STILL_STORM, init_DRY_TIME);

        if ( ErrorFlag == ERROR ) {
          if ( options.CONTINUEONERROR == TRUE ) {
            // Handle grid cell solution error
            fprintf(stderr, "ERROR: Grid cell %i failed in record %i so the simulation has not finished.  An incomplete output file has been generated, check your inputs before rerunning the simulation.\n", soil_con.gridcel, rec);
            break;
          } else {
	    // Else exit program on cell solution error as in previous versions
            sprintf(ErrStr, "ERROR: Grid cell %i failed in record %i so the simulation has ended. Check your inputs before rerunning the simulation.\n", soil_con.gridcel, rec);
            vicerror(ErrStr);
	  }
        }

        NEWCELL=FALSE;
	for ( veg = 0; veg <= veg_con[0].vegetat_type_num; veg++ )
	  init_DRY_TIME[veg] = -999;

      }	/* End Rec Loop */

#endif /* !OUTPUT_FORCE */

      close_files(&filep,out_data_files,&filenames); 

#if !OUTPUT_FORCE

#if QUICK_FS
      if(options.FROZEN_SOIL) {
	for(i=0;i<MAX_LAYERS;i++) {
	  for(j=0;j<6;j++) 
	    free((char *)soil_con.ufwc_table_layer[i][j]);
	  free((char *)soil_con.ufwc_table_layer[i]);
	}
	for(i=0;i<MAX_NODES;i++) {
	  for(j=0;j<6;j++) 
	    free((char *)soil_con.ufwc_table_node[i][j]);
	  free((char *)soil_con.ufwc_table_node[i]);
	}
      }
#endif /* QUICK_FS */
      free_dist_prcp(&prcp,veg_con[0].vegetat_type_num);
      free_vegcon(&veg_con);
      free((char *)soil_con.AreaFract);
      free((char *)soil_con.BandElev);
      free((char *)soil_con.Tfactor);
      free((char *)soil_con.Pfactor);
      free((char *)soil_con.AboveTreeLine);
      free((char*)init_STILL_STORM);
      free((char*)init_DRY_TIME);
#endif /* !OUTPUT_FORCE */
    }	/* End Run Model Condition */
  } 	/* End Grid Loop */

  /** cleanup **/
  free_atmos(global_param.nrecs, &atmos);
  free_dmy(&dmy);
  free_out_data_files(&out_data_files);
  free_out_data(&out_data);
#if !OUTPUT_FORCE
  free_veglib(&veg_lib);
#endif /* !OUTPUT_FORCE */
  fclose(filep.soilparam);
#if !OUTPUT_FORCE
  fclose(filep.vegparam);
  fclose(filep.veglib);
  if (options.SNOW_BAND>1)
    fclose(filep.snowband);
  if (options.LAKES)
    fclose(filep.lakeparam);
  if ( options.INIT_STATE )
    fclose(filep.init_state);
  if ( options.SAVE_STATE && strcmp( filenames.statefile, "NONE" ) != 0 )
    fclose(filep.statefile);

#endif /* !OUTPUT_FORCE */

  return EXIT_SUCCESS;
}	/* End Main Program */
Ejemplo n.º 7
0
	void write_data( t_uint8 id, t_uint16 data )
	{
		write_header( id, XTYPE_DATA, data );
	}
Ejemplo n.º 8
0
void VrmlExporter::begin() {
    file_.open(file_name_, std::ios::out);
    write_header();
}
Ejemplo n.º 9
0
/*************************************************************************
 * int main
 *************************************************************************/
int main(int argc, char *argv[])
{
  /* Data structures. */
  int       num_motifs;         /* The number of motifs in the model. */
  MOTIF_T   motifs[2 * MAX_MOTIFS]; /* The motifs. */
  STRING_LIST_T* motif_occurrences = NULL; /* Strings describing occurrences of
                                              motifs */
  BOOLEAN_T has_reverse_strand = FALSE;    /* MEME file contained both strands */
  ARRAY_T*  background;         /* Background probs for alphabet. */
  ORDER_T*  order_spacing;      /* Linear HMM order and spacing. */
  MATRIX_T* transp_freq = NULL; /* Matrix of inter-motif transitions freqs. */
  MATRIX_T* spacer_ave = NULL;  /* Matrix of average spacer lengths. */
  MHMM_T *  the_hmm = NULL;     /* The HMM being constructed. */

  /* Command line parameters. */
  char *    meme_filename;      /* Input file containg motifs. */
  char *    hmm_type_str;       /* HMM type. */
  HMM_T     hmm_type;
  STRING_LIST_T* requested_motifs; /* Indices of requested motifs. */
  int       request_n;          /* The user asked for the first n motifs. */
  double    e_threshold;        /* E-value threshold for motif inclusion. */
  double    complexity_threshold; // For eliminating low-complexity motifs.
  double    p_threshold;        /* p-value threshold for motif occurences. */
  char*     order_string;       /* Motif order and spacing. */
  int       spacer_states;      /* Number of states in each spacer. */
  BOOLEAN_T fim;                /* Represent spacers as free insertion
				   modules? */
  BOOLEAN_T keep_unused;        // Drop unused inter-motif transitions?
  double    trans_pseudo;       /* Transition pseudocount. */
  double    spacer_pseudo;      // Spacer (self-loop) pseudocount. */
  char*     description;        // Descriptive text to be stored in model.
  BOOLEAN_T print_header;       /* Print file header? */
  BOOLEAN_T print_params;       /* Print parameter summary? */
  BOOLEAN_T print_time;         /* Print timing data (dummy: always false). */

  /* Local variables. */
  int       i_motif;

  /**********************************************
   * COMMAND LINE PROCESSING
   **********************************************/
  // Define command line options.
  cmdoption const options[] = {
    {"type", OPTIONAL_VALUE},
    {"description", REQUIRED_VALUE},
    {"motif", REQUIRED_VALUE},
    {"nmotifs", REQUIRED_VALUE},
    {"ethresh", REQUIRED_VALUE},
    {"lowcomp", REQUIRED_VALUE},
    {"pthresh", REQUIRED_VALUE},
    {"order", REQUIRED_VALUE},
    {"nspacer", REQUIRED_VALUE},
    {"fim", NO_VALUE},
    {"keep-unused", NO_VALUE},
    {"transpseudo", REQUIRED_VALUE},
    {"spacerpseudo", REQUIRED_VALUE},
    {"verbosity", REQUIRED_VALUE},
    {"noheader", NO_VALUE},
    {"noparams", NO_VALUE},
    {"notime", NO_VALUE},
    {"quiet", NO_VALUE},
  };
  int option_count = 18;
  int option_index = 0;

  // Define the usage message.
  char      usage[1000] = "";
  strcat(usage, "USAGE: mhmm [options] <MEME file>\n");
  strcat(usage, "\n");
  strcat(usage, "   Options:\n");
  strcat(usage, "     --type [linear|complete|star] (default=linear)\n");
  strcat(usage, "     --description <string> (may be repeated)\n");
  strcat(usage, "     --motif <motif #> (may be repeated)\n");
  strcat(usage, "     --nmotifs <#>\n");
  strcat(usage, "     --ethresh <E-value>\n");
  strcat(usage, "     --lowcomp <value>\n");
  strcat(usage, "     --pthresh <p-value>\n");
  strcat(usage, "     --order <string>\n");
  strcat(usage, "     --nspacer <spacer length> (default=1)\n");
  strcat(usage, "     --fim\n");
  strcat(usage, "     --keep-unused\n");
  strcat(usage, "     --transpseudo <pseudocount>\n");
  strcat(usage, "     --spacerpseudo <pseudocount>\n");
  strcat(usage, "     --verbosity 1|2|3|4|5 (default=2)\n");
  strcat(usage, "     --noheader\n");
  strcat(usage, "     --noparams\n");
  strcat(usage, "     --notime\n");
  strcat(usage, "     --quiet\n");
  strcat(usage, "\n");

  /* Make sure various options are set to NULL or defaults. */
  meme_filename = NULL;
  hmm_type_str = NULL;
  hmm_type = INVALID_HMM;
  requested_motifs = new_string_list();
  request_n = 0;
  e_threshold = 0.0;
  complexity_threshold = 0.0;
  p_threshold = 0.0;
  order_string = NULL;
  spacer_states = DEFAULT_SPACER_STATES,
  fim = FALSE;
  keep_unused = FALSE;
  trans_pseudo = DEFAULT_TRANS_PSEUDO;
  spacer_pseudo = DEFAULT_SPACER_PSEUDO;
  description = NULL;
  print_header = TRUE;
  print_params = TRUE;
  print_time = FALSE;

	simple_setopt(argc, argv, option_count, options);

  // Parse the command line.
  while (1) { 
    int c = 0;
    char* option_name = NULL;
    char* option_value = NULL;
    const char * message = NULL;


    // Read the next option, and break if we're done.
    c = simple_getopt(&option_name, &option_value, &option_index);
    if (c == 0) {
      break;
    } else if (c < 0) {
    	simple_getopterror(&message);
      die("Error processing command line options (%s)\n", message);
    }

    if (strcmp(option_name, "type") == 0) {
			if (option_value != NULL) {
      	hmm_type_str = option_value;
			}
    } else if (strcmp(option_name, "description") == 0) {
      description = option_value;
    } else if (strcmp(option_name, "motif") == 0) {
      add_string(option_value, requested_motifs);
    } else if (strcmp(option_name, "nmotifs") == 0) {
      request_n = atoi(option_value);
    } else if (strcmp(option_name, "ethresh") == 0) {
      e_threshold = atof(option_value);
    } else if (strcmp(option_name, "lowcomp") == 0) {
      complexity_threshold = atof(option_value);
    } else if (strcmp(option_name, "pthresh") == 0) {
      p_threshold = atof(option_value);
    } else if (strcmp(option_name, "order") == 0) {
      order_string = option_value;
    } else if (strcmp(option_name, "nspacer") == 0) {
      spacer_states = atoi(option_value);
    } else if (strcmp(option_name, "fim") == 0) {
      fim = TRUE;
    } else if (strcmp(option_name, "keep-unused") == 0) {
      keep_unused = TRUE;
    } else if (strcmp(option_name, "transpseudo") == 0) {
      trans_pseudo = atof(option_value);
    } else if (strcmp(option_name, "spacerpseudo") == 0) {
      spacer_pseudo = atof(option_value);
    } else if (strcmp(option_name, "verbosity") == 0) {
      verbosity = (VERBOSE_T)atoi(option_value);
    } else if (strcmp(option_name, "noheader") == 0) {
      print_header = FALSE;
    } else if (strcmp(option_name, "noparams") == 0) {
      print_params = FALSE;
    } else if (strcmp(option_name, "notime") == 0) {
      print_time = FALSE;
    } else if (strcmp(option_name, "quiet") == 0) {
      print_header = print_params = print_time = FALSE;
      verbosity = QUIET_VERBOSE;
    }
  }

  // Read the single required argument.
  if (option_index + 1 != argc) {
    fprintf(stderr, "%s", usage);
    exit(1);
  }
  meme_filename = argv[option_index];

  // Set up motif requests. 
  if (request_n != 0) {
    if (get_num_strings(requested_motifs) != 0) {
      die("Can't combine the -motif and -nmotifs options.\n");
    } else {
      for (i_motif = 0; i_motif < request_n; i_motif++) {
        char motif_id[MAX_MOTIF_ID_LENGTH + 1];
        sprintf(motif_id, "%d", i_motif + 1);
        add_string(motif_id, requested_motifs);
      }
    }
  }

  /* Set the model type. */
  hmm_type = convert_enum_type_str(hmm_type_str, LINEAR_HMM, HMM_STRS, 
				   NUM_HMM_T);

  /* Gotta have positive spacer length. */
  if (spacer_states <= 0) {
    die("Negative spacer length (%d).\n", spacer_states);
  }

  /* Make sure motifs weren't selected redundantly. */
  // FIXME: Add tests for complexity threshold.
  if ((get_num_strings(requested_motifs) != 0) && (e_threshold != 0.0)) {
    die("Can't use -motif or -nmotifs with -ethresh.");
  }
  if ((get_num_strings(requested_motifs) != 0) && (order_string != NULL)) {
    die("Can't use -motif or -nmotifs with -order.");
  }
  if ((order_string != NULL) && (e_threshold != 0.0)) {
    die("Can't use -ethresh and -order.");
  }

  /* Prevent trying to build a complete or star model with ordering. */
  if (order_string != NULL) {
    if (hmm_type == COMPLETE_HMM) 
      die("Can't specify motif order with a completely connected model.");
    else if (hmm_type == STAR_HMM)
      die("Can't specify motif order with a star model.");
  } 

  // Parse the order string. 
  order_spacing = create_order(order_string);

  /**********************************************
   * READING THE MOTIFS
   **********************************************/

  BOOLEAN_T read_file = FALSE;
  double pseudocount = 0;

  read_meme_file(
		 meme_filename,
		 "motif-file", // Take bg freq. from motif file.
		 pseudocount,
     REQUIRE_PSPM,
		 &num_motifs,
		 motifs,
		 &motif_occurrences,
		 &has_reverse_strand,
		 &background
		 );

  process_raw_motifs_for_model(
       &num_motifs,
       motifs,
       motif_occurrences,
       requested_motifs,
       has_reverse_strand,
       keep_unused,
       p_threshold,
       e_threshold, 
       complexity_threshold, 
       &order_spacing,
       &transp_freq,
       &spacer_ave,
       trans_pseudo,
       spacer_pseudo
  );

  /**********************************************
   * BUILDING THE HMM
   **********************************************/

  /* Build the motif-based HMM. */
  if (hmm_type == LINEAR_HMM) {

    if (order_spacing != NULL) {
      reorder_motifs(order_spacing, &num_motifs, motifs);
    }
    else {
      die("No order specified for the motifs.\n"
          "For the linear model the motif file must contain motif occurence\n" 
          "data or the motif order must be specified using "
          "the --order option.");
    }

    build_linear_hmm(
      background,
		  order_spacing,
		  spacer_states,
		  motifs,
		  num_motifs, 
		  fim,
		  &the_hmm
    );

  } else if (hmm_type == COMPLETE_HMM) {

    build_complete_hmm(
      background,
		  spacer_states,
		  motifs,
		  num_motifs,
		  transp_freq,
		  spacer_ave,
		  fim,
		  &the_hmm
    );

  } else if (hmm_type == STAR_HMM) {

    build_star_hmm(
      background,
		  spacer_states,
		  motifs,
		  num_motifs,
		  fim,
		  &the_hmm
    );

  }

  // Add some global information.
  copy_string(&(the_hmm->motif_file), meme_filename);

  /**********************************************
   * WRITING THE HMM
   **********************************************/

  /* Print the header. */
  if (print_header)
    write_header(
     program, 
     "",
		 description,
		 meme_filename,
		 NULL,
		 NULL, 
		 stdout
    );

  /* Write the HMM. */
  write_mhmm(verbosity, the_hmm, stdout);

  /* Print the program parameters. */
  if (print_params) {
    printf("Program parameters for mhmm\n");
    printf("  MEME file: %s\n", meme_filename);
    printf("  Motifs:");
    write_string_list(" ", requested_motifs, stdout);
    printf("\n");
    printf("  Model topology: %s\n",
	   convert_enum_type(hmm_type, HMM_STRS, NUM_HMM_T));
    printf("  States per spacer: %d\n", spacer_states);
    printf("  Spacers are free-insertion modules: %s\n",
	   boolean_to_string(fim));
    printf("\n");
  }

  free_array(background);
  free_string_list(requested_motifs);
  free_order(order_spacing);
  free_matrix(transp_freq);
  free_matrix(spacer_ave);
  for (i_motif = 0; i_motif < num_motifs; i_motif++)
    free_motif(&(motifs[i_motif]));
  free_mhmm(the_hmm);
  return(0);
}
Ejemplo n.º 10
0
/*=== write_type_header ===================================================
 * <Description Text> 
 *
 * Assumes:    ?
 *
 * Returns:    nothing
 *
 * Modifies:   ?
 *
 * Calls:      nothing
 *
 * Called By:  ?
 *
 * History:    MM/DD/YY  MRH  Created initial version based on function
 *                            'func()' in motif/csr/file.c
 */
void
write_type_header(FILE *header)
{
  char   description[512];
  FIELD *field;
  char   define_name[80];
  
  /*
   * Write header comment
   */
  sprintf(description, "\
Public interface to the '%s' Abstract Datatype.  \
This file was automatically generated.",
          gAPIType);
  write_header(header, gTypeHeaderName, description);

  fprintf(header, "#ifndef _%s_\n", gTypeHeaderNameUC);
  fprintf(header, "#define _%s_\n", gTypeHeaderNameUC);

  /*
   * extern "C" if a C++ compiler.
   */
  fprintf(header, "\n#ifdef %s\n", CPLUSPLUS);
  fprintf(header, "extern \"C\"\n{\n");
  fprintf(header, "#endif\n");
  
  /*
   * #Defines for string lengths of objects.
   */
  sprintf(description, "\n\n\
The following are '#define's of the string lengths of each of the %s \
field objects.",
          gAPIType);
  write_comment(header, 0, description);

  for (field = gFields; field; field = field->next)
  {
    if (field->exclude ||
        (field->join && ! field->is_master_join))
      continue;

    if (field->field_type == FIELD_STRING_TYPE)
    {
      sprintf(define_name, "GAL_%s_%s_LEN", gAPINameUC, ((field->has_alias)
                                                         ? field->alias_uc
                                                         : field->name_uc));
      write_define(header, define_name, field->field_length + 1);
    }
  }

  /*
   * Insert type prototype.  The key is public knowledge.
   */
  sprintf(description, "\n\n\
%s provides a unique key interface to the %s abstract datatype.",
          gAPIKeyType, gAPIType);
  write_comment(header, 0, description);
  fprintf(header, "typedef struct _gal_%s_key_data_\n{\n", gAPIName);
  
  for (field = gFields; field; field = field->next)
    if (field->is_key)
      write_object_field(header, field);
  
  fprintf(header, "\n}");
  fprintf(header,  "               %s,\n", gAPIKeyTypeData);
  fprintf(header, "               *%s;\n", gAPIKeyType);

  /*
   * extern "C" if a C++ compiler.
   */
  fprintf(header, "\n#ifdef %s\n", CPLUSPLUS);
  fprintf(header, "}\n");
  fprintf(header, "#endif\n\n");

  fprintf(header, "#endif /* #ifndef _%s_ */\n", gTypeHeaderNameUC);
}
Ejemplo n.º 11
0
/*
 * This filter is used to en/de-cipher data with a symmetric algorithm
 */
int
cipher_filter_cfb (void *opaque, int control,
                   iobuf_t a, byte *buf, size_t *ret_len)
{
  cipher_filter_context_t *cfx = opaque;
  size_t size = *ret_len;
  int rc = 0;

  if (control == IOBUFCTRL_UNDERFLOW) /* decrypt */
    {
      rc = -1; /* not yet used */
    }
  else if (control == IOBUFCTRL_FLUSH) /* encrypt */
    {
      log_assert (a);
      if (!cfx->wrote_header)
        write_header (cfx, a);
      if (cfx->mdc_hash)
        gcry_md_write (cfx->mdc_hash, buf, size);
      gcry_cipher_encrypt (cfx->cipher_hd, buf, size, NULL, 0);
      if (cfx->short_blklen_warn)
        {
          cfx->short_blklen_count += size;
          if (cfx->short_blklen_count > (150 * 1024 * 1024))
            {
              log_info ("WARNING: encrypting more than %d MiB with algorithm "
                        "%s should be avoided\n", 150,
                        openpgp_cipher_algo_name (cfx->dek->algo));
              cfx->short_blklen_warn = 0; /* Don't show again.  */
            }
        }

      rc = iobuf_write (a, buf, size);
    }
  else if (control == IOBUFCTRL_FREE)
    {
      if (cfx->mdc_hash)
        {
          byte *hash;
          int hashlen = gcry_md_get_algo_dlen (gcry_md_get_algo(cfx->mdc_hash));
          byte temp[22];

          log_assert (hashlen == 20);
          /* We must hash the prefix of the MDC packet here. */
          temp[0] = 0xd3;
          temp[1] = 0x14;
          gcry_md_putc (cfx->mdc_hash, temp[0]);
          gcry_md_putc (cfx->mdc_hash, temp[1]);

          gcry_md_final (cfx->mdc_hash);
          hash = gcry_md_read (cfx->mdc_hash, 0);
          memcpy(temp+2, hash, 20);
          gcry_cipher_encrypt (cfx->cipher_hd, temp, 22, NULL, 0);
          gcry_md_close (cfx->mdc_hash); cfx->mdc_hash = NULL;
          if (iobuf_write( a, temp, 22))
            log_error ("writing MDC packet failed\n");
	}

      gcry_cipher_close (cfx->cipher_hd);
    }
  else if (control == IOBUFCTRL_DESC)
    {
      mem2str (buf, "cipher_filter_cfb", *ret_len);
    }

  return rc;
}
Ejemplo n.º 12
0
/* Same as write_arduino_code(), but uses the verbose versions of functions */
void write_arduino_code_verbose(FILE *output_file, MuxPipe *pipes, size_t total_pipes)
{
    write_header(output_file);
    write_setup_verbose(output_file, pipes, total_pipes);
    write_loop_verbose(output_file);
}
Ejemplo n.º 13
0
/* ===========================================================================
  Opens a gzip (.gz) file for reading or writing. The mode parameter
  is as in fopen ("rb" or "wb"). The file is given either by file descriptor
  or path name (if fd == -1).
  az_open returns NULL if the file could not be opened or if there was
  insufficient memory to allocate the (de)compression state; errno
  can be checked to distinguish the two cases (if errno is zero, the
  zlib error is Z_MEM_ERROR).
*/
int az_open (azio_stream *s, const char *path, int Flags, File fd)
{
  int err;
  int level = Z_DEFAULT_COMPRESSION; /* compression level */
  int strategy = Z_DEFAULT_STRATEGY; /* compression strategy */

  s->stream.zalloc = (alloc_func)0;
  s->stream.zfree = (free_func)0;
  s->stream.opaque = (voidpf)0;
  memset(s->inbuf, 0, AZ_BUFSIZE_READ);
  memset(s->outbuf, 0, AZ_BUFSIZE_WRITE);
  s->stream.next_in = s->inbuf;
  s->stream.next_out = s->outbuf;
  s->stream.avail_in = s->stream.avail_out = 0;
  s->z_err = Z_OK;
  s->z_eof = 0;
  s->in = 0;
  s->out = 0;
  s->back = EOF;
  s->crc = crc32(0L, Z_NULL, 0);
  s->transparent = 0;
  s->mode = 'r';
  s->version = (unsigned char)az_magic[1]; /* this needs to be a define to version */
  s->minor_version= (unsigned char) az_magic[2]; /* minor version */
  s->dirty= AZ_STATE_CLEAN;

  /*
    We do our own version of append by nature. 
    We must always have write access to take card of the header.
  */
  DBUG_ASSERT(Flags | O_APPEND);
  DBUG_ASSERT(Flags | O_WRONLY);

  if (Flags & O_RDWR) 
    s->mode = 'w';

  if (s->mode == 'w') 
  {
    err = deflateInit2(&(s->stream), level,
                       Z_DEFLATED, -MAX_WBITS, 8, strategy);
    /* windowBits is passed < 0 to suppress zlib header */

    s->stream.next_out = s->outbuf;
    if (err != Z_OK)
    {
      destroy(s);
      return Z_NULL;
    }
  } else {
    s->stream.next_in  = s->inbuf;

    err = inflateInit2(&(s->stream), -MAX_WBITS);
    /* windowBits is passed < 0 to tell that there is no zlib header.
     * Note that in this case inflate *requires* an extra "dummy" byte
     * after the compressed stream in order to complete decompression and
     * return Z_STREAM_END. Here the gzip CRC32 ensures that 4 bytes are
     * present after the compressed stream.
   */
    if (err != Z_OK)
    {
      destroy(s);
      return Z_NULL;
    }
  }
  s->stream.avail_out = AZ_BUFSIZE_WRITE;

  errno = 0;
  s->file = fd < 0 ? my_open(path, Flags, MYF(0)) : fd;

  if (s->file < 0 ) 
  {
    destroy(s);
    return Z_NULL;
  }

  if (Flags & O_CREAT || Flags & O_TRUNC) 
  {
    s->rows= 0;
    s->forced_flushes= 0;
    s->shortest_row= 0;
    s->longest_row= 0;
    s->auto_increment= 0;
    s->check_point= 0;
    s->comment_start_pos= 0;
    s->comment_length= 0;
    s->frm_start_pos= 0;
    s->frm_length= 0;
    s->dirty= 1; /* We create the file dirty */
    s->start = AZHEADER_SIZE + AZMETA_BUFFER_SIZE;
    write_header(s);
    my_seek(s->file, 0, MY_SEEK_END, MYF(0));
  }
  else if (s->mode == 'w') 
  {
    uchar buffer[AZHEADER_SIZE + AZMETA_BUFFER_SIZE];
    my_pread(s->file, buffer, AZHEADER_SIZE + AZMETA_BUFFER_SIZE, 0,
             MYF(0));
    read_header(s, buffer); /* skip the .az header */
    my_seek(s->file, 0, MY_SEEK_END, MYF(0));
  }
  else
  {
    check_header(s); /* skip the .az header */
  }

  return 1;
}
Ejemplo n.º 14
0
Archivo: print_data.c Proyecto: hfs/afd
/*######################### print_data_button() #########################*/
void
print_data_button(Widget w, XtPointer client_data, XtPointer call_data)
{
   char message[MAX_MESSAGE_LENGTH],
        sum_sep_line[MAX_OUTPUT_LINE_LENGTH + SHOW_LONG_FORMAT + 1];

   /* Prepare separator line. */
   (void)memset(sum_sep_line, '=', sum_line_length);
   sum_sep_line[sum_line_length] = '\0';

   if (range_type == SELECTION_TOGGLE)
   {
      int no_selected,
          *select_list;

      if (XmListGetSelectedPos(listbox_w, &select_list, &no_selected) == False)
      {
         show_message(statusbox_w, "No data selected for printing!");
         XtPopdown(printshell);

         return;
      }
      else
      {
         int           fd,
                       prepare_status;
         char          *line,
                       line_buffer[256];
         XmStringTable all_items;

         if (device_type == PRINTER_TOGGLE)
         {
            prepare_status = prepare_printer(&fd);
         }
         else
         {
            prepare_status = prepare_file(&fd, (device_type == MAIL_TOGGLE) ? 0 : 1);
            if ((prepare_status != SUCCESS) && (device_type == MAIL_TOGGLE))
            {
               prepare_tmp_name();
               prepare_status = prepare_file(&fd, 1);
            }
         }
         if (prepare_status == SUCCESS)
         {
            register int  i,
                          length;

            write_header(fd, sum_sep_line);

            XtVaGetValues(listbox_w, XmNitems, &all_items, NULL);
            for (i = 0; i < no_selected; i++)
            {
               XmStringGetLtoR(all_items[select_list[i] - 1], XmFONTLIST_DEFAULT_TAG, &line);
               length = sprintf(line_buffer, "%s\n", line);
               if (write(fd, line_buffer, length) != length)
               {
                  (void)fprintf(stderr, "write() error : %s (%s %d)\n",
                                strerror(errno), __FILE__, __LINE__);
                  XtFree(line);
                  exit(INCORRECT);
               }
               XtFree(line);
               XmListDeselectPos(listbox_w, select_list[i]);
            }
            write_summary(fd, sum_sep_line);

            /*
             * Remember to insert the correct summary, since all files
             * have now been deselected.
             */
            (void)strcpy(summary_str, total_summary_str);
            SHOW_SUMMARY_DATA();

            if (device_type == PRINTER_TOGGLE)
            {
               int  status;
               char buf;

               /* Send Control-D to printer queue. */
               buf = CONTROL_D;
               if (write(fd, &buf, 1) != 1)
               {
                  (void)fprintf(stderr, "write() error : %s (%s %d)\n",
                                strerror(errno), __FILE__, __LINE__);
                  XtFree(line);
                  exit(INCORRECT);
               }

               if ((status = pclose(fp)) < 0)
               {
                  (void)sprintf(message,
                                "Failed to send printer command (%d) : %s",
                                status, strerror(errno));
               }
               else
               {
                  (void)sprintf(message, "Send job to printer (%d)", status);
               }
            }
            else
            {
               if (close(fd) < 0)
               {
                  (void)fprintf(stderr, "close() error : %s (%s %d)\n",
                                strerror(errno), __FILE__, __LINE__);
               }
               if (device_type == MAIL_TOGGLE)
               {
                  send_mail_cmd(message);
               }
               else
               {
                  (void)sprintf(message, "Send job to file %s.", file_name);
               }
            }
         }
         XtFree((char *)select_list);
      }
   }
   else /* Print everything! */
   {
      int           fd,
                    no_of_items,
                    prepare_status;
      char          *line,
                    line_buffer[256];
      XmStringTable all_items;

      if (device_type == PRINTER_TOGGLE)
      {
         prepare_status = prepare_printer(&fd);
      }
      else
      {
         prepare_status = prepare_file(&fd, (device_type == MAIL_TOGGLE) ? 0 : 1);
         if ((prepare_status != SUCCESS) && (device_type == MAIL_TOGGLE))
         {
            prepare_tmp_name();
            prepare_status = prepare_file(&fd, 1);
         }
      }
      if (prepare_status == SUCCESS)
      {
         register int i,
                      length;

         write_header(fd, sum_sep_line);

         XtVaGetValues(listbox_w,
                       XmNitemCount, &no_of_items,
                       XmNitems,     &all_items,
                       NULL);
         for (i = 0; i < no_of_items; i++)
         {
            XmStringGetLtoR(all_items[i], XmFONTLIST_DEFAULT_TAG, &line);
            length = sprintf(line_buffer, "%s\n", line);
            if (write(fd, line_buffer, length) != length)
            {
               (void)fprintf(stderr, "write() error : %s (%s %d)\n",
                             strerror(errno), __FILE__, __LINE__);
               XtFree(line);
               exit(INCORRECT);
            }
            XtFree(line);
         }
         write_summary(fd, sum_sep_line);

         if (device_type == PRINTER_TOGGLE)
         {
            int  status;
            char buf;

            /* Send Control-D to printer queue. */
            buf = CONTROL_D;
            if (write(fd, &buf, 1) != 1)
            {
               (void)fprintf(stderr, "write() error : %s (%s %d)\n",
                             strerror(errno), __FILE__, __LINE__);
               XtFree(line);
               exit(INCORRECT);
            }

            if ((status = pclose(fp)) < 0)
            {
               (void)sprintf(message, "Failed to send printer command (%d) : %s",
                             status, strerror(errno));
            }
            else
            {
               (void)sprintf(message, "Send job to printer (%d)", status);
            }
         }
         else
         {
            if (close(fd) < 0)
            {
               (void)fprintf(stderr, "close() error : %s (%s %d)\n",
                             strerror(errno), __FILE__, __LINE__);
            }
            if (device_type == MAIL_TOGGLE)
            {
               send_mail_cmd(message);
            }
            else
            {
               (void)sprintf(message, "Send job to file %s.", file_name);
            }
         }
      }
   }

   show_message(statusbox_w, message);
   XtPopdown(printshell);

   return;
}
Ejemplo n.º 15
0
Archivo: vmware3.cpp Proyecto: iver6/BA
/* This could be done much better, I'm sure. In fact, the whole header doesn't
 * need to be re-written each time a new tlb is allocated nor does the whole
 * slb need to be re-written (most of the time) but that can be changed whenever
 * it becomes an issue... image I/O is not a bottleneck.
 */
bool vmware3_image_t::sync()
{
    if(current->synced)
        return true;

    unsigned relative_offset = (unsigned)(current->offset - current->min_offset);
    unsigned i = relative_offset >> FL_SHIFT;
    unsigned j = (relative_offset & ~FL_MASK) / tlb_size;

    if (current->slb[i][j] == 0)
    {
        if (current->flb[i] == 0)
        {
            unsigned slb_size = slb_count * 4;

            /* Re-write the FLB */
            current->flb[i] = current->header.next_sector_to_allocate;
            if(::lseek(current->fd, current->header.flb_offset_sectors * 512, SEEK_SET) < 0)
            {
                BX_DEBUG(("could not seek vmware3 COW image to flb on sync"));
                return false;
            }
            if(write_ints(current->fd, current->flb, current->header.flb_count) < 0)
            {
                BX_DEBUG(("could not re-write flb to vmware3 COW image on sync"));
                return false;
            }
            current->header.next_sector_to_allocate += (slb_size / 512) + ((slb_size % 512) ? 1 : 0);
        }

        /* Re-write the SLB */
        current->slb[i][j] = current->header.next_sector_to_allocate;
        if(::lseek(current->fd, current->flb[i] * 512, SEEK_SET) < 0)
        {
            BX_DEBUG(("could not seek vmware3 COW image to slb on sync"));
            return false;
        }
        if(write_ints(current->fd, current->slb[i], slb_count) < 0)
        {
            BX_DEBUG(("could not re-write slb to vmware3 COW image on sync"));
            return false;
        }
        current->header.next_sector_to_allocate += current->header.tlb_size_sectors;

        /* Update the header */
        if(::lseek(current->fd, 0, SEEK_SET) < 0)
        {
            BX_DEBUG(("could not seek to vmware3 COW image to offset 0 on sync"));
            return false;
        }
        if(write_header(current->fd, current->header) < 0)
        {
            BX_DEBUG(("could not re-write header to vmware3 COW image on sync"));
            return false;
        }
    }
    if(::lseek(current->fd, current->slb[i][j] * 512, SEEK_SET) < 0)
    {
        BX_DEBUG(("could not seek vmware3 COW image to offset %d on sync", current->slb[i][j] * 512));
        return false;
    }
    if(::write(current->fd, current->tlb, tlb_size) < 0)
    {
        BX_DEBUG(("could not write tlb to vmware3 COW image on sync"));
        return false;
    }
    current->synced = true;
    return true;
}
Ejemplo n.º 16
0
void csa_new_from_bwt(CSA csa, char *fname, char *fidx, int psi_id, int idx_id, bool coded)
{
	int k;
	i64 i,j,v,m;
	FILE *f2;
	i64 psize,isize;
	i64 n;

	psi_id = csa.id;
	if (psi_id >= 0) {
		printf("create psi: id=%d\n",psi_id);
	}
	if (idx_id >= 0) {
		printf("create idx: id=%d D=%d D2=%d\n",idx_id,csa.D,csa.D2);
	}

	psize = 0;

	if (psi_id >= 0) {
		switch (psi_id & 0x3f) {
			case ID_DIFF_GAMMA:
			case ID_DIFF_GAMMA_RL:
			case ID_DIFF_GAMMA_SPARSE:
			case ID_DIFF_GAMMA_RL_SPARSE:
				psize = psi1_makeindex(&csa, fname);
				printf("n     %ld\n",csa.n);
				printf("Psi   %ld bytes (%1.3f bpc)\n",
						psize,(double)psize*8/csa.n);
				break;
			case ID_DIFF_GAMMA_RR:
				psize = psi12_makeindex(&csa, fname);
				printf("n     %ld\n",csa.n);
				printf("Psi   %ld bytes (%1.3f bpc)\n",
						psize,(double)psize*8/csa.n);
				break;
			case ID_BWT_DNA:
				psize = lf_dna_makeindex(&csa, fname, coded);
				printf("n     %ld\n",csa.n);
				printf("BW    %ld bytes (%1.3f bpc)\n",
						psize,(double)psize*8/csa.n);
				break;
			case ID_BWT_BIT:
				psize = lf_bit_makeindex(&csa, fname);
				printf("n     %ld\n",csa.n);
				printf("BW    %ld bytes (%1.3f bpc)\n",
						psize,(double)psize*8/csa.n);
				break;
			case ID_BWT_WT:
			case ID_BWT_WT_HUF:
			case ID_BWT_WT_DENSE:
			case ID_BWT_WT_SPARSE4:
			case ID_BWT_WT_RR:
				psize = lf_wt_makeindex(&csa, fname);
				printf("n     %ld\n",csa.n);
				printf("BW    %ld bytes (%1.3f bpc)\n",
						psize,(double)psize*8/csa.n);
				break;
#if 0
			case ID_BWT_HUF:
				psize = lf_bwt_makeindex(&csa, fname);
				printf("n     %ld\n",csa.n);
				printf("BW    %ld bytes (%1.3f bpc)\n",
						psize,(double)psize*8/csa.n);
				break;
#endif
			case ID_SPARSE4:
				psize = psi2_makeindex(&csa, fname);
				printf("n     %ld\n",csa.n);
				printf("Psi   %ld bytes (%1.3f bpc)\n",
						psize,(double)psize*8/csa.n);
				break;
			default:
				printf("psi_id = %d\n",psi_id);
				exit(1);
		}
	}

	csa.k = (blog(csa.n+1)+1+8-1)/8;

	for (i=0; i<SIGMA; i++) csa.CtoA[i] = -1;
	//  csa.K[-1+1] = 0;
	csa.K[-1+1] = 1;
	for (m=0,v=1,i=0; i<SIGMA; i++) {
		if (csa.C[i]>0) {
			csa.AtoC[m] = i;
			csa.CtoA[i] = m;
			csa.K[m+1] = v;
			//      printf("i=%ld v = %ld C[i] = %ld\n",i,v,csa.C[i]);
			v += csa.C[i];
			m++;
		}
	}
	csa.K[m+1] = v;
	csa.m = m;

	if (csa.D >= csa.n) {
		printf("D=%d >= n=%ld\n",csa.D,csa.n);
		exit(0);
	}
	if (csa.D2 >= csa.n) {
		printf("D2=%d >= n=%ld\n",csa.D2,csa.n);
		exit(0);
	}

	if (idx_id >= 0) {
		n = csa.n;
		k = csa.k;
		////  compute SA and ISA
		if (csa.D > 0) csa.SA = (uchar *) mymalloc(((n-1)/csa.D+1+1)*k);
		if (csa.D2 > 0) csa.ISA = (uchar *) mymalloc(((n-1)/csa.D2+1+1)*k);
		if (csa.D == 0 && csa.D2 == 0) goto brk;

		switch (psi_id & 0x3f) {
			case ID_DIFF_GAMMA:
			case ID_DIFF_GAMMA_RL:
			case ID_DIFF_GAMMA_SPARSE:
			case ID_DIFF_GAMMA_RL_SPARSE:
			case ID_SPARSE4:
			case ID_DIFF_GAMMA_RR:
				j = 0;
				for (i=0; i<=n; i++) {
					display_progressbar("making sa ",i,n);
					j = csa.psi(&csa,j);
					//  sa[j] = i;
					if (csa.D > 0 && j % csa.D == 0) {
						putuint(csa.SA,j / csa.D,i,k);
					}
					if (csa.D2 > 0 && i % csa.D2 == 0) {
						putuint(csa.ISA,i / csa.D2,j,k);
					}
				}
				//      putuint(csa.SA,0,n,k);
				break;
			case ID_BWT_DNA:
			case ID_BWT_BIT:
			case ID_BWT_WT:
			case ID_BWT_WT_HUF:
			case ID_BWT_WT_DENSE:
			case ID_BWT_WT_SPARSE4:
			case ID_BWT_WT_RR:
			case ID_BWT_HUF:
				j = 0;
				for (i=n-1; i>=0; i--) {
					display_progressbar("making sa ",i,n);
					v = csa.LF(&csa,j);
					//        printf("LF[%ld] = %ld\n",j,v);
					j = v;
					if (csa.D > 0 && j % csa.D == 0) putuint(csa.SA, j/csa.D , i, k);
					if (csa.D2 > 0 && i % csa.D2 == 0) putuint(csa.ISA, i/csa.D2, j, k);
				}
				putuint(csa.SA,0,n,k);
				break;
			default:
				break;
		}
brk:
		////      write idx
		f2 = fopen(fidx,"wb"); /* directory */
		if (f2 == NULL) {
			perror("csa2_new1: ");
			exit(1);
		}

		isize = 0;

		writeint(4,VERSION,f2); /* version */
		isize += 4;

		writeint(1,ID_HEADER,f2); // header ID
		isize += 1;
		isize = write_header(&csa, f2, isize);

		if (csa.D > 0) {
			writeint(1,ID_SA,f2);
			isize += 1;
			isize = write_sa(&csa, f2, isize);
		}

		if (csa.D2 > 0) {
			writeint(1,ID_ISA,f2);
			isize += 1;
			isize = write_isa(&csa, f2, isize);
		}


		fclose(f2);

		if (csa.D > 0) free(csa.SA);
		if (csa.D2 > 0) free(csa.ISA);

		printf("Total %ld bytes (%1.3f bpc)\n",(psize+isize),
				(double)(psize+isize)*8/csa.n);
	}
	free(fidx);
}
Ejemplo n.º 17
0
/**
@details
-# This restart is targetted at DMTCP restarts where we only need to rewrite the header.
*/
int Trick::DataRecordGroup::dmtcp_restart() {
    return write_header() ;
}
Ejemplo n.º 18
0
void SamWriter::add_header(const SamHeader& header) { 
  m_header = header;
  write_header();
}
Ejemplo n.º 19
0
Archivo: lz78.c Proyecto: LoBiagio/LZ78
/**
 * @brief In the main we can choose these options without any argument:
 * c for compress, d for decompress, h for help, v for verbose;
 *    and these options with the relative argument:
 *    i with the name of the input file, o with the name of the output file and
 *    s with the size of the dictionary.
 */
int main(int argc, char *argv []) {
    int fd, s = 0, v = 0, compr = 0, h = 0;
    //compr is set to 1 if we want to compress, set to 2 if we want to decompress
    char *source = NULL, *dest = NULL;
    unsigned int dict_size = DICT_SIZE, d_dict_size;
    struct bitio *fd_bitio;
    int opt;

    while ((opt = getopt(argc,argv,"cdhvi:o:s:")) != -1) {
        switch (opt) {
        case 'c':
            compr = (compr==2?compr:1);
            break;
        case 'd':
            compr = (compr==1?compr:2);
            break;
        case 'i':
                source = optarg; //take the input name from optarg
            break;
        case 'o':
                dest = optarg; //take the output name from optarg
            break;
        case 's':
            dict_size = atoi(optarg);
            s = 1;
            break;
        case 'h':
	    h = 1;
	    print_help();
            break;
        case 'v':
            v = 1;
            break;
        case '?':
            if(optopt == 'i'){
                fprintf(stderr,"An input file is required\n");
                exit(1);
            } else if (optopt == 'o'){
                fprintf(stderr,"No name specified for destination file\n");
                exit(1);
            } else if (optopt == 's'){
                fprintf(stderr,"No dimension specified for dictionary size\n");
                exit(1);
            } else {
                fprintf(stderr,"Try -h for help\n");
                exit(1);
            }
            break;
        default:
            fprintf(stderr,"Try -h for help\n");
            exit(1);
         } //switch (opt)
    } //while ()

    /* Checking if either -d or -c option has been set */
    if (compr == 0 && h == 0) {
	    fprintf(stderr, "Error: you must specify either -c to compress or -d to decompress\n");
	    print_help();
	    exit(1);
    }
    
    if(compr == 1) {    //compressing
        if ((fd = open(source, O_RDONLY)) < 0) {
            perror("Error opening file in read mode: ");
            exit(1);
        }
        if ((fd_bitio = bitio_open(dest, 'w')) == NULL) {
            perror("Error opening file in write mode: ");
            close(fd);
            exit(1);
        }
	if (dict_size < 512) {
		//fprintf(stderr, "Warning: for better results, you should use a dictionary size greater than 512 elements.\nThe dictionary size has been set to 512\n");
		//dict_size = 512;
	}
        write_header(fd,fd_bitio,source,dict_size);
        if(v == 1){
            printf("Compressing...\n");
        }
        compress(fd,fd_bitio,dict_size,v);
        if (v == 1){
            printf("Compress completed\n");
        }
    } //if (compr == 1)
    
    if (compr == 2) { //decompressing
        if (s == 1) { //if s is set on the decompressor we return an error
            fprintf(stderr,"Error on  specifying dictionary size\n");
            exit(1); 
        }
        if ((fd = open (dest, (O_CREAT | O_TRUNC | O_WRONLY) , 0666)) < 0) {
            perror("Error opening file in write mode: ");
            exit(1);
        }
        if ((fd_bitio = bitio_open (source, 'r')) == NULL){
            perror("Error opening file in read mode: ");
            close(fd);
            exit(1);
        }
        read_header(fd_bitio,&d_dict_size, v);
        if (v == 1) {
            printf("Decompressing...\n");
        }
        decompress(fd,fd_bitio,d_dict_size,v);
        if (v == 1) {
            printf("Decompress completed\n");
        }
    } // if (compr == 2)
    return 0;
}
Ejemplo n.º 20
0
SamWriter::SamWriter(const SamHeader& header, const std::string& output_fname, const bool binary) :
  m_out_file {open_file(output_fname, binary ? "wb" : "w")},
  m_header{header}
{
  write_header();
}
Ejemplo n.º 21
0
	void write_int( t_uint8 id, t_uint32 value )
	{
		write_header( id, XTYPE_INT, 4 );
		m_file->write_lendian_t( value, m_abort );
	}
Ejemplo n.º 22
0
int write_av_packet(AppContext *para, am_packet_t *pkt)
{
    int write_bytes = 0, len = 0, ret;
    unsigned char *buf;
    int size ;

    if (pkt->avpkt_newflag) {
        if (pkt->type != CODEC_SUBTITLE) {
            if (pkt->avpkt_isvalid) {
                ret = check_in_pts(para, pkt);
                if (ret != PLAYER_SUCCESS) {
                    log_error("check in pts failed\n");
                    return PLAYER_WR_FAILED;
                }
            }
            if (write_header(para, pkt) == PLAYER_WR_FAILED) {
                log_error("[%s]write header failed!\n", __FUNCTION__);
                return PLAYER_WR_FAILED;
            }
        } else {
            // process_es_subtitle(para, pkt);
        }
        pkt->avpkt_newflag = 0;
    }
	
    buf = pkt->data;
    size = pkt->data_size ;
    if (size == 0 && pkt->avpkt_isvalid) {
        if ((pkt->type == CODEC_VIDEO)) {
            para->write_size.vpkt_num++;
        }
        if (pkt->avpkt) {
            av_free_packet(pkt->avpkt);
        }
        pkt->avpkt_isvalid = 0;
    }
    while (size > 0 && pkt->avpkt_isvalid) {
        write_bytes = codec_write(pkt->codec, (char *)buf, size);
        if (write_bytes < 0 || write_bytes > size) {
            if (-errno != AVERROR(EAGAIN)) {
                para->playctrl_info.check_lowlevel_eagain_cnt = 0;
                log_print("write codec data failed!\n");
                return PLAYER_WR_FAILED;
            } else {
                // EAGAIN to see if buffer full or write time out too much		
                if (check_avbuffer_enough(para, pkt)) {
                  para->playctrl_info.check_lowlevel_eagain_cnt++;
                } else {
                  para->playctrl_info.check_lowlevel_eagain_cnt = 0;
                }
                
                if (para->playctrl_info.check_lowlevel_eagain_cnt > 50) {
                    // reset decoder
                    para->playctrl_info.check_lowlevel_eagain_cnt = 0;
                    para->playctrl_info.reset_flag = 1;
                    para->playctrl_info.end_flag = 1;
                    if (para->state.start_time != -1) {
                        para->playctrl_info.time_point = (para->state.pts_video - para->state.start_time)/ PTS_FREQ;
                    } else {
                        para->playctrl_info.time_point = para->state.pts_video/ PTS_FREQ;
                    }
                    
                    log_print("$$$$$$[type:%d] write blocked, need reset decoder!$$$$$$\n", pkt->type);
                }				
                pkt->data += len;
                pkt->data_size -= len;
                usleep(RW_WAIT_TIME);
                if (para->playctrl_info.check_lowlevel_eagain_cnt > 0) {
                    log_print("[%s]eagain:data_size=%d type=%d rsize=%lld wsize=%lld cnt=%d\n", \
                        __FUNCTION__, pkt->data_size, pkt->type, para->read_size.total_bytes, \
                        para->write_size.total_bytes, para->playctrl_info.check_lowlevel_eagain_cnt);
                }
                return PLAYER_SUCCESS;
            }
        } else {
            para->playctrl_info.check_lowlevel_eagain_cnt = 0;
            len += write_bytes;
            if (len == pkt->data_size) {
                if ((pkt->type == CODEC_VIDEO)) {
                    para->write_size.vpkt_num++;
                }
                if (pkt->avpkt) {
                    av_free_packet(pkt->avpkt);
                }
                pkt->avpkt_isvalid = 0;
                pkt->data_size = 0;
                //log_print("[%s:%d]write finish pkt->data_size=%d\r",__FUNCTION__, __LINE__,pkt->data_size);               
                break;
            } else if (len < pkt->data_size) {
                buf += write_bytes;
                size -= write_bytes;
            } else {
                return PLAYER_WR_FAILED;
            }

        }
    }
    if (check_write_finish(para, pkt) == PLAYER_WR_FINISH) {
        return PLAYER_WR_FINISH;
    }
    return PLAYER_SUCCESS;
}
Ejemplo n.º 23
0
/*---------------------------------------------------------------------------*/
static int
merge_log(coffee_page_t file_page, int extend)
{
  struct file_header hdr, hdr2;
  int fd, n;
  cfs_offset_t offset;
  coffee_page_t max_pages;
  struct file *new_file;
  int i;

  read_header(&hdr, file_page);

  fd = cfs_open(hdr.name, CFS_READ);
  if(fd < 0) {
    return -1;
  }

  /*
   * The reservation function adds extra space for the header, which has
   * already been accounted for in the previous reservation.
   */
  max_pages = hdr.max_pages << extend;
  new_file = reserve(hdr.name, max_pages, 1, 0);
  if(new_file == NULL) {
    cfs_close(fd);
    return -1;
  }

  offset = 0;
  do {
    char buf[hdr.log_record_size == 0 ? COFFEE_PAGE_SIZE : hdr.log_record_size];
    n = cfs_read(fd, buf, sizeof(buf));
    if(n < 0) {
      remove_by_page(new_file->page, !REMOVE_LOG, !CLOSE_FDS, ALLOW_GC);
      cfs_close(fd);
      return -1;
    } else if(n > 0) {
      COFFEE_WRITE(buf, n, absolute_offset(new_file->page, offset));
      offset += n;
    }
  } while(n != 0);

  for(i = 0; i < COFFEE_FD_SET_SIZE; i++) {
    if(coffee_fd_set[i].flags != COFFEE_FD_FREE && 
       coffee_fd_set[i].file->page == file_page) {
      coffee_fd_set[i].file = new_file;
      new_file->references++;
    }
  }

  if(remove_by_page(file_page, REMOVE_LOG, !CLOSE_FDS, !ALLOW_GC) < 0) {
    remove_by_page(new_file->page, !REMOVE_LOG, !CLOSE_FDS, !ALLOW_GC);
    cfs_close(fd);
    return -1;
  }

  /* Copy the log configuration and the EOF hint. */
  read_header(&hdr2, new_file->page);
  hdr2.log_record_size = hdr.log_record_size;
  hdr2.log_records = hdr.log_records;
  write_header(&hdr2, new_file->page);

  new_file->flags &= ~COFFEE_FILE_MODIFIED;
  new_file->end = offset;

  cfs_close(fd);

  return 0;
}
Ejemplo n.º 24
0
  int
  main( int     argc,
        char**  argv )
  {
    grEvent  event;


    /* Initialize engine */
    handle = FTDemo_New();

    parse_cmdline( &argc, &argv );

    handle->encoding  = status.encoding;
    handle->use_sbits = 0;
    FTDemo_Update_Current_Flags( handle );

    for ( ; argc > 0; argc--, argv++ )
    {
      error = FTDemo_Install_Font( handle, argv[0], 0 );

      if ( error )
      {
        fprintf( stderr, "failed to install %s", argv[0] );
        if ( error == FT_Err_Invalid_CharMap_Handle )
          fprintf( stderr, ": missing valid charmap\n" );
        else
          fprintf( stderr, "\n" );
      }
    }

    if ( handle->num_fonts == 0 )
      PanicZ( "could not open any font file" );

    display = FTDemo_Display_New( gr_pixel_mode_gray,
                                  status.width, status.height );
    display->back_color.value = 0;
    display->fore_color.value = 0xff;

    if ( !display )
      PanicZ( "could not allocate display surface" );

    grSetTitle( display->surface,
                "FreeType String Viewer - press ? for help" );

    event_gamma_change( 0 );
    event_font_change( 0 );
    status.header = 0;

    for ( ;; )
    {
      FTDemo_Display_Clear( display );

      switch ( status.render_mode )
      {
      case RENDER_MODE_STRING:
        status.sc.center = 1L << 15;
        error = FTDemo_String_Draw( handle, display,
                                    &status.sc,
                                    display->bitmap->width / 2,
                                    display->bitmap->rows / 2 );
        break;

      case RENDER_MODE_KERNCMP:
        {
          FTDemo_String_Context  sc = status.sc;
          FT_Int                 x, y;
          FT_Int                 height;


          x = 55;

          height = ( status.ptsize * status.res / 72 + 32 ) >> 6;
          if ( height < CELLSTRING_HEIGHT )
            height = CELLSTRING_HEIGHT;

          /* First line: none */
          sc.center         = 0;
          sc.kerning_mode   = 0;
          sc.kerning_degree = 0;
          sc.vertical       = 0;
          sc.matrix         = NULL;

          y = CELLSTRING_HEIGHT * 2 + display->bitmap->rows / 4 + height;
          grWriteCellString( display->bitmap, 5,
                             y - ( height + CELLSTRING_HEIGHT ) / 2,
                             "none", display->fore_color );
          error = FTDemo_String_Draw( handle, display, &sc, x, y );

          /* Second line: track kern only */
          sc.kerning_degree = status.sc.kerning_degree;

          y += height;
          grWriteCellString( display->bitmap, 5,
                             y - ( height + CELLSTRING_HEIGHT ) / 2,
                             "track", display->fore_color );
          error = FTDemo_String_Draw( handle, display, &sc, x, y );

          /* Third line: track kern + pair kern */
          sc.kerning_mode = status.sc.kerning_mode;

          y += height;
          grWriteCellString( display->bitmap, 5,
                             y - ( height + CELLSTRING_HEIGHT ) / 2,
                             "both", display->fore_color );
          error = FTDemo_String_Draw( handle, display, &sc, x, y );
        }
        break;
      }

      if ( !error && status.sc.gamma_ramp )
        gamma_ramp_draw( status.gamma_ramp, display->bitmap );

      write_header( error );

      status.header = 0;
      grListenSurface( display->surface, 0, &event );
      if ( Process_Event( &event ) )
        break;
    }

    printf( "Execution completed successfully.\n" );

    FTDemo_Display_Done( display );
    FTDemo_Done( handle );
    exit( 0 );      /* for safety reasons */

    /* return 0; */ /* never reached */
  }
Ejemplo n.º 25
0
static int au_rewrite(struct ast_filestream *s, const char *comment)
{
	if (write_header(s->f))
		return -1;
	return 0;
}
Ejemplo n.º 26
0
static int gen_multy_bakup_files(b_fil* file_list, FILE_DESC input_file_desc, SLONG file_num)
{
/********************************************************************
**
**	g e n _ m u l t y _ b a c k u p _ f i l e s
**
*********************************************************************
**
**	Functional description:
**
**		processing input data from stdin and splits the data into
**		multiple back-up files.
**
**		allocates an 16K bytes I/O buffer
**		intilializes header record common fields
**		do forever
**			walk through the backup file chain
**			intilializes header record unique fields
**			open backup file
**			writes out header record to backup file
**			points to the next backup file in the chain
**			calculates the actual file size ( minus header record length )
**			if the actual file size less than 16K bytes
**				set I/O size to actual file size
**			otherwise
**				set I/O size to 16K byte long
**			when it is the last backup file
**				reads data from standard input as much as indicated by I/O size
**					and writes it out to the last backup file until no EOF.
**				issues error message when disk space full condition is detected
**			otherwise reads and writes to backup files util EOF
**				if disk full cobdition is detected
**					flush the remaining data in the I/O buffer to subsequence
**							backup files
**				go back to normal read and write process util EOF
**
*********************************************************************
*/

	SLONG byte_write, ret_cd;
	TEXT header_str[header_rec_len], num_arr[5];
	header_rec hdr_rec;

	// CVC: there's a can of worms here. First, this function assumes it can free
	// the io_buffer's allocated memory without keeping a second copy of that pointer.
	// However, io_buffer can't be declared UCHAR* const because its address is
	// passed to final_read_and_write() and read_and_write() and both functions
	// thus suggest, by taking a UCHAR** that they can change the pointer's address;
	// but in practice they never affect it, so fixing those functions to take simply
	// UCHAR* would allow the correct declaration for io_buffer to succeed.
	//UCHAR* const io_buffer = (UCHAR *) malloc(IO_BUFFER_SIZE);
	UCHAR* io_buffer = (UCHAR *) malloc(IO_BUFFER_SIZE);

	if (!io_buffer)
	{
		fprintf(stderr, "I/O buffer allocation failed\n");
		return FB_FAILURE;
	}

	size_t pos;
	for (pos = 0; pos < header_rec_len; pos++)
		header_str[pos] = BLANK;

	pos = 0;
	ret_cd = set_hdr_str(header_str, header_rec_name, pos, sizeof(hdr_rec.name));
	size_t indx;
	for (indx = 0; indx < sizeof(hdr_rec.name); indx++)
		hdr_rec.name[indx] = BLANK;

	pos = pos + sizeof(hdr_rec.name);
	time_t clock = time(0);  // was SLONG
	ret_cd = set_hdr_str(header_str, ctime(&clock), pos, sizeof(hdr_rec.date_time));
	for (indx = 0; indx < sizeof(hdr_rec.date_time); indx++)
		hdr_rec.date_time[indx] = BLANK;

	pos = pos + sizeof(hdr_rec.date_time);
	ret_cd = set_hdr_str(header_str, ", file No. ", pos, sizeof(hdr_rec.text1));
	for (indx = 0; indx < sizeof(hdr_rec.text1); indx++)
		hdr_rec.text1[indx] = BLANK;

	for (indx = 0; indx < sizeof(hdr_rec.num); indx++)
		hdr_rec.num[indx] = BLANK;

	pos = pos + sizeof(hdr_rec.text1) + sizeof(hdr_rec.num);
	ret_cd = set_hdr_str(header_str, " of ", pos, sizeof(hdr_rec.text2));
	for (indx = 0; indx < sizeof(hdr_rec.text2); indx++)
		hdr_rec.text2[indx] = BLANK;

	ret_cd = conv_ntoc(file_num, num_arr);
	if (ret_cd == FB_FAILURE)
	{
		free(io_buffer);
		fprintf(stderr, "gsplit could not convert numeric data to character data\n");
		return FB_FAILURE;
	}

	num_arr[sizeof(num_arr) - 1] = TERMINAL;
	pos = pos + sizeof(hdr_rec.text2);
	ret_cd = set_hdr_str(header_str, num_arr, pos, sizeof(hdr_rec.total));
	for (indx = 0; indx < sizeof(hdr_rec.total); indx++)
		hdr_rec.total[indx] = BLANK;

	pos = pos + sizeof(hdr_rec.total);
	ret_cd = set_hdr_str(header_str, ", ", pos, sizeof(hdr_rec.text3));
	for (indx = 0; indx < sizeof(hdr_rec.text3); indx++)
		hdr_rec.text3[indx] = BLANK;

	for (indx = 0; indx < sizeof(hdr_rec.fl_name); indx++)
		hdr_rec.fl_name[indx] = BLANK;

	FILE_DESC output_fl_desc = 0;
	bool end_of_input = false, flush_done = false;
	const TEXT* file_name = NULL;
	SLONG io_size = 0;
	b_fil* fl_ptr = file_list;

	SINT64 byte_read = 0;
	SINT64 file_size = 0;

	while (true)
	{
		if (fl_ptr != NULL)
		{
			byte_read = 0;
			byte_write = 0;
			if (!fl_ptr->b_fil_next && (fl_ptr->b_fil_size == 0))
			{
				fl_ptr->b_fil_size = MIN_FILE_SIZE;
			}

			file_size = fl_ptr->b_fil_size - header_rec_len;
			file_name = fl_ptr->b_fil_name;

			output_fl_desc = open_platf(file_name, 1);
			if (output_fl_desc == INVALID_HANDLE_VALUE)
			{
				free(io_buffer);
				fprintf(stderr, "can not open back up file %s\n", file_name);
				return FB_FAILURE;
			}

			ret_cd = write_header(fl_ptr, hdr_rec, output_fl_desc, header_str);
			if (ret_cd == FB_FAILURE)
			{
				free(io_buffer);
				fprintf(stderr, "could not write header record to file %s\n", file_name);
				return FB_FAILURE;
			}

			fl_ptr = fl_ptr->b_fil_next;
		}

		if (file_size < IO_BUFFER_SIZE)
			io_size = (SLONG) file_size;
		else
			io_size = IO_BUFFER_SIZE;

		if (!fl_ptr)
		{
			while (!end_of_input)
			{
				ret_cd = final_read_and_write(input_file_desc, output_fl_desc,
											  file_name, io_size, &io_buffer, &end_of_input);
				if (ret_cd == FB_FAILURE)
				{
					free(io_buffer);
					return FB_FAILURE;
				}

				if (end_of_input)
				{
					free(io_buffer);
					return FB_SUCCESS;
				}
			}
		}
		else
		{
			while ((file_size > byte_read) && (fl_ptr != NULL))
			{
				ret_cd = read_and_write(input_file_desc, output_fl_desc,
										/*file_name,*/ io_size, file_size,
										&io_buffer, &end_of_input,
										&byte_read, &byte_write);
				switch (ret_cd)
				{
				case FB_FAILURE:
					free(io_buffer);
					return FB_FAILURE;

				case FILE_IS_FULL:
					{
						byte_read = 0;	// reset byte read count, prepare for next read

						const UCHAR* remaining_io = io_buffer + byte_write;
						SLONG remaining_io_len = IO_BUFFER_SIZE - byte_write;
						while (!flush_done && (fl_ptr != NULL))
						{
							if (!fl_ptr->b_fil_next && fl_ptr->b_fil_size == 0)
								fl_ptr->b_fil_size = MIN_FILE_SIZE;

							file_size = fl_ptr->b_fil_size - header_rec_len;
							file_name = fl_ptr->b_fil_name;

							output_fl_desc = open_platf(file_name, 1);
							if (output_fl_desc == INVALID_HANDLE_VALUE)
							{
								free(io_buffer);
								fprintf(stderr, "can not open back up file %s\n", file_name);
								return FB_FAILURE;
							}
							ret_cd = write_header(fl_ptr, hdr_rec, output_fl_desc, header_str);
							if (ret_cd == FB_FAILURE)
							{
								free(io_buffer);
								fprintf(stderr, "fail to write header rec to file %s\n", file_name);
								return FB_FAILURE;
							}

							fl_ptr = fl_ptr->b_fil_next;
							if (!fl_ptr)
							{
								ret_cd = final_flush_io_buff(remaining_io,
															 remaining_io_len,
															 output_fl_desc);
								if (ret_cd == FB_FAILURE)
								{
									fprintf(stderr, "gsplit could not do backup due");
									fprintf(stderr, " to lack of space or I/O problem\n");
									free(io_buffer);
									return FB_FAILURE;
								}
							}
							else
							{
								// got a lot of backup files

								ret_cd = flush_io_buff(remaining_io,
													   remaining_io_len,
													   output_fl_desc,
													   file_size,
													   &byte_write,
													   &flush_done);
								if (ret_cd == FB_FAILURE)
								{
									fprintf(stderr, "gsplit could not do backup due");
									fprintf(stderr, " I/O problem\n");
									free(io_buffer);
									return FB_FAILURE;
								}
								if (flush_done)
								{
									file_size = file_size - byte_write;
									byte_write = 0;
								}
								else
								{
									remaining_io = remaining_io + byte_write;
									remaining_io_len = remaining_io_len - byte_write;
								}
							}
						}		// end of while loop
						break;
					} // case FILE_IS_FULL

				default:
					break;
				}

				if (end_of_input)
				{
					free(io_buffer);
					return FB_SUCCESS;
				}
			}
		}
	}	// end of while ( true )
}
Ejemplo n.º 27
0
const char * Nes_File_Writer::begin_group( nes_tag_t tag )
{
	depth_++;
	return write_header( tag, group_begin_size );
}
Ejemplo n.º 28
0
int group_recorder::init(OBJECT *obj){
	OBJECT *gr_obj = 0;

	// check for group
	if(0 == group_def[0]){
		if(strict){
			gl_error("group_recorder::init(): no group defined");
			/* TROUBLESHOOT
				group_recorder must define a group in "group_def".
			 */
			return 0;
		} else {
			gl_warning("group_recorder::init(): no group defined");
			tape_status = TS_ERROR;
			return 1; // nothing more to do
		}
	}

	// check for filename
	if(0 == filename[0]){
		// if no filename, auto-generate based on ID
		if(strict){
			gl_error("group_recorder::init(): no filename defined in strict mode");
			return 0;
		} else {
			sprintf(filename, "%256s-%256i.csv", oclass->name, obj->id);
			gl_warning("group_recorder::init(): no filename defined, auto-generating '%s'", filename);
			/* TROUBLESHOOT
				group_recorder requires a filename.  If none is provided, a filename will be generated
				using a combination of the classname and the core-assigned ID number.
			*/
		}
	}

	// check valid write interval
	write_interval = (int64)(dInterval);
	if(-1 > write_interval){
		gl_error("group_recorder::init(): invalid write_interval of %i, must be -1 or greater", write_interval);
		/* TROUBLESHOOT
			The group_recorder interval must be -1, 0, or a positive number of seconds.
		*/
		return 0;
	}

	// all flush intervals are valid
	flush_interval = (int64)dFlush_interval;
	
	
	// build group
	//	* invariant?
	//	* non-empty set?
	items = gl_find_objects(FL_GROUP, group_def);
	if(0 == items){
		if(strict){
			gl_error("group_recorder::init(): unable to construct a set with group definition");
			/* TROUBLESHOOT
				An error occured while attempting to build and populate the find list with the specified group definition.
			 */
			return 0;
		} else {
			gl_warning("group_recorder::init(): unable to construct a set with group definition");
			tape_status = TS_ERROR;
			return 1; // nothing more to do
		}
	}
	if(1 > items->hit_count){
		if(strict){
			gl_error("group_recorder::init(): the defined group returned an empty set");
			/* TROUBLESHOOT
				Placeholder.
			 */
			return 0;
		} else {
			gl_warning("group_recorder::init(): the defined group returned an empty set");
			tape_status = TS_ERROR;
			return 1;
		}
	}
	
	// open file
	rec_file = fopen(filename, "w");
	if(0 == rec_file){
		if(strict){
			gl_error("group_recorder::init(): unable to open file '%s' for writing", filename);
			return 0;
		} else {
			gl_warning("group_recorder::init(): unable to open file '%s' for writing", filename);
			/* TROUBLESHOOT
				If the group_recorder cannot open the specified output file, it will 
			 */
			tape_status = TS_ERROR;
			return 1;
		}
	}

	// turn list into objlist, count items
	obj_count = 0;
	for(gr_obj = gl_find_next(items, 0); gr_obj != 0; gr_obj = gl_find_next(items, gr_obj) ){
		prop_ptr = gl_get_property(gr_obj, property_name);
		// might make this a 'strict-only' issue in the future
		if(prop_ptr == NULL){
			gl_error("group_recorder::init(): unable to find property '%s' in an object of type '%s'", property_name, gr_obj->oclass->name);
			/* TROUBLESHOOT
				An error occured while reading the specified property in one of the objects.
			 */
			return 0;
		}
		++obj_count;
		if(obj_list == 0){
			obj_list = new quickobjlist(gr_obj, prop_ptr);
		} else {
			obj_list->tack(gr_obj, prop_ptr);
		}
	}

	tape_status = TS_OPEN;
	if(0 == write_header()){
		gl_error("group_recorder::init(): an error occured when writing the file header");
		/* TROUBLESHOOT
			Unexpected IO error.
		 */
		tape_status = TS_ERROR;
		return 0;
	}
	

	return 1;
}
Ejemplo n.º 29
0
const char * Nes_File_Writer::end_group()
{
	depth_--;
	return write_header( group_end_tag, 0 );
}
Ejemplo n.º 30
0
static int tail_file(struct file_struct *f, unsigned long n_units, char mode, char forever)
{
	ssize_t bytes_read = 0;
	off_t offset = 0;
	char *buf;
	struct stat finfo;

	if (strcmp(f->name, "-") == 0)
		f->fd = STDIN_FILENO;
	else {
		f->fd = open(f->name, O_RDONLY);
		if (unlikely(f->fd < 0)) {
			fprintf(stderr, "Error: Could not open file '%s' (%s)\n", f->name, strerror(errno));
			ignore_file(f);
			return -1;
		}
	}

	if (fstat(f->fd, &finfo) < 0) {
		fprintf(stderr, "Error: Could not stat file '%s' (%s)\n", f->name, strerror(errno));
		ignore_file(f);
		return -1;
	}

	if (!IS_TAILABLE(finfo.st_mode)) {
		fprintf(stderr, "Error: '%s' of unsupported file type (%s)\n", f->name, strerror(errno));
		ignore_file(f);
		return -1;
	}

	/* Cannot seek on these */
	if (IS_PIPELIKE(finfo.st_mode) || f->fd == STDIN_FILENO)
		return tail_pipe(f);

	f->size = finfo.st_size;
	f->blksize = finfo.st_blksize;	/* TODO: Can this value be 0? */

	if (mode == M_LINES)
		offset = lines_to_offset(f, n_units);
	else
		offset = bytes_to_offset(f, n_units);

	/* We only get negative offsets on errors */
	if (unlikely(offset < 0)) {
		ignore_file(f);
		return -1;
	}

	if (verbose)
		write_header(f->name);

	if (lseek(f->fd, offset, SEEK_SET) == (off_t) -1) {
		fprintf(stderr, "Error: Could not seek in file '%s' (%s)\n", f->name, strerror(errno));
		return -1;
	}

	buf = emalloc(f->blksize);

	while ((bytes_read = read(f->fd, buf, f->blksize)) > 0) {
		auparse_feed(au,buf, bytes_read);
		}

	if (!forever) {
		if (close(f->fd) < 0) {
			fprintf(stderr, "Error: Could not close file '%s' (%s)\n", f->name, strerror(errno));
			free(buf);
			return -1;
		}
	} /* Let the fd open otherwise, we'll need it */

	free(buf);
	return 0;
}