static int benc_decode_full(char *in, struct benc **b) { unsigned char first = *in++; switch (first) { case 'i': return 1 + read_number(b, in, 'e', 0); case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': return 1 + read_string(b, in, ':', first - '0'); case 'l': return 1 + read_list(b, in, 'e'); case 'd': return 1 + read_dict(b, in, 'e'); default: fprintf(stderr, "%s: stream possibly corrupt\n", "benc_decode_full"); return -1; } }
VALUE read_complex_type(unsigned char **pData, int arity, VALUE forceToEncoding) { VALUE type = read_any_raw(pData, forceToEncoding); ID id = SYM2ID(type); if(id == rb_intern("nil")) { return Qnil; } else if(id == rb_intern("true")) { return Qtrue; } else if(id == rb_intern("false")) { return Qfalse; } else if(id == rb_intern("time")) { VALUE megasecs = read_any_raw(pData, forceToEncoding); VALUE msecs = rb_funcall(megasecs, rb_intern("*"), 1, INT2NUM(1000000)); VALUE secs = read_any_raw(pData, forceToEncoding); VALUE microsecs = read_any_raw(pData, forceToEncoding); VALUE stamp = rb_funcall(msecs, rb_intern("+"), 1, secs); return rb_funcall(rb_cTime, rb_intern("at"), 2, stamp, microsecs); } else if(id == rb_intern("regex")) { VALUE source = read_any_raw(pData, forceToEncoding); VALUE opts = read_any_raw(pData, forceToEncoding); int flags = 0; if(rb_ary_includes(opts, ID2SYM(rb_intern("caseless")))) flags = flags | 1; if(rb_ary_includes(opts, ID2SYM(rb_intern("extended")))) flags = flags | 2; if(rb_ary_includes(opts, ID2SYM(rb_intern("multiline")))) flags = flags | 4; return rb_funcall(rb_cRegexp, rb_intern("new"), 2, source, INT2NUM(flags)); } else if(id == rb_intern("dict")) { return read_dict(pData, forceToEncoding); } else { return Qnil; } }
AST_expr* readASTExpr(BufferedReader* reader) { uint8_t type = reader->readByte(); if (VERBOSITY("parsing") >= 2) printf("type = %d\n", type); if (type == 0) return NULL; uint8_t checkbyte = reader->readByte(); assert(checkbyte == 0xae); switch (type) { case AST_TYPE::Attribute: return read_attribute(reader); case AST_TYPE::BinOp: return read_binop(reader); case AST_TYPE::BoolOp: return read_boolop(reader); case AST_TYPE::Call: return read_call(reader); case AST_TYPE::Compare: return read_compare(reader); case AST_TYPE::Dict: return read_dict(reader); case AST_TYPE::DictComp: return read_dictcomp(reader); case AST_TYPE::IfExp: return read_ifexp(reader); case AST_TYPE::Index: return read_index(reader); case AST_TYPE::Lambda: return read_lambda(reader); case AST_TYPE::List: return read_list(reader); case AST_TYPE::ListComp: return read_listcomp(reader); case AST_TYPE::Name: return read_name(reader); case AST_TYPE::Num: return read_num(reader); case AST_TYPE::Repr: return read_repr(reader); case AST_TYPE::Slice: return read_slice(reader); case AST_TYPE::Str: return read_str(reader); case AST_TYPE::Subscript: return read_subscript(reader); case AST_TYPE::Tuple: return read_tuple(reader); case AST_TYPE::UnaryOp: return read_unaryop(reader); default: fprintf(stderr, "Unknown expr node type (parser.cpp:" STRINGIFY(__LINE__) "): %d\n", type); abort(); break; } }
int main(int argc, char *argv[]) { struct dict_radix *dict = new_dict_radix(); allocate_nodes(dict, 200000, 100000, 10000); read_dict(dict, NULL); print_sizes(dict); print_stats(dict); return 0; }
main(int argc,char **argv) { int len; int set[20], currset[20]; char vals[20]; int i; if( argc < 2 ) { printf("permute <word>\n"); exit(10); } len = strlen(argv[1]); if( len > 19 ) /* really 10 or 11 is getting impractically big! 40 million possibilities at 11! */ { printf("String too long\n"); exit(10); } /* find all the n-digit numbers that have each digit mentioned only once. */ /* all the numbers including only the numbers 1-n; */ printf("Reading %s...\n", DICT); read_dict(); printf("Done, wasn't that quick?\n"); strcpy(vals,argv[1]); /* the initial set */ for(i=0;i<len;i++) { set[i] = i; } printf("Searching... (Note: if you repeat the same letter more than once in a word, expect multiple matches!)\n"); permute(set, len, vals, 0, currset, 0); printf("Found %d matches in the dictionary!\n", match_count); }
/* Load the data files. Returns 0 on success, -1 if couldn't read the dictionary. */ static int load_data(struct dict_radix **dictp) { clock_t t1, t2; if(hspell_debug){ fprintf(stderr,"Loading data files... "); t1=clock(); } *dictp = new_dict_radix(); if(!read_dict(*dictp, hspell_dictionary)){ delete_dict_radix(*dictp); return -1; } if(hspell_debug){ t2=clock(); fprintf(stderr,"done (%d ms).\n", (int)((t2-t1)/(CLOCKS_PER_SEC/1000))); } return 0; }
int main() { char a[LETTER_NUM]; cordinate_t c; new_char_set(a); printf("%s\n", a); perm_start = malloc(sizeof(permutes)); perm_head = perm_start; permute (a, 0, LETTER_NUM-1); read_dict(); possible_words(); init_board(); init_players(); c.x = 0; c.y = 0; take_word(c, RIGHT, "fosfalo"); calculate_word_score(c, RIGHT, HUMAN); print_board(); printf("score: %d\n", players[HUMAN].score); free_data(); return 0; }
int main(int argc, char *argv[]) { int r = EXIT_OK; int i, ii; int m; time_t t_total; #if defined(__EMX__) _response(&argc,&argv); _wildcard(&argc,&argv); #endif if (argv[0]) argv0 = argv[0]; align_mem(); (void) my_clock(); printf("\nLZO real-time data compression library (v%s, %s).\n", LZO_VERSION_STRING, LZO_VERSION_DATE); printf("Copyright (C) 1996-2002 Markus Franz Xaver Johannes Oberhumer\n\n"); if (lzo_init() != LZO_E_OK) { printf("lzo_init() failed !!!\n"); exit(EXIT_LZO_INIT); } if (argc < 2) usage(argv0,-1,0); i = get_options(argc,argv); if (methods_n == 0) add_method(default_method); if (methods_n > 1 && opt_read_from_stdin) { printf("%s: cannot use multiple methods and '-@'\n", argv0); exit(EXIT_USAGE); } if (opt_block_size < 16) opt_block_size = 16; if (opt_block_size > MAX_BLOCK_SIZE) opt_block_size = MAX_BLOCK_SIZE; dict_len = 0; #ifndef USE_DICT opt_dict = 0; #else if (opt_dict) { opt_optimize_compressed_data = 0; if (opt_dictionary_file) { read_dict(opt_dictionary_file); if (opt_max_dict_len > 0 && dict_len > (lzo_uint) opt_max_dict_len) dict_len = opt_max_dict_len; if (dict_len > 0) printf("Using dictionary '%s', %ld bytes, ID 0x%08lx.\n", opt_dictionary_file, (long) dict_len, (long) dict_adler32); } if (dict_len <= 0) { init_default_dict(); if (opt_max_dict_len > 0 && dict_len > (lzo_uint) opt_max_dict_len) dict_len = opt_max_dict_len; printf("Using default dictionary, %ld bytes, ID 0x%08lx.\n", (long) dict_len, (long) dict_adler32); } if (opt_max_dict_len == -1) printf("Dictionary size will be adjusted to file size.\n"); else if (opt_max_dict_len <= 0) printf("Dictionary size will be adjusted to file size.\n"); } #endif t_total = time(NULL); (void) my_clock(); ii = i; for (m = 0; m < methods_n && r == EXIT_OK; m++) { int method = methods[m]; i = ii; if (i >= argc && opt_calgary_corpus_path == NULL && !opt_read_from_stdin) usage(argv0,-1,0); if (m == 0 && opt_verbose >= 1) printf("%lu block-size\n\n", (long) opt_block_size); if (!info(method,NULL)) info(method,stdout); #ifdef USE_CORPUS if (opt_calgary_corpus_path != NULL) r = do_corpus(calgary_corpus,method,opt_calgary_corpus_path, opt_c_loops,opt_d_loops); else #endif { for ( ; i < argc && r == EXIT_OK; i++) { r = do_file(method,argv[i],opt_c_loops,opt_d_loops,NULL,NULL); if (r == EXIT_FILE) /* ignore file errors */ r = EXIT_OK; } if (opt_read_from_stdin) { char buf[512], *p; while (r == EXIT_OK && fgets(buf,sizeof(buf)-1,stdin) != NULL) { buf[sizeof(buf)-1] = 0; p = buf + strlen(buf); while (p > buf && is_space(p[-1])) *--p = 0; p = buf; while (*p && is_space(*p)) p++; if (*p) r = do_file(method,p,opt_c_loops,opt_d_loops,NULL,NULL); if (r == EXIT_FILE) /* ignore file errors */ r = EXIT_OK; } opt_read_from_stdin = 0; } } } t_total = time(NULL) - t_total; if (opt_totals) print_totals(); if (opt_execution_time || (methods_n > 1 && opt_verbose >= 1)) printf("\n%s: execution time: %lu seconds\n", argv0, (long) t_total); if (r != EXIT_OK) printf("\n%s: exit code: %d\n", argv0, r); return r; }
int main(int argc,char **argv) { int len; int set[20], currset[20]; char vals[40]; struct ast_hashtab_iter *it; char *sptr; int i; int matched =0; int unmatched = 0; int wronglen = 0; if( argc < 3 ) { printf("twoword <scrambleOfTwoWords> <longestwordlen>\n"); exit(10); } /* find all the n-digit numbers that have each digit mentioned only once. */ /* all the numbers including only the numbers 1-n; */ printf("Reading %s...\n", DICT); read_dict(); printf("Done, wasn't that quick?\n"); strcpy(vals,argv[1]); len = atoi(argv[2]); /* the initial set */ for(i=0;i<len;i++) { set[i] = i; } printf("Searching... (Note: if you repeat the same letter more than once in a word, expect multiple matches!)\n"); it = ast_hashtab_start_traversal(dict); while( (sptr = ast_hashtab_next(it)) ) { char buf3[40]; char *p; int tlen = strlen(sptr); int i; if(tlen != len) { wronglen++; continue; // skip all the words not exactly "len" chars long! } strcpy(buf3, vals); for(i=0; i<tlen; i++) { char y; y=sptr[i]; p = strchr(buf3,y); if(p) { rmcharfromset(buf3,y); } else { break; // this ain't the word } } if( p ) { char buf4[200]; int len7; printf("Match: Word=%s remainder=%s\n", sptr, buf3); /* the initial set */ len7 = strlen(buf3); for(i=0;i<len7;i++) { set[i] = i; } permute(set, len7, buf3, 0, currset, 0); matched++; } else { unmatched++; } } printf ("Matched: %d\nUnmatched: %d\nWrong Length: %d\n", matched, unmatched, wronglen); }
int main (int argc, char **argv) { heapelement_t **CDheap=NULL; hashelement_t **CDhash=NULL; phnhashelement_t **CIhash=NULL; dicthashelement_t **dicthash=NULL; int32 cilistsize=0, cdheapsize=0, threshold, tph_list_given, ncd; char *phnlist, *incimdef, *triphnlist, *incdmdef; char *lsnfile, *dictfn, *fillerdictfn, **CIlist=NULL; char *cimdeffn, *alltphnmdeffn, *untiedmdeffn, *countfn; parse_cmd_ln(argc,argv); /* Test all flags before beginning */ cimdeffn = (char *)cmd_ln_access("-ocimdef"); alltphnmdeffn = (char *)cmd_ln_access("-oalltphnmdef"); untiedmdeffn = (char *)cmd_ln_access("-ountiedmdef"); countfn = (char *)cmd_ln_access("-ocountfn"); if (cimdeffn) E_INFO("Will write CI mdef file %s\n",cimdeffn); if (alltphnmdeffn) E_INFO("Will write alltriphone mdef file %s\n",alltphnmdeffn); if (untiedmdeffn) E_INFO("Will write untied mdef file %s\n",untiedmdeffn); if (countfn) E_INFO("Will write triphone counts file %s\n",countfn); if (!cimdeffn && !alltphnmdeffn && !untiedmdeffn && !countfn) E_FATAL("No output mdef files or count files specified!\n"); dictfn = (char *)cmd_ln_access("-dictfn"); fillerdictfn = (char *)cmd_ln_access("-fdictfn"); lsnfile = (char*)cmd_ln_access("-lsnfn"); if ((untiedmdeffn || countfn) && (!lsnfile || !dictfn)) { E_WARN("Either dictionary or transcript file not given!\n"); if (untiedmdeffn) E_WARN("Untied mdef will not be made\n"); if (countfn) E_WARN("Phone counts will not be generated\n"); untiedmdeffn = countfn = NULL; } phnlist = (char *)cmd_ln_access("-phnlstfn"); triphnlist = (char *)cmd_ln_access("-triphnlstfn"); incimdef = (char *)cmd_ln_access("-inCImdef"); incdmdef = (char *)cmd_ln_access("-inCDmdef"); if (!incdmdef && !incimdef && !phnlist && !triphnlist) E_FATAL("No input mdefs or phone list given\n"); if (triphnlist) { if (phnlist) E_WARN("Both -triphnlist %s and -phnlist given.\n",triphnlist); E_WARN("Ignoring -phnlist %s\n",phnlist); phnlist = triphnlist; } tph_list_given = (triphnlist || incdmdef) ? 1 : 0; if (incdmdef) { if (incimdef || phnlist){ E_WARN("Using only input CD mdef %s!\n",incdmdef); E_WARN("Using only triphones from input CD mdef %s!\n",incdmdef); if (incimdef) E_WARN("CImdef %s will be ignored\n",incimdef); if (phnlist) E_WARN("phonelist %s will be ignored\n",phnlist); incimdef = phnlist = NULL; } make_ci_list_cd_hash_frm_mdef(incdmdef,&CIlist,&cilistsize, &CDhash,&ncd); } else{ if (phnlist) make_ci_list_cd_hash_frm_phnlist(phnlist,&CIlist, &cilistsize,&CDhash,&ncd); if (incimdef) { if (CIlist) ckd_free_2d((void**)CIlist); make_ci_list_frm_mdef(incimdef,&CIlist,&cilistsize); } } if (cimdeffn) make_mdef_from_list(cimdeffn,CIlist,cilistsize,NULL,0,argv[0]); if (!tph_list_given && !cimdeffn) { read_dict(dictfn, fillerdictfn, &dicthash); if (CDhash) freehash(CDhash); make_dict_triphone_list (dicthash, &CDhash); } if (alltphnmdeffn){ threshold = -1; make_CD_heap(CDhash,threshold,&CDheap,&cdheapsize); make_mdef_from_list(alltphnmdeffn,CIlist,cilistsize, CDheap,cdheapsize,argv[0]); } if (countfn || untiedmdeffn) count_triphones(lsnfile, dicthash, CDhash, &CIhash); if (countfn){ print_counts(countfn,CIhash,CDhash); } if (untiedmdeffn){ threshold = find_threshold(CDhash); make_CD_heap(CDhash,threshold,&CDheap,&cdheapsize); make_mdef_from_list(untiedmdeffn,CIlist,cilistsize, CDheap,cdheapsize,argv[0]); } return 0; }
/** * @brief Program entry point. * @param argc The argument count * @param argv The argument vector * @return EXIT_SUCCESS, EXIT_PARITY_ERROR, EXIT_MULTIPLE_ERRORS **/ int main(int argc, char *argv[]) { uint8_t client_count = 0; int shmfd; /* setup signal handlers */ const int signals[] = {SIGINT, SIGTERM}; struct sigaction s; s.sa_handler = signal_handler; s.sa_flags = 0; if(sigfillset(&s.sa_mask) < 0) { bail_out(EXIT_FAILURE, "sigfillset"); } for(int i = 0; i < COUNT_OF(signals); i++) { if (sigaction(signals[i], &s, NULL) < 0) { bail_out(EXIT_FAILURE, "sigaction"); } } /* Handle arguments */ if (argc > 0) { progname = argv[0]; } if (argc > 2) { fprintf(stderr, "USAGE: %s [input_file]", progname); exit(EXIT_FAILURE); } switch (argc) { /* Handle dict input from stdin */ case 1: printf("Please input your dictionary: \n"); in_stream = stdin; read_dict(); break; /* Handle dict input from file */ case 2: if ((in_stream = fopen(argv[1], "r")) == NULL) { bail_out(EXIT_FAILURE, "Invalid File"); } read_dict(); break; default: fprintf(stderr, "USAGE: %s [input_file]", progname); exit(EXIT_FAILURE); break; } /* Create a new Shared Memory Segment (shm_open) */ shmfd = shm_open(SHM_NAME, O_RDWR | O_CREAT | O_EXCL, PERMISSION); if (shmfd == (-1)) { bail_out(EXIT_FAILURE, "shm_open failed"); } /* Truncate a file to a specified length extend (set size) */ if (ftruncate(shmfd, sizeof *shared) == -1) { (void) close(shmfd); bail_out(EXIT_FAILURE, "ftruncate failed"); } /* Map shared memory object */ shared = mmap(NULL, sizeof *shared, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0); if (shared == MAP_FAILED) { (void) close(shmfd); bail_out(EXIT_FAILURE, "MMAP failed"); } if (close(shmfd) == -1) { bail_out(EXIT_FAILURE, "close(shmfd) failed"); } shared->sc_terminate = -1; /* Create new named semaphores */ s_server = sem_open(S_SERVER, O_CREAT | O_EXCL, PERMISSION, 0); s_client = sem_open(S_CLIENT, O_CREAT | O_EXCL, PERMISSION, 1); s_return = sem_open(S_RETURN, O_CREAT | O_EXCL, PERMISSION, 0); if(s_server == SEM_FAILED || s_client == SEM_FAILED || s_return == SEM_FAILED) { bail_out(EXIT_FAILURE, "sem_open(3) failed"); } sem_set = 1; struct ClientList *el_pre = NULL; struct ClientList *el_cur = NULL; /* Keep server open until it gets killed. */ while (!want_quit) { /* Critical section entry. */ /* Wait until server is allowed to access SHM. */ if (sem_wait(s_server) == -1) { if(errno == EINTR) continue; bail_out(EXIT_FAILURE, "sem_wait(3) failed"); } /* Setup data for existing client. */ if (shared->s_id >= 0) { el_cur = client_list; while (el_cur != NULL && el_cur->server_id != shared->s_id) { printf("elpre\n"); el_pre = el_cur; printf("elcur\n"); el_cur = el_cur->next; } if (el_cur == NULL) { bail_out(EXIT_FAILURE, "Client does not exist"); } el_cur->guess = shared->c_guess; } /* Setup new client to list. */ if (shared->s_id == -1) { /* Allocate element and set to zero. */ el_cur = (struct ClientList *) calloc (1, sizeof(struct ClientList)); if (el_cur == NULL) { bail_out(EXIT_FAILURE, "calloc(3) failed"); } /* Assign unique ID based on client count. */ el_cur->server_id = client_count++; el_cur->game_count = 0; /* Add the current element to our list. */ el_cur->next = client_list; client_list = el_cur; } /* Check if client has terminated. */ if (shared->sc_terminate >= 1) { DEBUG("Terminating client...\n"); /* Remove client from list. */ if (client_list == el_cur) { client_list = el_cur->next; } else { el_pre->next = el_cur->next; } free(el_cur); /* Free allocated resources. */ /* Reset sc_terminate. */ shared->sc_terminate = -1; if (sem_post(s_client) == -1) { bail_out(EXIT_FAILURE, "sem_post(3) failed"); } /* Skip the rest of the game as we have nothing to do here */ continue; } /* Check game status of client. */ if (shared->status_id == CreateGame) { /* Start a new game. */ DEBUG("Setting up game for client %d\n", shared->s_id); create_game(el_cur); } if (shared->status_id == Running) { /* Check guess. */ DEBUG("Checking guess of client %d\n", shared->s_id); check_guess(el_cur); } /* Write server answer back into SHM. */ shared->s_id = el_cur->server_id; shared->status_id = el_cur->status_id; shared->s_errors = el_cur->errors; strncpy(shared->s_word, el_cur->client_word, MAX_DATA); /* Let the client know that there is an answer. */ if (sem_post(s_return) == -1) { bail_out(EXIT_FAILURE, "sem_post(3) failed"); } /* critical section end. */ } /* Free stuff */ free(strings); }