static Boolean test_length_of_int(int verbose) { static struct stringtest { int d; int len; } tests[] = { { 0, 1 }, { 1, 1 }, { 11, 2 }, { 111, 3 }, { 100, 3 }, { 100000, 6 }, { 999999, 6 }, { 1000000, 7 }, { 101, 3 }, { 001, 1 }, }; size_t i; Boolean result = True; for (i = 0; i < (sizeof tests / sizeof tests[0]); i++) { if (verbose) { INFO((stderr, "length_of_int(%d) -> %d == %d?\n", tests[i].d, length_of_int(tests[i].d), tests[i].len)); } if (length_of_int(tests[i].d) != tests[i].len) { result = False; } } return result; }
Vector convert_to_bignum(int n){ Vector result; result.size = length_of_int(n); int* numbers = (int*) malloc(sizeof(int)*result.size); result.integers = numbers; int i = 0; for(; i<result.size; i++){ result.integers[i] = n%10; n = n/10; } return result; }
/* like above, but output goes to the statusline, and it prints only up to HISTORY_MAX_CONTEXT items around the current one. */ static void page_history_show_statusline(struct dl_list *head, const struct dl_list *curr, const char *msg) { #define HIST_LEN 1024 /* should be ample since statusline is limited to 512 MAX_LEN */ int file_idx = 0; int n; char history[HIST_LEN]; char *ptr = history; int tot_len = m_page_history_length; int curr_pos = m_page_history_currpos; int initial_offset = 0; int printed_len = 0; #if DEBUG fprintf(stderr, "tot_len: %d, curr_pos: %d\n", tot_len, curr_pos); #endif if (!(resource.expert_mode & XPRT_SHOW_STATUSLINE)) { /* too distracting for stdout */ return; } if (head == NULL){ strcpy(ptr, "Page history empty."); ptr += strlen("Page history empty."); } else { strcpy(ptr, "Page history:"); ptr += strlen("Page history:"); } /* check if we need to truncate at the beginning or end */ if (tot_len > HISTORY_MAX_CONTEXT) { /* need to truncate, try to make first and second chunk around current position of same length */ int good_pos = HISTORY_MAX_CONTEXT / 2.0 + 0.5; while (curr_pos > good_pos /* && */ /* m_page_history_length - m_page_history_currpos > good_pos */) { #if DEBUG fprintf(stderr, "%d > %d; %d > %d\n", curr_pos, good_pos, m_page_history_length - m_page_history_currpos, good_pos); #endif curr_pos--; initial_offset++; } #if DEBUG fprintf(stderr, "initial offset: %d\n", initial_offset); #endif /* if we're more to the end, adjust good_pos and initial_offset */ while (good_pos - 1 > m_page_history_length - m_page_history_currpos) { #if DEBUG fprintf(stderr, "%d > %d\n", m_page_history_length - m_page_history_currpos, good_pos); #endif initial_offset--; good_pos--; } #if DEBUG fprintf(stderr, "initial offset adjusted: %d\n", initial_offset); #endif } for (n = 0; head != NULL; head = head->next, n++) { struct page_history *item; /* skip initial offset, and insert truncation marker at beginning/end */ if (initial_offset == 1 || printed_len >= HISTORY_MAX_CONTEXT) { strcpy(ptr, " ..."); ptr += strlen(" ..."); if (printed_len >= HISTORY_MAX_CONTEXT) break; } if (initial_offset > 0) { initial_offset--; continue; } printed_len++; item = (struct page_history *)(head->item); /* insert a marker if item is in different file ... */ if (item->file_idx != file_idx) { if (n > 0) { /* ... but only if we're not at the beginning of the list */ #if 1 strcpy(ptr, " -"); ptr += 2; #else char *fname = m_filename_list[item->file_idx]; char *tmp; if ((tmp = strrchr(m_filename_list[item->file_idx], '/')) != NULL) fname = tmp + 1; strcpy(ptr, " ["); ptr += 2; strcpy(ptr, fname); ptr += strlen(fname); strcpy(ptr, "]"); ptr++; #endif } file_idx = item->file_idx; } if (head == curr) { xdvi_assert(XDVI_VERSION_INFO, __FILE__, __LINE__, m_page_history_currpos == n + 1, "%d == %d + 1", m_page_history_currpos, n); sprintf(ptr, " [%d]", item->pageno + 1); ptr += 3 + length_of_int(item->pageno + 1); } else { sprintf(ptr, " %d", item->pageno + 1); ptr += 1 + length_of_int(item->pageno + 1); } } #if DEBUG fprintf(stderr, "Statusline string: |%s|; printed len: %d\n", history, printed_len); #endif statusline_info(STATUS_MEDIUM, "%s %s", history, msg ? msg : ""); #undef HIST_LEN }