Ejemplo n.º 1
0
// Prints a horizontal bar chart element in a grid box
void grid_bar(struct kingrid_info *data, int c, int percent)
{
	int charcount;

	if(percent > 100) {
		percent = 100;
	} else if(percent < 0) {
		percent = 0;
	}

	charcount = percent * data->boxwidth / 100;

	printf("| ");
	repeat_char(c, charcount);
	repeat_char(' ', data->boxwidth - charcount);
	putchar(' ');
}
Ejemplo n.º 2
0
// Prints horizontal border between grid rows
void grid_hline(struct kingrid_info *data)
{
	int i;
	for(i = 0; i < data->divisions; i++) {
		putchar('+');
		repeat_char('-', data->boxwidth + 2);
	}
	puts("+");
}
Ejemplo n.º 3
0
Archivo: cat.c Proyecto: esheldon/misc
struct cat* _cat_read(FILE* fptr, 
                      size_t nlines,
                      int64 nside,
                      double radius_arcsec,
                      int verbose)
{
    int radius_in_file=0;
    double ra=0, dec=0;
    double radius_radians=0;
    double cos_radius_global=0;
    struct vector* listpix = vector_new(0,sizeof(int64));

    size_t index=0;
    int barsize=70;

    struct cat* self = cat_new(nlines, nside);

    if (verbose) {
        wlog("    reading and building hash table\n");
        repeat_char('.', barsize); wlog("\n");
    }

    radius_in_file = _cat_get_radius(radius_arcsec,&radius_radians,
                                     &cos_radius_global);

    struct point_hash* pthash = NULL;
    struct point* pt = &self->pts[0];
    for (size_t i=0; i<self->size; i++) {
        pt->index=index;
        if (2 != fscanf(fptr, "%lf %lf", &ra, &dec)) {
            wlog("expected to read point at line %lu\n", i);
            exit(EXIT_FAILURE);
        }
        if (radius_in_file) {
            if (1 != fscanf(fptr, "%lf", &radius_arcsec)) {
                wlog("expected to read radius at line %lu\n", i);
                exit(EXIT_FAILURE);
            }
            radius_radians = radius_arcsec/3600.*D2R;
            pt->cos_radius = cos(radius_radians);
        } else {
            pt->cos_radius = cos_radius_global;
        }

        hpix_eq2xyz(ra,dec,&pt->x,&pt->y,&pt->z);
        hpix_disc_intersect(self->hpix, pt->x, pt->y, pt->z, radius_radians, 
                            listpix);

        // insert a pointer to this point for each pixel it intersected
        int64* pix_ptr = vector_front(listpix);
        int64* end     = vector_end(listpix);
        while (pix_ptr != end) {
            pthash=point_hash_insert(pthash, (*pix_ptr), pt);
            pix_ptr++;
        }

        pt++;
        index++;
        if (verbose) incr_bar(i+1, self->size, barsize, '=');
    }

    self->pthash=pthash;

    listpix=vector_delete(listpix);

    if (verbose) wlog("\n");

    if (index != self->size) {
        wlog("expected %lu lines but read %lu\n", self->size, index);
        exit(EXIT_FAILURE);
    }

    return self;
}
Ejemplo n.º 4
0
char *
format_editorial(char *s, int width, int flags, HANDLE_S **handlesp, gf_io_t pc)
{
    gf_io_t	 gc;
    int		*margin;
    EDITORIAL_S  es;
    URL_HILITE_S uh;

    /* ASSUMPTION #2,341: All MIME-decoding is done by now */
    gf_set_readc(&gc, s, strlen(s), CharStar, 0);

    margin = format_view_margin();
    if(flags & FM_NOINDENT)
      margin[0] = margin[1] = 0;

    /* safety net */
    if(width - (margin[0] + margin[1]) < 5){
	margin[0] = margin[1] = 0;
	if(width < 5)
	  width = 80;
    }

    width -= (margin[0] + margin[1]);

    if(width > 40){
	width -= 12;

	es.prelen = MAX(2, MIN(margin[0] + 6, sizeof(es.prefix) - 3));
	snprintf(es.prefix, sizeof(es.prefix), "%s[ ", repeat_char(es.prelen - 2, ' '));
	es.postlen = 2;
	strncpy(es.postfix, " ]", sizeof(es.postfix));
	es.postfix[sizeof(es.postfix)-1] = '\0';
    }
    else if(width > 20){
	width -= 6;

	es.prelen = MAX(2, MIN(margin[0] + 3, sizeof(es.prefix) - 3));
	snprintf(es.prefix, sizeof(es.prefix), "%s[ ", repeat_char(es.prelen - 2, ' '));
	es.postlen = 2;
	strncpy(es.postfix, " ]", sizeof(es.postfix));
	es.postfix[sizeof(es.postfix)-1] = '\0';
    }
    else{
	width -= 2;
	strncpy(es.prefix, "[", sizeof(es.prefix));
	es.prefix[sizeof(es.prefix)-1] = '\0';
	strncpy(es.postfix, "]", sizeof(es.postfix));
	es.postfix[sizeof(es.postfix)-1] = '\0';
	es.prelen = 1;
	es.postlen = 1;
    }

    es.do_color = (!(flags & FM_NOCOLOR) && (flags & FM_DISPLAY) && pico_usingcolor());

    gf_filter_init();

    /* catch urls */
    if((F_ON(F_VIEW_SEL_URL, ps_global)
	|| F_ON(F_VIEW_SEL_URL_HOST, ps_global)
	|| F_ON(F_SCAN_ADDR, ps_global))
       && handlesp){
	gf_link_filter(gf_line_test,
		       gf_line_test_opt(url_hilite,
				        gf_url_hilite_opt(&uh,handlesp,0)));
    }
      
    gf_link_filter(gf_wrap, gf_wrap_filter_opt(width, width, NULL, 0,
					       (handlesp ? GFW_HANDLES : GFW_NONE)));
    gf_link_filter(gf_line_test, gf_line_test_opt(quote_editorial, &es));

    /* If not for display, change to local end of line */
    if(!(flags & FM_DISPLAY))
	gf_link_filter(gf_nvtnl_local, NULL);

    return(gf_pipe(gc, pc));
}
Ejemplo n.º 5
0
Archivo: text.c Proyecto: mavit/alpine
/*
 * This one is a little more complicated because it comes late in the
 * filtering sequence after colors have been added and url's hilited and
 * so on. So if it wants to look at the beginning of the line it has to
 * wade through some stuff done by the other filters first. This one is
 * skipping the leading indent added by the viewer-margin stuff and leading
 * tags.
 */
int
delete_quotes(long int linenum, char *line, LT_INS_S **ins, void *local)
{
    DELQ_S *dq;
    char   *lp;
    int     i, lines, not_a_quote = 0, code;
    size_t  len;

    dq = (DELQ_S *) local;
    if(!dq)
      return(0);

    if(dq->lines > 0 || dq->lines == Q_DEL_ALL){

	lines = (dq->lines == Q_DEL_ALL) ? 0 : dq->lines;

	/*
	 * First determine if this line is part of a quote.
	 */

	/* skip over indent added by viewer-margin-left */
	lp = line;
	for(i = dq->indent_length; i > 0 && !not_a_quote && *lp; i--)
	  if(*lp++ != SPACE)
	    not_a_quote++;
	while(isspace((unsigned char) *lp))
	   lp++;
	
	/* skip over leading tags */
	while(!not_a_quote
	      && ((unsigned char) (*lp) == (unsigned char) TAG_EMBED)){
	    switch(*++lp){
	      case '\0':
		not_a_quote++;
	        break;

	      default:
		++lp;
		break;

	      case TAG_FGCOLOR:
	      case TAG_BGCOLOR:
	      case TAG_HANDLE:
		switch(*lp){
		  case TAG_FGCOLOR:
		  case TAG_BGCOLOR:
		    len = RGBLEN + 1;
		    break;

		  case TAG_HANDLE:
		    len = *++lp + 1;
		    break;
		}

		if(strlen(lp) < len)
		  not_a_quote++;
		else
		  lp += len;

		break;

	      case TAG_EMBED:
		break;
	    }
	}

	len = lp - line;
	if(strlen(tmp_20k_buf) > len)
	  strcpy(tmp_20k_buf, tmp_20k_buf+len);
        code =  (dq->is_flowed ? IS_FLOWED : NO_FLOWED) | DELETEQUO;
	select_quote(linenum, lp, ins,  &code);
	if (!not_a_quote && !tmp_20k_buf[0])
	  not_a_quote++;

	if(not_a_quote){
	    if(dq->in_quote > lines+1 && !dq->delete_all){
	      char tmp[500];
	      COLOR_PAIR *col = NULL;
	      char cestart[2 * RGBLEN + 5];
	      char ceend[2 * RGBLEN + 5];

	      /*
	       * Display how many lines were hidden.
	       */

	      cestart[0] = ceend[0] = '\0';
	      if(dq->do_color
		 && ps_global->VAR_METAMSG_FORE_COLOR
		 && ps_global->VAR_METAMSG_BACK_COLOR
		 && (col = new_color_pair(ps_global->VAR_METAMSG_FORE_COLOR,
					  ps_global->VAR_METAMSG_BACK_COLOR)))
	        if(!pico_is_good_colorpair(col))
		  free_color_pair(&col);

	      if(col){
		  strncpy(cestart, color_embed(col->fg, col->bg), sizeof(cestart));
		  cestart[sizeof(cestart)-1] = '\0';
		  strncpy(ceend, color_embed(ps_global->VAR_NORM_FORE_COLOR,
					     ps_global->VAR_NORM_BACK_COLOR), sizeof(ceend));
		  ceend[sizeof(ceend)-1] = '\0';
		  free_color_pair(&col);
	      }

	      snprintf(tmp, sizeof(tmp),
		      "%s[ %s%d lines of quoted text hidden from view%s ]\r\n",
		      repeat_char(dq->indent_length, SPACE),
		      cestart, dq->in_quote - lines, ceend);
	      if(strlen(tmp)-strlen(cestart)-strlen(ceend)-2 > ps_global->ttyo->screen_cols){

		snprintf(tmp, sizeof(tmp), "%s[ %s%d lines of quoted text hidden%s ]\r\n",
			repeat_char(dq->indent_length, SPACE),
		        cestart, dq->in_quote - lines, ceend);

		if(strlen(tmp)-strlen(cestart)-strlen(ceend)-2 > ps_global->ttyo->screen_cols){
		  snprintf(tmp, sizeof(tmp), "%s[ %s%d lines hidden%s ]\r\n",
			  repeat_char(dq->indent_length, SPACE),
		          cestart, dq->in_quote - lines, ceend);

		  if(strlen(tmp)-strlen(cestart)-strlen(ceend)-2 > ps_global->ttyo->screen_cols){
		    snprintf(tmp, sizeof(tmp), "%s[ %s%d hidden%s ]\r\n",
			    repeat_char(dq->indent_length, SPACE),
		            cestart, dq->in_quote - lines, ceend);
		  
		    if(strlen(tmp)-strlen(cestart)-strlen(ceend)-2 > ps_global->ttyo->screen_cols){
		      snprintf(tmp, sizeof(tmp), "%s[...]\r\n",
			      repeat_char(dq->indent_length, SPACE));

		      if(strlen(tmp)-2 > ps_global->ttyo->screen_cols){
		        snprintf(tmp, sizeof(tmp), "%s...\r\n",
			        repeat_char(dq->indent_length, SPACE));

			if(strlen(tmp)-2 > ps_global->ttyo->screen_cols){
		          snprintf(tmp, sizeof(tmp), "%s\r\n",
			      repeat_char(MIN(ps_global->ttyo->screen_cols,3),
					  '.'));
			}
		      }
		    }
		  }
		}
	      }

	      ins = gf_line_test_new_ins(ins, line, tmp, strlen(tmp));
	      mark_handles_in_line(line, dq->handlesp, 1);
	      ps_global->some_quoting_was_suppressed = 1;
	    }
	    else if(dq->in_quote == lines+1
		    && dq->saved_line && *dq->saved_line){
		/*
		 * We would have only had to delete a single line. Instead
		 * of deleting it, just show it.
		 */
		ins = gf_line_test_new_ins(ins, line,
					   *dq->saved_line,
					   strlen(*dq->saved_line));
		mark_handles_in_line(*dq->saved_line, dq->handlesp, 1);
	    }
	    else
	      mark_handles_in_line(line, dq->handlesp, 1);

	    if(dq->saved_line && *dq->saved_line)
	      fs_give((void **) dq->saved_line);

	    dq->in_quote = 0;
	}
	else{
	    dq->in_quote++;				/* count it */
	    if(dq->in_quote > lines){
		/*
		 * If the hidden part is a single line we'll show it instead.
		 */
		if(dq->in_quote == lines+1 && !dq->delete_all){
		    if(dq->saved_line && *dq->saved_line)
		      fs_give((void **) dq->saved_line);
		    
		    *dq->saved_line = fs_get(strlen(line) + 3);
		    snprintf(*dq->saved_line, strlen(line)+3, "%s\r\n", line);
		}

		mark_handles_in_line(line, dq->handlesp, 0);
		/* skip it, at least for now */
		return(2);
	    }

	    mark_handles_in_line(line, dq->handlesp, 1);
	}
    }

    return(0);
}
Ejemplo n.º 6
0
/*
 * Prints a separation line
 */
void print_sep() {
	PRINT_COLUMNS("+", "-", repeat_char("-", 36), \
		repeat_char("-", 10), repeat_char("-", 10),repeat_char("-", 14), \
		repeat_char("-", 50));
}
Ejemplo n.º 7
0
/*
 * do_busy_cue - paint the busy cue and return how long caller
 *               should pause before calling us again
 */
int
do_busy_cue(void *data)
{
    int space_left, slots_used, period;
    char dbuf[MAX_SCREEN_COLS+1];

    /* Don't wipe out any displayed status message prematurely */
    if(status_message_remaining() || busy_cue_pause)
      return(BUSY_MSG_RETRY);

    space_left = (ps_global->ttyo ? ps_global->ttyo->screen_cols : 80) -
	busy_width - 2;  /* 2 is for [] */
    slots_used = MAX(0, MIN(space_left-3, 10));

    if(percent_done_ptr && slots_used >= 4){
	int completed, pd;
	char *s;

	pd = (*percent_done_ptr)();
	pd = MIN(MAX(0, pd), 100);

	completed = (pd * slots_used) / 100;
	snprintf(dbuf, sizeof(dbuf), "%s |%s%s%*s|", busy_message,
	    completed > 1 ? repeat_char(completed-1, pd==100 ? ' ' : '-') : "",
	    (completed > 0 && pd != 100) ? ">" : "",
	    slots_used - completed, "");
	dbuf[sizeof(dbuf)-1] = '\0';

	if(slots_used == 10){
	    s = dbuf + strlen(dbuf) - 8;
	    if(pd < 10){
		s++; s++;
		*s++ = '0' + pd;
	    }
	    else if(pd < 100){
		s++;
		*s++ = '0' + pd / 10;
		*s++ = '0' + pd % 10;
	    }
	    else{
		*s++ = '1';
		*s++ = '0';
		*s++ = '0';
	    }

	    *s   = '%';
	}

	period = BUSY_PERIOD_PERCENT;
    }
    else{
	char b[MAX_SPINNER_WIDTH + 2];
	int ind;

	ind = (dotcount % spinners[spinner].elements);

	spinners[spinner].used_this_round = 1;
	if(space_left >= spinners[spinner].width + 1){
	    b[0] = SPACE;
	    strncpy(b+1,
		    (ps_global->active_status_interval > 0)
		      ? spinners[spinner].bars[ind] : "... ", sizeof(b)-1);
	    b[sizeof(b)-1] = '\0';
	}
	else if(space_left >= 2 && space_left < sizeof(b)){
	    b[0] = '.';
	    b[1] = '.';
	    b[2] = '.';
	    b[space_left] = '\0';
	}
	else
	  b[0] = '\0';

	snprintf(dbuf, sizeof(dbuf), "%s%s", busy_message, b);
	dbuf[sizeof(dbuf)-1] = '\0';

	/* convert interval to delay in 100ths of second */
	period = (ps_global->active_status_interval > 0)
		  ? (100 / MIN(10, ps_global->active_status_interval)) : BUSY_MSG_DONE;
    }

    status_message_write(dbuf, 1);
    dotcount++;
    fflush(stdout);

    return(period);
}
Ejemplo n.º 8
0
/*  Command line arguments are three char[]s of digits:
 *    integer   (binary digits)
 *    exponent  (decimal value)
 *    fraction  (binary digits)
 *  Outputs are normalized decimal and binary representations
 *  of the input value, one part per line:
 *    decimal integer
 *    decimal fraction
 *    decimal exponent
 *    decimal recurrence digits
 *    decimal recurrence start position
 *    binary recurrence digits
 *    binary recurrence start
 *    binary integer
 *    binary fraction
 *    binary exponent
*/
int main(int argc, char* argv[]) {
    if (argc != 4) {
        puts("Need arguments!");
        exit(1);
    }

    //const int BINARY_PRECISION = 128;
    int       decimal_exponent;
    int       binary_exponent_v;
    char*    binary_integer_v = malloc(BUF);
    char*    binary_fraction_v = malloc(BUF);
    char*    decimal_fraction = malloc(BUF);
    char*    decimal_integer = malloc(BUF);
    int       decimal_recurrence_start = -1;
    int       binary_recurrence_start = -1;
    char*    decimal_recurrence = malloc(BUF);
    strcpy(decimal_recurrence, "0");
    char*    binary_recurrence = malloc(BUF);
    strcpy(binary_recurrence, "0");
    char*    temp = malloc(BUF);
    //int       precision_so_far = 0;

    strcpy(binary_integer_v, argv[1]);
    binary_exponent_v = atoi(argv[2]);
    strcpy(binary_fraction_v, argv[3]);
//printf("bfv %s\n", binary_fraction_v);

    mpz_t result;
    mpz_init(result);

    //char* binfractemp = binary_fraction_v;

    // The n versions of the variable are the ones that are normalized and used
    // for the binary ouput but are not used for generating the decimal values
    char* binary_integer_n = malloc(BUF);
    strcpy(binary_integer_n, binary_integer_v);
    char* binary_fraction_n = malloc(BUF);
    strcpy(binary_fraction_n, binary_fraction_v);
    int binary_exponent_n = binary_exponent_v;

    while(atoi(binary_integer_n) > 1) {
        temp[0] = binary_integer_n[strlen(binary_integer_n)-1];
        temp[1] = '\0';
//      strcpy(temp,&(binary_integer_n[strlen(binary_integer_n)-1]));
        strcpy(binary_fraction_n, strcat(temp, binary_fraction_n));
        //strcpy(binary_integer_n, substr(binary_integer_n,0, strlen(binary_integer_n) - 1));
        binary_integer_n[strlen(binary_integer_n)-1] = '\0';
        binary_exponent_n++;
    }

    while (strcmp(binary_integer_n,"0") == 0) {
        binary_integer_n[0] = binary_fraction_n[0];
        binary_integer_n[1] = '\0';
//	strcpy(binary_integer_n, binary_fraction_n);
//      strcpy(binary_fraction_n,  substr(binary_fraction_n,1,strlen(binary_fraction_n)-1));
        substr1(binary_fraction_n);
        binary_exponent_n--;
    }

    if (binary_fraction_n[0] == '\0') {
        strcpy(binary_fraction_n, "0");
    }
    //printf("bi %s\n",binary_integer_v);

    //these are copies of the originals
    char* binary_integer = malloc(BUF);
    strcpy(binary_integer, binary_integer_v);
    char* binary_fraction = malloc(BUF);
    strcpy(binary_fraction, binary_fraction_v);
    int binary_exponent     = binary_exponent_v;

    //printf("bi %s\n",binary_integer);
    while (binary_exponent > 0) {
        int a = strlen(binary_integer);

        binary_integer[a] = binary_fraction[0];
        binary_integer[a+1] = '\0';
        //strcpy(binary_fraction, substr(binary_fraction,1,strlen(binary_fraction-1)));
        substr1(binary_fraction);
        if (binary_fraction[0] == '\0') {
            strcpy(binary_fraction, "0");
        }
        binary_exponent--;
    }
    ////printf("space needed: %d\n", (int) binary_fraction - (int) binary_fraction_backing);


    //printf("bi %s\n",binary_integer);

    while (binary_exponent < 0) {
        //strcpy(temp, &(binary_integer[strlen(binary_integer) - 1]));
        temp[0] = binary_integer[strlen(binary_integer) - 1];
        temp[1] = '\0';
        strcpy(binary_fraction, strcat(temp, binary_fraction));
        //strcpy(binary_integer, substr(binary_integer,0, strlen(binary_integer) - 1));
        binary_integer[strlen(binary_integer)-1] = '\0';
        if (binary_integer[0] == '\0') {
            strcpy(binary_integer, "0");
        }
        binary_exponent++;
    }

    //  Decimal integer part
    strcpy(decimal_integer, "0");

    mpz_t power_of_two;
    mpz_init(power_of_two);
    mpz_set_ui(power_of_two, 1);

    mpz_t di;
    mpz_init(di);
    mpz_set_ui(di, 0);

    //printf("bin int len: %s\n", binary_integer);
    //int aa = 0, b = 0;
    for (int i = strlen(binary_integer) - 1; i > -1; i--) {
        //  if it's a 1 you add the decimal integer to the power of two (power of
        //  two starts at 1)
        if (binary_integer[i] == '1') {
            mpz_add(di, di, power_of_two);
            //printf("di %s\n", mpz_get_str(NULL,10,power_of_two));
            //aa++;
        }
        mpz_add(power_of_two, power_of_two, power_of_two); //double the power_of_two
        //printf("power_of_two b=%d %s\n", b, mpz_get_str(NULL,10,power_of_two));
        //b++;
    }
    //printf("aa = %d\nb = %d\n",aa,b);

    //  Decimal fraction part
    //  reset the power_of_two back to 1
    mpz_set_ui(power_of_two, 1);

    mpz_t df;
    mpz_init(df);
    mpz_set_ui(df, 0);

    strcpy(decimal_fraction, "0");

    //printf("binary_fraction pre-add: %s\n", binary_fraction);
    int j = 0, k = 0;
    for (int i = strlen(binary_fraction) - 1; i > -1; i--) {
        //  if it is 1 add the decimal fraction to the power of two
        if (binary_fraction[i] == '1') {
            j++;
            mpz_add(df, df, power_of_two);
        }
        mpz_add(power_of_two, power_of_two, power_of_two); //double the power_of_two
        k++;
    }
    //printf("j = %d\nk = %d\n",j,k);

    //printf("df %s\n", mpz_get_str(NULL, 10, df));
    //printf("powtwo %s\n", mpz_get_str(NULL, 10, power_of_two));

    mpf_set_default_prec(1000);
    mpf_t decf;
    mpf_init(decf);
    mpf_set_z(decf, df);
    //int n = 10;

    mpf_t powtwo;
    mpf_init(powtwo);
    mpf_set_z(powtwo, power_of_two);

    mpf_div(decf, decf, powtwo);


    //  Normalize
    decimal_exponent = 0;

    char* ditemp = malloc(BUF);
    char* dftemp = malloc(BUF);

    mpz_get_str(ditemp, 10, di);
    //printf("ditemp %s\n", ditemp);
    strcpy(decimal_integer, ditemp);

    mp_exp_t a;

    mpf_get_str(dftemp, &a, 10, 10000, decf);
    strcpy(decimal_fraction, dftemp);

    /*
        while( a <0)
        {
          strcpy(decimal_fraction, strcat("0", decimal_fraction));
          a++;
        }
    */
    prepend(decimal_fraction, repeat_char('0', abs(a)));


    while (strlen(decimal_integer)>1) {
        temp[0] = decimal_integer[strlen(decimal_integer)-1];
        temp[1] = '\0';
        //strcpy(temp, &(decimal_integer[strlen(decimal_integer)-1]));
        strcpy(decimal_fraction, strcat(temp, decimal_fraction));
        //strcpy(decimal_integer, substr(decimal_integer,0, strlen(decimal_integer) - 1));
        decimal_integer[strlen(decimal_integer)-1] = '\0';
        decimal_exponent++;
    }

    while (strcmp(decimal_integer, "0") == 0) {
        decimal_integer[0] = decimal_fraction[0];
        decimal_integer[1] = '\0';
        //strcpy(decimal_integer, decimal_fraction);
        //strcpy(decimal_fraction, substr(decimal_fraction,1,strlen(decimal_fraction)-1));
        substr1(decimal_fraction);
        decimal_exponent--;
    }

    if (decimal_integer[0] == '\0') {
        strcpy(decimal_integer, "0");
    }

    if (decimal_fraction[0] == '\0') {
        strcpy(decimal_fraction, "0");
    }

    char* tempDI = malloc(BUF);
    strcpy( tempDI, decimal_integer);
    while (tempDI[0] == '0') {
        //strcpy(tempDI, substr(tempDI,1,strlen(tempDI)-1));
        substr1(tempDI);
    }
    if (tempDI[0] == '\0') strcpy(tempDI, "0");

    strcpy(decimal_integer, tempDI);

    printf("%s\n", decimal_integer);
    printf("%s\n", decimal_fraction);
    printf("%d\n", decimal_exponent);
    printf("%s\n", decimal_recurrence);
    printf("%d\n", decimal_recurrence_start);
    printf("%s\n", binary_recurrence);
    printf("%d\n", binary_recurrence_start);
    // These bottom three are the normalized binary values that also have to be
    // returned to the Javascript
    printf("%s\n", binary_integer_n);
    printf("%s\n", binary_fraction_n);
    printf("%d\n", binary_exponent_n);

    mpf_clear(decf);
    mpf_clear(powtwo);
    mpz_clear(result);
    mpz_clear(di);
    mpz_clear(df);

    free(binary_integer_n);
    free(binary_fraction_n);
    free(binary_integer_v);
    free(binary_fraction_v);
    free(binary_integer);
    free(binary_fraction);
    free(binary_recurrence);
    free(decimal_recurrence);
    free(decimal_integer);
    free(decimal_fraction);
    free(tempDI);
    free(ditemp);
    free(dftemp);
    free(temp);

    return 0;
}
Ejemplo n.º 9
0
/*----------------------------------------------------------------------
   Function to control flag set/clearing

   Basically, turn the flags into a fake list of features...

   Returns 0 unless user has added a keyword, then 1.

 ----*/
int
flag_maintenance_screen(struct pine *ps, struct flag_screen *flags)
{
    int		  i, lv, lc, maxwidth, offset, need, rv = 0;
    char	  tmp[1200], **p, *spacer;
    CONF_S	 *ctmpa, *first_line;
    struct	  flag_table  *fp;
    OPT_SCREEN_S  screen;

try_again:
    maxwidth = MAX(MIN((ps->ttyo ? ps->ttyo->screen_cols : 80), 150), 30);
    first_line = NULL;
    ctmpa = NULL;

    for(p = flags->explanation; p && *p; p++) {
        new_confline(&ctmpa);
        ctmpa->keymenu   = &flag_keymenu;
        ctmpa->help      = NO_HELP;
        ctmpa->tool      = flag_checkbox_tool;
        ctmpa->flags    |= CF_NOSELECT;
        ctmpa->valoffset = 0;
        ctmpa->value     = cpystr(_(*p));
    }

    /* Now wire flags checkboxes together */
    for(lv = 0, lc = 0, fp = (flags->flag_table ? *flags->flag_table : NULL);
            fp && fp->name; fp++) {	/* longest name */
        if(fp->flag != F_COMMENT) {
            if(lv < (i = utf8_width(_(fp->name))))
                lv = i;
            if(fp->comment && lc < (i = utf8_width(fp->comment)))
                lc = i;
        }
    }

    lv = MIN(lv,100);
    lc = MIN(lc,100);
    if(lc > 0)
        spacer = "    ";
    else
        spacer = "";

    offset = 6;
    if((need = offset + 5 + lv + strlen(spacer) + lc) > maxwidth) {
        offset -= (need - maxwidth);
        offset = MAX(0,offset);
        if((need = offset + 5 + lv + strlen(spacer) + lc) > maxwidth) {
            spacer = " ";
            if((need = offset + 5 + lv + strlen(spacer) + lc) > maxwidth) {
                lc -= (need - maxwidth);
                lc = MAX(0,lc);
                if(lc == 0)
                    spacer = "";
            }
        }
    }

    new_confline(&ctmpa);
    ctmpa->keymenu   = &flag_keymenu;
    ctmpa->help      = NO_HELP;
    ctmpa->tool      = flag_checkbox_tool;
    ctmpa->flags    |= CF_NOSELECT;
    ctmpa->valoffset = 0;
    ctmpa->value     = cpystr("");

    new_confline(&ctmpa);
    ctmpa->keymenu   = &flag_keymenu;
    ctmpa->help      = NO_HELP;
    ctmpa->tool      = flag_checkbox_tool;
    ctmpa->flags    |= CF_NOSELECT;
    ctmpa->valoffset = 0;
    utf8_snprintf(tmp, sizeof(tmp), "%*.*w  %s", offset+3, offset+3, _("Set"), _("Flag/Keyword Name"));
    tmp[sizeof(tmp)-1] = '\0';
    ctmpa->value = cpystr(tmp);

    new_confline(&ctmpa);
    ctmpa->keymenu   = &flag_keymenu;
    ctmpa->help      = NO_HELP;
    ctmpa->tool      = flag_checkbox_tool;
    ctmpa->flags    |= CF_NOSELECT;
    ctmpa->valoffset = 0;
    snprintf(tmp, sizeof(tmp), "%*.*s---  %.*s",
             offset, offset, "",
             lv+lc+strlen(spacer), repeat_char(lv+lc+strlen(spacer), '-'));
    tmp[sizeof(tmp)-1] = '\0';
    ctmpa->value = cpystr(tmp);

    for(fp = (flags->flag_table ? *flags->flag_table : NULL);
            fp && fp->name; fp++) {	/* build the list */
        new_confline(&ctmpa);
        if(!first_line && (fp->flag != F_COMMENT))
            first_line = ctmpa;

        ctmpa->keymenu   = &flag_keymenu;
        ctmpa->tool      = flag_checkbox_tool;
        ctmpa->valoffset = offset;

        if(fp->flag == F_COMMENT) {
            ctmpa->help   = NO_HELP;
            ctmpa->flags |= CF_NOSELECT;
            ctmpa->value  = cpystr(fp->name);
        }
        else {
            ctmpa->help		  = fp->help;
            ctmpa->d.f.ftbl	  = flags->flag_table;
            ctmpa->d.f.fp	  = fp;

            utf8_snprintf(tmp, sizeof(tmp), "[%c]  %-*.*w%s%-*.*w",
                          (fp->set == 0) ? ' ' : (fp->set == 1) ? 'X' : '?',
                          lv, lv, _(fp->name),
                          spacer, lc, lc, fp->comment ? fp->comment : "");
            ctmpa->value = cpystr(tmp);
        }
    }

    memset(&screen, 0, sizeof(screen));
    /*
     * TRANSLATORS: FLAG MAINTENANCE is a screen title.
     * Print something1 using something2.  configuration is something1
     */
    if(conf_scroll_screen(ps, &screen, first_line,
                          _("FLAG MAINTENANCE"),
                          _("configuration"), 0) == FLAG_ADD_RETURN) {
        int flags, r;
        char keyword[500];
        char nickname[500];
        char prompt[500];
        char *error = NULL;
        KEYWORD_S *kw;
        HelpType help;

        /*
         * User is asking to add a new keyword. We will add it to the
         * mailbox if necessary and add it to the keywords list from
         * Setup/Config. Then we will modify the flag_table and present
         * the flag modification screen again.
         */

        ps->mangled_screen = 1;
        keyword[0] = '\0';
        flags = OE_APPEND_CURRENT;
        help = NO_HELP;

        do {
            if(error) {
                q_status_message(SM_ORDER, 3, 4, error);
                fs_give((void **) &error);
            }

            strncpy(prompt, _("Keyword to be added : "), sizeof(prompt)-1);
            prompt[sizeof(prompt)-1] = '\0';
            r = optionally_enter(keyword, -FOOTER_ROWS(ps_global), 0,
                                 sizeof(keyword), prompt, NULL, help, &flags);

            if(r == 3)
                help = help == NO_HELP ? h_type_keyword : NO_HELP;
            else if(r == 1) {
                cmd_cancelled("Add Keyword");
                goto try_again;
            }

            removing_leading_and_trailing_white_space(keyword);

        } while(r == 3 || keyword_check(keyword, &error));

        for(kw = ps->keywords; kw; kw = kw->next) {
            if(kw->kw && !strucmp(kw->kw, keyword)) {
                q_status_message(SM_ORDER, 3, 4, _("Keyword already configured, changing nickname"));
                break;
            }
        }

        snprintf(prompt, sizeof(prompt), _("Optional nickname for \"%s\" : "), keyword);

        nickname[0] = '\0';
        help = NO_HELP;

        do {
            r = optionally_enter(nickname, -FOOTER_ROWS(ps_global), 0,
                                 sizeof(nickname), prompt, NULL, help, &flags);

            if(r == 3)
                help = help == NO_HELP ? h_type_keyword_nickname : NO_HELP;
            else if(r == 1) {
                cmd_cancelled("Add Keyword");
                goto try_again;
            }

            removing_leading_and_trailing_white_space(nickname);

        } while(r == 3);

        if(keyword[0]) {
            char ***alval;
            int offset = -1;
            struct variable *var;

            var = &ps_global->vars[V_KEYWORDS];
            alval = ALVAL(var, Main);

            for(kw = ps->keywords; kw; kw = kw->next) {
                offset++;
                if(kw->kw && !strucmp(kw->kw, keyword)) {
                    /* looks like it should already exist at offset */
                    break;
                }
            }

            if(!kw)
                offset = -1;

            if(offset >= 0 && (*alval) && (*alval)[offset]) {
                fs_give((void **) &(*alval)[offset]);
                (*alval)[offset] = put_pair(nickname, keyword);
            }
            else if(!*alval) {
                offset = 0;
                *alval = (char **) fs_get(2*sizeof(char *));
                (*alval)[offset] = put_pair(nickname, keyword);
                (*alval)[offset+1] = NULL;
            }
            else {
                for(offset=0; (*alval)[offset]; offset++);
                ;

                fs_resize((void **) alval, (offset + 2) * sizeof(char *));
                (*alval)[offset] = put_pair(nickname, keyword);
                (*alval)[offset+1] = NULL;
            }

            set_current_val(var, TRUE, FALSE);
            if(ps_global->prc)
                ps_global->prc->outstanding_pinerc_changes = 1;

            if(ps_global->keywords)
                free_keyword_list(&ps_global->keywords);

            if(var->current_val.l && var->current_val.l[0])
                ps_global->keywords = init_keyword_list(var->current_val.l);

            clear_index_cache(ps_global->mail_stream, 0);

            rv = 1;
        }
    }

    ps->mangled_screen = 1;

    return(rv);
}
Ejemplo n.º 10
0
struct cat* read_cat(const char* fname, int64 nside, double radius_arcsec, int verbose) {

    int barsize=70;

    FILE* fptr=open_file(fname);


    size_t nlines=countlines(fptr);
    if (verbose) wlog("    found %lu lines\n", nlines);
    rewind(fptr);

    struct cat* cat = alloc_or_die(sizeof(struct cat), "catalog struct");

    double radius_radians=0;
    if (radius_arcsec <= 0) {
        cat->radius_in_file=1;
        cat->cos_radius = alloc_or_die(nlines*sizeof(double), "cos_radius array");
    } else {
        cat->radius_in_file=0;
        cat->cos_radius = alloc_or_die(sizeof(double), "cos_radius");

        radius_radians = radius_arcsec/3600.*D2R;
        cat->cos_radius[0] = cos(radius_radians);
    }

    cat->hpix=NULL;
    cat->tree=NULL;

    cat->pts = alloc_or_die(nlines*sizeof(struct point),"points");
    cat->size = nlines;

    if (verbose) wlog("    creating hpix\n");
    cat->hpix = hpix_new(nside);

    if (verbose) {
        wlog("    reading and building tree\n");
        repeat_char('.', barsize); wlog("\n");
    }

    double ra=0, dec=0;
    struct i64stack* listpix = i64stack_new(0);

    // this will produce a more balanced tree across the whole sky
    int64 half_npix=cat->hpix->npix/2;

    size_t count=0;
    struct point* pt = &cat->pts[0];
    for (size_t i=0; i<cat->size; i++) {
        if (2 != fscanf(fptr, "%lf %lf", &ra, &dec)) {
            wlog("expected to read point at line %lu\n", i);
            exit(EXIT_FAILURE);
        }
        if (cat->radius_in_file) {
            if (1 != fscanf(fptr, "%lf", &radius_arcsec)) {
                wlog("expected to read radius at line %lu\n", i);
                exit(EXIT_FAILURE);
            }
            radius_radians = radius_arcsec/3600.*D2R;
            cat->cos_radius[i] = cos(radius_radians);
        }

        hpix_eq2xyz(ra,dec,&pt->x,&pt->y,&pt->z);

        hpix_disc_intersect(cat->hpix, pt->x, pt->y, pt->z, radius_radians, 
                            listpix);

        int64* ptr=listpix->data;
        while (ptr < listpix->data + listpix->size) {
            tree_insert(&cat->tree, (*ptr)-half_npix, count);
            ptr++;
        }

        pt++;
        count++;
        if (verbose) incr_bar(i+1, cat->size, barsize, '=');
    }
    listpix=i64stack_delete(listpix);

    if (verbose) wlog("\n");

    if (count != nlines) {
        wlog("expected %lu lines but read %lu\n", nlines, count);
        exit(EXIT_FAILURE);
    }

    if (verbose)
        wlog("fullest node has %lu members\n", tree_most_members(cat->tree));

    fclose(fptr);
    return cat;
}