Example #1
0
static void
show_run(const struct run *run) {
	max_line_length = Page_Width / 2 - 1;
	max_line_length_UTF8 = max_line_length * FONT_SIZE;

	/* The animals came in two by two ... */
	const struct chunk *cnk0 = &run->rn_chunk0;
	const struct chunk *cnk1 = &run->rn_chunk1;
	size_t nl_cnt0 = cnk0->ch_last.ps_nl_cnt - cnk0->ch_first.ps_nl_cnt;
	size_t nl_cnt1 = cnk1->ch_last.ps_nl_cnt - cnk1->ch_first.ps_nl_cnt;
	FILE *f0;
	FILE *f1;

	/* display heading of chunk */
	if (!is_set_option('d')) {
		/* no assumptions about the lengths of the file names! */
		size_t size = run->rn_size;
		int pos = print_header(cnk0);
		print_spaces(max_line_length - pos);
		print_char('|');
		pos = print_header(cnk1);
		print_spaces(max_line_length - pos - length_size_t(size) - 2);
		fprintf(Output_File, "[%s]\n", size_t2string(size));
	}
	else {
		/* diff-like format */
		(void)print_header(cnk0);
		print_char('\n');
		(void)print_header(cnk1);
		print_char('\n');
	}

	/* stop if that suffices */
	if (is_set_option('n'))
		return;			/* ... had enough so soon ... */

	/* open the files that hold the chunks */
	f0 = open_chunk(cnk0);
	f1 = open_chunk(cnk1);

	/* display the chunks in the required format */
	if (!is_set_option('d')) {
		/* print 2-column format */
		while (nl_cnt0 != 0 || nl_cnt1 != 0) {
			int pos_UTF8 = 0;
			if (nl_cnt0) {
				pos_UTF8 = print_UTF8_line(f0);
				nl_cnt0--;
			}
			print_UTF8_spaces(max_line_length_UTF8 - pos_UTF8);
			print_char('|');
			if (nl_cnt1) {
				(void)print_UTF8_line(f1);
				nl_cnt1--;
			}
			print_char('\n');
		}
	}
	else {
		/* display the chunks in a diff(1)-like format */
		while (nl_cnt0--) {
			show_1C_line(f0, "<");
		}
		(void)print_string("---\n");
		while (nl_cnt1--) {
			show_1C_line(f1, ">");
		}
	}

	/* close the pertinent files */
	fclose(f0);
	fclose(f1);
}
Example #2
0
static void check_gt(args_t *args)
{
    int i,ret, *gt2ipl = NULL, m_gt2ipl = 0, *gt_arr = NULL, ngt_arr = 0;
    int fake_pls = args->no_PLs;

    // Initialize things: check which tags are defined in the header, sample names etc.
    if ( bcf_hdr_id2int(args->gt_hdr, BCF_DT_ID, "GT")<0 ) error("[E::%s] GT not present in the header of %s?\n", __func__, args->files->readers[1].fname);
    if ( bcf_hdr_id2int(args->sm_hdr, BCF_DT_ID, "PL")<0 )
    {
        if ( bcf_hdr_id2int(args->sm_hdr, BCF_DT_ID, "GT")<0 )
            error("[E::%s] Neither PL nor GT present in the header of %s\n", __func__, args->files->readers[0].fname);
        if ( !args->no_PLs )
            fprintf(pysamerr,"Warning: PL not present in the header of %s, using GT instead\n", args->files->readers[0].fname);
        fake_pls = 1;
    }

    FILE *fp = args->plot ? open_file(NULL, "w", "%s.tab", args->plot) : stdout;
    print_header(args, fp);

    int tgt_isample = -1, query_isample = 0;
    if ( args->target_sample )
    {
        tgt_isample = bcf_hdr_id2int(args->gt_hdr, BCF_DT_SAMPLE, args->target_sample);
        if ( tgt_isample<0 ) error("No such sample in %s: [%s]\n", args->files->readers[1].fname, args->target_sample);
    }
    if ( args->all_sites )
    {
        if ( tgt_isample==-1 )
        {
            fprintf(pysamerr,"No target sample selected for comparison, using the first sample in %s: %s\n", args->gt_fname,args->gt_hdr->samples[0]);
            tgt_isample = 0;
        }
    }
    if ( args->query_sample )
    {
        query_isample = bcf_hdr_id2int(args->sm_hdr, BCF_DT_SAMPLE, args->query_sample);
        if ( query_isample<0 ) error("No such sample in %s: [%s]\n", args->files->readers[0].fname, args->query_sample);
    }
    if ( args->all_sites )
        fprintf(fp, "# [1]SC, Site by Site Comparison\t[2]Chromosome\t[3]Position\t[4]-g alleles\t[5]-g GT (%s)\t[6]match log LK\t[7]Query alleles\t[8-]Query PLs (%s)\n",
                args->gt_hdr->samples[tgt_isample],args->sm_hdr->samples[query_isample]);

    // Main loop
    float prev_lk = 0;
    while ( (ret=bcf_sr_next_line(args->files)) )
    {
        if ( ret!=2 ) continue;
        bcf1_t *sm_line = args->files->readers[0].buffer[0];    // the query file
        bcf1_t *gt_line = args->files->readers[1].buffer[0];    // the -g target file
        bcf_unpack(sm_line, BCF_UN_FMT);
        bcf_unpack(gt_line, BCF_UN_FMT);

        // Init mapping from target genotype index to the sample's PL fields
        int n_gt2ipl = gt_line->n_allele*(gt_line->n_allele + 1)/2;
        if ( n_gt2ipl > m_gt2ipl )
        {
            m_gt2ipl = n_gt2ipl;
            gt2ipl   = (int*) realloc(gt2ipl, sizeof(int)*m_gt2ipl);
        }
        if ( !init_gt2ipl(args, gt_line, sm_line, gt2ipl, n_gt2ipl) ) continue;

        // Target genotypes
        int ngt, npl;
        if ( (ngt=bcf_get_genotypes(args->gt_hdr, gt_line, &gt_arr, &ngt_arr)) <= 0 )
            error("GT not present at %s:%d?", args->gt_hdr->id[BCF_DT_CTG][gt_line->rid].key, gt_line->pos+1);
        ngt /= bcf_hdr_nsamples(args->gt_hdr);
        if ( ngt!=2 ) continue; // checking only diploid genotypes

        // Sample PLs
        if ( !fake_pls )
        {
            if ( (npl=bcf_get_format_int32(args->sm_hdr, sm_line, "PL", &args->pl_arr, &args->npl_arr)) <= 0 )
            {
                if ( sm_line->n_allele==1 )
                {
                    // PL values may not be present when ALT=. (mpileup/bcftools output), in that case 
                    // switch automatically to GT at these sites
                    npl = fake_PLs(args, args->sm_hdr, sm_line);
                }
                else
                    error("PL not present at %s:%d?\n", args->sm_hdr->id[BCF_DT_CTG][sm_line->rid].key, sm_line->pos+1);
            }
            else
                npl /= bcf_hdr_nsamples(args->sm_hdr);
        }
        else
            npl = fake_PLs(args, args->sm_hdr, sm_line);

        // Calculate likelihoods for all samples, assuming diploid genotypes

        // For faster access to genotype likelihoods (PLs) of the query sample
        int max_ipl, *pl_ptr = args->pl_arr + query_isample*npl;
        double sum_pl = 0; // for converting PLs to probs
        for (max_ipl=0; max_ipl<npl; max_ipl++)
        {
            if ( pl_ptr[max_ipl]==bcf_int32_vector_end ) break;
            if ( pl_ptr[max_ipl]==bcf_int32_missing ) continue;
            sum_pl += pow(10, -0.1*pl_ptr[max_ipl]);
        }
        if ( sum_pl==0 ) continue; // no PLs present
        if ( fake_pls && args->no_PLs==1 ) sum_pl = -1;

        // The main stats: concordance of the query sample with the target -g samples
        for (i=0; i<bcf_hdr_nsamples(args->gt_hdr); i++)
        {
            int *gt_ptr = gt_arr + i*ngt;
            if ( gt_ptr[1]==bcf_int32_vector_end ) continue;    // skip haploid genotypes
            if ( bcf_gt_is_missing(gt_ptr[0]) || bcf_gt_is_missing(gt_ptr[1]) ) continue;
            int a = bcf_gt_allele(gt_ptr[0]);
            int b = bcf_gt_allele(gt_ptr[1]);
            if ( args->hom_only && a!=b ) continue; // heterozygous genotype
            int igt_tgt = igt_tgt = bcf_alleles2gt(a,b); // genotype index in the target file
            int igt_qry = gt2ipl[igt_tgt];  // corresponding genotype in query file
            if ( igt_qry>=max_ipl || pl_ptr[igt_qry]<0 ) continue;   // genotype not present in query sample: haploid or missing
            args->lks[i] += sum_pl<0 ? -pl_ptr[igt_qry] : log(pow(10, -0.1*pl_ptr[igt_qry])/sum_pl);
            args->sites[i]++;
        }
        if ( args->all_sites )
        {
            // Print LKs at all sites for debugging
            int *gt_ptr = gt_arr + tgt_isample*ngt;
            if ( gt_ptr[1]==bcf_int32_vector_end ) continue;    // skip haploid genotypes
            int a = bcf_gt_allele(gt_ptr[0]);
            int b = bcf_gt_allele(gt_ptr[1]);
            if ( args->hom_only && a!=b ) continue; // heterozygous genotype
            fprintf(fp, "SC\t%s\t%d", args->gt_hdr->id[BCF_DT_CTG][gt_line->rid].key, gt_line->pos+1);
            for (i=0; i<gt_line->n_allele; i++) fprintf(fp, "%c%s", i==0?'\t':',', gt_line->d.allele[i]);
            fprintf(fp, "\t%s/%s", a>=0 ? gt_line->d.allele[a] : ".", b>=0 ? gt_line->d.allele[b] : ".");
            fprintf(fp, "\t%f", args->lks[query_isample]-prev_lk);
            prev_lk = args->lks[query_isample];

            int igt, *pl_ptr = args->pl_arr + query_isample*npl; // PLs of the query sample
            for (i=0; i<sm_line->n_allele; i++) fprintf(fp, "%c%s", i==0?'\t':',', sm_line->d.allele[i]);
            for (igt=0; igt<npl; igt++)
                if ( pl_ptr[igt]==bcf_int32_vector_end ) break;
                else if ( pl_ptr[igt]==bcf_int32_missing ) fprintf(fp, ".");
                else fprintf(fp, "\t%d", pl_ptr[igt]);
            fprintf(fp, "\n");
        }
    }
    free(gt2ipl);
    free(gt_arr);
    free(args->pl_arr);
    free(args->tmp_arr);

    // To be able to plot total discordance (=number of mismatching GTs with -G1) in the same
    // plot as discordance per site, the latter must be scaled to the same range
    int nsamples = bcf_hdr_nsamples(args->gt_hdr);
    double extreme_lk = 0, extreme_lk_per_site = 0;
    for (i=0; i<nsamples; i++)
    {
        if ( args->lks[i] < extreme_lk ) extreme_lk = args->lks[i];
        if ( args->sites[i] && args->lks[i]/args->sites[i] < extreme_lk_per_site ) extreme_lk_per_site = args->lks[i]/args->sites[i];
    }

    // Sorted output
    double **p = (double**) malloc(sizeof(double*)*nsamples);
    for (i=0; i<nsamples; i++) p[i] = &args->lks[i];
    qsort(p, nsamples, sizeof(int*), cmp_doubleptr);

    fprintf(fp, "# [1]CN\t[2]Discordance with %s (total)\t[3]Discordance (avg score per site)\t[4]Number of sites compared\t[5]Sample\t[6]Sample ID\n", args->sm_hdr->samples[query_isample]);
    for (i=0; i<nsamples; i++)
    {
        int idx = p[i] - args->lks;
        double per_site = 0;
        if ( args->sites[idx] )
        {
            if ( args->sites[idx] && extreme_lk_per_site )
            {
                per_site = args->lks[idx]/args->sites[idx];
                per_site *= extreme_lk / extreme_lk_per_site;
            }
            else
                per_site = 0;
        }
        fprintf(fp, "CN\t%e\t%e\t%.0f\t%s\t%d\n", fabs(args->lks[idx]), fabs(per_site), args->sites[idx], args->gt_hdr->samples[idx], i);
    }

    if ( args->plot )
    {
        fclose(fp);
        plot_check(args, args->target_sample ? args->target_sample : "", args->sm_hdr->samples[query_isample]);
    }
}
Example #3
0
/* void protocol1
 *   buffer* recv_buffer - buffer containing the message sent by the client to the proxy
 *   server_stat* status - status and general information from the server
 * Processes the recv_buf as the class protocol dictates
 */
void protocol1(buffer* recv_buf, server_stat* status){
    struct timeval timeout;
    struct timeval timeout_0;
    // timeouts
    timeout.tv_sec = 1;
    timeout.tv_usec = 0;
    timeout_0.tv_sec = 0;
    timeout_0.tv_usec = 0;

    timeout_setup(status->udp_sock, timeout);

    // giant mess of conditionals
    fprintf(stdout, "\tversion 1 protocol\n");
    int error = 0;
    header msg_header = extract_header(recv_buf);
    if (status->connected == 0) {
        if (check_pass(&msg_header, 0)) {
            if (get_command(&msg_header) == CONNECT) {
                fprintf(stdout, "\tReplying with password\n");
                status->connected = 1;
                msg_header.data[1] = status->password;
                insert_header(recv_buf, msg_header);
                if (udp_send(recv_buf, status) < 0) {
                    fprintf(stderr, "sendto()\n");
                }
            } else {
                fprintf(stderr, "ERROR: Unexpected Command\n");
                error = -2;
            }
        } else {
            fprintf(stderr, "ERROR: Incorrect Password\n");
        }
    } else if (status->connected == 1) {
        if (check_pass(&msg_header, status->password)) {
            if (get_command(&msg_header) == CONNECT) {
                status->connected = 2;
                timeout_setup(status->udp_sock, timeout_0);
                fprintf(stdout, "\tConnected to a client\n");
            } else {
                fprintf(stderr, "ERROR: Unexpected Command\n");
                error = -2;
            }
        } else {
            fprintf(stderr, "ERROR: Incorrect Password\n");
            error = -2;
        }
    } else {
        if (!check_pass(&msg_header, status->password)) {
            fprintf(stderr, "ERROR: Incorrect Password\n");
                error = -2;
        } else {
            fprintf(stdout, "\t\tReceived Message:\n");
            print_header(recv_buf);
            switch (get_command(&msg_header)) {
                case CONNECT:
                    fprintf(stderr, "ERROR: Unexpected Command\n");
                error = -2;
                    break;
                case QUIT:
                    quit(recv_buf, status);
                    timeout_setup(status->udp_sock, timeout_0);
                    break;
                default:
                    error = request_command(recv_buf, status);
                    break;
            }
        }
    }
    if (error == -2) {
        fprintf(stderr, "ERROR: Invalid client request\n");
        send_error(status, CLIENT_ERROR);
    } else if (error == -1) {
        fprintf(stderr, "ERROR: Problem with http response\n");
        send_error(status, HTTP_ERROR);
    }
}
Example #4
0
int main(int argc,
         char **argv)
{
    int rval = 0;
    double *x = 0;
    struct CG *cg = 0;

    struct LP lp;
    char *column_types = 0;

    rval = parse_args(argc, argv);
    if (rval) return 1;

    if (LOG_FILENAME[0])
    {
        LOG_FILE = fopen(LOG_FILENAME, "w");
        abort_if(!LOG_FILE, "could not open log file");
    }

    print_header(argc, argv);

    STATS_init();
    STATS_set_input_filename(PROBLEM_FILENAME);

    rval = LP_open(&lp);
    abort_if(rval, "LP_open failed");

    rval = LP_create(&lp, "multirow");
    abort_if(rval, "LP_create failed");

    rval = LP_read_problem(&lp, PROBLEM_FILENAME);
    abort_if(rval, "LP_read_problem failed");

    int ncols = LP_get_num_cols(&lp);
    int nrows = LP_get_num_rows(&lp);

    log_info("    %d rows, %d cols\n", nrows, ncols);

    x = (double *) malloc(ncols * sizeof(double));
    abort_if(!x, "could not allocate x");

    if (!KEEP_INTEGRALITY)
    {
        column_types = (char *) malloc(ncols * sizeof(char));
        abort_if(!column_types, "could not allocate column_types");

        log_info("Storing column types...\n");
        rval = LP_get_column_types(&lp, column_types);
        abort_if(rval, "LP_get_column_types failed");

        log_info("Relaxing integrality...\n");
        rval = LP_relax(&lp);
        abort_if(rval, "LP_relax failed");

        log_info("Disabling presolve...\n");
        LP_disable_presolve(&lp);
    }

    if(BASIS_FILENAME[0])
    {
        rval = LP_read_basis(&lp, BASIS_FILENAME);
        abort_if(rval, "LP_read_basis failed");
    }

    log_info("Optimizing...\n");
    int infeasible;
    rval = LP_optimize(&lp, &infeasible);
    abort_if(rval, "LP_optimize failed");

    double obj;
    rval = LP_get_obj_val(&lp, &obj);
    abort_if(rval, "LP_get_obj_val failed");

    log_info("    opt = %lf\n", obj);

    STATS_set_obj_value(obj);
    STATS_finish_round();

    if(OUTPUT_BASIS_FILENAME[0])
    {
        rval = LP_write_basis(&lp, OUTPUT_BASIS_FILENAME);
        abort_if(rval, "LP_write_basis failed");
    }

    if(GENERATE_MIR || GENERATE_GREEDY)
    {
        cg = (struct CG *) malloc(sizeof(struct CG));
        abort_if(!cg, "could not allocate cg");

        log_info("Reading tableau rows...\n");
        rval = CG_init(&lp, column_types, cg);
        abort_if(rval, "CG_init failed");

        if (strlen(KNOWN_SOLUTION_FILENAME) > 0)
        {
            rval = LP_read_solution(&lp, KNOWN_SOLUTION_FILENAME, x);
            abort_if(rval, "LP_read_solution failed");

            rval = CG_set_integral_solution(cg, x);
            abort_if(rval, "CG_set_integral_solution failed");
        }

        rval = LP_get_x(&lp, x);
        abort_if(rval, "LP_get_x failed");

        rval = CG_set_basic_solution(cg, x);
        abort_if(rval, "CG_set_basic_solution failed");

        if (GENERATE_MIR)
        {
            log_info("Adding MIR cuts...\n");
            rval = CG_add_single_row_cuts(
                cg,
                (SingleRowGeneratorCallback)
                MIR_generate_cut);
            abort_if(rval, "CG_add_single_row_cuts failed");

            log_info("Optimizing...\n");
            rval = LP_optimize(&lp, &infeasible);
            abort_if(rval, "LP_optimize failed");

            rval = LP_get_obj_val(&lp, &obj);
            abort_if(rval, "LP_get_obj_val failed");

            log_info("    opt = %lf\n", obj);
            STATS_set_obj_value(obj);

            STATS_finish_round();
        }

        if (GENERATE_GREEDY)
        {
            for(int k = MIN_N_ROWS; k <= MAX_N_ROWS; k++)
            {
                log_info("Adding greedy intersection cuts (%d rows)...\n", k);

                rval = CG_add_multirow_cuts(cg, k, (MultirowGeneratorCallback)
                                            GREEDY_generate_cut);
                abort_if(rval, "CG_add_multirow_cuts failed");
            }

            log_info("Optimizing...\n");
            rval = LP_optimize(&lp, &infeasible);
            abort_if(rval, "LP_optimize failed");

            rval = LP_get_obj_val(&lp, &obj);
            abort_if(rval, "LP_get_obj_val failed");

            log_info("    opt = %lf\n", obj);
            STATS_set_obj_value(obj);

            STATS_finish_round();
        }

        CG_free(cg);
    }

    if(OUTPUT_SOLUTION_FILENAME[0])
    {
        rval = LP_write_solution(&lp, OUTPUT_SOLUTION_FILENAME);
        abort_if(rval, "LP_write_solution failed");
    }

    if(STATS_FILENAME[0])
    {
        log_info("Writing stats to file %s...\n", STATS_FILENAME);
        rval = STATS_print_yaml(STATS_FILENAME);
        abort_if(rval, "STATS_print_yaml failed");
    }

CLEANUP:
    if (LOG_FILE) fclose(LOG_FILE);
    if (x) free(x);
    if (column_types) free(column_types);
    LP_free(&lp);

    return rval;
}
Example #5
0
int main(int argc, char **argv)
{
    char id[7];
    int32_t l;
    uint16_t s;
    int verbose = 0;
    int32_t offset;
    int foundnullsegment = 0;
    int version;
    int32_t segmentcontentlength = 0;
    int nsegments = 0;
    int32_t headerlength = 0;
    int32_t objectlength = 0;

    printf("RDOFF dump utility, version %s\n", PROGRAM_VERSION);
    printf("RDOFF2 revision %s\n", RDOFF2_REVISION);
    puts("Copyright (c) 1996,99 Julian R Hall\n"
         "Improvements and fixes (c) 2002-2004 RET & COM Research.");

    if (argc < 2) {
        fputs("Usage: rdfdump [-v] <filename>\n", stderr);
        exit(1);
    }

    rdoff_init();

    if (!strcmp(argv[1], "-v")) {
        verbose = 1;
        if (argc < 3) {
            fputs("required parameter missing\n", stderr);
            exit(1);
        }
        argv++;
    }

    infile = fopen(argv[1], "rb");
    if (!infile) {
        fprintf(stderr, "rdfdump: Could not open %s\n", argv[1]);
        exit(1);
    }

    nasm_read(id, 6, infile);
    if (strncmp(id, "RDOFF", 5)) {
        fputs("rdfdump: File does not contain valid RDOFF header\n",
              stderr);
        exit(1);
    }

    printf("File %s: RDOFF version %c\n\n", argv[1], id[5]);
    if (id[5] < '1' || id[5] > '2') {
        fprintf(stderr, "rdfdump: unknown RDOFF version '%c'\n", id[5]);
        exit(1);
    }
    version = id[5] - '0';

    if (version > 1) {
        nasm_read(&l, 4, infile);
        objectlength = translateint32_t(l);
        printf("Object content size: %"PRId32" bytes\n", objectlength);
    }

    nasm_read(&l, 4, infile);
    headerlength = translateint32_t(l);
    printf("Header (%"PRId32" bytes):\n", headerlength);
    print_header(headerlength, version);

    if (version == 1) {
        nasm_read(&l, 4, infile);
        l = translateint32_t(l);
        printf("\nText segment length = %"PRId32" bytes\n", l);
        offset = 0;
        while (l--) {
            nasm_read(id, 1, infile);
            if (verbose) {
                if (offset % 16 == 0)
                    printf("\n%08"PRIx32" ", offset);
                printf(" %02x", (int)(uint8_t)id[0]);
                offset++;
            }
        }
        if (verbose)
            printf("\n\n");

        nasm_read(&l, 4, infile);
        l = translateint32_t(l);
        printf("Data segment length = %"PRId32" bytes\n", l);

        if (verbose) {
            offset = 0;
            while (l--) {
                nasm_read(id, 1, infile);
                if (offset % 16 == 0)
                    printf("\n%08"PRIx32" ", offset);
                printf(" %02x", (int)(uint8_t)id[0]);
                offset++;
            }
            printf("\n");
        }
    } else {
        do {
            nasm_read(&s, 2, infile);
            s = translateint16_t(s);
            if (!s) {
                printf("\nNULL segment\n");
                foundnullsegment = 1;
                break;
            }
            printf("\nSegment:\n  Type   = %04X (%s)\n", (int)s,
                   translatesegmenttype(s));
            nsegments++;

            nasm_read(&s, 2, infile);
            printf("  Number = %04X\n", (int)translateint16_t(s));
            nasm_read(&s, 2, infile);
            printf("  Resrvd = %04X\n", (int)translateint16_t(s));
            nasm_read(&l, 4, infile);
            l = translateint32_t(l);
            printf("  Length = %"PRId32" bytes\n", l);
            segmentcontentlength += l;

            offset = 0;
            while (l--) {
                nasm_read(id, 1, infile);
                if (verbose) {
                    if (offset % 16 == 0)
                        printf("\n%08"PRIx32" ", offset);
                    printf(" %02x", (int)(uint8_t)id[0]);
                    offset++;
                }
            }
            if (verbose)
                printf("\n");
        } while (!feof(infile));
        if (!foundnullsegment)
            printf("\nWarning: unexpected end of file - "
                   "NULL segment not found\n");

        printf("\nTotal number of segments: %d\n", nsegments);
        printf("Total segment content length: %"PRId32" bytes\n",
               segmentcontentlength);

        /* calculate what the total object content length should have been */
        l = segmentcontentlength + 10 * (nsegments + 1) + headerlength + 4;
        if (l != objectlength)
            printf("Warning: actual object length (%"PRId32") != "
                   "stored object length (%"PRId32")\n", l, objectlength);
    }
    fclose(infile);
    return 0;
}
Example #6
0
void line_break(void)
{
	int *d, ch;
	int spg = 1, rspg = 1, spgs = 0, gap = 0;

	if (line_ptr == 0)
		return;

	if (current_line == 0)
		print_header();

	if (page_length > 12 &&
	    current_line + pending_nl > page_length - 6) {
		print_footer();
		print_header();
	}

	if (current_line)
		current_line += 1 + pending_nl;
	for (; pending_nl > 0; pending_nl--)
		fprintf(ofd, "\n");

	if (right_adjust < 0) {
		int over = right_margin - left_indent - (line_ptr - line);
#ifdef SPLATTER
		fprintf(ofd, ">Gaps=%d, Over=%d, ", gaps_on_line, over);
#endif
		if (gaps_on_line && over) {
			spg = rspg = 1 + over / gaps_on_line;
			over = over % gaps_on_line;
			if (over) {
				if (current_line % 2) {
					spgs = over;
					spg++;
				} else {
					spgs = gaps_on_line - over;
					rspg++;
				}
			}
		}
#ifdef SPLATTER
		fprintf(ofd, " (%d,%d) sw=%d\n", spg, rspg, spgs);
#endif
		right_adjust = 1;
	}

	*line_ptr = 0;
	if (*line)
		for (ch = left_indent; ch > 0; ch--)
			fputc(' ', ofd);

	for (d = line; *d; d++) {
		ch = *d;
		if ((ch & 0xFF) == 0) {
			int i;
			if (gap++ < spgs)
				i = spg;
			else
				i = rspg;
			for (; i > 0; i--)
				fputc(' ', ofd);
		} else
			switch (ch >> 8) {
			case 2:
				fputc(ch & 0xFF, ofd);
				fputc('\b', ofd);
				fputc(ch & 0xFF, ofd);
				break;
			case 3:
				fputc('_', ofd);
				fputc('\b', ofd);
				fputc(ch & 0xFF, ofd);
				break;
			default:
				fputc(ch & 0xFF, ofd);
				break;
			}
	}
	fputc('\n', ofd);

	line_ptr = 0;

	if (next_line_indent > 0)
		left_indent = next_line_indent;
	next_line_indent = -1;
	gaps_on_line = 0;
}
Example #7
0
int
main(int argc, char *argv[])
{
	const char *filename = NULL;
	const char *ufile = NULL;
	int error = 0;
	int c, fd, ufd;

	ctf_data_t cd;
	const ctf_preamble_t *pp;
	ctf_header_t *hp;
	Elf *elf;
	GElf_Ehdr ehdr;

	(void) elf_version(EV_CURRENT);

	for (opterr = 0; optind < argc; optind++) {
		while ((c = getopt(argc, argv, "dfhlsStu:")) != (int)EOF) {
			switch (c) {
			case 'd':
				flags |= F_DATA;
				break;
			case 'f':
				flags |= F_FUNC;
				break;
			case 'h':
				flags |= F_HDR;
				break;
			case 'l':
				flags |= F_LABEL;
				break;
			case 's':
				flags |= F_STR;
				break;
			case 'S':
				flags |= F_STATS;
				break;
			case 't':
				flags |= F_TYPES;
				break;
			case 'u':
				ufile = optarg;
				break;
			default:
				if (optopt == '?')
					return (print_usage(stdout, 1));
				warn("illegal option -- %c\n", optopt);
				return (print_usage(stderr, 0));
			}
		}

		if (optind < argc) {
			if (filename != NULL)
				return (print_usage(stderr, 0));
			filename = argv[optind];
		}
	}

	if (filename == NULL)
		return (print_usage(stderr, 0));

	if (flags == 0 && ufile == NULL)
		flags = F_ALLMSK;

	if ((fd = open(filename, O_RDONLY)) == -1)
		die("failed to open %s", filename);

	if ((elf = elf_begin(fd, ELF_C_READ, NULL)) != NULL &&
	    gelf_getehdr(elf, &ehdr) != NULL) {

		Elf_Data *dp;
		Elf_Scn *ctfscn = findelfscn(elf, &ehdr, ".SUNW_ctf");
		Elf_Scn *symscn;
		GElf_Shdr ctfshdr;

		if (ctfscn == NULL || (dp = elf_getdata(ctfscn, NULL)) == NULL)
			die("%s does not contain .SUNW_ctf data\n", filename);

		cd.cd_ctfdata = dp->d_buf;
		cd.cd_ctflen = dp->d_size;

		/*
		 * If the sh_link field of the CTF section header is non-zero
		 * it indicates which section contains the symbol table that
		 * should be used. We default to the .symtab section if sh_link
		 * is zero or if there's an error reading the section header.
		 */
		if (gelf_getshdr(ctfscn, &ctfshdr) != NULL &&
		    ctfshdr.sh_link != 0) {
			symscn = elf_getscn(elf, ctfshdr.sh_link);
		} else {
			symscn = findelfscn(elf, &ehdr, ".symtab");
		}

		/* If we found a symbol table, find the corresponding strings */
		if (symscn != NULL) {
			GElf_Shdr shdr;
			Elf_Scn *symstrscn;

			if (gelf_getshdr(symscn, &shdr) != NULL) {
				symstrscn = elf_getscn(elf, shdr.sh_link);

				cd.cd_nsyms = shdr.sh_size / shdr.sh_entsize;
				cd.cd_symdata = elf_getdata(symscn, NULL);
				cd.cd_strdata = elf_getdata(symstrscn, NULL);
			}
		}
	} else {
		struct stat st;

		if (fstat(fd, &st) == -1)
			die("failed to fstat %s", filename);

		cd.cd_ctflen = st.st_size;
		cd.cd_ctfdata = mmap(NULL, cd.cd_ctflen, PROT_READ,
		    MAP_PRIVATE, fd, 0);
		if (cd.cd_ctfdata == MAP_FAILED)
			die("failed to mmap %s", filename);
	}

	/*
	 * Get a pointer to the CTF data buffer and interpret the first portion
	 * as a ctf_header_t.  Validate the magic number and size.
	 */

	if (cd.cd_ctflen < sizeof (ctf_preamble_t))
		die("%s does not contain a CTF preamble\n", filename);

	/* LINTED - pointer alignment */
	pp = (const ctf_preamble_t *)cd.cd_ctfdata;

	if (pp->ctp_magic != CTF_MAGIC)
		die("%s does not appear to contain CTF data\n", filename);

	if (pp->ctp_version == CTF_VERSION) {
		/* LINTED - pointer alignment */
		hp = (ctf_header_t *)cd.cd_ctfdata;
		cd.cd_ctfdata = (caddr_t)cd.cd_ctfdata + sizeof (ctf_header_t);

		if (cd.cd_ctflen < sizeof (ctf_header_t)) {
			die("%s does not contain a v%d CTF header\n", filename,
			    CTF_VERSION);
		}

	} else {
		die("%s contains unsupported CTF version %d\n", filename,
		    pp->ctp_version);
	}

	/*
	 * If the data buffer is compressed, then malloc a buffer large enough
	 * to hold the decompressed data, and use zlib to decompress it.
	 */
	if (hp->cth_flags & CTF_F_COMPRESS) {
		z_stream zstr;
		void *buf;
		int rc;

		if ((buf = malloc(hp->cth_stroff + hp->cth_strlen)) == NULL)
			die("failed to allocate decompression buffer");

		bzero(&zstr, sizeof (z_stream));
		zstr.next_in = (void *)cd.cd_ctfdata;
		zstr.avail_in = cd.cd_ctflen;
		zstr.next_out = buf;
		zstr.avail_out = hp->cth_stroff + hp->cth_strlen;

		if ((rc = inflateInit(&zstr)) != Z_OK)
			die("failed to initialize zlib: %s\n", zError(rc));

		if ((rc = inflate(&zstr, Z_FINISH)) != Z_STREAM_END)
			die("failed to decompress CTF data: %s\n", zError(rc));

		if ((rc = inflateEnd(&zstr)) != Z_OK)
			die("failed to finish decompression: %s\n", zError(rc));

		if (zstr.total_out != hp->cth_stroff + hp->cth_strlen)
			die("CTF data is corrupt -- short decompression\n");

		cd.cd_ctfdata = buf;
		cd.cd_ctflen = hp->cth_stroff + hp->cth_strlen;
	}

	if (flags & F_HDR)
		error |= print_header(hp, &cd);
	if (flags & (F_LABEL))
		error |= print_labeltable(hp, &cd);
	if (flags & (F_DATA | F_STATS))
		error |= read_data(hp, &cd);
	if (flags & (F_FUNC | F_STATS))
		error |= read_funcs(hp, &cd);
	if (flags & (F_TYPES | F_STATS))
		error |= read_types(hp, &cd);
	if (flags & (F_STR | F_STATS))
		error |= read_strtab(hp, &cd);
	if (flags & F_STATS)
		error |= print_stats();

	/*
	 * If the -u option is specified, write the uncompressed CTF data to a
	 * raw CTF file.  CTF data can already be extracted compressed by
	 * applying elfdump -w -N .SUNW_ctf to an ELF file, so we don't bother.
	 */
	if (ufile != NULL) {
		ctf_header_t h;

		bcopy(hp, &h, sizeof (h));
		h.cth_flags &= ~CTF_F_COMPRESS;

		if ((ufd = open(ufile, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0 ||
		    write(ufd, &h, sizeof (h)) != sizeof (h) ||
		    write(ufd, cd.cd_ctfdata, cd.cd_ctflen) != cd.cd_ctflen) {
			warn("failed to write CTF data to '%s'", ufile);
			error |= E_ERROR;
		}

		(void) close(ufd);
	}

	if (elf != NULL)
		(void) elf_end(elf);

	(void) close(fd);
	return (error);
}
Example #8
0
int main(int argc, char *argv[])
{
    int i = 0;
    char* file_name = NULL;

    LASReaderH reader = NULL;
    LASHeaderH header = NULL;
    LASWriterH writer = NULL;

    int check_points = FALSE;
    int repair_header = FALSE;
    int change_header = FALSE;
    int repair_bounding_box = FALSE;
    int use_stdin = FALSE;
    int update_return_counts = FALSE;
    int skip_vlr = FALSE;
    int wkt = FALSE;

    char *system_identifier = NULL;
    char *generating_software = NULL;
    unsigned char file_creation_day = 0;
    unsigned char file_creation_year = 0;

    int err = 0;

    LASPointSummary* summary = NULL;

    for (i = 1; i < argc; i++)
    {
        if (strcmp(argv[i],"-v") == 0 || strcmp(argv[i],"--version") == 0)
        {
            char* ver = LAS_GetFullVersion();
            fprintf(stderr,"%s", ver);
            LASString_Free(ver);
            exit(0);
        }
        else if (strcmp(argv[i],"-h") == 0 || strcmp(argv[i],"--help") == 0)
        {
            usage();
            exit(0);
        }
        else if (strcmp(argv[i],"--input") == 0
              || strcmp(argv[i],"-input") == 0
              || strcmp(argv[i],"-i") == 0
              || strcmp(argv[i],"-in") == 0)
        {
            i++;
            file_name = argv[i];
        }
        else if (strcmp(argv[i], "--points") == 0
              || strcmp(argv[i], "--check") == 0
              || strcmp(argv[i], "--check_points") == 0
              || strcmp(argv[i], "-c") == 0
              || strcmp(argv[i], "-points") == 0
              || strcmp(argv[i], "-check") == 0
              || strcmp(argv[i], "-check_points") == 0)
        {
            check_points = TRUE;
        }
        else if (strcmp(argv[i], "--nocheck") == 0
              || strcmp(argv[i], "-nocheck") == 0)
        {
            check_points = FALSE;
        }
        else if (strcmp(argv[i], "--stdin") == 0
              || strcmp(argv[i], "-ilas") == 0) 
        {
            use_stdin = TRUE;
        }
        else if (strcmp(argv[i], "--repair") == 0
              || strcmp(argv[i], "-r") == 0
              || strcmp(argv[i], "-repair_header") == 0
              || strcmp(argv[i], "-repair") == 0) 
        {
            repair_header = TRUE;
            check_points = TRUE;
        }
        else if (strcmp(argv[i], "--repair_bb") == 0
              || strcmp(argv[i], "--repair_bounding_box") == 0
              || strcmp(argv[i], "--repair_boundingbox") == 0
              || strcmp(argv[i], "-repair_bb") == 0
              || strcmp(argv[i], "-repair_bounding_box") == 0
              || strcmp(argv[i], "-repair_boundingbox") == 0
              || strcmp(argv[i], "-repair") == 0
              || strcmp(argv[i], "-rb") == 0) 
        {
            repair_bounding_box = TRUE;
            check_points = TRUE;
        }

        else if (strcmp(argv[i],"--system_identifier") == 0
              || strcmp(argv[i],"-system_identifier") == 0
              || strcmp(argv[i],"-s") == 0
              || strcmp(argv[i],"-sys_id") == 0)
        {
            i++;
            system_identifier = (char*) malloc(31 * sizeof(char));
            strcpy(system_identifier, argv[i]);
            change_header = TRUE;
        }

        else if (strcmp(argv[i],"--generating_software") == 0
              || strcmp(argv[i],"-generating_software") == 0
              || strcmp(argv[i],"-g") == 0
              || strcmp(argv[i],"-gen_soft") == 0)
        {
            i++;
            generating_software = (char*) malloc(31*sizeof(char));
            strcpy(generating_software, argv[i]);
            change_header = TRUE;
        }
        else if (strcmp(argv[i],"--file_creation") == 0
              || strcmp(argv[i],"-file_creation") == 0)
        {
            /* XXX - mloskot: Consider replacing atoi with strtol,
            see http://www.iso-9899.info/wiki/Converting */
            i++;
            file_creation_day = (unsigned char)atoi(argv[i]);
            i++;
            file_creation_year = (unsigned char)atoi(argv[i]);
            change_header = TRUE;
        }
        else if (strcmp(argv[i],"--skip_vlr") == 0 || strcmp(argv[i],"--no_vlr") == 0)
        {
            skip_vlr = TRUE;
        }
        else if (strcmp(argv[i],"--wkt") == 0)
        {
            wkt = TRUE;
        }    
        else if (file_name == NULL)
        {
            file_name = argv[i];
        } 
        else
        {
            usage();
            fprintf(stderr, "ERROR: unknown argument '%s'\n",argv[i]);
            exit(1);
        }
    }

    if (use_stdin) {
        file_name = "stdin";
    }

    if (!file_name) {
        LASError_Print("No filename was provided to be opened");
        usage();
        exit(1);
    }

    reader = LASReader_Create(file_name);
    if (!reader) { 
        LASError_Print("Could not open file ");
        exit(1);
    } 

    header = LASReader_GetHeader(reader);
    if (!header) { 
        LASError_Print("Could not get LASHeader ");
        exit(1);
    } 

    print_header(stdout, header, file_name, skip_vlr, wkt);

    if (change_header) {
        if (system_identifier) {
            err = LASHeader_SetSystemId (header, system_identifier);
            if (err) LASError_Print("Could not set SystemId");
        }
        if (generating_software) {
            err = LASHeader_SetSoftwareId(header, generating_software);
            if (err) LASError_Print("Could not set SoftwareId");
        }
        if ( file_creation_day || file_creation_year) {
            err = LASHeader_SetCreationDOY(header, file_creation_day);
            if (err) LASError_Print("Could not set file creation day");
            err = LASHeader_SetCreationYear(header, file_creation_year);
            if (err) LASError_Print("Could not set file creation year");
        }

        /* We need to wipe out the reader and make a writer. */
        if (reader) {
            LASReader_Destroy(reader);
            reader = NULL;
        }

        writer = LASWriter_Create(file_name, header, LAS_MODE_APPEND);
        if (!writer) {
            LASError_Print("Problem creating LASWriterH object");
            LASHeader_Destroy(header);
            header = NULL;
            exit(1);
        }

        if (writer) LASWriter_Destroy(writer);
        writer = NULL;
        if (header) LASHeader_Destroy(header);
        header = NULL;
    }

    if (check_points)
    {
        if (!reader) {
            reader = LASReader_Create(file_name);
            if (!reader) { 
                LASError_Print("Could not open file ");
                exit(1);
            } 
        }

        if (! header) {
            header = LASReader_GetHeader(reader);
            if (!header) { 
                LASError_Print("Could not get LASHeader ");
                exit(1);
            } 
        } 

        if (!summary)
            summary = SummarizePoints(reader);
        print_point_summary(stdout, summary, header);

        if (repair_header) {
            fprintf(stdout, "\n---------------------------------------------------------\n");
            fprintf(stdout, "  Repair Summary\n");
            fprintf(stdout, "---------------------------------------------------------\n");

            if (use_stdin) {
                LASError_Print("Cannot update header information on piped input!");
                exit(1);
            }

            if (! header) {
                header = LASReader_GetHeader(reader);
                if (!header) { 
                    LASError_Print("Could not get LASHeader ");
                    exit(1);
                }
            } 

            if (! repair_bounding_box) {
                if ( LASHeader_GetMinX(header) != LASPoint_GetX(summary->pmin) )
                    repair_bounding_box = TRUE;
                if ( LASHeader_GetMinY(header) != LASPoint_GetY(summary->pmin) )
                    repair_bounding_box = TRUE;
                if ( LASHeader_GetMinZ(header) != LASPoint_GetZ(summary->pmin) )
                    repair_bounding_box = TRUE;

                if ( LASHeader_GetMaxX(header) != LASPoint_GetX(summary->pmax) )
                    repair_bounding_box = TRUE;
                if ( LASHeader_GetMaxY(header) != LASPoint_GetY(summary->pmax) )
                    repair_bounding_box = TRUE;
                if ( LASHeader_GetMaxZ(header) != LASPoint_GetZ(summary->pmax) )
                    repair_bounding_box = TRUE;
            }

            if (repair_bounding_box) {
                fprintf(stdout, "  Reparing Bounding Box...\n");
                err = LASHeader_SetMin( header, 
                    LASPoint_GetX(summary->pmin), 
                    LASPoint_GetY(summary->pmin), 
                    LASPoint_GetZ(summary->pmin)
                    );
                if (err) {
                    LASError_Print("Could not set minimum for header ");
                    exit(1);
                }
                err = LASHeader_SetMax( header, 
                    LASPoint_GetX(summary->pmax), 
                    LASPoint_GetY(summary->pmax), 
                    LASPoint_GetZ(summary->pmax)
                    );
                if (err) {
                    LASError_Print("Could not set minimum for header ");
                    exit(1);
                }
            }

            for (i = 0; i < 5; i++) {

                if (LASHeader_GetPointRecordsByReturnCount(header, i) != 
                    summary->number_of_points_by_return[i]) 
                {
                    update_return_counts = TRUE;
                    break;
                }
            }

            if (update_return_counts) {
                fprintf(stdout, "  Reparing Point Count by Return...\n");
                for (i = 0; i < 5; i++) {
                    LASHeader_SetPointRecordsByReturnCount( header,  
                        i, 
                        summary->number_of_points_by_return[i]);
                }                
            }

            if (reader) {
                LASReader_Destroy(reader);
                reader = NULL;
            }

            writer = LASWriter_Create(file_name, header, LAS_MODE_APPEND);
            if (!writer) {
                LASError_Print("Problem creating LASWriterH object for append");
                LASHeader_Destroy(header);
                header = NULL;
                exit(1);
            }
            LASWriter_Destroy(writer);
            writer = NULL;
            LASHeader_Destroy(header);
            header = NULL;            
        }

        if (summary) {
            LASPoint_Destroy(summary->pmin);
            LASPoint_Destroy(summary->pmax);
            free(summary);

        }
    }   

    if (reader) LASReader_Destroy(reader);
    if (header) LASHeader_Destroy(header);

#ifdef HAVE_GDAL
    /* Various GDAL related cleanups */
#ifdef OSRCleanup
    OSRCleanup();
#endif
    CPLFinderClean();
    CPLFreeConfig();
    CPLCleanupTLS();
#endif

    return 0;
}
int snake_game::start_game()
{
    std::vector<std::pair<int, int> > vSnakeBody;
    std::map<int, std::map<int, bool> > mSnakeBody;

    int iOffsetX = (TERMX > FULL_SCREEN_WIDTH) ? (TERMX - FULL_SCREEN_WIDTH) / 2 : 0;
    int iOffsetY = (TERMY > FULL_SCREEN_HEIGHT) ? (TERMY - FULL_SCREEN_HEIGHT) / 2 : 0;

    WINDOW *w_snake = newwin(FULL_SCREEN_HEIGHT, FULL_SCREEN_WIDTH, iOffsetY, iOffsetX);
    print_header(w_snake);

    //Snake start position
    vSnakeBody.push_back(std::make_pair(FULL_SCREEN_HEIGHT / 2, FULL_SCREEN_WIDTH / 2));
    mSnakeBody[FULL_SCREEN_HEIGHT / 2][FULL_SCREEN_WIDTH / 2] = true;
    mvwputch(w_snake, vSnakeBody[vSnakeBody.size() - 1].first, vSnakeBody.back().second, c_white, '#');

    //Snake start direction
    int iDirY = 0;
    int iDirX = 1;

    //Snake start length
    size_t iSnakeBody = 10;

    //GameSpeed aka inputdelay/timeout
    int iGameSpeed = 100;

    //Score
    int iScore = 0;
    int iFruitPosY = 0;
    int iFruitPosX = 0;

    //Draw Score
    print_score(w_snake, iScore);

    long ch;
    InputEvent input;

    do {
        //Check if we hit a border
        if (vSnakeBody[vSnakeBody.size() - 1].first + iDirY == 0) {
            vSnakeBody.push_back(std::make_pair(vSnakeBody[vSnakeBody.size() - 1].first +
                                                iDirY + FULL_SCREEN_HEIGHT - 2,
                                                vSnakeBody[vSnakeBody.size() - 1].second + iDirX));

        } else if (vSnakeBody[vSnakeBody.size() - 1].first + iDirY == FULL_SCREEN_HEIGHT - 1) {
            vSnakeBody.push_back(std::make_pair(vSnakeBody[vSnakeBody.size() - 1].first +
                                                iDirY - FULL_SCREEN_HEIGHT + 2,
                                                vSnakeBody[vSnakeBody.size() - 1].second + iDirX));

        } else if (vSnakeBody[vSnakeBody.size() - 1].second + iDirX == 0) {
            vSnakeBody.push_back(std::make_pair(vSnakeBody[vSnakeBody.size() - 1].first + iDirY,
                                                vSnakeBody[vSnakeBody.size() - 1].second +
                                                iDirX + FULL_SCREEN_WIDTH - 2));

        } else if (vSnakeBody[vSnakeBody.size() - 1].second + iDirX == FULL_SCREEN_WIDTH - 1) {
            vSnakeBody.push_back(std::make_pair(vSnakeBody[vSnakeBody.size() - 1].first + iDirY,
                                                vSnakeBody[vSnakeBody.size() - 1].second +
                                                iDirX - FULL_SCREEN_WIDTH + 2));

        } else {
            vSnakeBody.push_back(std::make_pair(vSnakeBody[vSnakeBody.size() - 1].first + iDirY,
                                                vSnakeBody[vSnakeBody.size() - 1].second + iDirX));
        }

        //Check if we hit ourself
        if (mSnakeBody[vSnakeBody[vSnakeBody.size() - 1].first][vSnakeBody[vSnakeBody.size() - 1].second]) {
            //We are dead :(
            break;
        } else {
            //Add new position to map
            mSnakeBody[vSnakeBody[vSnakeBody.size() - 1].first][vSnakeBody[vSnakeBody.size() - 1].second] = true;
        }

        //Have we eaten the forbidden fruit?
        if (vSnakeBody[vSnakeBody.size() - 1].first == iFruitPosY &&
            vSnakeBody[vSnakeBody.size() - 1].second == iFruitPosX) {
            iScore += 500;
            iSnakeBody += 10;
            iGameSpeed -= 3;

            print_score(w_snake, iScore);

            iFruitPosY = 0;
            iFruitPosX = 0;
        }

        //Check if we are longer than our max size
        if (vSnakeBody.size() > iSnakeBody) {
            mSnakeBody[vSnakeBody[0].first][vSnakeBody[0].second] = false;
            mvwputch(w_snake, vSnakeBody[0].first, vSnakeBody[0].second, c_black, ' ');
            vSnakeBody.erase(vSnakeBody.begin(), vSnakeBody.begin() + 1);
        }

        //Draw Snake
        mvwputch(w_snake, vSnakeBody[vSnakeBody.size() - 1].first,
                 vSnakeBody[vSnakeBody.size() - 1].second, c_white, '#');
        mvwputch(w_snake, vSnakeBody[vSnakeBody.size() - 2].first,
                 vSnakeBody[vSnakeBody.size() - 2].second, c_ltgray, '#');

        //On full length add a fruit
        if (iFruitPosY == 0 && iFruitPosY == 0) {
            do {
                iFruitPosY = rng(1, FULL_SCREEN_HEIGHT - 2);
                iFruitPosX = rng(1, FULL_SCREEN_WIDTH - 2);
            } while (mSnakeBody[iFruitPosY][iFruitPosX]);

            mvwputch(w_snake, iFruitPosY, iFruitPosX, c_ltred, '*');
        }

        wrefresh(w_snake);

        //Check input
        timeout(iGameSpeed);
        ch = getch();
        input = get_input(ch);
        timeout(-1);

        switch (input) {
        case DirectionN: /* up */
            if (iDirY != 1) {
                iDirY = -1;
                iDirX = 0;
            }
            break;
        case DirectionS: /* down */
            if (iDirY != -1) {
                iDirY = 1;
                iDirX = 0;
            }
            break;
        case DirectionW: /* left */
            if (iDirX != 1) {
                iDirY = 0;
                iDirX = -1;
            }
            break;
        case DirectionE: /* right */
            if (iDirX != -1) {
                iDirY = 0;
                iDirX = 1;
            }
            break;
        case Cancel:
            return iScore;
            break;
        default:
            break;
        }

    } while (true);

    snake_over(w_snake, iScore);
    return iScore;
}
Example #10
0
int
dr_writer_init (FILE * pfile)
{
  print_header (pfile);
  return 0;
}
int main ()
{
   int file_size , padding = 0;
   HEADER bmp_header;
   INFO_HEADER bmp_info_header;
   COLOR_PALLET *bgr_pallete = malloc (sizeof( COLOR_PALLET ) * CHAR_RANGE);
   unsigned char *data_buffer , *out_buffer ;
   char *file_name ;
   FILE *in_file = NULL;

   file_name = get_file_name ();
   in_file =  fopen ( file_name , "rb" );
   if (in_file == NULL)
   {
      printf("file opening failed\n");
      return 1;
   }

   if ( fread ( &bmp_header , sizeof ( HEADER ) , 1 , in_file) < 1 )
   {
      printf("Some issue in reading file\n");
      return 0;
   }

   if ( fread ( &bmp_info_header , sizeof ( INFO_HEADER ) , 1 , in_file) < 1 )
   {
      printf("Some issue in reading file\n");
      return 1;
   }
   print_header( bmp_header , bmp_info_header );
   //checking if the image is bmp or not
   if ( bmp_header.signature != BMP_SIGNATURE )
   {
      printf("no a bmp\n");
      return 1;
   }

   if ( bmp_info_header.bits_per_pixel != RGB_SIZE )
   {
      printf("already in greysacle\n");
      return 1;
   }

   if ( fseek ( in_file , bmp_header.offset , SEEK_SET ) == -1 )
   {
      printf("file operation failed\n");
      return 1;
   }

   file_size = bmp_info_header.image_data_size ;
   //based on file_size allocate memory
   data_buffer = malloc (sizeof (char) * file_size + 1 );
   if (data_buffer == NULL)
   {
     printf("malloc Failed\n");
     return 1;
   }
   *(data_buffer  + file_size) = CHAR_NULL;
   //read the data into buffer
   if ( fread ( data_buffer , sizeof(char) , file_size , in_file ) < file_size )
   {
     printf("Some issue in reading file\n");
     fclose (in_file);
     free (data_buffer);
     return 1;
   }

   if ( fclose ( in_file ) == 0  )
   {
      printf("File not closing\n" );
   }

   out_buffer = covert_to_mono ( data_buffer , file_size , bmp_info_header);

   if ( ( bmp_info_header.image_width * 3 ) % 4 != 0)
   padding = 4 - ( ( bmp_info_header.image_width * 3 ) % 4 );

   //change the header details
   bmp_header.offset = COLOR_PALLET_SIZE + HEADER_SIZE;
   bmp_header.bmp_file_size = HEADER_SIZE + ( bmp_info_header.image_width * bmp_info_header.image_height ) + ( padding * bmp_info_header.image_height ) + COLOR_PALLET_SIZE;
   bmp_info_header.bits_per_pixel = 8;
   bmp_info_header.image_data_size =( bmp_info_header.image_width * bmp_info_header.image_height ) + ( padding * bmp_info_header.image_height );

   print_header( bmp_header , bmp_info_header );

   //get bgr pallete
   get_color_pallet ( bgr_pallete );

   to_file (out_buffer, bmp_header , bmp_info_header , bgr_pallete , bmp_info_header.image_data_size) ;


   free (data_buffer);
   data_buffer = NULL;
   free ( out_buffer );
   out_buffer = NULL;
   free (bgr_pallete);
   bgr_pallete = NULL;

   return 0;
}
Example #12
0
void print_program()
{
#ifdef DEBUG
	fprintf(stdout, "starting print program...\n");
#endif

	output = fopen("output_a.out.c", "w");
	if(!output){
		fprintf(stderr, "error in print_program\n");
		exit(0);
	}

	print_header();	
	print_func_decaration();
	print_safety_guard();
	print_print_pc();
	
	//yang
	unsigned  instsize = 0;
	unsigned int globalsize = 0;
	globalsize = print_dependence_data_to_file();
	
////////	
	for(PROGRAM::iterator it = g_current_program->begin();//dump functions
			it != g_current_program->end();it++){
		
#ifdef WINDOWS_NAKED
	fprintf(output, "__declspec( naked ) ");
#endif
		if(g_main_pc == it->first){
			fprintf(output, "void main(){\n");
//Hush.b
//extern variables in dst
#ifdef DEBUG
			fprintf(stderr, "----dst_map size is %d\n", dst_map.size());	
#endif

#if 0
			if(!dst_map.empty()){
				for(map<unsigned int, Dst_item*>::iterator it= dst_map.begin();
					it!=dst_map.end();it++){
					fprintf(output, "extern %s;\n", it->second->name);	
				}	
			}
#endif
			fprintf(output, "output=fopen(\"log\", \"w\");\n");
//Hush.e
		}else{
			fprintf(output, "int func_0x%x(){\n", it->first);
		}
#ifdef DEBUG
		fprintf(stderr, "----function start pc:%x\t%x\n", it->first, it->second);
#endif
		g_current_func = it->second;
		print_func(it->second);
		fprintf(output, "}\n");

		instsize += it->second->size();
	}
	fclose(output);
#ifdef STATISTICS
	fprintf(stderr, "program:%s\n", PEMU_binary_name);
	fprintf(stderr, "number of executed inst:\t%d\n", g_inst_num);
	fprintf(stderr, "symbolized addresses %d\n", g_symbol_nums);
	fprintf(stderr, "direct jmp:\t%d\njcc:\t%d\ndirect call:\t%d\ndata:\t%d\ndis:\t%d\nimm:\t%d\n", 
			g_jmp_num, g_jcc_num, g_call_num, g_dump_data, g_dis_nums, g_imm_nums);
	fprintf(stderr, "checks num:%d\n", g_check_nums);
#endif
}
Example #13
0
int main( int argc, char** argv )
{
  GetOptContext ctx;
  init_getopt_context( &ctx );
  char c;

  while( (c = getopt( argc, argv, "?i:o:a:de:x:lrh:v", &ctx )) != -1 )
  {
    switch( c )
    {
    case 'i':
      g_inputFileName = ctx.optarg;
      break;
    case 'o':
      g_outputFileName = ctx.optarg;
      break;
    case 'e':
      if( strcmp( ctx.optarg, "big" ) == 0 )
        g_swapEndian = true;
      else if( strcmp( ctx.optarg, "little" ) == 0 )
        g_swapEndian = false;
      else
      {
        fprintf( stderr, "error: unknown argument for option -e: %s\n",
          ctx.optarg );
        return -1;
      }
      break;
    case 'a':
      g_asmFileName = ctx.optarg;
      break;
    case 'l':
      g_printIncludes = true;
      break;
    case 'v':
      /* verbose, print extra funny stuff! */
      break;
    case 'h':
      g_outputHeaderName = ctx.optarg;
      break;
    case ':':
      print_usage();
      return -1;
      break;
    case '?':
      print_usage();
      return 0;
      break;
    }
  }

  if(ctx.optind != argc)
  {
    fprintf( stdout, "%s: unexpected argument '%s'\n", argv[0], argv[ctx.optind] );
    print_usage();
    return -1;
  }

  int returnCode = 0;

  if( !g_inputFileName )
  {
    returnCode = -1;
    fprintf( stderr, "error: No input file given.\n" );
  }

  // Convert all \ to / in the input file name, in order to
  // aid the include path translation
  for( char* p = g_inputFileName; p && *p; ++p )
  {
    if( *p == '\\' )
      *p = '/';
  }

  if( returnCode == 0 )
  {
    Allocator a;
    a.m_Alloc = &allocate_memory;
    a.m_Free = &free_memory;
    BehaviorTreeContext btc = create_bt_context( a );

    ParserContextFunctions pcf;
    pcf.m_Read = &read_file;
    pcf.m_Error = &parser_error;
    pcf.m_Warning = &parser_warning;
    pcf.m_Translate = &parser_translate_include;

    ParsingInfo pi;
    pi.m_Name = g_inputFileName;
    pi.m_File = fopen( pi.m_Name, "r" );
    if( !pi.m_File )
    {
      fprintf( stderr, "%s(0): error: unable to open input file \"%s\" for reading.\n",
        g_inputFileName, pi.m_Name );
      returnCode = -1;
    }

    if( returnCode == 0 )
    {
      ParserContext pc = create_parser_context( btc );
      set_extra( pc, &pi );
      set_current( pc, pi.m_Name );
      returnCode = parse( pc, &pcf );
      destroy( pc );
    }

    if( pi.m_File )
      fclose( pi.m_File );

    Include* include = get_first_include( btc );
    while( returnCode == 0 && include )
    {
      pi.m_Name = include->m_Name;
      pi.m_File = fopen( pi.m_Name, "r" );
      if( !pi.m_File )
      {
        fprintf( stderr, "%s(%d): error: unable to open include file \"%s\" for reading.\n",
          include->m_Parent, include->m_LineNo, pi.m_Name );
        returnCode = -1;
        break;
      }

      ParserContext pc = create_parser_context( btc );
      set_extra( pc, &pi );
      set_current( pc, pi.m_Name );
      returnCode = parse( pc, &pcf );
      destroy( pc );

      if( pi.m_File )
        fclose( pi.m_File );

      if( returnCode != 0 )
        break;

      include = include->m_Next;
    }

    include = get_first_include( btc );
    while( returnCode == 0 && include && g_printIncludes )
    {
      printf( "%s\n", include->m_Name );
      include = include->m_Next;
    }

    if( g_outputHeaderName && returnCode == 0 )
    {
      FILE* header = fopen( g_outputHeaderName, "w" );
      if( !header )
      {
        fprintf( stderr, "%s(0): error: Unable to open output file %s for writing.\n",
          g_inputFileName, g_outputHeaderName );
        returnCode = -1;
      }
      else
      {
        returnCode = print_header( header, g_inputFileName, btc );
        if( returnCode != 0 )
          fprintf( stderr, "%s(0): error: unspecified error when writing header %s.\n",
            g_inputFileName, g_outputHeaderName );
        fclose( header );
      }
    }

    if( g_outputFileName && returnCode == 0 )
    {
      Program p;

      unsigned int debug_hash = hashlittle( "debug_info" );
      Parameter* debug_param = find_by_hash( get_options( btc ), debug_hash );
      if( debug_param )
        p.m_I.SetGenerateDebugInfo( as_integer( *debug_param ) );

      returnCode = setup( btc, &p );
      if( returnCode == 0 )
      {
        returnCode = generate( &p );
        if( returnCode != 0 )
          fprintf( stderr, "%s(0): error: Internal compiler error in generate.\n", g_inputFileName );
      }
      else
      {
        fprintf( stderr, "%s(0): error: Internal compiler error in setup.\n", g_inputFileName );
      }

      teardown( &p );

      if( returnCode == 0 )
      {
        g_outputFile = fopen( g_outputFileName, "wb" );
        if( !g_outputFile )
        {
          fprintf( stderr, "%s(0): error: Unable to open output file %s for writing.\n",
            g_inputFileName, g_outputFileName );
          returnCode = -2;
        }

        if( returnCode == 0 )
          returnCode = save_program( g_outputFile, g_swapEndian, &p );
        if( returnCode != 0 )
        {
          fprintf( stderr, "%s(0): error: Failed to write output file %s.\n",
            g_inputFileName, g_outputFileName );
          returnCode = -5;
        }
      }

      if( !g_asmFileName )
      {
        unsigned int hash = hashlittle( "force_asm" );
        Parameter* force_asm = find_by_hash( get_options( btc ), hash );
        if( force_asm && as_bool( *force_asm ) )
        {
          unsigned int len = strlen( g_outputFileName );
          g_asmFileNameMemory = (char*)malloc( len + 5 );
          memcpy( g_asmFileNameMemory, g_outputFileName, len );
          g_asmFileNameMemory[len + 0] = '.';
          g_asmFileNameMemory[len + 1] = 'a';
          g_asmFileNameMemory[len + 2] = 's';
          g_asmFileNameMemory[len + 3] = 'm';
          g_asmFileNameMemory[len + 4] = 0;
          g_asmFileName = g_asmFileNameMemory;
        }
      }

      if( returnCode == 0 && g_asmFileName )
      {
        FILE* asmFile = fopen( g_asmFileName, "w" );
        if( !asmFile )
        {
          fprintf( stderr, "%s(0): error: Unable to open assembly file %s for writing.\n",
            g_inputFileName, g_asmFileName );
          returnCode = -1;
        }
        else
        {
          print_program( asmFile, &p );
          fclose( asmFile );
        }
      }
    }
    destroy( btc );
  }

  if( g_asmFileNameMemory )
    free( g_asmFileNameMemory );

  if( g_outputFile )
    fclose( g_outputFile );

  return returnCode;
}
Example #14
0
void
diff_archive (void)
{
  struct stat stat_data;
  int name_length;
  int status;

  errno = EPIPE;		/* FIXME: errno should be read-only */
				/* FIXME: remove perrors */

  set_next_block_after (current_header);
  decode_header (current_header, &current_stat, &current_format, 1);

  /* Print the block from `current_header' and `current_stat'.  */

  if (verbose_option)
    {
      if (now_verifying)
	fprintf (stdlis, _("Verify "));
      print_header ();
    }

  switch (current_header->header.typeflag)
    {
    default:
      WARN ((0, 0, _("Unknown file type '%c' for %s, diffed as normal file"),
		 current_header->header.typeflag, current_file_name));
      /* Fall through.  */

    case AREGTYPE:
    case REGTYPE:
    case GNUTYPE_SPARSE:
    case CONTTYPE:

      /* Appears to be a file.  See if it's really a directory.  */

      name_length = strlen (current_file_name) - 1;
      if (current_file_name[name_length] == PATHSEP)
	goto really_dir;

      if (!get_stat_data (&stat_data))
	{
	  if (current_header->oldgnu_header.isextended)
	    skip_extended_headers ();
	  skip_file ((long) current_stat.st_size);
	  goto quit;
	}

      if (!S_ISREG (stat_data.st_mode))
	{
	  report_difference (_("Not a regular file"));
	  skip_file ((long) current_stat.st_size);
	  goto quit;
	}

      stat_data.st_mode &= 07777;
      if (stat_data.st_mode != current_stat.st_mode)
	report_difference (_("Mode differs"));

#if !MSDOS && !WIN32
      /* stat() in djgpp's C library gives a constant number of 42 as the
	 uid and gid of a file.  So, comparing an FTP'ed archive just after
	 unpack would fail on MSDOS.  */
      if (stat_data.st_uid != current_stat.st_uid)
	report_difference (_("Uid differs"));
      if (stat_data.st_gid != current_stat.st_gid)
	report_difference (_("Gid differs"));
#endif

      if (stat_data.st_mtime != current_stat.st_mtime)
	report_difference (_("Mod time differs"));
      if (current_header->header.typeflag != GNUTYPE_SPARSE &&
	  stat_data.st_size != current_stat.st_size)
	{
	  report_difference (_("Size differs"));
	  skip_file ((long) current_stat.st_size);
	  goto quit;
	}

      diff_handle = open (current_file_name, O_NDELAY | O_RDONLY | O_BINARY);

      if (diff_handle < 0 && !absolute_names_option)
	{
	  char *tmpbuf = xmalloc (strlen (current_file_name) + 2);

	  *tmpbuf = PATHSEP;
	  strcpy (tmpbuf + 1, current_file_name);
	  diff_handle = open (tmpbuf, O_NDELAY | O_RDONLY);
	  free (tmpbuf);
	}
      if (diff_handle < 0)
	{
	  ERROR ((0, errno, _("Cannot open %s"), current_file_name));
	  if (current_header->oldgnu_header.isextended)
	    skip_extended_headers ();
	  skip_file ((long) current_stat.st_size);
	  report_difference (NULL);
	  goto quit;
	}

      /* Need to treat sparse files completely differently here.  */

      if (current_header->header.typeflag == GNUTYPE_SPARSE)
	diff_sparse_files (current_stat.st_size);
      else
	{
	  if (multi_volume_option)
	    {
	      assign_string (&save_name, current_file_name);
	      save_totsize = current_stat.st_size;
	      /* save_sizeleft is set in read_and_process.  */
	    }

	  read_and_process ((long) (current_stat.st_size), process_rawdata);

	  if (multi_volume_option)
	    assign_string (&save_name, NULL);
	}

      status = close (diff_handle);
      if (status < 0)
	ERROR ((0, errno, _("Error while closing %s"), current_file_name));

    quit:
      break;

#if !MSDOS
    case LNKTYPE:
      {
	dev_t dev;
	ino_t ino;

	if (!get_stat_data (&stat_data))
	  break;

	dev = stat_data.st_dev;
	ino = stat_data.st_ino;
	status = stat (current_link_name, &stat_data);
	if (status < 0)
	  {
	    if (errno == ENOENT)
	      report_difference (_("Does not exist"));
	    else
	      {
		WARN ((0, errno, _("Cannot stat file %s"), current_file_name));
		report_difference (NULL);
	      }
	    break;
	  }

	if (stat_data.st_dev != dev || stat_data.st_ino != ino)
	  {
	    char *message = (char *)
	      xmalloc (MESSAGE_BUFFER_SIZE + strlen (current_link_name));

	    sprintf (message, _("Not linked to %s"), current_link_name);
	    report_difference (message);
	    free (message);
	    break;
	  }

	break;
      }
#endif /* not MSDOS */

#ifdef S_ISLNK
    case SYMTYPE:
      {
	char linkbuf[NAME_FIELD_SIZE + 3]; /* FIXME: may be too short.  */

	status = readlink (current_file_name, linkbuf, (sizeof linkbuf) - 1);

	if (status < 0)
	  {
	    if (errno == ENOENT)
	      report_difference (_("No such file or directory"));
	    else
	      {
		WARN ((0, errno, _("Cannot read link %s"), current_file_name));
		report_difference (NULL);
	      }
	    break;
	  }

	linkbuf[status] = '\0';	/* null-terminate it */
	if (strncmp (current_link_name, linkbuf, (size_t) status) != 0)
	  report_difference (_("Symlink differs"));

	break;
      }
#endif /* not S_ISLNK */

#ifdef S_IFCHR
    case CHRTYPE:
      current_stat.st_mode |= S_IFCHR;
      goto check_node;
#endif /* not S_IFCHR */

#ifdef S_IFBLK
      /* If local system doesn't support block devices, use default case.  */

    case BLKTYPE:
      current_stat.st_mode |= S_IFBLK;
      goto check_node;
#endif /* not S_IFBLK */

#ifdef S_ISFIFO
      /* If local system doesn't support FIFOs, use default case.  */

    case FIFOTYPE:
# ifdef S_IFIFO
      current_stat.st_mode |= S_IFIFO;
# endif
      current_stat.st_rdev = 0;	/* FIXME: do we need this? */
      goto check_node;
#endif /* S_ISFIFO */

    check_node:
      /* FIXME: deal with umask.  */

      if (!get_stat_data (&stat_data))
	break;

      if (current_stat.st_rdev != stat_data.st_rdev)
	{
	  report_difference (_("Device numbers changed"));
	  break;
	}

      if (
#ifdef S_IFMT
	  current_stat.st_mode != stat_data.st_mode
#else
	  /* POSIX lossage.  */
	  (current_stat.st_mode & 07777) != (stat_data.st_mode & 07777)
#endif
	  )
	{
	  report_difference (_("Mode or device-type changed"));
	  break;
	}

      break;

    case GNUTYPE_DUMPDIR:
      {
	char *dumpdir_buffer = get_directory_contents (current_file_name, 0);

	if (multi_volume_option)
	  {
	    assign_string (&save_name, current_file_name);
	    save_totsize = current_stat.st_size;
	    /* save_sizeleft is set in read_and_process.  */
	  }

	if (dumpdir_buffer)
	  {
	    dumpdir_cursor = dumpdir_buffer;
	    read_and_process ((long) (current_stat.st_size), process_dumpdir);
	    free (dumpdir_buffer);
	  }
	else
	  read_and_process ((long) (current_stat.st_size), process_noop);

	if (multi_volume_option)
	  assign_string (&save_name, NULL);
	/* Fall through.  */
      }

    case DIRTYPE:
      /* Check for trailing /.  */

      name_length = strlen (current_file_name) - 1;

    really_dir:
      while (name_length && current_file_name[name_length] == PATHSEP)
	current_file_name[name_length--] = '\0';	/* zap / */

      if (!get_stat_data (&stat_data))
	break;

      if (!S_ISDIR (stat_data.st_mode))
	{
	  report_difference (_("No longer a directory"));
	  break;
	}

      if ((stat_data.st_mode & 07777) != (current_stat.st_mode & 07777))
	report_difference (_("Mode differs"));
      break;

    case GNUTYPE_VOLHDR:
      break;

    case GNUTYPE_MULTIVOL:
      {
	off_t offset;

	name_length = strlen (current_file_name) - 1;
	if (current_file_name[name_length] == PATHSEP)
	  goto really_dir;

	if (!get_stat_data (&stat_data))
	  break;

	if (!S_ISREG (stat_data.st_mode))
	  {
	    report_difference (_("Not a regular file"));
	    skip_file ((long) current_stat.st_size);
	    break;
	  }

	stat_data.st_mode &= 07777;
	offset = from_oct (1 + 12, current_header->oldgnu_header.offset);
	if (stat_data.st_size != current_stat.st_size + offset)
	  {
	    report_difference (_("Size differs"));
	    skip_file ((long) current_stat.st_size);
	    break;
	  }

	diff_handle = open (current_file_name, O_NDELAY | O_RDONLY | O_BINARY);

	if (diff_handle < 0)
	  {
	    WARN ((0, errno, _("Cannot open file %s"), current_file_name));
	    report_difference (NULL);
	    skip_file ((long) current_stat.st_size);
	    break;
	  }

	status = lseek (diff_handle, offset, 0);
	if (status != offset)
	  {
	    WARN ((0, errno, _("Cannot seek to %ld in file %s"),
		   offset, current_file_name));
	    report_difference (NULL);
	    break;
	  }

	if (multi_volume_option)
	  {
	    assign_string (&save_name, current_file_name);
	    save_totsize = stat_data.st_size;
	    /* save_sizeleft is set in read_and_process.  */
	  }

	read_and_process ((long) (current_stat.st_size), process_rawdata);

	if (multi_volume_option)
	  assign_string (&save_name, NULL);

	status = close (diff_handle);
	if (status < 0)
	  ERROR ((0, errno, _("Error while closing %s"), current_file_name));

	break;
      }
    }
}
Example #15
0
unsigned int results_lcl ( 	const char * filename,    /* output filename */
                       		struct TSeq * t,          /* text */          
                       		unsigned int n,           /* length of t */
                       		struct TSeq * p,          /* pattern */
                       		unsigned int m,           /* length of p */
                       		double max_score, 
                       		unsigned int * gaps_pos, 
		       		unsigned int l,	
                       		unsigned int * gaps_len, 
                       		unsigned int * where, 
                       		unsigned int istart, 
                       		unsigned int iend, 
                       		unsigned int jstart, 
                       		unsigned int jend, 
                       		unsigned int swap, 
                       		unsigned int matrix,
                       		double gap_penalty,
                       		double ext_penalty, int L )
 {

   FILE          * output;

   char          * seqa;            //sequence a with the inserted gaps 
   unsigned int	   aa;
   unsigned int	   ii;
   char          * seqb;            //sequence b with the inserted gaps 
   unsigned int	   bb;
   unsigned int	   jj;
   char          * mark_mis;        //a string with the mismatches marked as '|' (and the matches as ' ')
   unsigned int    min_mis = 0;     //the number of mismatches in the alignment
   unsigned int	   mm = 0;

   char          * Cmark_mis; 	//cropped sequences      
   char          * Cseqa;        
   char          * Cseqb;        
   
   unsigned int i;

   /* Here we calculate the number of gaps and their total length */	
   unsigned int numgaps = 0;			
   unsigned int gapslength = 0;		

   for ( i = 0; i < l; i ++ )
    {
      if ( gaps_len[i] > 0 )
       {		
      	 numgaps++;
      	 gapslength += gaps_len[i];
       }
    }

   /* dynamic memory allocation for the 3 sequences */
   if ( ! ( seqa = ( char * ) calloc ( n + gapslength + 1, sizeof ( char ) ) ) )
    {
      fprintf ( stderr, "Error: seqa could not be allocated!!!\n" );
      return ( 0 );
    }
 
   if ( ! ( seqb = ( char * ) calloc ( n + gapslength + 1, sizeof ( char ) ) ) )
    {
      fprintf ( stderr, "Error: seqb could not be allocated!!!\n" );
      return ( 0 );
    } 
   
   if ( ! ( mark_mis = ( char* ) calloc ( n + gapslength + 1, sizeof( char ) ) ) )
    {
      fprintf ( stderr, "Error: mark_mis could not be allocated!!!\n" );
      return ( 0 );
    }
   
   if ( ! ( Cseqa = ( char * ) calloc ( n + gapslength + 1, sizeof ( char ) ) ) )
    {
      fprintf ( stderr, "Error: seqa could not be allocated!!!\n" );
      return ( 0 );
    }
 
   if ( ! ( Cseqb = ( char * ) calloc ( n + gapslength + 1, sizeof ( char ) ) ) )
    {
      fprintf ( stderr, "Error: seqb could not be allocated!!!\n" );
      return ( 0 );
    } 
   
   if ( ! ( Cmark_mis = ( char* ) calloc ( n + gapslength + 1, sizeof( char ) ) ) )
    {
      fprintf ( stderr, "Error: mark_mis could not be allocated!!!\n" );
      return ( 0 );
    }
 
   /* Here we open the output file */
   if ( ! ( output = fopen ( filename, "w" ) ) )
    {
      fprintf ( stderr, "Error: cannot open file %s!!!\n", filename );
      return ( 0 );
    }
   
   /* Here we print the header */
   print_header ( output, filename, matrix, gap_penalty, ext_penalty, L );
 
   #if 0
   fprintf ( stderr, "%s\n", t->data );
   fprintf ( stderr, "%s\n", p->data ); getchar();
   #endif

   /* Here we go through the gaps to create the 2 sequences */
   int g;
   unsigned int gapsuma = 0;  //currently added gap length in seqa
   unsigned int gapsumb = 0;  //currently added gap length in seqb
   ii = aa = jj = bb = 0;
   for ( g = numgaps - 1; g >= 0; g-- )
    {
      if ( gaps_len[g] > 0 )
       {
	 unsigned int gpos = gaps_pos[g];
	 unsigned int glen = gaps_len[g];
         if ( where[g] == 1 )
          {
            /* Add the letters before the gap */
            for ( ; ii < gpos; ii++, aa++ )
             seqa[aa] = t -> data[ii];

            /* Add the gap */
            for ( ; aa < ii + gapsuma + glen; aa++ )
             seqa[aa] = '-';

            gapsuma += glen;
          }
         if ( where[g] == 2 )
          {
            /* Add the letters before the gap */
            for ( ; jj < gpos; jj++, bb++ )
             seqb[bb] = p -> data[jj];

            /* Add the gap */
            for ( ; bb < jj + gapsumb + glen; bb++ )
             seqb[bb] = '-';

            gapsumb += glen;
          }
       }
    }

   /* Add what is left from both */
   for ( ; ii < istart; ii++, aa++ )
     seqa[aa] = t -> data[ii];
   seqa[aa] = '\0';
   for ( ; jj < jstart; jj++, bb++ )
     seqb[bb] = p -> data[jj];
   seqb[bb] = '\0';

   #if 0
   fprintf ( stderr, "%s\n", seqa );
   fprintf ( stderr, "%s\n", seqb ); getchar();
   #endif

   /* Crop the alignment */
   strncat ( &Cseqa[0], &seqa[iend-1], strlen(seqa) - iend + 1 );
   strncat ( &Cseqb[0], &seqb[jend-1], strlen(seqb) - jend + 1 );

   #if 0
   fprintf ( stderr, "%s\n", Cseqa );
   fprintf ( stderr, "%s\n", Cseqb ); getchar();
   #endif

   /* Here we create the match/mismatch sequence */
   unsigned int alignlen = min ( strlen( Cseqa ), strlen( Cseqb ) ); 
   for ( ; mm < alignlen; mm++ )
    {
      if ( Cseqa[mm] == '-' || Cseqb[mm] == '-' )
        mark_mis[mm] = ' ';
      else if ( Cseqa[mm] == Cseqb[mm] )
        mark_mis[mm] = '|';
      else 
	{
          mark_mis[mm] = '.';
        }
    }
   mark_mis[mm] = '\0';


   unsigned int first = 0;
   unsigned int last = 0;
   for ( ii = 0; ii < mm; ii++ )
   {
     if ( mark_mis[ii] == '|' ) // If the number of characters read is equal to where the traceback ends then we have found the pos of the first aligned pair
      {
        first = last = ii;
        break;
      }
   }
   for ( ; i < mm; i++ )
   {
     if ( mark_mis[i] == '|' ) // The position of the last aligner pair is simply the last match in the match/mismatch sequence
        last = i;
   }

   // Now we are ready to crop the sequences based on first and last variables -- We also count the mismatches
   aa = bb = mm = 0;
   for ( ii = first; ii <= last; ii++ )
    { 
         Cseqa[aa]=Cseqa[ii];
         aa++;
         Cseqb[bb]=Cseqb[ii];
         bb++;
         Cmark_mis[mm]=mark_mis[ii];
         if ( Cmark_mis[mm] == '.' ) min_mis++;
         mm++;
    }
   Cseqa[aa] = 0;
   Cseqb[bb] = 0;
   Cmark_mis[mm] = 0;

   #if 0
   fprintf ( stderr, "%s\n", Cseqa );
   fprintf ( stderr, "%s\n", Cmark_mis );
   fprintf ( stderr, "%s\n", Cseqb ); getchar();
   #endif

   free ( t -> data );
   t -> data = Cseqa;
   free ( p -> data );
   p -> data = Cseqb;
   iend = iend + first;
   jend = jend + first;

   if ( ! swap )
    {       
      wrap_lcl ( t, iend - 1, p, jend - 1, Cmark_mis, LINE_LNG, output ); 
    }
   else
    {        
      wrap_lcl ( p, jend - 1, t, iend - 1, Cmark_mis, LINE_LNG, output ); 
    }
   
   fprintf ( output, "\n" );
   fprintf ( output, "Alignment score: %lf\n", max_score );
   fprintf ( output, "Number of mismatches: %d\n", min_mis );
   fprintf ( output, "Number of gaps: %d\n", numgaps );
   fprintf ( output, "Length of gaps: %d\n", gapslength );
   
   if ( fclose ( output ) ) 
           fprintf ( stderr, "Error: cannot close file %s!!!\n", filename );
   
   free ( seqa );
   free ( seqb );
   free ( mark_mis );
   free ( Cseqa );
   free ( Cseqb );
   free ( Cmark_mis );

   return ( 1 );	
 }
Example #16
0
int main(int argc, char **argv)
{
	char id[UUID_STR_LEN + 1];
    int cf_index, ret, type;
    Octstr *os, *store_type, *store_location, *status;
	Msg *msg;
	CfgGroup *grp;

	conf_file = NULL;

    gwlib_init();

    //This can be overwritten with the -v flag at runtime
    log_set_output_level(DEFAULT_LOG_LEVEL);

    cf_index = get_and_set_debugs(argc, argv, check_args);

    if (argv[cf_index] == NULL) {
        print_usage(argv[0]);
        goto error;
    }

    if (conf_file == NULL)
    	conf_file = octstr_create("kannel.conf");

    cfg = cfg_create(conf_file);

    if (cfg_read(cfg) == -1)
    	panic(0, "Couldn't read configuration from `%s'.", octstr_get_cstr(conf_file));
	info(0, "1");
    grp = cfg_get_single_group(cfg, octstr_imm("core"));
    if (grp == NULL) {
    	printf("FATAL: Could not load Kannel's core group. Exiting.\n");
    	return 2;
    }

    store_location = cfg_get(grp, octstr_imm("store-location"));
    store_type = cfg_get(grp, octstr_imm("store-type"));

    store_init(store_type, store_location, -1, msg_pack, msg_unpack_wrapper);

    switch (command) {
    case COMMAND_LIST:
    	printf("Listing records %d -> %d\n", list_from+1, list_limit);
    	print_header();
		store_load(print_msg);
		if (counter == 0) {
			printf("|%60s%14s%60s|\n", "", "Store is Empty", "");
		}
		print_sep();
		break;
    case COMMAND_DELETE:
    	store_load(msg_count);
    	msg = msg_create(ack);
    	msg->ack.nack = ack_failed;
    	msg->ack.time = time(NULL);
    	uuid_parse(octstr_get_cstr(param_1), msg->ack.id);
    	ret = store_save(msg);
        if (ret == 0) {
        	printf("Deleted message %s\n", octstr_get_cstr(param_1));
        	counter--;
        } else {
        	printf("Could not delete message %s\n", octstr_get_cstr(param_1));
        }
        msg_destroy(msg);
		break;
    case COMMAND_EXPORT:
    	counter = 0;
    	type = 0;
    	list = gwlist_create();
    	store_load(msg_push);
    	printf("Exporting %ld messages...\n", gwlist_len(list));
    	if ((octstr_compare(param_1, octstr_imm("file")) == 0) ||
    		(octstr_compare(param_1, octstr_imm("spool")) == 0)) {
        	store_shutdown();
        	store_init(param_1, param_2, -1, msg_pack, msg_unpack_wrapper);
        	store_load(msg_count);
        	while ((os = gwlist_extract_first(list)) != NULL) {
        		msg = msg_unpack_wrapper(os);
        		if (msg != NULL) {
    				ret = store_save(msg);
    				if (ret == 0) {
    					counter++;
    				} else {
    					printf("Error saving message\n");
    				}
        		} else {
        			printf("Error extracting message\n");
        		}
        		msg_destroy(msg);
        	}
        	status = NULL;
    	} else if (octstr_compare(param_1, octstr_imm("text")) == 0) {
    		status = store_status(BBSTATUS_TEXT);
    	} else if (octstr_compare(param_1, octstr_imm("html")) == 0) {
    		status = store_status(BBSTATUS_HTML);
    	} else if (octstr_compare(param_1, octstr_imm("xml")) == 0) {
    		status = store_status(BBSTATUS_XML);
		} else {
			status = NULL;
		}
    	if (status != NULL) {
    	    file = fopen(octstr_get_cstr(param_2), "w");
    	    if (file == NULL) {
    	        error(errno, "Failed to open '%s' for writing, cannot create output file",
    		      octstr_get_cstr(param_2));
    	        return -1;
    	    }
    	    octstr_print(file, status);
    	    fflush(file);
    	    if (file != NULL)
    	    	fclose(file);
    		//printf("%s", octstr_get_cstr(status));
    	}
    	gwlist_destroy(list, octstr_destroy_item);

		break;
    default:
    	break;
    }

    octstr_destroy(store_type);
    octstr_destroy(store_location);
    cfg_destroy(cfg);
    store_shutdown();
error:
    gwlib_shutdown();

    return 1;
}
Example #17
0
/* Creates the output file with the alignment */
unsigned int results ( const char * filename,    /* output filename */
                       struct TSeq * t,          /* text */          
                       unsigned int n,           /* length of t */
                       struct TSeq * p,          /* pattern */
                       unsigned int m,           /* length of p */
                       double max_score, 
                       unsigned int * gaps_pos, 
		       unsigned int l,	
                       unsigned int * gaps_len, 
                       unsigned int * where, 
                       unsigned int swap, 
                       unsigned int matrix,
                       double gap_penalty,
                       double ext_penalty, int L )
 {

   FILE          * output;
   unsigned int    min_mis = 0;     //the number of mismatches in the alignment

   char          * seqa;            //sequence a with the inserted gaps 
   unsigned int	   aa = 0;
   unsigned int	   ii = 0;
   char          * seqb;            //sequence b with the inserted gaps 
   unsigned int	   bb = 0;
   unsigned int	   jj = 0;
   char          * mark_mis;        //a string with the mismatches marked as '|' (and the matches as ' ')
   unsigned int	   mm = 0;
   
   unsigned int i;

   /* Here we calculate the number of gaps and their total length */	
   unsigned int numgaps = 0;			
   unsigned int gapslength = 0;		

   for ( i = 0; i < l; i ++ )
    {
      if ( gaps_len[i] > 0 )
       {		
      	 numgaps++;
      	 gapslength += gaps_len[i];
       }
    }
   //fprintf( stderr, "Score: %.2f Gaps: %d Length: %d\n", max_score, numgaps, gapslength );

   /* dynamic memory allocation for the 3 sequences */
   if ( ! ( seqa = ( char * ) calloc ( n + gapslength + 1, sizeof ( char ) ) ) )
    {
      fprintf ( stderr, "Error: seqa could not be allocated!!!\n" );
      return ( 0 );
    }
 
   if ( ! ( seqb = ( char * ) calloc ( n + gapslength + 1, sizeof ( char ) ) ) )
    {
      fprintf ( stderr, "Error: seqb could not be allocated!!!\n" );
      return ( 0 );
    } 
   
   if ( ! ( mark_mis = ( char* ) calloc ( n + gapslength + 1, sizeof( char ) ) ) )
    {
      fprintf ( stderr, "Error: mark_mis could not be allocated!!!\n" );
      return ( 0 );
    }
 
   /* Here we open the output file */
   if ( ! ( output = fopen ( filename, "w" ) ) )
    {
      fprintf ( stderr, "Error: cannot open file %s!!!\n", filename );
      return ( 0 );
    }
   
   /* Here we print the header */
   print_header ( output, filename, matrix, gap_penalty, ext_penalty, L );

   /* Here we go through the gaps to create the 2 sequences */
   int g;
   unsigned int gapsuma = 0;  //currently added gap length in seqa
   unsigned int gapsumb = 0;  //currently added gap length in seqb
   for ( g = numgaps - 1; g >=0; g-- )
    {
      if ( gaps_len[g] > 0 )
       {
	 unsigned int gpos = gaps_pos[g];
	 unsigned int glen = gaps_len[g];
         if ( where[g] == 1 )
          {
            /* Add the letters before the gap */
            for ( ; ii < gpos; ii++, aa++ )
             seqa[aa] = t -> data[ii];

            /* Add the gap */
            for ( ; aa < ii + gapsuma + glen; aa++ )
             seqa[aa] = '-';

            gapsuma += glen;
          }
         if ( where[g] == 2 )
          {
            /* Add the letters before the gap */
            for ( ; jj < gpos; jj++, bb++ )
             seqb[bb] = p -> data[jj];

            /* Add the gap */
            for ( ; bb < jj + gapsumb + glen; bb++ )
             seqb[bb] = '-';

            gapsumb += glen;
          }
       }
    }

   /* Add what is left from both */
   for ( ; ii < n; ii++, aa++ )
     seqa[aa] = t -> data[ii];
   seqa[aa] = '\0';
   for ( ; jj < m; jj++, bb++ )
     seqb[bb] = p -> data[jj];
   seqb[bb] = '\0';

   unsigned int alignlen = min ( aa, bb); 
   for ( ; mm < alignlen; mm++ )
    {
      if ( seqa[mm] == '-' || seqb[mm] == '-' )
        mark_mis[mm] = ' ';
      else if ( seqa[mm] == seqb[mm] )
        mark_mis[mm] = '|';
      else 
	{
          min_mis++;
          mark_mis[mm] = '.';
        }
    }
   mark_mis[mm] = '\0';

   free ( t -> data );
   t -> data = seqa;
   free ( p -> data );
   p -> data = seqb;

   if ( ! swap )
    {       
      wrap ( t, p, mark_mis, LINE_LNG, output ); 
    }
   else
    {        
      wrap ( p, t, mark_mis, LINE_LNG, output ); 
    }
   
   fprintf ( output, "\n" );
   fprintf ( output, "Alignment score: %lf\n", max_score );
   fprintf ( output, "Number of mismatches: %d\n", min_mis );
   fprintf ( output, "Number of gaps: %d\n", numgaps );
   fprintf ( output, "Length of gaps: %d\n", gapslength );
   
   if ( fclose ( output ) ) 
           fprintf ( stderr, "Error: cannot close file %s!!!\n", filename );
   
   free ( seqa );
   free ( seqb );
   free ( mark_mis );

   return ( 1 );	
 }
Example #18
0
void* readFile(void *arg) {
    struct readThreadArgs *rTA = (struct readThreadArgs*) arg;
    LASReaderH reader = NULL;
    LASHeaderH header = NULL;
    LASPointH p = NULL;
    unsigned int index = 0;
    int read_index = 0;
    char *file_name_in = NULL;
    int i, j;

    while(1) {
        file_name_in = NULL;
        /*Get next file to read*/
        MT_set_lock(&dataLock);
        file_name_in = files_name_in[files_in_index];
        if (file_name_in == NULL) {
            MT_unset_lock(&dataLock);
            return NULL;
        }
        read_index = (files_in_index % rTA->num_read_threads);
        files_in_index++;

        struct writeT *dataWriteTT = (struct writeT*) malloc(sizeof(struct writeT)*rTA->num_of_entries);
        /*Lets read the data*/
        reader = LASReader_Create(file_name_in);
        if (!reader) {
            LASError_Print("Unable to read file");
            MT_unset_lock(&dataLock);
            exit(1);
        }
        MT_unset_lock(&dataLock);

        header = LASReader_GetHeader(reader);
        if (!header) {
            LASError_Print("Unable to fetch header for file");
            exit(1);
        }

        if (verbose)
        {
            print_header(stderr, header, file_name_in);
        }

        /*Allocate arrays for the columns*/
	long num_points = LASHeader_GetPointRecordsCount(header);
	for (i = 0; i < rTA->num_of_entries; i++) {
		dataWriteTT[i].num_points = num_points;
		dataWriteTT[i].values = malloc(entriesType[i]*num_points);
		dataWriteTT[i].type = entriesType[i];
	}

	/*Changes for Oscar's new Morton code function*/
	//unsigned int factorX = (unsigned int) (LASHeader_GetOffsetX(header) / LASHeader_GetScaleX(header));
	//unsigned int factorY = (unsigned int) (LASHeader_GetOffsetY(header) / LASHeader_GetScaleY(header));

    /*Compute factors to add to X and Y and cehck sanity of generated codes*/
    double file_scale_x = LASHeader_GetScaleX(header);
    double file_scale_y = LASHeader_GetScaleY(header);
    double file_scale_z = LASHeader_GetScaleZ(header);
    //printf("The scales are x:%lf y:%lf z:%lf\n", file_scale_x, file_scale_y, file_scale_z);

	/* scaled offsets to add for the morton encoding */
	int64_t factorX =  ((int64_t) (LASHeader_GetOffsetX(header) / file_scale_x)) - rTA->global_offset_x;
	int64_t factorY =  ((int64_t) (LASHeader_GetOffsetY(header) / file_scale_y)) - rTA->global_offset_y;

	if (rTA->check)
	{
	        // Check specified scales are like in the LAS file
		if (fabs(rTA->scale_x - file_scale_x) > TOLERANCE){
			fprintf(stderr, "ERROR: x scale in input file (%lf) does not match specified x scale (%lf)\n",file_scale_x, rTA->scale_x);
			exit(1);
		}
		if (fabs(rTA->scale_y - file_scale_y) > TOLERANCE){
			fprintf(stderr, "ERROR: y scale in input file (%lf) does not match specified y scale (%lf)\n",file_scale_y, rTA->scale_y);
			exit(1);
		}
		/* Check that the extent of the file (taking into account the global offset)
		 * is within 0,2^31 */
		double check_min_x = 1.0 + LASHeader_GetMinX(header) - (((double) rTA->global_offset_x) * rTA->scale_x);
		if (check_min_x < TOLERANCE) {
			fprintf(stderr, "ERROR: Specied X global offset is too large. (MinX - (GlobalX*ScaleX)) < 0\n");
			exit(1);
		}
		double check_min_y = 1.0 + LASHeader_GetMinY(header) - (((double) rTA->global_offset_y) * rTA->scale_y);
		if (check_min_y < TOLERANCE) {
			fprintf(stderr, "ERROR: Specied Y global offset is too large. (MinY - (GlobalY*ScaleY)) < 0\n");
			exit(1);
		}
		double check_max_x = LASHeader_GetMaxX(header) - (((double) rTA->global_offset_x) * rTA->scale_x);
		if (check_max_x > (MAX_INT_31 * rTA->scale_x)) {
			fprintf(stderr, "ERROR: Specied X global offset is too small. (MaxX - (GlobalX*ScaleX)) > (2^31)*ScaleX\n");
			exit(1);
		}
		double check_max_y = LASHeader_GetMaxY(header) - (((double) rTA->global_offset_y) * rTA->scale_y);
		if (check_max_y > (MAX_INT_31 * rTA->scale_y)) {
			fprintf(stderr, "ERROR: Specied Y global offset is too small. (MaxY - (GlobalY*ScaleY)) > (2^31)*ScaleY\n");
			exit(1);
		}
	}

        p = LASReader_GetNextPoint(reader);
        index = 0;
        while (p)
        {
            if (skip_invalid && !LASPoint_IsValid(p)) {
                if (verbose) {
                    LASError_Print("Skipping writing invalid point...");
                }
                p = LASReader_GetNextPoint(reader);
                index -=1;
                continue;
            }

            LASColorH color = NULL;
            for (j = 0; j < rTA->num_of_entries; j++) {
                uint64_t res;
                switch (entries[j]) {
                    case ENTRY_x:
                    case ENTRY_y:
                    case ENTRY_z:
                        ((double*) dataWriteTT[j].values)[index] = entriesFunc[j](p);
                        //printf(" Point is:%lf\n", ((double*) dataWriteTT[j].values)[index]);
                        break;
                    case ENTRY_X:
                        ((int*) dataWriteTT[j].values)[index] = entriesFunc[j](p) / file_scale_x;
                        break;
                    case ENTRY_Y:
                        ((int*) dataWriteTT[j].values)[index] = entriesFunc[j](p) / file_scale_y;
                        break;
                    case ENTRY_Z:
                        ((int*) dataWriteTT[j].values)[index] = entriesFunc[j](p) / file_scale_z;
                        break;
                    case ENTRY_k:
                        entriesFunc[j](&res, p, factorX, factorY);
                        ((int64_t*)dataWriteTT[j].values)[index] = res;
                        break;
                    case ENTRY_R:
                    case ENTRY_G:
                    case ENTRY_B:
                        color = (color == NULL) ? LASPoint_GetColor(p) : color;
                        dataWriteTT[j].values[index] = (double) entriesFunc[j](color);
                        break;
                    case ENTRY_M:
                        dataWriteTT[j].values[index] = index;
                        break;
                    default:
                        LASError_Print("las2col:readFile: Invalid Entry.");
                }
            }
            if (color != NULL)
                LASColor_Destroy(color);

            p = LASReader_GetNextPoint(reader);
            index +=1;
        }
        if (verbose)
            printf("Num of points:%d %ld for file:%s \n", index, num_points, file_name_in);

        /*Give the data to the writer threads*/
        MT_set_lock(&dataLock);
        LASHeader_Destroy(header);
        header = NULL;
        LASReader_Destroy(reader);
	    reader = NULL;

        /*TODO: make sure you are not overtaking other reading threads*/
        while (data[read_index] != NULL) {
            MT_cond_wait(&readCond, &dataLock);
        }
        data[read_index] = dataWriteTT;
        /*Wake up the main*/
        pthread_cond_broadcast(&mainCond);
        MT_unset_lock(&dataLock);

    }
    return NULL;
}
Example #19
0
int main(int argc, char *argv[])
{
	char cmd = '_';
	unsigned char no_header = 0;

	int long_opt_ind = 0;
	int long_opt_val = 0;
	
	static struct option long_opts[] = {
		{ "wii", no_argument, 0, 'w' },
		{ "neo2", no_argument, 0, 'q' },
		{ "neo3", no_argument, 0, 'z' },
		{ "sim", required_argument, 0, 's' },
		{ "config", required_argument, 0, 'd' },
		{ "gui", no_argument, 0, 'g' },
		{ "accel", no_argument, 0, 'a' },
		{ "new", required_argument, 0, 'n' },
		{ "view", required_argument, 0, 'o' },
		{ "train", required_argument, 0, 'e' },
		{ "record", required_argument, 0, 'r' },
		{ "upload", required_argument, 0, 'u' },
		{ "verbose", no_argument, 0, 'p' },
		{ "confirm", no_argument, 0, 'c' },
		{ "no-header", no_argument, 0, 'H' },
		{ "version", no_argument, 0, 'v' },
		{ "help", no_argument, 0, 'h' },
		{ 0, 0, 0, 0 }
	};
	
	g_mode = console;
	g_dev = dev_none;
	dir[0] = '\0';
	file[0] = '\0';
	verbose = 0;
	confirm = 0;
	opterr = 0;
	while ((long_opt_val = getopt_long(argc, argv, "wqzsd:gan:o:e:rupcHvh", long_opts, &long_opt_ind)) != -1) 
	{
		switch (long_opt_val)
		{
			case 'w': /* --wii */
				g_dev = (g_dev == dev_none) ? dev_wii : g_dev;
				break;
			case 'q': /* --neo2 */
				g_dev = (g_dev == dev_none) ? dev_neo2 : g_dev;
				break;
			case 'z': /* --neo3 */
				g_dev = (g_dev == dev_none) ? dev_neo3 : g_dev;
				break;
			case 's': /* --sim */
				g_dev = (g_dev == dev_none) ? dev_sim : g_dev;

				strncpy(sim_filename, optarg, sizeof(sim_filename));
				sim_filename[sizeof(sim_filename) / sizeof(sim_filename[0]) - 1] = '\0';
				break;
			case 'd': /* --config */
				strncpy(dir, optarg, sizeof(dir));
				dir[sizeof(dir) / sizeof(dir[0]) - 1] = '\0';
				break;
			case 'n': /* --new */
			case 'o': /* --view */
			case 'e': /* --train */
			case 'r': /* --record */
			case 'u': /* --upload */
				strncpy(file, optarg, sizeof(file));
				file[sizeof(file) / sizeof(file[0]) - 1] = '\0';
			case 'g': /* --gui */
			case 'a': /* --accel */
				cmd = long_opt_val;
				break;
			case 'p': /* --verbose */
				verbose = 1;
				break;
			case 'c': /* --confirm */
				confirm = 1;
				break;
			case 'H': /* no-header */
				no_header = 1;
				break;
			case 'v': /* --version */
				print_version();
				
				exit(0);
				break;
			case 'h': /* --help */
			case '?':
				print_usage();
				
				exit(0);
				break;
		}
	}
	
	if (!no_header) {
		print_header();
	}
		
	/* minimum requirements */
	if ((g_dev == dev_none) || (dir[0] == '\0') || (cmd == '_'))
	{
		print_usage();
		exit(1);
	}
	/* should be ok here */
	if (cmd == 'g') /* --gui */
	{
		g_mode = graphical;
		main_gui(argc, argv);
	} else if (cmd == 'u') { /* --upload */
		chdir(dir);
		if (upload(file)) {
			printf("File '%s' uploaded successfully\n", file);
			fflush(stdout);
		} else {
			fprintf(stderr, "Sorry, could not upload file '%s'\n", file);
			fflush(stderr);
			exit(1);
		}
	} else {
		handshake(cmd);
	}
			
	dev_close(g_dev);

	return 0;	
}
Example #20
0
ssize_t
qb_rb_write_to_file(struct qb_ringbuffer_s * rb, int32_t fd)
{
	ssize_t result;
	ssize_t written_size = 0;
	uint32_t hash = 0;
	uint32_t version = QB_RB_FILE_HEADER_VERSION;

	if (rb == NULL) {
		return -EINVAL;
	}
	print_header(rb);

	/*
 	 * 1. word_size
 	 */
	result = write(fd, &rb->shared_hdr->word_size, sizeof(uint32_t));
	if (result != sizeof(uint32_t)) {
		return -errno;
	}
	written_size += result;

	/*
	 * 2. 3. store the read & write pointers
	 */
	result = write(fd, (void *)&rb->shared_hdr->write_pt, sizeof(uint32_t));
	if (result != sizeof(uint32_t)) {
		return -errno;
	}
	written_size += result;
	result = write(fd, (void *)&rb->shared_hdr->read_pt, sizeof(uint32_t));
	if (result != sizeof(uint32_t)) {
		return -errno;
	}
	written_size += result;

	/*
	 * 4. version used
	 */
	result = write(fd, &version, sizeof(uint32_t));
	if (result != sizeof(uint32_t)) {
		return -errno;
	}
	written_size += result;

	/*
	 * 5. hash helps us verify header is not corrupted on file read
	 */
	hash = rb->shared_hdr->word_size + rb->shared_hdr->write_pt + rb->shared_hdr->read_pt + QB_RB_FILE_HEADER_VERSION;
	result = write(fd, &hash, sizeof(uint32_t));
	if (result != sizeof(uint32_t)) {
		return -errno;
	}
	written_size += result;

	result = write(fd, rb->shared_data,
		       rb->shared_hdr->word_size * sizeof(uint32_t));
	if (result != rb->shared_hdr->word_size * sizeof(uint32_t)) {
		return -errno;
	}
	written_size += result;

	qb_util_log(LOG_DEBUG, " writing total of: %zd\n", written_size);

	return written_size;
}
Example #21
0
static int find_word_unified(const char *word, const regimen_t regimen, const char *dict_name, FILE *dict_fp)
{
	wchar_t word_wc[MAX_WORD_SIZE];
	wchar_t str_wc[MAX_WORD_SIZE];

	char *str = NULL;
	size_t str_len = 0;

	long index_pos = -1;
	int translate_count = 0;

	bool break_end_flag = false;


	if ( strlen(word) < 1 )
		return 0;

	if ( strncpy_lower_wc(word_wc, word, MAX_WORD_SIZE - 1) == NULL ) {
		fprintf(stderr, "Cannot convert \"%s\" to (wchar_t *): %s\n", word, strerror(errno));
		return -1;
	}

	if ( regimen == usually_regimen || regimen == first_concurrence_regimen || list_regimen ) {
		index_pos = linear_index_pos(word_wc[0], dict_fp);
		if ( index_pos == 0 ) {
			return 0;
		}
		else if ( index_pos > 0 ) {
			if ( fseek(dict_fp, index_pos, SEEK_SET) != 0 )
				fprintf(stderr, "Seek fail on index \"%lc %ld\": %s: ignored\n", word_wc[0], index_pos, strerror(errno));
		}
		else {
			rewind(dict_fp);
		}
	}

	while ( getline(&str, &str_len, dict_fp) != -1 ) {
		if ( str[0] == '#' || str[0] == '\n' )
			continue;

		if ( (str_wc[0] = first_lower_wc(str)) == L'\0' )
			continue;

		if ( regimen == usually_regimen || regimen == first_concurrence_regimen || list_regimen ) {
			if ( word_wc[0] != str_wc[0] && break_end_flag )
				break;
			if ( word_wc[0] != str_wc[0] )
				continue;
			else
				break_end_flag = true;
		}

		if ( strncpy_lower_filter_wc(str_wc, str, MAX_WORD_SIZE - 1) == NULL ) {
			break_end_flag = false; // ill_defined_regimen not required this
			continue;
		}

		if ( regimen == usually_regimen ) {
			if ( !strcmp_full_wc(str_wc, word_wc) ) {
				++translate_count;

				if ( translate_count == 1 )
					print_header(dict_name, word_wc);
				print_translate(str, translate_count);

				if ( translate_count >= settings.max_translate_count )
					break;
			}
		}
		else if ( regimen == first_concurrence_regimen ) {
			if ( !strcmp_noend_wc(str_wc, word_wc) ) {
				++translate_count;

				if ( translate_count == 1 )
					print_header(dict_name, word_wc);
				print_translate(str, translate_count);

				break;
			}
		}
		else if ( regimen == list_regimen ) {
			if ( !strcmp_noend_wc(str_wc, word_wc) ) {
				++translate_count;

				if ( translate_count == 1 )
					print_header(dict_name, word_wc);
				print_list_item(str_wc, translate_count);

				if ( translate_count >= settings.max_translate_count )
					break;
			}
		}
		else if ( regimen == ill_defined_regimen ) {
			if ( !strcmp_jump_wc(str_wc, word_wc, settings.ill_defined_search_percent) ) {
				++translate_count;

				if ( translate_count == 1 )
					print_header(dict_name, word_wc);
				print_list_item(str_wc, translate_count);

				if ( translate_count >= settings.max_translate_count )
					break;
			}
		}
	}

	free(str);

	return translate_count;
}
Example #22
0
qb_ringbuffer_t *
qb_rb_create_from_file(int32_t fd, uint32_t flags)
{
	ssize_t n_read;
	size_t n_required;
	size_t total_read = 0;
	uint32_t read_pt;
	uint32_t write_pt;
	struct qb_ringbuffer_s *rb;
	uint32_t word_size = 0;
	uint32_t version = 0;
	uint32_t hash = 0;
	uint32_t calculated_hash = 0;

	if (fd < 0) {
		return NULL;
	}

	/*
	 * 1. word size
	 */
	n_required = sizeof(uint32_t);
	n_read = read(fd, &word_size, n_required);
	if (n_read != n_required) {
		qb_util_perror(LOG_ERR, "Unable to read blackbox file header");
		return NULL;
	}
	total_read += n_read;

	/*
	 * 2. 3. read & write pointers
	 */
	n_read = read(fd, &write_pt, sizeof(uint32_t));
	assert(n_read == sizeof(uint32_t));
	total_read += n_read;

	n_read = read(fd, &read_pt, sizeof(uint32_t));
	assert(n_read == sizeof(uint32_t));
	total_read += n_read;

	/*
	 * 4. version
	 */
	n_required = sizeof(uint32_t);
	n_read = read(fd, &version, n_required);
	if (n_read != n_required) {
		qb_util_perror(LOG_ERR, "Unable to read blackbox file header");
		return NULL;
	}
	total_read += n_read;

	/*
	 * 5. Hash
	 */
	n_required = sizeof(uint32_t);
	n_read = read(fd, &hash, n_required);
	if (n_read != n_required) {
		qb_util_perror(LOG_ERR, "Unable to read blackbox file header");
		return NULL;
	}
	total_read += n_read;

	calculated_hash = word_size + write_pt + read_pt + version;
	if (hash != calculated_hash) {
		qb_util_log(LOG_ERR, "Corrupt blackbox: File header hash (%d) does not match calculated hash (%d)", hash, calculated_hash);
		return NULL;
	} else if (version != QB_RB_FILE_HEADER_VERSION) {
		qb_util_log(LOG_ERR, "Wrong file header version. Expected %d got %d",
			QB_RB_FILE_HEADER_VERSION, version);
		return NULL;
	}

	/*
	 * 6. data
	 */
	n_required = (word_size * sizeof(uint32_t));

	/*
	 * qb_rb_open adds QB_RB_CHUNK_MARGIN + 1 to the requested size.
	 */
	rb = qb_rb_open("create_from_file", n_required - (QB_RB_CHUNK_MARGIN + 1),
			QB_RB_FLAG_CREATE | QB_RB_FLAG_NO_SEMAPHORE, 0);
	if (rb == NULL) {
		return NULL;
	}
	rb->shared_hdr->read_pt = read_pt;
	rb->shared_hdr->write_pt = write_pt;

	n_read = read(fd, rb->shared_data, n_required);
	if (n_read < 0) {
		qb_util_perror(LOG_ERR, "Unable to read blackbox file data");
		goto cleanup_fail;
	}
	total_read += n_read;

	if (n_read != n_required) {
		qb_util_log(LOG_WARNING, "read %zd bytes, but expected %zu",
			    n_read, n_required);
		goto cleanup_fail;
	}

	qb_util_log(LOG_DEBUG, "read total of: %zd", total_read);
	print_header(rb);

	return rb;

cleanup_fail:
	qb_rb_close(rb);
	return NULL;
}
Example #23
0
int main(int argc, char *argv[])
{
    int i, numprocs, rank, size, align_size, disp;
    int skip;
    double latency = 0.0, t_start = 0.0, t_stop = 0.0;
    double timer=0.0;
    int max_msg_size = 1048576, full = 0; 
    uint64_t requested_mem_limit = 0; 
    double avg_time = 0.0, max_time = 0.0, min_time = 0.0; 
    char *sendbuf, *recvbuf, *s_buf1, *r_buf1;
    int *rdispls, *recvcounts;
    MPI_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);

    if (process_args(argc, argv, rank, &max_msg_size, &full)) {
        MPI_Finalize();
        return EXIT_SUCCESS;
    }

    if(numprocs < 2) {
        if(rank == 0) {
            fprintf(stderr, "This test requires at least two processes\n");
        }

        MPI_Finalize();

        return EXIT_FAILURE;
    }

    requested_mem_limit = (uint64_t) (max_msg_size) * numprocs;
    if( requested_mem_limit > max_mem_limit) {
      max_msg_size = max_mem_limit/numprocs;
    }


    print_header(rank, full);

    recvcounts=rdispls=NULL;
    recvcounts = (int *) malloc (numprocs*sizeof(int));
    if(NULL == recvcounts) {
        fprintf(stderr, "malloc failed.\n");
        exit(1);
    }
    
    rdispls = (int *) malloc (numprocs*sizeof(int));
    if(NULL == rdispls) {
        fprintf(stderr, "malloc failed.\n");
        exit(1);
    }

    s_buf1 = r_buf1 = NULL;

    s_buf1 = (char *) malloc(sizeof(char)*max_msg_size + MAX_ALIGNMENT);
    if(NULL == s_buf1) {
        fprintf(stderr, "malloc failed.\n");
        exit(1);
    }
    
    r_buf1 = (char *) malloc(sizeof(char)*max_msg_size * numprocs + MAX_ALIGNMENT);
    if(NULL == r_buf1) {
        fprintf(stderr, "malloc failed.\n");
        exit(1);
    }


    align_size = getpagesize();

    sendbuf = (char *)(((unsigned long) s_buf1 + (align_size - 1)) / align_size
                    * align_size);
    recvbuf = (char *)(((unsigned long) r_buf1 + (align_size - 1)) / align_size
                    * align_size);

    memset(sendbuf, 1, max_msg_size);
    memset(recvbuf, 0, max_msg_size * numprocs);

    for(size=1; size <= max_msg_size; size *= 2) {

        if(size > LARGE_MESSAGE_SIZE) {
            skip = SKIP_LARGE;
            iterations = iterations_large;
        } else {
            skip = SKIP;
            
        }

        MPI_Barrier(MPI_COMM_WORLD);

        disp =0;
        for ( i = 0; i < numprocs; i++) {
            recvcounts[i] = size;
            rdispls[i] = disp;
            disp += size;
        }

        MPI_Barrier(MPI_COMM_WORLD);       
        timer=0.0;
        for(i=0; i < iterations + skip ; i++) {

            t_start = MPI_Wtime();

            MPI_Gatherv(sendbuf, size, MPI_CHAR, recvbuf, recvcounts, rdispls, MPI_CHAR, 0, MPI_COMM_WORLD);
        
            t_stop = MPI_Wtime();

            if(i >= skip) {
                timer+= t_stop-t_start;
            }
            MPI_Barrier(MPI_COMM_WORLD);
 
        }
        
        MPI_Barrier(MPI_COMM_WORLD);

        latency = (double)(timer * 1e6) / iterations;

        MPI_Reduce(&latency, &min_time, 1, MPI_DOUBLE, MPI_MIN, 0, 
                MPI_COMM_WORLD); 
        MPI_Reduce(&latency, &max_time, 1, MPI_DOUBLE, MPI_MAX, 0, 
                MPI_COMM_WORLD); 
        MPI_Reduce(&latency, &avg_time, 1, MPI_DOUBLE, MPI_SUM, 0, 
                MPI_COMM_WORLD); 
        avg_time = avg_time/numprocs; 

        print_data(rank, full, size, avg_time, min_time, max_time, iterations);
        MPI_Barrier(MPI_COMM_WORLD);
    }
    
    free(s_buf1);
    free(r_buf1);

    free(recvcounts);
    free(rdispls);

    MPI_Finalize();

    return EXIT_SUCCESS;
}
Example #24
0
/*
 * Create a spoiler file for artifacts
 */
static void spoil_artifact(void)
{
	int i, j;

	object_type forge;
	object_type *q_ptr;

	const bool old_spoil_art = spoil_art;
	const bool old_spoil_base = spoil_base;
	const bool old_cheat_item = cheat_item;

	/* Use full spoilers, but no cheating. */
	spoil_art = spoil_base = TRUE;
	cheat_item = FALSE;

	/* Dump the header */
	print_header();

	/* List the artifacts by tval */
	for (i = 0; group_artifact[i].idx; i++)
	{
		/* Write out the group title */
		if (group_artifact[i].str)
		{
			spoiler_blanklines(2);
			spoiler_underline(group_artifact[i].str);
			spoiler_blanklines(1);
		}

		/* Now search through all of the artifacts */
		for (j = 1; j < MAX_A_IDX; ++j)
		{
			artifact_type *a_ptr = &a_info[j];

			/* We only want objects in the current group */
			if (k_info[a_ptr->k_idx].tval != group_artifact[i].idx) continue;

			/* Get local object */
			q_ptr = &forge;

			/* Wipe the object */
			object_wipe(q_ptr);

			/* Attempt to "forge" the artifact */
			if (!make_fake_artifact(q_ptr, j)) continue;

			/* Know most things about the object. */
			object_known(q_ptr);

			my_fprintf(fff, "%v\n", object_desc_f3, q_ptr, OD_ART | OD_SHOP, 1);

			/* Describe the artifact in a relatively brief way. */
			identify_fully_file(q_ptr, fff, TRUE);

			/* Provide some allocation data. */
			fprintf(fff, "     Level %d", a_ptr->level);

			if (a_ptr->level2 && a_ptr->level2 != a_ptr->level)
			{
				fprintf(fff, "/%d", a_ptr->level2);
			}

			fprintf(fff, ", Rarity %d, %d.%d lbs, %ld Gold\n", a_ptr->rarity,
				a_ptr->weight/10, a_ptr->weight%10, a_ptr->cost);
		}
	}

	/* Reset spoilers. */
	spoil_art = old_spoil_art;
	spoil_base = old_spoil_base;
	cheat_item = old_cheat_item;
}
Example #25
0
int main(int argc, char **argv)
{
  struct insert_data idata;
  PGresult *ret;
  FILE *f;
  unsigned char fbuf[SRVBUFLEN];
  char logfile[SRVBUFLEN];
  char default_pwd[] = "arealsmartpwd";
  int have_pwd = 0, have_logfile = 0, n;
  int result = 0, position = 0, howmany = 0; 
  int do_nothing = 0;
  char *cl_sql_host = NULL, *cl_sql_user = NULL, *cl_sql_db = NULL, *cl_sql_table = NULL;

  char *sql_host;

  struct template_entry *teptr;
  int tot_size = 0, cnt = 0;
  u_char *te;

  struct template_header th;
  struct db_cache data;

  /* getopt() stuff */
  extern char *optarg;
  extern int optind, opterr, optopt;
  int errflag = 0, cp;

  memset(&idata, 0, sizeof(idata));
  memset(sql_data, 0, sizeof(sql_data));
  memset(update_clause, 0, sizeof(update_clause));
  memset(insert_clause, 0, sizeof(insert_clause));
  memset(lock_clause, 0, sizeof(lock_clause));
  memset(where, 0, sizeof(where));
  memset(values, 0, sizeof(values));
  memset(&data, 0, sizeof(data));
  memset(timebuf, 0, sizeof(timebuf));

  pp_size = sizeof(struct db_cache);

  while (!errflag && ((cp = getopt(argc, argv, ARGS)) != -1)) {
    switch (cp) {
    case 'd':
      debug = TRUE;
      break;
    case 'f':
      strlcpy(logfile, optarg, sizeof(logfile));
      have_logfile = TRUE;
      break;
    case 'o':
      position = atoi(optarg);
      if (!position) {
	printf("ERROR: invalid offset. Exiting.\n");
	exit(1);
      }
      break;
    case 'n':
      howmany = atoi(optarg);
      if (!howmany) {
        printf("ERROR: invalid number of elements. Exiting.\n");
        exit(1);
      }
      break;
    case 't':
      do_nothing = TRUE;
      break;
    case 'i':
      sql_dont_try_update = TRUE;
      break;
    case 'e':
      sql_history_since_epoch = TRUE;
      break;
    case 'P':
      strlcpy(sql_pwd, optarg, sizeof(sql_pwd));
      have_pwd = TRUE;
      break;
    case 'U':
      cl_sql_user = malloc(SRVBUFLEN);
      memset(cl_sql_user, 0, SRVBUFLEN);
      strlcpy(cl_sql_user, optarg, SRVBUFLEN);
      break;
    case 'D':
      cl_sql_db = malloc(SRVBUFLEN);
      memset(cl_sql_db, 0, SRVBUFLEN);
      strlcpy(cl_sql_db, optarg, SRVBUFLEN);
      break;
    case 'H':
      cl_sql_host = malloc(SRVBUFLEN);
      memset(cl_sql_host, 0, SRVBUFLEN);
      strlcpy(cl_sql_host, optarg, SRVBUFLEN);
      break;
    case 'T':
      cl_sql_table = malloc(SRVBUFLEN);
      memset(cl_sql_table, 0, SRVBUFLEN);
      strlcpy(cl_sql_table, optarg, SRVBUFLEN);
      break;
    case 'h':
      usage(argv[0]);
      exit(0);
      break;
    default:
      usage(argv[0]);
      exit(1);
    }
  }

  /* searching for user supplied values */ 
  if (!howmany) howmany = -1;
  if (!have_pwd) memcpy(sql_pwd, default_pwd, sizeof(default_pwd));
  if (!have_logfile) {
    usage(argv[0]);
    printf("\nERROR: missing logfile (-f)\nExiting...\n");
    exit(1);
  }

  f = fopen(logfile, "r");
  if (!f) {
    printf("ERROR: %s does not exists\nExiting...\n", logfile);
    exit(1);
  }

  fread(&lh, sizeof(lh), 1, f);
  lh.sql_table_version = ntohs(lh.sql_table_version);
  lh.sql_optimize_clauses = ntohs(lh.sql_optimize_clauses);
  lh.sql_history = ntohs(lh.sql_history);
  lh.what_to_count = ntohl(lh.what_to_count);
  lh.magic = ntohl(lh.magic);

  if (lh.magic == MAGIC) {
    if (debug) printf("OK: Valid logfile header read.\n");
    printf("sql_db: %s\n", lh.sql_db); 
    printf("sql_table: %s\n", lh.sql_table);
    printf("sql_user: %s\n", lh.sql_user);
    printf("sql_host: %s\n", lh.sql_host);
    if (cl_sql_db||cl_sql_table||cl_sql_user||cl_sql_host)
      printf("OK: Overrided by commandline options:\n");
    if (cl_sql_db) printf("sql_db: %s\n", cl_sql_db);
    if (cl_sql_table) printf("sql_table: %s\n", cl_sql_table);
    if (cl_sql_user) printf("sql_user: %s\n", cl_sql_user);
    if (cl_sql_host) printf("sql_host: %s\n", cl_sql_host);
  }
  else {
    printf("ERROR: Invalid magic number. Exiting.\n");
    exit(1);
  }

  /* binding SQL stuff */
  if (cl_sql_db) sql_db = cl_sql_db;
  else sql_db = lh.sql_db;
  if (cl_sql_table) sql_table = cl_sql_table;
  else sql_table = lh.sql_table;
  if (cl_sql_user) sql_user = cl_sql_user;
  else sql_user = lh.sql_user;
  if (cl_sql_host) sql_host = cl_sql_host;
  else sql_host = lh.sql_host;

  fread(&th, sizeof(th), 1, f);
  th.magic = ntohl(th.magic);
  th.num = ntohs(th.num);
  th.sz = ntohs(th.sz);

  if (th.magic == TH_MAGIC) {
    if (debug) printf("OK: Valid template header read.\n");
    if (th.num > N_PRIMITIVES) {
      printf("ERROR: maximum number of primitives exceeded. Exiting.\n");
      exit(1);
    }
    te = malloc(th.num*sizeof(struct template_entry));
    memset(te, 0, th.num*sizeof(struct template_entry));
    fread(te, th.num*sizeof(struct template_entry), 1, f);
  }
  else {
    if (debug) printf("ERROR: no template header found.\n");
    exit(1);
  }

  /* checking template */
  if (th.sz >= sizeof(fbuf)) {
    printf("ERROR: Objects are too big. Exiting.\n");
    exit(1);
  }
  teptr = (struct template_entry *) te;
  for (tot_size = 0, cnt = 0; cnt < th.num; cnt++, teptr++)
    tot_size += teptr->size;
  if (tot_size != th.sz) {
    printf("ERROR: malformed template header. Size mismatch. Exiting.\n");
    exit(1);
  }
  TPL_check_sizes(&th, &data, te);
  
  if (!do_nothing) {
    PG_compose_conn_string(&p, sql_host);
    if (!PG_DB_Connect2(&p)) {
      printf("ALERT: PG_DB_Connect2(): PGSQL daemon failed.\n");
      exit(1);
    }
  }
  else {
    if (debug) print_header();
  }

  /* composing the proper (filled with primitives used during
     the current execution) SQL strings */
  idata.num_primitives = PG_compose_static_queries();
  idata.now = time(NULL);

  /* handling offset */ 
  if (position) n = fseek(f, (th.sz*position), SEEK_CUR);

  /* handling single or iterative request */
  if (!do_nothing) ret = PQexec(p.desc, lock_clause);
  while(!feof(f)) {
    if (!howmany) break;
    else if (howmany > 0) howmany--;

    memset(fbuf, 0, th.sz);
    n = fread(fbuf, th.sz, 1, f);
    if (n) {
      re++;
      TPL_pop(fbuf, &data, &th, te);

      if (!do_nothing) result = PG_cache_dbop(&p, &data, &idata);
      else {
        if (debug) print_data(&data, lh.what_to_count, (position+re));
      }

      if (!result) we++;
      if (re != we) printf("WARN: unable to write element %u.\n", re);
    }
  }

  if (!do_nothing) {
    ret = PQexec(p.desc, "COMMIT");
    if (PQresultStatus(ret) != PGRES_COMMAND_OK) {
      we = 0; /* if we fail to commit, no elements will be written */
      PQclear(ret);
    }
    printf("\nOK: written [%u/%u] elements.\n", we, re);
  }
  else printf("OK: read [%u] elements.\n", re);
  PQfinish(p.desc);
  fclose(f);

  return 0;
}
Example #26
0
void wallcycle_print(FILE *fplog, int nnodes, int npme, double realtime,
                     gmx_wallcycle_t wc, wallclock_gpu_t *gpu_t)
{
    double     *cyc_sum;
    double      tot, tot_for_pp, tot_for_rest, tot_gpu, tot_cpu_overlap, gpu_cpu_ratio, tot_k;
    double      c2t, c2t_pp, c2t_pme;
    int         i, j, npp, nth_pp, nth_pme, nth_tot;
    char        buf[STRLEN];
    const char *hline = "-----------------------------------------------------------------------------";

    if (wc == NULL)
    {
        return;
    }

    nth_pp  = wc->nthreads_pp;
    nth_pme = wc->nthreads_pme;

    cyc_sum = wc->cycles_sum;

    npp     = nnodes - npme;
    nth_tot = npp*nth_pp + npme*nth_pme;

    /* When using PME-only nodes, the next line is valid for both
       PP-only and PME-only nodes because they started ewcRUN at the
       same time. */
    tot        = cyc_sum[ewcRUN];
    tot_for_pp = 0;

    /* Conversion factor from cycles to seconds */
    if (tot > 0)
    {
        c2t     = realtime/tot;
        c2t_pp  = c2t * nth_tot / (double) (npp*nth_pp);
        c2t_pme = c2t * nth_tot / (double) (npme*nth_pme);
    }
    else
    {
        c2t     = 0;
        c2t_pp  = 0;
        c2t_pme = 0;
    }

    fprintf(fplog, "\n     R E A L   C Y C L E   A N D   T I M E   A C C O U N T I N G\n\n");

    print_header(fplog, npp, nth_pp, npme, nth_pme);

    fprintf(fplog, "%s\n", hline);
    for (i = ewcPPDURINGPME+1; i < ewcNR; i++)
    {
        if (is_pme_subcounter(i))
        {
            /* Do not count these at all */
        }
        else if (npme > 0 && is_pme_counter(i))
        {
            /* Print timing information for PME-only nodes, but add an
             * asterisk so the reader of the table can know that the
             * walltimes are not meant to add up. The asterisk still
             * fits in the required maximum of 19 characters. */
            char buffer[STRLEN];
            snprintf(buffer, STRLEN, "%s *", wcn[i]);
            print_cycles(fplog, c2t_pme, buffer,
                         npme, nth_pme,
                         wc->wcc[i].n, cyc_sum[i], tot);
        }
        else
        {
            /* Print timing information when it is for a PP or PP+PME
               node */
            print_cycles(fplog, c2t_pp, wcn[i],
                         npp, nth_pp,
                         wc->wcc[i].n, cyc_sum[i], tot);
            tot_for_pp += cyc_sum[i];
        }
    }
    if (wc->wcc_all != NULL)
    {
        for (i = 0; i < ewcNR; i++)
        {
            for (j = 0; j < ewcNR; j++)
            {
                snprintf(buf, 20, "%-9.9s %-9.9s", wcn[i], wcn[j]);
                print_cycles(fplog, c2t_pp, buf,
                             npp, nth_pp,
                             wc->wcc_all[i*ewcNR+j].n,
                             wc->wcc_all[i*ewcNR+j].c,
                             tot);
            }
        }
    }
    tot_for_rest = tot * (npp * nth_pp) / (double) nth_tot;
    print_cycles(fplog, c2t_pp, "Rest",
                 npp, nth_pp,
                 -1, tot_for_rest - tot_for_pp, tot);
    fprintf(fplog, "%s\n", hline);
    print_cycles(fplog, c2t, "Total",
                 npp, nth_pp,
                 -1, tot, tot);
    fprintf(fplog, "%s\n", hline);

    if (npme > 0)
    {
        fprintf(fplog,
                "(*) Note that with separate PME ranks, the walltime column actually sums to\n"
                "    twice the total reported, but the cycle count total and %% are correct.\n"
                "%s\n", hline);
    }

    if (wc->wcc[ewcPMEMESH].n > 0)
    {
        fprintf(fplog, " Breakdown of PME mesh computation\n");
        fprintf(fplog, "%s\n", hline);
        for (i = ewcPPDURINGPME+1; i < ewcNR; i++)
        {
            if (is_pme_subcounter(i))
            {
                print_cycles(fplog, npme > 0 ? c2t_pme : c2t_pp, wcn[i],
                             npme > 0 ? npme : npp, nth_pme,
                             wc->wcc[i].n, cyc_sum[i], tot);
            }
        }
        fprintf(fplog, "%s\n", hline);
    }

#ifdef GMX_CYCLE_SUBCOUNTERS
    fprintf(fplog, " Breakdown of PP computation\n");
    fprintf(fplog, "%s\n", hline);
    for (i = 0; i < ewcsNR; i++)
    {
        print_cycles(fplog, c2t_pp, wcsn[i],
                     npp, nth_pp,
                     wc->wcsc[i].n, cyc_sum[ewcNR+i], tot);
    }
    fprintf(fplog, "%s\n", hline);
#endif

    /* print GPU timing summary */
    if (gpu_t)
    {
        const char *k_log_str[2][2] = {
            {"Nonbonded F kernel", "Nonbonded F+ene k."},
            {"Nonbonded F+prune k.", "Nonbonded F+ene+prune k."}
        };

        tot_gpu = gpu_t->pl_h2d_t + gpu_t->nb_h2d_t + gpu_t->nb_d2h_t;

        /* add up the kernel timings */
        tot_k = 0.0;
        for (i = 0; i < 2; i++)
        {
            for (j = 0; j < 2; j++)
            {
                tot_k += gpu_t->ktime[i][j].t;
            }
        }
        tot_gpu += tot_k;

        tot_cpu_overlap = wc->wcc[ewcFORCE].c;
        if (wc->wcc[ewcPMEMESH].n > 0)
        {
            tot_cpu_overlap += wc->wcc[ewcPMEMESH].c;
        }
        tot_cpu_overlap *= realtime*1000/tot; /* convert s to ms */

        fprintf(fplog, "\n GPU timings\n%s\n", hline);
        fprintf(fplog, " Computing:                         Count  Wall t (s)      ms/step       %c\n", '%');
        fprintf(fplog, "%s\n", hline);
        print_gputimes(fplog, "Pair list H2D",
                       gpu_t->pl_h2d_c, gpu_t->pl_h2d_t, tot_gpu);
        print_gputimes(fplog, "X / q H2D",
                       gpu_t->nb_c, gpu_t->nb_h2d_t, tot_gpu);

        for (i = 0; i < 2; i++)
        {
            for (j = 0; j < 2; j++)
            {
                if (gpu_t->ktime[i][j].c)
                {
                    print_gputimes(fplog, k_log_str[i][j],
                                   gpu_t->ktime[i][j].c, gpu_t->ktime[i][j].t, tot_gpu);
                }
            }
        }

        print_gputimes(fplog, "F D2H",  gpu_t->nb_c, gpu_t->nb_d2h_t, tot_gpu);
        fprintf(fplog, "%s\n", hline);
        print_gputimes(fplog, "Total ", gpu_t->nb_c, tot_gpu, tot_gpu);
        fprintf(fplog, "%s\n", hline);

        gpu_cpu_ratio = tot_gpu/tot_cpu_overlap;
        fprintf(fplog, "\nForce evaluation time GPU/CPU: %.3f ms/%.3f ms = %.3f\n",
                tot_gpu/gpu_t->nb_c, tot_cpu_overlap/wc->wcc[ewcFORCE].n,
                gpu_cpu_ratio);

        /* only print notes related to CPU-GPU load balance with PME */
        if (wc->wcc[ewcPMEMESH].n > 0)
        {
            fprintf(fplog, "For optimal performance this ratio should be close to 1!\n");

            /* print note if the imbalance is high with PME case in which
             * CPU-GPU load balancing is possible */
            if (gpu_cpu_ratio < 0.75 || gpu_cpu_ratio > 1.2)
            {
                /* Only the sim master calls this function, so always print to stderr */
                if (gpu_cpu_ratio < 0.75)
                {
                    if (npp > 1)
                    {
                        /* The user could have used -notunepme,
                         * but we currently can't check that here.
                         */
                        md_print_warn(NULL, fplog,
                                      "\nNOTE: The GPU has >25%% less load than the CPU. This imbalance causes\n"
                                      "      performance loss. Maybe the domain decomposition limits the PME tuning.\n"
                                      "      In that case, try setting the DD grid manually (-dd) or lowering -dds.");
                    }
                    else
                    {
                        /* We should not end up here, unless the box is
                         * too small for increasing the cut-off for PME tuning.
                         */
                        md_print_warn(NULL, fplog,
                                      "\nNOTE: The GPU has >25%% less load than the CPU. This imbalance causes\n"
                                      "      performance loss.");
                    }
                }
                if (gpu_cpu_ratio > 1.2)
                {
                    md_print_warn(NULL, fplog,
                                  "\nNOTE: The GPU has >20%% more load than the CPU. This imbalance causes\n"
                                  "      performance loss, consider using a shorter cut-off and a finer PME grid.");
                }
            }
        }
    }

    if (wc->wcc[ewcNB_XF_BUF_OPS].n > 0 &&
        (cyc_sum[ewcDOMDEC] > tot*0.1 ||
         cyc_sum[ewcNS] > tot*0.1))
    {
        /* Only the sim master calls this function, so always print to stderr */
        if (wc->wcc[ewcDOMDEC].n == 0)
        {
            md_print_warn(NULL, fplog,
                          "NOTE: %d %% of the run time was spent in pair search,\n"
                          "      you might want to increase nstlist (this has no effect on accuracy)\n",
                          (int)(100*cyc_sum[ewcNS]/tot+0.5));
        }
        else
        {
            md_print_warn(NULL, fplog,
                          "NOTE: %d %% of the run time was spent in domain decomposition,\n"
                          "      %d %% of the run time was spent in pair search,\n"
                          "      you might want to increase nstlist (this has no effect on accuracy)\n",
                          (int)(100*cyc_sum[ewcDOMDEC]/tot+0.5),
                          (int)(100*cyc_sum[ewcNS]/tot+0.5));
        }
    }

    if (cyc_sum[ewcMoveE] > tot*0.05)
    {
        /* Only the sim master calls this function, so always print to stderr */
        md_print_warn(NULL, fplog,
                      "NOTE: %d %% of the run time was spent communicating energies,\n"
                      "      you might want to use the -gcom option of mdrun\n",
                      (int)(100*cyc_sum[ewcMoveE]/tot+0.5));
    }
}
Example #27
0
static void cross_check_gts(args_t *args)
{
    int nsamples = bcf_hdr_nsamples(args->sm_hdr), ndp_arr = 0;
    unsigned int *dp = (unsigned int*) calloc(nsamples,sizeof(unsigned int)), *ndp = (unsigned int*) calloc(nsamples,sizeof(unsigned int)); // this will overflow one day...
    int fake_pls = args->no_PLs, ignore_dp = 0;

    int i,j,k,idx, pl_warned = 0, dp_warned = 0;
    int32_t *dp_arr = NULL;
    int *is_hom = args->hom_only ? (int*) malloc(sizeof(int)*nsamples) : NULL;
    if ( bcf_hdr_id2int(args->sm_hdr, BCF_DT_ID, "PL")<0 )
    {
        if ( bcf_hdr_id2int(args->sm_hdr, BCF_DT_ID, "GT")<0 )
            error("[E::%s] Neither PL nor GT present in the header of %s\n", __func__, args->files->readers[0].fname);
        if ( !args->no_PLs )
            fprintf(pysamerr,"Warning: PL not present in the header of %s, using GT instead\n", args->files->readers[0].fname);
        fake_pls = 1;
    }
    if ( bcf_hdr_id2int(args->sm_hdr, BCF_DT_ID, "DP")<0 ) ignore_dp = 1;

    FILE *fp = args->plot ? open_file(NULL, "w", "%s.tab", args->plot) : stdout;
    print_header(args, fp);
    if ( args->all_sites ) fprintf(fp,"# [1]SD, Average Site Discordance\t[2]Chromosome\t[3]Position\t[4]Number of available pairs\t[5]Average discordance\n");

    while ( bcf_sr_next_line(args->files) )
    {
        bcf1_t *line = args->files->readers[0].buffer[0];
        bcf_unpack(line, BCF_UN_FMT);

        int npl;
        if ( !fake_pls )
        {
            npl = bcf_get_format_int32(args->sm_hdr, line, "PL", &args->pl_arr, &args->npl_arr);
            if ( npl<=0 ) { pl_warned++; continue; }
            npl /= nsamples;
        }
        else
            npl = fake_PLs(args, args->sm_hdr, line);
        int mdp = 0;
        if ( !ignore_dp && (mdp=bcf_get_format_int32(args->sm_hdr, line, "DP", &dp_arr, &ndp_arr)) <= 0 ) dp_warned++;

        if ( args->hom_only )
        {
            for (i=0; i<nsamples; i++)
                is_hom[i] = is_hom_most_likely(line->n_allele, args->pl_arr+i*npl);
        }

        double sum = 0; int nsum = 0;
        idx = 0;
        for (i=0; i<nsamples; i++)
        {
            int *ipl = &args->pl_arr[i*npl];
            if ( *ipl==-1 ) { idx += i; continue; } // missing genotype
            if ( mdp>0 && (dp_arr[i]==bcf_int32_missing || !dp_arr[i]) ) { idx += i; continue; }
            if ( args->hom_only && !is_hom[i] ) { idx += i; continue; }

            for (j=0; j<i; j++)
            {
                int *jpl = &args->pl_arr[j*npl];
                if ( *jpl==-1 ) { idx++; continue; } // missing genotype
                if ( mdp>0 && (dp_arr[j]==bcf_int32_missing || !dp_arr[j]) ) { idx++; continue; }
                if ( args->hom_only && !is_hom[j] ) { idx++; continue; }

                int min_pl = INT_MAX;
                for (k=0; k<npl; k++)
                {
                    if ( ipl[k]==bcf_int32_missing || jpl[k]==bcf_int32_missing ) break;
                    if ( ipl[k]==bcf_int32_vector_end || jpl[k]==bcf_int32_vector_end ) { k = npl; break; }
                    if ( min_pl > ipl[k]+jpl[k] ) min_pl = ipl[k]+jpl[k];
                }
                if ( k!=npl ) { idx++; continue; }

                if ( args->all_sites ) { sum += min_pl; nsum++; }
                args->lks[idx] += min_pl;
                args->cnts[idx]++;

                if ( mdp>0 )
                {
                    args->dps[idx] += dp_arr[i] < dp_arr[j] ? dp_arr[i] : dp_arr[j];
                    dp[i] += dp_arr[i]; ndp[i]++;
                    dp[j] += dp_arr[j]; ndp[j]++;
                }
                else
                {
                    args->dps[idx]++;
                    dp[i]++; ndp[i]++;
                    dp[j]++; ndp[j]++;
                }
                idx++;
            }
        }
        if ( args->all_sites )
            fprintf(fp,"SD\t%s\t%d\t%d\t%.0f\n", args->sm_hdr->id[BCF_DT_CTG][line->rid].key, line->pos+1, nsum, nsum?sum/nsum:0);
    }
    if ( dp_arr ) free(dp_arr);
    if ( args->pl_arr ) free(args->pl_arr);
    if ( args->tmp_arr ) free(args->tmp_arr);
    if ( is_hom ) free(is_hom);

    if ( pl_warned ) fprintf(pysamerr, "[W::%s] PL was not found at %d site(s)\n", __func__, pl_warned);
    if ( dp_warned ) fprintf(pysamerr, "[W::%s] DP was not found at %d site(s)\n", __func__, dp_warned);

    // Output samples sorted by average discordance
    double *score  = (double*) calloc(nsamples,sizeof(double));
    args->sites = (double*) calloc(nsamples,sizeof(double));
    idx = 0;
    for (i=0; i<nsamples; i++)
    {
        for (j=0; j<i; j++)
        {
            score[i] += args->lks[idx];
            score[j] += args->lks[idx];
            args->sites[i] += args->cnts[idx];
            args->sites[j] += args->cnts[idx];
            idx++;
        }
    }
    for (i=0; i<nsamples; i++)
        if ( args->sites[i] ) score[i] /= args->sites[i];
    double **p = (double**) malloc(sizeof(double*)*nsamples), avg_score = 0;
    for (i=0; i<nsamples; i++) p[i] = &score[i];
    qsort(p, nsamples, sizeof(int*), cmp_doubleptr);
    // The average discordance gives the number of differing sites in % with -G1
    fprintf(fp, "# [1]SM\t[2]Average Discordance\t[3]Average depth\t[4]Average number of sites\t[5]Sample\t[6]Sample ID\n");
    for (i=0; i<nsamples; i++)
    {
        idx = p[i] - score;
        double adp = ndp[idx] ? (double)dp[idx]/ndp[idx] : 0;
        double nsites = args->sites[idx]/(nsamples-1);
        avg_score += score[idx];
        fprintf(fp, "SM\t%f\t%.2lf\t%.0lf\t%s\t%d\n", score[idx]*100., adp, nsites, args->sm_hdr->samples[idx],i);
    }

    //  // Overall score: maximum absolute deviation from the average score
    //  fprintf(fp, "# [1] MD\t[2]Maximum deviation\t[3]The culprit\n");
    //  fprintf(fp, "MD\t%f\t%s\n", (score[idx] - avg_score/nsamples)*100., args->sm_hdr->samples[idx]);    // idx still set
    free(p);
    free(score);
    free(dp);
    free(ndp);

    // Pairwise discordances
    fprintf(fp, "# [1]CN\t[2]Discordance\t[3]Number of sites\t[4]Average minimum depth\t[5]Sample i\t[6]Sample j\n");
    idx = 0;
    for (i=0; i<nsamples; i++)
    {
        for (j=0; j<i; j++)
        {
            fprintf(fp, "CN\t%.0f\t%d\t%.2f\t%s\t%s\n", args->lks[idx], args->cnts[idx], args->cnts[idx]?(double)args->dps[idx]/args->cnts[idx]:0.0,
                    args->sm_hdr->samples[i],args->sm_hdr->samples[j]);
            idx++;
        }
    }
    fclose(fp);
    if ( args->plot )
        plot_cross_check(args);
}
/*
 exe_commande
    Descr: Execute les différentes opérations demandés par l'utilisateur
    Param:
        - c: Commande (voir #define ci dessus)
        - nomfichier: Nom du fichier à lire
        - nm_index: Argument de l'option -x, NULL si l'option n'a pas été choisis
    Return:
        Etat de sortie de la fonction:
        - -1: Impossible d'ouvrir le fichier donné
        - 0: Ouverture du fichier réussi
 */
int exe_commande(unsigned char c, char *nomfichier,char* nm_index)
{
	if (nomfichier==NULL)
		return NOMFICHIERNULL;
    FILE *f;
    
    Elf32_Ehdr header;
    Elf32_Shdr *section_header_table;
    Elf32_Sym *symbol_tab;
    sectioncontent section;
    
    int nbSym = 0;
    int valid = 0;
    
    f = fopen(nomfichier,"r"); // Ouverture du fichier nomfichier

    if(f == NULL) { // Echec d'ouverture
        return FICHIERINEXISTANT;
    }
    else {
        /* Lecture Header */
        header = read_header(f,&valid);
        
        if(valid==1) { // Header valide
            
            /* Lecture Table de section header */
            section_header_table = read_section_header_table(f,header);
            
            /* Lecture Table des symboles */
            symbol_tab =
                read_symbol_table(f ,section_header_table, header, &nbSym);

            if(c&COMMANDE_HEAD) //COMMANDE_HEAD
            {
                printf("ELF Header:\n");
                print_header(header);
                printf("\n");
                 
            }
            if(c&COMMANDE_SECTIONTAB)//COMMANDE_SECTIONTAB
            {
                printf("Section Headers:\n");
                print_section_header_table(f,section_header_table,header);
                printf("\n");
            }
            
            if(c&COMMANDE_CONTENT)//COMMANDE_CONTENT
            {
                printf("Section %s content:\n", nm_index);
                
                section = read_section_content(f,nm_index, section_header_table, header);
                if(section.content==NULL) {
                    printf("Warning, section : %s,was not dumped because it does not exist \n",nm_index);
                }
                else {
                    print_section_content(section);
                }
            }
            if(c&COMMANDE_RELOC)//COMMANDE_RELOC
            {
                print_all_reloc_table(f, header, section_header_table, symbol_tab);
                printf("\n");
            }
            
           
            if(c&COMMANDE_SYM)//COMMANDE_SYM
            {
                printf("Symbol table\n");
                print_symbol_table(symbol_tab, nbSym, f, section_header_table, header);
                freeSymbolTable(symbol_tab);
            }
            }
        else // Header non valide
        {
            if(valid ==-1) { // Fichier non valid (vide ou autre)
                return FILEEMPTY;fprintf(stderr, "%s : Error while reading the file (possibly empty)\n",nomfichier);
            }
            else if(valid==0) { //Fichier pas au format elf32
                return NOTELF32;
            }
            else { // Cas normalement impossible
                fprintf(stderr, "Impossible error append\n");
            }
        }
            
        fclose(f); // Fermeture du fichier
    }
    return 0;
}
Example #29
0
int request_custom_command(buffer* recv_buf, server_stat* status) {
    unsigned char http_message[1000];      // used to hold http message from robot
    buffer* http_data = create_buffer(BUFFER_LEN);
    int n, retries;
    cst_header request_header;   // header from the client
    buffer* response;        // buffer to send to client
    struct timeval timeout;

    timeout.tv_sec = 0;
    timeout.tv_usec = 500000;

    fprintf(stdout, "\tProcessing request\n");

    // acknowledgement of command to client
    udp_send(recv_buf, status);

    // create the http request
    memset(http_message, '\0', 1000);
    request_header = extract_custom_header(recv_buf);
    switch (request_header.data[CST_COMMAND]) {
        case IMAGE:
            fprintf(stdout, "\t\tContacting image port\n");
            snprintf((char*)http_message, 100, "GET /snapshot?topic=/robot_%d/image?width=600?height=500 HTTP/1.1\r\n\r\n", status->r_stat.id);
            status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, IMAGE_PORT);
            break;
        case GPS:
            fprintf(stdout, "\t\tContacting gps port\n");
            snprintf((char*)http_message, 100, "GET /state?id=%s HTTP/1.1\r\n\r\n", status->r_stat.name);
            status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, GPS_PORT);
            break;
        case LASERS:
            fprintf(stdout, "\t\tContacting lasers port\n");
            snprintf((char*)http_message, 100, "GET /state?id=%s HTTP/1.1\r\n\r\n", status->r_stat.name);
            status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, LASERS_PORT);
            break;
        case dGPS:
            fprintf(stdout, "\t\tContacting dgps port\n");
            snprintf((char*)http_message, 100, "GET /state?id=%s HTTP/1.1\r\n\r\n", status->r_stat.name);
            status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, dGPS_PORT);
            break;
        case MOVE:
            fprintf(stdout, "\t\tContacting move port\n");
            snprintf((char*)http_message, 100, "GET /twist?id=%s&lx=1 HTTP/1.1\r\n\r\n", status->r_stat.name);
            status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, GPS_PORT);
            break;
        case TURN:
            fprintf(stdout, "\t\tContacting turn port\n");
            snprintf((char*)http_message, 100, "GET /twist?id=%s&az=1 HTTP/1.1\r\n\r\n", status->r_stat.name);
            status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, GPS_PORT);
            break;
        case STOP:
            fprintf(stdout, "\t\tContacting stop port\n");
            snprintf((char*)http_message, 100, "GET /twist?id=%s&lx=0 HTTP/1.1\r\n\r\n", status->r_stat.name);
            status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, GPS_PORT);
            break;
        default:
            fprintf(stderr, "ERROR: Invalid client request\n");
            return -2;
            break;
    }

    // send the http request
    fprintf(stdout, "\t\tWriting request to server\n");
    timeout_setup(status->r_stat.http_sock, timeout);
    write(status->r_stat.http_sock, (char*)http_message, strlen((char*)http_message));

    // read http message into a buffer
    fprintf(stdout, "\t\tReceiving reply from server\n");
    memset(http_message, '\0', 1000);
    retries = 0;
    while (1) {
        n = read(status->r_stat.http_sock, (char*)http_message, 1000);
        if (n == -1) {
            if (retries > 2) {
                break;
            }
            retries++;
            write(status->r_stat.http_sock, (char*)http_message, strlen((char*)http_message));
            continue;
        } else if (n == 0) {
            break;
        }
        fprintf(stdout, "\t\t\tReceived %d bytes\n", n);
        append_buffer(http_data, (unsigned char*)http_message, n);
        memset(http_message, '\0', 1000);
    }
    fprintf(stdout, "\t\tTotal bytes received: %d\n", http_data->len);


    // see what we go
    fprintf(stdout, "\t\tAssembled Message:\n%s\n", http_data->data);

    // check for '200 OK'
    char ret_code[4];
    memcpy(ret_code, http_data->data + 9, 3); // HTTP/1.1 <ret_code> ~~~~
    ret_code[3] = '\0';
    if (atoi(ret_code) != 200) {
        fprintf(stderr, "ERROR: bad http request\n");
        response = create_custom_message(GROUP_NUMBER, status->password, HTTP_ERROR, 0, 0, 0);
        udp_send(response, status);
        return 0;
    }

    // send it to client
    int http_header_len = http_get_data(http_data->data) - http_data->data;
    int a = 0;
    while ((a + http_header_len) < http_data->len) {
        response = create_custom_message(request_header.data[CST_VERSION],
                request_header.data[CST_PASSWORD],
                request_header.data[DATA],
                request_header.data[CST_SEQUENCE],
                (http_data->len - http_header_len),
                ((http_data->len - (a + http_header_len)) > CST_MAX_PAYLOAD ? CST_MAX_PAYLOAD:(http_data->len - (a + http_header_len))));

        append_buffer(response, http_data->data + a + http_header_len, CST_MAX_PAYLOAD);
        fprintf(stdout, "\n\t\tAssembled packet:\n");
        print_header(response);
        fprintf(stdout, "%s\n", response->data + UP_HEADER_LEN);
        fprintf(stdout, "\t\tSending packet\n");
        udp_send(response, status);
        fprintf(stdout, "\t\tPacket sent\n");
        delete_buffer(response);
        a += 370;
    }
    fprintf(stdout, "\t\tRequest complete!\n");
    return 1;
}
Example #30
0
/*
 * Print the symbol table according to the flags that were
 * set, if any.  Input is an opened ELF file, the section name,
 * the section header, the section descriptor, and the filename.
 * First get the symbol table with a call to elf_getdata.
 * Then translate the symbol table data in memory by calling
 * readsyms().  This avoids duplication of function calls
 * and improves sorting efficiency.  qsort is used when sorting
 * is requested.
 */
static void
print_symtab(Elf *elf_file, unsigned int shstrndx,
	Elf_Scn *p_sd, GElf_Shdr *shdr, char *filename)
{

	Elf_Data * sd;
	SYM	*sym_data;
	SYM	*s;
	GElf_Sxword	count = 0;
	const int ndigits_arr[] = {
		10,		/* FMT_T_DEC */
		8,		/* FMT_T_HEX */
		11,		/* FMT_T_OCT */
	};
	int ndigits;

	/*
	 * Determine # of digits to use for each numeric value.
	 */
	ndigits = ndigits_arr[fmt_flag];
	if (gelf_getclass(elf_file) == ELFCLASS64)
		ndigits *= 2;

	/*
	 * print header
	 */
	print_header(ndigits);

	/*
	 * get symbol table data
	 */
	if (((sd = elf_getdata(p_sd, NULL)) == NULL) || (sd->d_size == 0)) {
		(void) fprintf(stderr,
		    gettext("%s: %s: no symbol table data\n"),
		    prog_name, filename);
		return;
	}
	count = shdr->sh_size / shdr->sh_entsize;

	/*
	 * translate symbol table data
	 */
	sym_data = readsyms(sd, count, elf_file, shdr->sh_link,
	    (unsigned int)elf_ndxscn(p_sd));
	if (sym_data == NULL) {
		(void) fprintf(stderr, gettext(
		    "%s: %s: problem reading symbol data\n"),
		    prog_name, filename);
		return;
	}
	qsort((char *)sym_data, count-1, sizeof (SYM),
	    (int (*)(const void *, const void *))compare);
	s = sym_data;
	while (count > 1) {
#ifndef XPG4
		if (u_flag) {
			/*
			 * U_flag specified
			 */
			print_with_uflag(sym_data, filename);
		} else if (p_flag)
#else
		if (p_flag)
#endif
			print_with_pflag(ndigits, elf_file, shstrndx,
			    sym_data, filename);
		else if (P_flag)
			print_with_Pflag(ndigits, elf_file, shstrndx,
			    sym_data);
		else
			print_with_otherflags(ndigits, elf_file,
			    shstrndx, sym_data, filename);
		sym_data++;
		count--;
	}

	free(s);		/* allocated in readsym() */
}