static gchar* get_prefix(glong index) { gchar* prefix; gchar space = ' '; if (view.current == index && (view.top_x > 0 || is_white(index)) ) space = '>'; prefix = g_strnfill(1, space); if (conf.numbers) { gchar* tmp = prefix; glong max_n = num_digits(SL); glong n = num_digits(index); gchar* str_index = g_strdup_printf("%ld", index); gchar* num_prefix = g_strnfill(max_n - n, ' '); prefix = g_strconcat(num_prefix, str_index, tmp, NULL); g_free(tmp); g_free(num_prefix); g_free(str_index); } if (conf.checkbox) { gchar* symbol = " "; if (is_checked(index)) { symbol = "x"; if (conf.radiobox) symbol = "o"; } gchar* tmp = prefix; prefix = g_strconcat(tmp, "[", symbol, "]", NULL); g_free(tmp); } return prefix; }
int main(int argc, char **argv) { // Pre: User passes as a parameter the number of digits to find the product from // better checking would be required int n = atoi(argv[1]); // Create biggest number with n digits and assign to n1 and n2 char *s_biggest_with_n = malloc(n); int i, j; for(i=0; i<n; i++) s_biggest_with_n[i] = '9'; int biggest_with_n; sscanf(s_biggest_with_n, "%d", &biggest_with_n); free(s_biggest_with_n); //Keep decreasing the numbers until the bigger palindrom id found int max = 0; for (i=biggest_with_n; num_digits(i) == n; i--) { for (j=biggest_with_n; num_digits(j) == n; j--) { int pal = i*j; if (is_palindrom(pal) && pal > max) { max = pal; } } } printf("%d\n", max); return 0; }
int main(void) { printf("%d\n", num_digits(905)); printf("%d\n", num_digits(4)); return 0; }
void db_add_tag_to_file(int tag_id, int file_id) { char *query = NULL; char query_outline[] = "INSERT INTO file_has_tag VALUES(, )"; int query_length = 0; int rc = SQLITE_ERROR; /* return code of sqlite operation */ int written = 0; /* number of characters written */ sqlite3 *conn = NULL; sqlite3_stmt *res = NULL; DEBUG(ENTRY); assert(tag_id >= 0); assert(file_id > 0); /* prepare query */ query_length = strlen(query_outline) + num_digits(tag_id) + num_digits(file_id); query = malloc(query_length * sizeof(*query) + 1); assert(query != NULL); written = snprintf(query, query_length + 1, "INSERT INTO file_has_tag VALUES(%d, %d)", file_id, tag_id); assert(written == query_length); /* connect to database */ conn = db_connect(); assert(conn != NULL); rc = db_execute_statement(conn, query, &res); db_finalize_statement(conn, query, res); db_disconnect(conn); free_single_ptr((void **)&query); DEBUG("Adding tag ID %d to file ID %d was %ssuccessful", tag_id, file_id, rc == SQLITE_DONE ? "" : "not "); DEBUG(EXIT); } /* db_add_tag_to_file */
static unsigned int send_files(int s, const struct sockaddr *cli, int cli_len, const char *filename, const char *prefix, __u32 num_blocks, __u32 files_per_block, __u16 padding, int send_data, unsigned int fr) { unsigned int i, j; int size; unsigned int num_dropped = 0; /* Loop over all blocks. */ for (i = 1; i <= files_per_block; i++) { __s16 chunk_id = send_data ? i : -i; for (j = 0; j < num_blocks; j++) { char *chunk_path; size = asprintf(&chunk_path, "%s/%s/b%0*d/%s%0*d", ENCODED_DIR, filename, num_digits(num_blocks - 1), j, prefix, num_digits(DATA_FILES_PER_BLOCK), i); if (size == -1) { fprintf(stderr, "asprintf: cannot alloc chunk path\n"); return -1; } usleep(100); if ((unsigned)rand() % 100 >= fr) send_file(s, cli, cli_len, filename, chunk_path, num_blocks, j, chunk_id, padding); else num_dropped++; } } return num_dropped; }
int main() { int i, j, nd; int prod, cand, max = 0; char numstr[32]; /* * We stop the loop at 9999 since any number after that multiplied by * atleast 1 and 2 and concatenated will results in more than 9 digits. */ for (i = 1; i < 10000; i++) { memset(numstr, 0, 32); nd = 0; for (j = 1; j < 10; j++) { prod = i * j; nd += num_digits(prod); sprintf(numstr, "%s%d", numstr, prod); if (nd >= 9) { break; } } if (nd == 9) { cand = atoi(numstr); if (is_candidate(cand) && cand > max) { max = cand; } } } printf("%d\n", max); return (0); }
int hbc::write(int fd, const unsigned char* payload, int length) { if (length > PAYLOAD_MAX) { int buffer_length = num_digits(length); unsigned char buffer[buffer_length]; sprintf((char*) buffer, "%i", length); int w = hbc::write_packet(fd, buffer, length); if (w < 0) return w; } int o = 0; do { int w = hbc::write_packet(fd, &payload[o], length - o); if (w < 0) return w; o += w; } while (o < length); // std::string pstr(reinterpret_cast<const char*>(payload), length); // fprintf(stdout, "<- fd[%i], chunked[%i], %s\n", fd, length > PAYLOAD_MAX, pstr.c_str()); return o; }
int db_files_from_tag_id(int tag_id, int **files) { char query_outline[] = "SELECT file_id FROM all_tables WHERE tag_id = "; int query_outline_length = 0; char *query = NULL; int count = 0; int query_length = 0; int written = 0; /* number of characters written */ int num_digits_in_id = 0; DEBUG(ENTRY); assert(*files == NULL); DEBUG("Retrieving files from tag ID %d", tag_id); if(tag_id > 0) { /* prepare query */ num_digits_in_id = num_digits(tag_id); query_outline_length = strlen(query_outline); query_length = query_outline_length + num_digits_in_id; query = malloc(query_length * sizeof(*query) + 1); assert(query != NULL); written = snprintf(query, query_length + 1, "%s%d", query_outline, tag_id); assert(written == query_length); count = db_int_array_from_query("file_id", query, files); free_single_ptr((void **)&query); } else { count = db_int_array_from_query("file_id", "SELECT * FROM files WHERE file_id NOT IN (SELECT file_id FROM file_has_tag)", files); } DEBUG("Returning a list of %d files", count); DEBUG(EXIT); return count; } /* db_files_from_tag_id */
void db_delete_tag(int tag_id) { char *query = NULL; char query_outline[] = "DELETE FROM tags WHERE tag_id = \"\""; int query_length = 0; int rc = SQLITE_ERROR; /* return code of sqlite operation */ int written = 0; /* number of characters written */ sqlite3 *conn = NULL; sqlite3_stmt *res = NULL; DEBUG(ENTRY); assert(tag_id > 0); DEBUG("Deleting tag with tag ID %d", tag_id); /* prepare query */ query_length = strlen(query_outline) + num_digits(tag_id); query = malloc(query_length * sizeof(*query) + 1); assert(query != NULL); written = snprintf(query, query_length + 1, "DELETE FROM tags WHERE tag_id = \"%d\"", tag_id); assert(written == query_length); /* connect to database */ conn = db_connect(); assert(conn != NULL); rc = db_execute_statement(conn, query, &res); db_finalize_statement(conn, query, res); db_disconnect(conn); free_single_ptr((void **)&query); DEBUG("File with ID %d was %sdeleted successfully.", tag_id, rc == 0 ? "" : "not ") DEBUG(EXIT); } /* db_delete_tag */
void create_font_from_dir (const char *dirname, const char *fontname, int latticeh, int latticew, int fontnum) { /* Call python from here */ int commandlen = strlen ("smoothscan-fontgen.py") + 1 + strlen (dirname) + 1 + strlen (fontname) + 1 + num_digits (latticeh) + 1 + num_digits (latticew) + 1 + num_digits (fontnum); char *command = malloc_guarded (commandlen + 1); sprintf (command, "smoothscan-fontgen.py %s %s %d %d %d", dirname, fontname, latticeh, latticew, fontnum); system (command); free (command); }
void db_remove_file(int file_id) { char *query = NULL; char query_outline[] = "DELETE FROM files WHERE file_id = "; int query_length = 0; int rc = SQLITE_ERROR; /* return code of sqlite operation */ int written = 0; /* number of characters written */ sqlite3 *conn = NULL; sqlite3_stmt *res = NULL; DEBUG(ENTRY); assert(file_id > 0); DEBUG("Removing file ID %d", file_id); /* prepare query */ query_length = strlen(query_outline) + num_digits(file_id); query = malloc(query_length * sizeof(*query) + 1); assert(query != NULL); written = snprintf(query, query_length + 1, "DELETE FROM files WHERE file_id = %d", file_id); assert(written == query_length); /* connect to database */ conn = db_connect(); assert(conn != NULL); rc = db_execute_statement(conn, query, &res); db_finalize_statement(conn, query, res); db_disconnect(conn); free_single_ptr((void **)&query); DEBUG("Removing file ID %d was %ssuccessful", file_id, rc == SQLITE_DONE ? "" : "not "); DEBUG(EXIT); } /* db_remove_file */
void tictactoe::field::print(std::ostream &os, empty_tile_caption_callback on_empty) const { const std::string::size_type caption_length = num_digits(this->size()), player_state_padding_length = caption_length / 2; // truncation by int division is intentional const std::string player_state_padding = std::string(player_state_padding_length, ' '), X(player_state_padding + ((caption_length % 2) ? "X" : "><") + player_state_padding), O(player_state_padding + ((caption_length % 2) ? "O" : "()") + player_state_padding), row_separator("\n +" + repeat_string(std::string(2 + caption_length, '-') + "+", order()) + " \n"); const char * const field_separator(" | "); for(size_type index=0; index < size(); ++index) { if (0 == index % order()) { // first field in a new row os << row_separator << field_separator; } switch(tiles[index]) { case tile::empty: os << on_empty(caption_length, index); break; case tile::player1: os << X; break; case tile::player2: os << O; break; // no default case - issue warning if cases are not specifically handled } os << field_separator; } os << row_separator; }
/* Return the number of digits in integer n, n >= 0. */ int num_digits(int n) { if(n < 10){ return 1; } else{ return 1 + num_digits(n/10); } }
void lazy_entry::construct_string(char const* start, int length) { TORRENT_ASSERT(m_type == none_t); m_type = string_t; m_data.start = start; m_size = length; m_begin = start - 1 - num_digits(length); m_len = start - m_begin + length; }
char is_palindrom(int n) { int size = num_digits(n); char s_n[size]; sprintf(s_n, "%d", n); int i = 0; while (i < size-i-1) { if (!(s_n[i] == s_n[size-i-1])) return 0; else i++; } return 1; }
int main( void ) { printf("Enter a positive number: "); int number; scanf("%d", &number); printf("The number %d has %d digits.", number, num_digits(number)); return EXIT_SUCCESS; }
std::vector<lint_t> n_digit_primes(int n) const{ std::vector<lint_t> result; int tmp; for (unsigned i=0 ; i<_primes.size() ; ++i){ tmp = num_digits(_primes[i]); if (n == tmp) result.push_back(_primes[i]); else if (tmp > n) break; } return result; }
int main() { const int a[] = { 1,2,5,6,7,8,3,2,5 }; const int b[] = { 0,2,5,6,9 }; const int c[] = { 7,2,5,6,7,8,3,2,5,5,3,2,7 }; int x = 123456789; int y = 45584234; int z = 2133123; size_t n = 9; int min, max; int *pmin, *pmax; pmin = &min; pmax = &max; min_max(a, n, pmin, pmax); printf("Min:%d Max:%d\n", min, max); n = 5; min_max(b, n, pmin, pmax); printf("Min:%d Max:%d\n", min, max); n = 13; min_max(c, n, pmin, pmax); printf("Min:%d Max:%d\n", min, max); printf("Number:%d num_digits:%d\n", x, num_digits(x)); printf("Number:%d num_digits:%d\n", y, num_digits(y)); printf("Number:%d num_digits:%d\n", z, num_digits(z)); printf("Number:%d reverse:%lu\n", x, reverse(x)); printf("Number:%d reverse:%lu\n", y, reverse(y)); printf("Number:%d reverse:%lu\n", z, reverse(z)); getchar(); return 0; }
char *db_get_file_location(int file_id) { char *file_location = NULL; char *query = NULL; char *tmp_file_directory = NULL; /* holds text from the query so it can be copied to a new memory location */ char *tmp_file_name = NULL; /* hold name of file until it can be copied to a new memory location */ char query_outline[] = "SELECT file_location, file_name FROM files WHERE file_id = "; int file_location_length = 0; /* length of the file location to return */ int query_length = 0; int written = 0; /* number of characters written */ sqlite3 *conn = NULL; sqlite3_stmt *res = NULL; DEBUG(ENTRY); assert(file_id > 0); DEBUG("Retrieving physical location for file with ID %d", file_id); /* prepare query */ query_length = strlen(query_outline) + num_digits(file_id); query = malloc(query_length * sizeof(*query) + 1); written = snprintf(query, query_length + 1, "SELECT file_location, file_name FROM files WHERE file_id = %d", file_id); assert(written == query_length); /* connect to database */ conn = db_connect(); assert(conn != NULL); db_execute_statement(conn, query, &res); /* get file location and name */ tmp_file_directory = (char *)sqlite3_column_text(res, 0); assert(tmp_file_directory != NULL); tmp_file_name = (char *)sqlite3_column_text(res, 1); assert(tmp_file_name != NULL); /* build full file path from name and directory */ file_location_length = strlen(tmp_file_directory) + strlen(tmp_file_name) + 1; file_location = malloc(file_location_length * sizeof(*file_location) + 1); written = snprintf((char *)file_location, file_location_length + 1, "%s/%s", tmp_file_directory, tmp_file_name); assert(written == file_location_length); db_finalize_statement(conn, query, res); db_disconnect(conn); free_single_ptr((void *)&query); DEBUG("File id %d corresponds to %s", file_id, file_location); DEBUG(EXIT); return file_location; } /* db_get_file_location */
void print_board(Board b) { print_horizontal(); for (int r=0; r < BOARDSIZE; r++) { printf("|"); for (int c=0; c < BOARDSIZE; c++) { int val = bget(b, r,c); for (int i=0; i < NUMCHARS - num_digits(val); i++) printf(" "); // Right-justify numbers printf("%d", val); } printf("|\n"); } print_horizontal(); }
unsigned long reverse(unsigned long n){ unsigned long numofdigits; unsigned long reversednum = 0; unsigned long zeros = 1; unsigned long tempn = n; unsigned long i; numofdigits = num_digits(n); for(i = 1; i < numofdigits; i++){ zeros *= 10; } for(i = 1; i <= numofdigits; i++){ reversednum += (tempn % 10 ) * zeros; tempn = tempn / 10; zeros /= 10; } return reversednum; }
char *db_tag_name_from_tag_id(int tag_id) { char *query = NULL; char *tag_name = NULL; char query_outline[] = "SELECT tag_name FROM tags WHERE tag_id = "; int num_digits_in_id = 0; int query_length = 0; int query_outline_length = 0; int written = 0; /* number of characters written */ sqlite3 *conn = NULL; sqlite3_stmt *res = NULL; DEBUG(ENTRY); assert(tag_id > 0); DEBUG("Retrieving tag name for tag ID %d", tag_id); /* prepare query */ num_digits_in_id = num_digits(tag_id); query_outline_length = strlen(query_outline); query_length = query_outline_length + num_digits_in_id; query = malloc(query_length * sizeof(*query) + 1); assert(query != NULL); written = snprintf(query, query_length + 1, "%s%d", query_outline, tag_id); assert(written == query_length); /* connect to database */ conn = db_connect(); assert(conn != NULL); db_execute_statement(conn, query, &res); /* get name corresponding to tag_id */ tag_name = strdup((char *)sqlite3_column_text(res, 0)); db_finalize_statement(conn, query, res); db_disconnect(conn); free_single_ptr((void **)&query); DEBUG("Tag ID %d corresponds to tag %s", tag_id, tag_name); DEBUG(EXIT); return tag_name; } /* db_tag_name_from_tag_id */
// Converts a string into a int int32_t atoi(const char *s) { const char *str = s; uint8_t negafier = 1; if(*str == '-') { negafier = -1; ++str; } else { if(*str == '+') { negafier = 1; ++str; } } uint32_t digits = num_digits(str); if(digits == 0) return(0); str += digits-1; int32_t integer = 0; for(uint32_t multiplier = 1; digits > 0; str--, digits--, multiplier *= 10) { integer += get_number(*str)*multiplier; } return(integer*negafier); }
int main(void){ printf("%u \n" , num_digits(0ul)); printf("%lu \n" , reverse(12ul)); printf("%lu \n", reversible(1000000ul)); }
int main (void) { short i,j,m,n,y,np,nb,mxp,ord,im,len,*p,*q; int quot; int chct; char nt,f,id,eq,fault,flnm[80]; reenter: printf("Do you wish to read permutations from a file (y/n)? "); if (getchar()=='y') { f=1; snl(); printf("Input filename: "); scanf("%s",flnm); snl(); if ((ip=fopen(flnm,"r"))==0) { fprintf(stderr,"Cannot open %s.\n",flnm); goto reenter; } fscanf(ip,"%hd%hd%hd%hd",&npt,&np,&nb,&m); seeknln(); if (nb!=0) { if (nb>=mb) { fprintf(stderr,"Too many base points. Increase MB.\n"); return(-1);} for (i=1;i<=nb;i++) fscanf(ip,"%hd",base+i); seeknln(); } if (m!=0) seeknln(); } else { f=0; snl(); printf("Input npt: "); scanf("%hd",&npt); snl(); nb=0; } if (npt>mpt) {fprintf(stderr,"npt too big. Increase NPT.\n"); exit(1); } quot=psp/npt; if (quot>mp) mxp=mp; else mxp=quot; printf("Perm nos 1 - %d can be read. Perm no 0 is always the identity\n", mxp-1); for (i=0;i<mxp;i++) { pptr[i]=perm-1+npt*i; pno[i]=0; } p=pptr[0]; for (i=1;i<=npt;i++) p[i]=i; pno[0]=1; if (f) { if (np>=mxp) { fprintf(stderr,"Not enough perm space to read perms from file."); fprintf(stderr," Increase PSP (or MP).\n"); exit(1); } for (i=1;i<=np;i++) { if (readperm(pptr[i])==2) { fprintf(stderr,"Perm no %i is not a permutation.\n"); exit(1);} pno[i]=1; seeknln(); } fclose(ip); } while(1) { printf("Choose option. Print 'l' for list.\n"); scanf("%s",opt); if (strcmp(opt,"l")==0) { snl(); printf("rp n = read perm no n.\n"); printf("pp n = print perm no n.\n"); printf("ppc n = print perm no n in cycles.\n"); printf("inv m n = calc inverse p[n] of p[m].\n"); printf("pr n l i(1)...i(l) = calc prod p[n] of p[i(1)]...p[i(l)].\n"); printf("im m x = print image of point x under p[m].\n"); printf("fp n = print fixed pts of p[n].\n"); printf("fpn l i(1)...i(l) = print common fixed pts of p[i(1)],...,p[i(l)].\n"); printf("cyc n x = print cycle of point x under p[n].\n"); printf("ord n = print order of p[n].\n"); printf("orb l i(1)...i(l) = print orbits of p[i(1)]...p[i(l)].\n"); printf("rb = read in a base for the group.\n"); printf("op filename = output some perms to 'filename'.\n"); printf("opord filename = output some perms to 'filename' and\n"); printf(" compute order of group they generate.\n"); printf(" (Only works if base known for G!).\n"); printf("rf n filename = read in additional perms from filename\n"); printf(" starting at perm. no. n.\n"); printf("q = quit.\n"); } else if (strcmp(opt,"rp")==0) { scanf("%hd",&n); snl(); if (n<=0 || n>=mxp) fprintf(stderr,"Invalid perm.no %d.\n",n); else { while (rp(pptr[n])==2) { fprintf(stderr,"That is not a permutation. Try again!\n"); snl(); } pno[n]=1; } } else if (strcmp(opt,"pp")==0) { scanf("%hd",&n); snl(); if (n<0 || n>=mxp || pno[n]==0) { fprintf(stderr,"Perm no %d is not defined.\n",n); continue;} p=pptr[n]; for (i=1;i<=npt;i++) printf("%4d",p[i]); printf("\n"); } else if (strcmp(opt,"ppc")==0) { scanf("%hd",&n); snl(); if (n<0 || n>=mxp || pno[n]==0) { fprintf(stderr,"Perm no %d is not defined.\n",n); continue;} for (i=1;i<=npt;i++) orb[i]=1; p=pptr[n]; id=1; chct=0; for (m=1;m<=npt;m++) if (orb[m]) { if ((im=p[m])!=m) { id=0; orb[im]=0; printf("(%d,%d",m,im); chct += (num_digits(m)+num_digits(im)+2); while ((im=p[im])!=m) { if (chct>=75) { printf(",\n%d",im); chct=num_digits(im);} else {printf(",%d",im); chct += (1+num_digits(im));} orb[im]=0; } printf(")"); chct++; if (chct>=72) { printf("\n"); chct=0;} } } if (id) printf("Perm no %d is the identity.\n",n); else printf("\n"); } else if (strcmp(opt,"inv")==0) { scanf("%hd%hd",&m,&n); snl(); if (m<0 || m>=mxp || pno[m]==0) { fprintf(stderr,"Perm no %d is not defined.\n",m); continue;} if (n<=0 || n>=mxp) { fprintf(stderr,"Invalid perm. no. %d.\n",n); continue;} invert(pptr[m],pptr[n]); pno[n]=1; } else if (strcmp(opt,"pr")==0) { scanf("%hd%hd",&m,cp); if (m<=0 || m>=mxp) { fprintf(stderr,"Invalid perm no. %d\n",m);snl(); continue;} fault=0; for (i=1;i<= *cp;i++) { scanf("%hd",cp+i); n= *(cp+i); if (n<0 || n>=mxp || pno[n]==0) { fprintf(stderr,"Perm no %d is not defined.\n",n);fault=1; snl(); break; } } if (fault) continue; snl(); p=pptr[m]; for (i=1;i<=npt;i++) p[i]=image(i); pno[m]=1; } else if (strcmp(opt,"im")==0) { scanf("%hd%hd",&n,&i); snl(); if (n<0 || n>=mxp || pno[n]==0) { fprintf(stderr,"Perm no %d is not defined.\n",n); continue;} if (i<=0 || i>npt) { fprintf(stderr,"Inappropriate point %d.\n",i); continue;} printf("%d\n",pptr[n][i]); } else if (strcmp(opt,"fp")==0) { scanf("%hd",&n); snl(); if (n<0 || n>=mxp || pno[n]==0) { fprintf(stderr,"Perm no %d is not defined.\n",n); continue;} p=pptr[n]; nt=1; for (i=1;i<=npt;i++) if (p[i]==i) { nt=0; printf("%4d",i);} if (nt) printf("No fixed points.\n"); else printf("\n"); } else if (strcmp(opt,"fpn")==0) { scanf("%hd",cp); fault=0; for (i=1;i<= *cp;i++) { scanf("%hd",cp+i); n= *(cp+i); if (n<0 || n>=mxp || pno[n]==0) { fprintf(stderr,"Perm no %d is not defined.\n",n); fault=1; snl(); break; } } if (fault) continue; snl(); nt=1; for (n=1;n<=npt;n++) { f=1; for (i=1;i<= *cp;i++) if (pptr[cp[i]][n]!=n) { f=0; break;} if (f) { nt=0; printf("%4d",n); } } if (nt) printf("No fixed points.\n"); else printf("\n"); } else if (strcmp(opt,"cyc")==0) { scanf("%hd%hd",&n,&i); snl(); if (i<=0 || i>npt) { fprintf(stderr,"Inappropriate point %d.\n",i); continue;} if (n<0 || n>=mxp || pno[n]==0) { fprintf(stderr,"Perm no %d is not defined.\n",n); continue;} p=pptr[n]; if ((im=p[i])==i) printf("%d is fixed under p[%d].\n",i,n); else { printf("(%d,%d",i,im); while ((im=p[im])!=i) printf(",%d",im); printf(")\n"); } } else if (strcmp(opt,"ord")==0) { scanf("%hd",&n); snl(); if (n<0 || n>=mxp || pno[n]==0) { fprintf(stderr,"Perm no %d is not defined.\n",n); continue;} p=pptr[n]; id=1; for (i=1;i<=npt;i++) orb[i]=1; ord=1; for (m=1;m<=npt;m++) if (orb[m]) { if ((im=p[m])!=m) { id=0; orb[im]=0; len=2; while ((im=p[im])!=m) { orb[im]=0; len++; } ord=lcm(ord,len); } } if (id) printf("Perm no %d is the identity.\n",n); else printf("%d\n",ord); } else if (strcmp(opt,"orb")==0) { scanf("%hd",cp); fault=0; for (i=1;i<= *cp;i++) { scanf("%hd",cp+i); n= *(cp+i); if (n<0 || n>=mxp || pno[n]==0) { fprintf(stderr,"Perm no %d is not defined.\n",n); fault=1; snl(); break; } } if (fault) continue; snl(); allorbs(); } else if (strcmp(opt,"eq")==0) { scanf("%hd%hd",&m,&n); snl(); if (n<0 || n>=mxp || pno[n]==0) { fprintf(stderr,"Perm no %d is not defined.\n",n); continue;} if (m<0 || m>=mxp || pno[m]==0) { fprintf(stderr,"Perm no %d is not defined.\n",m); continue;} p=pptr[m]; q=pptr[n]; eq=1; for (i=1;i<=npt;i++) if (p[i]!=q[i]) {eq=0; break;} if (eq) printf("Permutations are equal.\n"); else printf("Permutations are not equal on point %d.\n",i); } else if (strcmp(opt,"rb")==0) { snl(); printf("Input base points, preceded by their number.\n"); scanf("%hd",&nb); if (nb>=mb) { fprintf(stderr,"Too many base points. Increase MB.\n"); return(-1);} for (i=1;i<=nb;i++) scanf("%hd",base+i); snl(); } else if ((y=strcmp(opt,"opord"))==0 || strcmp(opt,"op")==0) { if (y==0 && nb==0) { fprintf(stderr,"Order can only be computed when base is known.\n"); continue; } scanf("%s",flnm); snl(); op=fopen(flnm,"w"); printf("Input perm nos to be saved, preceded by their number.\n"); scanf("%hd",&n); fprintf(op,"%3d %4d%4d%4d\n",npt,n,nb,0); if (nb!=0) if (npt>=10000) { for (i=1;i<=nb;i++) fprintf(op,"%6d",base[i]); fprintf(op,"\n");} else if (npt>=1000) { for (i=1;i<=nb;i++) fprintf(op,"%5d",base[i]); fprintf(op,"\n");} else { for (i=1;i<=nb;i++) fprintf(op,"%4d",base[i]); fprintf(op,"\n");} f=0; for (i=1;i<=n;i++) { scanf("%hd",&m); if (m<0 || m>=mxp || pno[m]==0) { printf("Perm no %d is not defined.\n",m); fclose(op); snl(); unlink(flnm); f=1; break; } printvec(pptr[m],0); } if (f) continue; snl(); fclose(op); if (y==0) { sprintf(sysstring,"cp %s optxxx.ip",flnm); system(sysstring); system("gprun -b optxxx ip ip"); unlink("optxxx.ip"); } } else if (strcmp(opt,"rf")==0) { scanf("%hd",&n); scanf("%s",flnm); snl(); if ((ip=fopen(flnm,"r"))==0) { fprintf(stderr,"Cannot open %s.\n",flnm); continue; } fscanf(ip,"%hd%hd%hd%hd",&i,&np,&j,&m); seeknln(); if (i!=npt) { fprintf(stderr,"npt does not agree.\n"); fclose(ip); continue;} if (j!=0) seeknln(); if (m!=0) seeknln(); if (n<=0 || np+n-1>=mxp) { fprintf(stderr,"Not enough perm space to read perms from file."); fclose(ip); continue; } for (i=1;i<=np;i++) { if (readperm(pptr[i+n-1])==2) { fprintf(stderr,"Perm no %i is not a permutation.\n"); pno[i+n-1]=0; break; } pno[i+n-1]=1; seeknln(); } fclose(ip); } else if (strcmp(opt,"q")==0) {snl(); break;} else {printf("Invalid option.\n"); snl();} } exit(0); }
int32_t vsprintf(char *s, const char *fmt, va_list args) { char *str; for(str = s; *fmt != '\0'; ++fmt) { // Simply copy character to buffer if not a format specifier if(*fmt != '%') { *str++ = *fmt; continue; } uint8_t flags = 0; uint32_t width = 0; int32_t precision = -1; uint8_t length; char specifier = 0; // First come the flags. Get all flags specified ++fmt; for(bool i = false; i != true;) { switch(*fmt) { case '-': flags |= LEFTJUSTIFY; ++fmt; break; case '+': flags |= PLUS; ++fmt; break; case ' ': flags = (flags & PLUS) ? flags : flags|SPACE; ++fmt; break; case '#': flags |= SPECIAL; ++fmt; break; case '0': flags |= ZEROPAD; ++fmt; break; default: i = true; break; } } // Next up is width sub specifier if(*fmt == '*') { // Next arg is the width sub specifier width = va_arg(args, uint32_t); ++fmt; } else if(is_number(*fmt)) { // Convert the string into a number size_t num = num_digits(fmt); width = atoi(fmt); fmt += num; } else { // Otherwise there is no width, turn of ZEROPAD and LEFTJUSTIFY width = 0; flags &= ~(ZEROPAD | LEFTJUSTIFY); } if(*fmt == '.') { // A precision has been specified ++fmt; if(*fmt == '*') { // Next arg is precision value, set ZEROPAD flag precision = va_arg(args, uint32_t); ++fmt; } else if(is_number(*fmt)) { // Convert string to number, set ZEROPAD flag size_t num = num_digits(fmt); precision = atoi(fmt); fmt += num; } else { // Otherwise no precision has been specified precision = 0; } } // Determine length sub specifier if there is one switch(*fmt) { case 'h': // Check if next character is h ++fmt; if(*fmt == 'h') { length = _hh; ++fmt; } else { length = _h; } break; case 'l': // Check if next character is l ++fmt; if(*fmt == 'l') { length = _ll; ++fmt; } else { length = _l; } break; case 'j': ++fmt; length = _j; break; case 'z': ++fmt; length = _z; break; case 't': ++fmt; length = _t; break; default: break; } // Now we can check for the specifier character and do the deeds switch(*fmt) { case 'd': case 'i': { specifier = 'd'; int32_t num = va_arg(args, int32_t); bool negative = false; char str_num[32]; if(precision == 0 && num == 0) break; if(num < 0) { negative = true; num *= -1; } itoa2(num, str_num, 10, false); number(fmt_num, str_num, flags, width, precision, negative, specifier); size_t len = strlen(fmt_num); for(uint32_t i = 0; i < len; i++) *str++ = fmt_num[i]; break; } case 'u': { specifier = 'u'; uint32_t num = va_arg(args, uint32_t); char str_num[32]; if(precision == 0 && num == 0) break; itoa2(num, str_num, 10, false); number(fmt_num, str_num, flags, width, precision, false, specifier); size_t len = strlen(fmt_num); for(uint32_t i = 0; i < len; i++) *str++ = fmt_num[i]; break; } case 'o': { specifier = 'o'; int32_t num = va_arg(args, int32_t); bool negative = false; char str_num[32]; if(precision == 0 && num == 0) break; if(num < 0) { negative = true; num *= -1; } itoa2(num, str_num, 8, false); number(fmt_num, str_num, flags, width, precision, negative, specifier); size_t len = strlen(fmt_num); for(uint32_t i = 0; i < len; i++) *str++ = fmt_num[i]; break; } case 'x': specifier = 'x'; case 'X': { bool upcase = true; if(specifier == 'x') upcase = false; else specifier = 'X'; uint32_t num = va_arg(args, uint32_t); bool negative = false; char str_num[32]; if(precision == 0 && num == 0) break; itoa2(num, str_num, 16, upcase); number(fmt_num, str_num, flags, width, precision, negative, specifier); size_t len = strlen(fmt_num); for(uint32_t i = 0; i < len; i++) *str++ = fmt_num[i]; break; } case 'c': specifier = 'c'; case 's': { flags &= ~(PLUS | SPACE | SPECIAL | ZEROPAD); if(specifier == 'c') { uint32_t c_int = va_arg(args, uint32_t); char c[2]; c[0] = (char)c_int; c[1] = '\0'; precision = -1; number(fmt_num, c, flags, width, precision, false, specifier); } else { specifier = 's'; char *string = va_arg(args, char*); number(fmt_num, string, flags, width, precision, false, specifier); } size_t len = strlen(fmt_num); for(uint32_t i = 0; i < len; i++) *str++ = fmt_num[i]; break; } case 'p': { specifier = 'p'; uintptr_t num = va_arg(args, uintptr_t); char str_num[32]; itoa2(num, str_num, 16, false); number(fmt_num, str_num, flags, width, precision, false, specifier); size_t len = strlen(fmt_num); for(uint32_t i = 0; i < len; i++) *str++ = fmt_num[i]; break; } case 'n': { specifier = 'n'; int32_t *n = va_arg(args, int32_t*); *n = (int32_t)(str-s); break; } case 'f': { specifier = 'f'; double num = va_arg(args, double); char str_num[32]; uint32_t whole = (uint32_t)num; itoa2(whole, str_num, 10, false); size_t len = strlen(str_num); for(uint32_t i = 0; i < len; i++) *str++ = str_num[i]; uint32_t pow10 = 10; for(int32_t i = precision; i > 0; i--) { pow10 *= pow10; } double frac_part = num - (double)((uint32_t)num); uint32_t frac = (precision > 6) ? (uint32_t)(frac_part * (double)pow10) : (uint32_t)(frac_part * (double)1000000); if(frac == 0 && (flags & SPECIAL)) { *str++ = '.'; *str++ = '0'; } else { *str++ = '.'; memset(str_num, 32, 0); itoa2(frac, str_num, 10, false); number(fmt_num, str_num, flags, width, precision, false, specifier); len = strlen(fmt_num); for(uint32_t i = 0; i < len; i++) *str++ = fmt_num[i]; } break; } case '%': { specifier = '%'; char c[2]; c[0] = '%'; c[1] = '\0'; flags &= ~(PLUS | SPACE | SPECIAL | ZEROPAD); precision = -1; number(fmt_num, c, flags, width, precision, false, specifier); size_t len = strlen(fmt_num); for(uint32_t i = 0; i < len; i++) *str++ = fmt_num[i]; break; } default: break; } } *str = '\0'; return(str-s); }
int num_digits(int n) { if (n < 10) return 1; else return 1 + num_digits(n/10); }
/* Return the number of digits in integer n, n >= 0. */ int num_digits(int n) { n = n / 10; if (n){ return (1 + num_digits(n)); } return 1; }
int db_tags_from_files(int *files, int num_files, int **tags) { GHashTable *table = NULL; GHashTableIter iter; char *file_id_str = NULL; char *query = NULL; char or_outline[] = " OR file_id = "; char query_outline[] = "SELECT DISTINCT tag_id FROM all_tables WHERE file_id = "; const int QUERY_LENGTH_CAP = 1024; gpointer key = NULL; gpointer value = NULL; int file_id = 0; int hash_table_size = 0; int i = 0; int initial_query_length = 0; int num_digits_in_id = 0; int query_outline_length = 0; int sql_length_avail = 0; int sql_max_length = 0; int written = 0; /* number of characters written */ sqlite3 *conn = NULL; DEBUG(ENTRY); assert(files != NULL); assert(num_files > 0); assert(*tags == NULL); DEBUG("Retrieving tags from %d files", num_files); /* prepare query/table */ query_outline_length = strlen(query_outline); table = g_hash_table_new(NULL, NULL); assert(table != NULL); /* connect to database */ conn = db_connect(); assert(conn != NULL); /* calculate maximum query length */ sql_max_length = sqlite3_limit(conn, SQLITE_LIMIT_SQL_LENGTH, -1); assert(sql_max_length > query_outline_length); DEBUG("Database reports maximum query length of %d", sql_max_length); sql_max_length = sql_max_length < QUERY_LENGTH_CAP ? sql_max_length : QUERY_LENGTH_CAP; assert(sql_max_length > query_outline_length); DEBUG("Maximum query length has been set to %d", sql_max_length); /* for each file id */ for(i = 0; i < num_files; i++) { num_digits_in_id = num_digits(files[i]); file_id = files[i]; /* start new query */ if(sql_length_avail <= 0) { DEBUG("Starting new query"); sql_length_avail = sql_max_length - query_outline_length; assert(sql_length_avail > 0); query = calloc(sql_max_length + 1, sizeof(*query)); assert(query != NULL); initial_query_length = query_outline_length + num_digits_in_id; written = snprintf(query, initial_query_length + 1, "%s%d", query_outline, file_id); assert(written == query_outline_length + num_digits_in_id); sql_length_avail -= num_digits_in_id; continue; } /* test if query can contain next file id */ sql_length_avail -= strlen(or_outline) + num_digits(files[i]); /* if the query is at maximum length */ if(sql_length_avail <= 0) { DEBUG("Query has reached maximum length..."); i--; /* leave it for next iteration */ db_insert_query_results_into_hashtable(conn, query, table); free_single_ptr((void *)&query); continue; } /* append file ID to query */ file_id_str = calloc(num_digits_in_id + 1, sizeof(*file_id_str)); snprintf(file_id_str, num_digits_in_id + 1, "%d", file_id); strncat(strncat(query, or_outline, strlen(or_outline)), file_id_str, strlen(file_id_str)); free_single_ptr((void *)&file_id_str); } db_insert_query_results_into_hashtable(conn, query, table); free_single_ptr((void *)&query); db_disconnect(conn); /* copy results of set to array */ hash_table_size = g_hash_table_size(table); *tags = malloc(hash_table_size * sizeof(**tags)); g_hash_table_iter_init(&iter, table); i = 0; while(g_hash_table_iter_next(&iter, &key, &value)) { (*tags)[i++] = (unsigned long)value; } g_hash_table_destroy(table); DEBUG("Returning array of %d elements", hash_table_size); DEBUG(EXIT); return hash_table_size; } /* db_tags_from_files */
/* * Returns the number of digits in a non negative integer. * */ unsigned num_digits(unsigned long n) { if (n <= 9) return 1; return 1 + num_digits(n / 10); }