void print_dict_aux (struct Dict *d) { if (d == NULL) { printf("}"); return; } if (d->value != NULL || d->key != NULL) { if (d->key == NULL) { printf("(null)"); } else { switch (d->key_type) { case 'l': print_list(d->key); break; case 'd': print_dict(d->key); break; case 's': print_str(d->key); break; case 'i': print_int(d->key); } } printf(":"); if (d->value == NULL) { printf("(null)"); } else { switch (d->value_type) { case 'l': print_list(d->value); break; case 'd': print_dict(d->value); break; case 's': print_str(d->value); break; case 'i': print_int(d->value); } } } if (d->next != NULL) { printf(", "); } print_dict_aux(d->next); }
void print_list_aux(struct List *l) { if (l == NULL) { return; } switch (l->type) { case 'l': print_list(l->cnt); break; case 'd': print_dict(l->cnt); break; case 's': print_str(l->cnt); break; case 'i': print_int(l->cnt); } if (l->next != NULL) { printf(", "); } print_list_aux(l->next); }
void print_dict(elem* head) { if(head==NULL) printf("NULL\n"); else { printf("%s:%i->", head->key, head->data); print_dict(head->next); } }
int main( int argc, char *argv[] ) { int NTHREADS = 4; pthread_t pth[NTHREADS]; void *wd; void *dc[4]; int i, j; dict_t *d = NULL; FILE *infile = stdin; if (argc >= 2) { infile = fopen (argv[1],"r"); } if( !infile ) { printf("Unable to open %s\n",argv[1]); exit( EXIT_FAILURE ); } for(i = 0; i<NTHREADS; i++) { pthread_create(&pth[i], NULL, words, infile); pthread_join(pth[i], &wd); dc[i] = wd; } for(j = 0; j<NTHREADS; j++){ print_dict( dc[j] ); } fclose( infile ); }
int main( int argc, char *argv[] ) { pthread_t threads[numThreads]; if(pthread_mutex_init(&lock,NULL)!=0) { printf("lock failed"); } d = NULL; FILE *infile = stdin; if (argc >= 2) { infile = fopen (argv[1],"r"); } if( !infile ) { printf("Unable to open %s\n",argv[1]); exit( EXIT_FAILURE ); } //creating threads int k; for( k = 0; k <= numThreads; k++ ){ pthread_create( &threads[k], NULL,&words, infile ); } //block until all threads complete int j; for( j = 0; j <= numThreads; j++ ){ pthread_join( threads[j], NULL ); } print_dict(d); pthread_mutex_destroy(&lock); fclose( infile ); return EXIT_SUCCESS; }
int main() { struct keys *dict = build_keys_dict(); add_word(dict, "Hello"); add_word(dict, "World!"); add_word(dict, "World!"); add_word(dict, "World!"); print_dict(dict); }
static void test_separators(const AVDictionary *m, const char pair, const char val) { AVDictionary *dict = NULL; char pairs[] = {pair , '\0'}; char vals[] = {val, '\0'}; char *buffer = NULL; av_dict_copy(&dict, m, 0); print_dict(dict); av_dict_get_string(dict, &buffer, val, pair); printf("%s\n", buffer); av_dict_free(&dict); av_dict_parse_string(&dict, buffer, vals, pairs, 0); av_freep(&buffer); print_dict(dict); av_dict_free(&dict); }
static PyObject *assert_empty_dict(PyObject *dict, char const *src) { if (PyDict_Size(dict) != 0) { debug("Warning: dict for %s contained unused keys:", src); print_dict(dict); } Py_DECREF(dict); Py_RETURN_NONE; }
int main( int argc, char *argv[] ) { if(pthread_mutex_init(&m_tex,NULL)!=0){ printf("Mutex lock not created...\n"); return 1; } d = NULL; FILE *infile = stdin; if (argc >= 2) { infile = fopen(argv[1],"r"); } if( !infile ) { printf("Unable to open %s\n",argv[1]); exit( EXIT_FAILURE ); } //int i=0; //while(i<4){ //i++; //} pthread_t thr_1, thr_2, thr_3, thr_4, thr_5, thr_6; pthread_create(&thr_1, NULL, &dic_words, infile); pthread_join(thr_1, NULL); pthread_create(&thr_2, NULL, &dic_words, infile); pthread_join(thr_2, NULL); pthread_create(&thr_3, NULL, &dic_words, infile); pthread_join(thr_3, NULL); pthread_create(&thr_4, NULL, &dic_words, infile); pthread_join(thr_4, NULL); pthread_create(&thr_5, NULL, &dic_words, infile); pthread_join(thr_5, NULL); pthread_create(&thr_6, NULL, &dic_words, infile); pthread_join(thr_6, NULL); print_dict(d); fclose( infile ); }
int main( int argc, char *argv[] ) { dict_t *d = NULL; FILE *infile = stdin; if (argc >= 2) { infile = fopen (argv[1],"r"); } if( !infile ) { printf("Unable to open %s\n",argv[1]); exit( EXIT_FAILURE ); } d = words( infile ); print_dict( d ); fclose( infile ); }
void print_bcont(struct Bcont *b) { switch (b->type) { case 'l': print_list(b->cnt); break; case 'd': print_dict(b->cnt); break; case 's': print_str(b->cnt); break; case 'i': print_int(b->cnt); } }
int main( int argc, char *argv[] ) { pthread_t thread_ID[NUM_THREADS]; void *status; pthread_mutex_init(&mutexdict, NULL); FILE *infile = stdin; if (argc >= 2) { infile = fopen (argv[1],"r"); } if( !infile ) { printf("Unable to open %s\n",argv[1]); exit( EXIT_FAILURE ); } clock_t begin, end; double time_spent; dict_t *wd = NULL; char wordbuf[MAXWORD]; thread_data *TD; TD->infile=infile; TD->dict=wd; TD->wordbuf=wordbuf; int i; for(i=0;i<NUM_THREADS;i++){ pthread_create(&thread_ID[i],NULL,worker_thread,(void*) TD); } for(i=0;i<NUM_THREADS;i++){ pthread_join(thread_ID[i],&status); } print_dict( TD->dict ); int sta=pthread_mutex_destroy(&mutexdict); printf("done %d\n",sta); pthread_mutex_destroy(&mutexword); fclose( infile ); }
int main( int argc, char *argv[] ) { if(pthread_mutex_init(&mtex,NULL)!=0){ printf("OOPS ,UNABLE TO CREATE THE MUTEX LOCK\n"); return 1; } d = NULL; //another way of creating multiple threads //pthread_t thread1,thread2,thread3,thread4; FILE *infile = stdin; if (argc >= 2) { infile = fopen(argv[1],"r"); } if( !infile ) { printf("Unable to open %s\n",argv[1]); exit( EXIT_FAILURE ); } //d = words( infile ); pthread_t n, y, a, w; pthread_create(&n,NULL,words,infile); pthread_join(n, NULL); pthread_create(&y,NULL,&words,infile); pthread_join(y, NULL); pthread_create(&a,NULL,&words,infile); pthread_join(a, NULL); //void * word(void* args) pthread_create(&w,NULL,&words,infile); pthread_join(w, NULL); print_dict(d); pthread_mutex_destroy(&mtex); fclose( infile ); }
int main( int argc, char *argv[] ) { d = NULL; infile = stdin; if (argc >= 2) { infile = fopen (argv[1],"r"); } if( !infile ) { printf("Unable to open %s\n",argv[1]); exit( EXIT_FAILURE ); } words(); print_dict(d); fclose( infile ); return EXIT_SUCCESS; }
int main( int argc, char *argv[] ) { if(pthread_mutex_init(&lock,NULL)!=0){ printf("Mutex initialization failed!!\n"); return 1; } d= NULL; FILE *infile = stdin; if (argc >= 2) { infile = fopen (argv[1],"r"); } if( !infile ) { printf("Unable to open %s\n",argv[1]); exit( EXIT_FAILURE ); } // d = words( infile ); pthread_t t1,t2,t3,t4; // this is our thread identifier pthread_create(&t1,NULL,&words,infile); pthread_join(t1,NULL); pthread_create(&t2,NULL,&words,infile); pthread_join(t2,NULL); pthread_create(&t3,NULL,&words,infile); pthread_join(t3,NULL); pthread_create(&t4,NULL,&words,infile); pthread_join(t4,NULL); print_dict( d ); pthread_mutex_destroy(&lock); fclose( infile ); }
int main( int argc, char *argv[] ) { pthread_t threads[NUM_THREADS]; int i; int res; pthread_attr_t threadattr; void *status; if (argc >= 2) { infile = fopen (argv[1],"r"); } if( !infile ) { printf("Unable to open %s\n",argv[1]); exit(EXIT_FAILURE); } //Initialize mutexes pthread_mutex_init(&dictMutex, NULL); pthread_mutex_init(&fileMutex, NULL); //Initialize attribute pthread_attr_init(&threadattr); pthread_attr_setdetachstate(&threadattr, PTHREAD_CREATE_JOINABLE); for (i=0; i<NUM_THREADS; i++) { res = pthread_create(&threads[i], &threadattr, words, NULL); if (res) {printf("Failed to create thread.\n");} } for (i=0; i<NUM_THREADS; i++) { pthread_join(threads[i], &status); } print_dict(); fclose(infile); //clean up pthread_attr_destroy(&threadattr); pthread_mutex_destroy(&dictMutex); pthread_mutex_destroy(&fileMutex); pthread_exit(NULL); }
int main( int argc, char *argv[] ) { int i = 0; int err; dict_t *d = NULL; FILE *infile = stdin; if (argc >= 2) { infile = fopen (argv[1],"r"); } if( !infile ) { printf("Unable to open %s\n",argv[1]); exit( EXIT_FAILURE ); } d = words( infile ); //print_dict( d ); fclose( infile ); while(i < 4) { err = pthread_create(&(tid[i]), NULL, &doSomeThing, NULL); if (err != 0) printf("\ncan't create thread :[%s]", strerror(err)); else i++; print_dict(d); } pthread_join(tid[1], NULL); pthread_join(tid[2], NULL); pthread_join(tid[3], NULL); pthread_join(tid[4], NULL); pthread_mutex_destroy(&lock); //sleep(4); return 0; }
int main(int argc, char *argv[]) { infile = stdin; outfile = stdout; // file stuff if (argc >= 2) infile = fopen (argv[1],"r"); if(!infile) { printf("Unable to open %s\n",argv[1]); exit(EXIT_FAILURE); } words(); // actual program logic print_dict(); // print results fclose(infile); fclose(outfile); }
int main( int argc, char *argv[] ) { dict_t *d = NULL; FILE *infile = stdin; if (argc >= 2) { infile = fopen (argv[1],"r"); } if( !infile ) { printf("Unable to open %s\n",argv[1]); exit( EXIT_FAILURE ); } d = words( infile ); print_dict( d ); pthread_t threads[5]; int i, j = 0; for( i = 0; i < 5; i++ ) pthread_create( &threads[i], NULL, words, &infile ); for( i = 0; i < 5; i++ ) pthread_join( threads[i], NULL ); fclose( infile ); }
int main(int argc, char const * const argv[]) { getdns_return_t r; getdns_dict *rr_dict; getdns_bindata *dns_name; getdns_bindata address = { 4, "\xb9\x31\x8d\x25" }; getdns_bindata fourth = { 11, "last string" }; size_t length; char *str; uint8_t *wire, *prev_wire; size_t wire_len; getdns_list *rr_list; FILE *in; uint8_t wire_buf[8200]; size_t i; size_t uavailable; int available; char str_buf[10000]; int str_len = sizeof(str_buf); /* Convert string to rr_dict */ if ((r = getdns_str2rr_dict( "some.domain.tld. 60 IN TXT \"first string\" second \"and third\"", &rr_dict, NULL, 3600))) FAIL_r("getdns_str2rr_dict"); /* Add a fourth text element. */ if ((r = getdns_dict_set_bindata(rr_dict, "/rdata/txt_strings/-", &fourth))) FAIL_r("getdns_list_set_bindata"); print_dict(rr_dict); /* Convert to wireformat from rdata_raw. * Added fourth list element should NOT show. */ wire = NULL; if ((r = getdns_rr_dict2wire(rr_dict, &wire, &wire_len))) FAIL_r("getdns_rr_dict2wire"); print_wire(wire, wire_len); free(wire); /* Convert to wireformat from parsing rdata fields. * Added fourth list element should show. */ if ((r = getdns_dict_remove_name(rr_dict, "/rdata/rdata_raw"))) FAIL_r("getdns_dict_remove_name"); printf("\nremoved \"/rdata/rdata_raw\":\n\n"); if ((r = getdns_rr_dict2wire(rr_dict, &wire, &wire_len))) FAIL_r("getdns_rr_dict2wire"); print_wire(wire, wire_len); free(wire); /* Remove second and third string elements and show text format. */ if ((r = getdns_dict_remove_name(rr_dict, "/rdata/txt_strings/1"))) FAIL_r("getdns_dict_remove_name"); if ((r = getdns_dict_remove_name(rr_dict, "/rdata/txt_strings/1"))) FAIL_r("getdns_dict_remove_name"); if ((r = getdns_rr_dict2str(rr_dict, &str))) FAIL_r("getdns_rr_dict2str"); printf("\n%s", str); free(str); /* Remove all string elements and show text format. */ if ((r = getdns_dict_remove_name(rr_dict, "/rdata/txt_strings/0"))) FAIL_r("getdns_dict_remove_name"); if ((r = getdns_dict_remove_name(rr_dict, "/rdata/txt_strings/0"))) FAIL_r("getdns_dict_remove_name"); if ((r = getdns_rr_dict2str(rr_dict, &str))) FAIL_r("getdns_rr_dict2str"); printf("%s", str); free(str); getdns_dict_destroy(rr_dict); /* Construct rr_dict and convert to string */ if (!(rr_dict = getdns_dict_create())) FAIL("getdns_dict_create returned NULL"); if ((r = getdns_convert_fqdn_to_dns_name("www.getdnsapi.net", &dns_name))) FAIL_r("getdns_convert_fqdn_to_dns_name"); r = getdns_dict_set_bindata(rr_dict, "name", dns_name); free(dns_name->data); free(dns_name); if (r) FAIL_r("getdns_dict_set_bindata"); if ((r = getdns_dict_set_int(rr_dict, "type", GETDNS_RRTYPE_A))) FAIL_r("getdns_dict_set_int"); if ((r = getdns_dict_set_bindata(rr_dict, "/rdata/ipv4_address", &address))) FAIL_r("getdns_dict_set_int"); if ((r = getdns_rr_dict2str(rr_dict, &str))) FAIL_r("getdns_rr_dict2str"); printf("\n%s\n", str); free(str); if ((r = getdns_rr_dict2wire(rr_dict, &wire, &wire_len))) FAIL_r("getdns_rr_dict2wire"); getdns_dict_destroy(rr_dict); print_wire(wire, wire_len); free(wire); /* Convert RR with special rdata fields and repeating last element * from string to rr_dict */ if ((r = getdns_str2rr_dict( "hip2 IN HIP 2 200100107B1A74DF365639CC39F1D578 AwEAAbdxyhNuSutc5EMzxTs9LBPCIkOFH8cIvM4p9+LrV4e19WzK00+CI6zBCQTdtWsuxKbWIy87UOoJTwkUs7lBu+Upr1gsNrut79ryra+bSRGQb1slImA8YVJyuIDsj7kwzG7jnERNqnWxZ48AWkskmdHaVDP4BcelrTI3rMXdXF5D rvs1.example.com. rvs2.example.com.", &rr_dict, "nlnetlabs.nl", 3600))) FAIL_r("getdns_str2rr_dict"); if ((r = getdns_dict_remove_name(rr_dict, "/rdata/rdata_raw"))) FAIL_r("getdns_dict_remove_name"); printf("\n"); print_dict(rr_dict); /* Convert RR with special rdata fields and repeating last element * back to string. */ if ((r = getdns_rr_dict2str(rr_dict, &str))) FAIL_r("getdns_rr_dict2str"); printf("%s", str); free(str); /* Convert RR with special rdata fields without repeating last element * to string. */ if ((r = getdns_dict_remove_name(rr_dict, "/rdata/rendezvous_servers"))) FAIL_r("getdns_dict_remove_name"); if ((r = getdns_dict_get_bindata(rr_dict, "name", &dns_name))) FAIL_r("getdns_dict_get_bindata"); dns_name->data[4] = '0'; if ((r = getdns_rr_dict2str(rr_dict, &str))) FAIL_r("getdns_rr_dict2str"); printf("%s\n", str); free(str); getdns_dict_destroy(rr_dict); /* Convert RR with repeat block from string to rr_dict */ if ((r = getdns_str2rr_dict( "apl APL 1:192.168.42.0/26 1:192.168.42.64/26 !1:192.168.42.128/25 1:224.0.0.0/4 2:FF00:0:0:0:0:0:0:0/8", &rr_dict, "net-dns.org", 3600))) FAIL_r("getdns_str2rr_dict"); if ((r = getdns_dict_remove_name(rr_dict, "/rdata/rdata_raw"))) FAIL_r("getdns_dict_remove_name"); print_dict(rr_dict); /* Convert repeat block from rr_dict back to string. */ if ((r = getdns_rr_dict2str(rr_dict, &str))) FAIL_r("getdns_rr_dict2str"); printf("%s", str); free(str); getdns_dict_destroy(rr_dict); if (!(in = fopen(argv[1], "r"))) FAIL("Could not fopen %s\n", argv[1]); if ((r = getdns_fp2rr_list(in, &rr_list, NULL, 0))) FAIL_r("getdns_fp2rr_list"); fclose(in); print_list(rr_list); print_json_list(rr_list, 1); /* Fill the wire_buf with wireformat RR's in rr_list * wire_buf is too small for last two rr's. */ wire = wire_buf; available = sizeof(wire_buf); for (i = 0; !(r = getdns_list_get_dict(rr_list, i, &rr_dict)); i++) { prev_wire = wire; if ((r = getdns_rr_dict2wire_scan(rr_dict,&wire,&available))) { if (r == GETDNS_RETURN_NEED_MORE_SPACE) { printf("record %.3zu, available buffer space: " "%d\n", i, available); /* The buffer was too small to fit the wire- * format representation. available now holds * a negative number. the wire pointer is this * much beyond the end of the buffer space. * * If we would add available to wire, wire * would be positioned at the end of the buffer * but would not be positioned at a clean RR * border. Therefore we have to remember the * previous position of wire, so we can reset * it at the end of the wireformat representa- * tion of the previously converted rr_dict. */ wire = prev_wire; break; } else FAIL_r("getdns_rr_dict2wire_scan"); } printf("record %3zu, available buffer space: " "%d\n", i, available); fflush(stdout); } if (r == GETDNS_RETURN_NO_SUCH_LIST_ITEM) r = GETDNS_RETURN_GOOD; getdns_list_destroy(rr_list); /* Now scan over the wireformat buffer and convert to rr_dicts again. * Then fill a string buffer with those rr_dicts. */ available = wire - wire_buf; if (available < 0) { fprintf(stderr, "Negative sized buffer!\n"); exit(EXIT_FAILURE); } uavailable = available; wire = wire_buf; str = str_buf; str_len = sizeof(str_buf); while (uavailable > 0 && str_len > 0) { rr_dict = NULL; if ((r = getdns_wire2rr_dict_scan( (const uint8_t **)&wire, &uavailable, &rr_dict))) FAIL_r("getdns_wire2rr_dict_scan"); if ((r = getdns_rr_dict2str_scan(rr_dict, &str, &str_len))) FAIL_r("getdns_rr_dict2str_scan"); getdns_dict_destroy(rr_dict); } *str = 0; /* Print the entire buffer */ printf("%s", str_buf); exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { Py_Initialize(); PyRun_SimpleString("import sys"); PyRun_SimpleString("import os"); PyRun_SimpleString("import time"); PyRun_SimpleString("import io"); PyRun_SimpleString("import socket"); //PyRun_SimpleString("print(os.getcwd())"); PyRun_SimpleString("sys.path.append(os.getcwd())"); PyRun_SimpleString("import buildtcp"); //int ret = PyRun_SimpleFile(fp, "./buildtcp.py"); PyObject *main_module = PyImport_AddModule("__main__"); PyObject *target = PyImport_AddModule("buildtcp"); if(!main_module) { printf("failed to PyImport_AddModule()\n"); return 0; } PyObject *g_dict = PyModule_GetDict(main_module); PyObject *l_dict = PyDict_New(); if(!l_dict) { printf("failed to PyDict_New()\n"); return 0; } FILE *fp = fopen("./buildtcp.py", "r"); //PyObject *res = PyRun_File(fp, "./buildtcp.py", Py_file_input, g_dict, l_dict); //PyObject *res = PyRun_File(fp, "./buildtcp.py", Py_file_input, g_dict, g_dict); //PyObject *res = PyRun_String("buildtcp.buildtcp()", Py_file_input, g_dict, l_dict); printf("PyDict_Size(g_dict):%d\n", PyDict_Size(g_dict)); print_dict(g_dict); printf("PyDict_Size(l_dict):%d\n", PyDict_Size(l_dict)); print_dict(l_dict); //PyObject *key = PyUnicode_FromString("buildtcp"); //PyObject *func = PyDict_GetItem(l_dict, key); //PyObject* f_str_exc_type = PyObject_Repr(func); //PyObject* f_pyStr = PyUnicode_AsEncodedString(f_str_exc_type, "utf-8", "Error ~"); //printf("value:%s\n", PyBytes_AsString(f_pyStr)); //res = PyObject_Call(func, PyTuple_New(), NULL); //res = PyObject_CallObject(func, NULL); PyObject *res = PyObject_CallMethodObjArgs(target, PyUnicode_FromString("buildtcp"), NULL); if(!res) { printf("failed to PyRun_File()\n"); return 0; } if(!PyObject_CheckBuffer(res)) { printf("res is not Py_buffer\n"); return 0; } Py_buffer buf; int ret = PyObject_GetBuffer(res, &buf, PyBUF_SIMPLE); if(ret == -1) { printf("failed to PyObject_GetBuffer()\n"); return 0; } hexdump("return val", buf.buf, buf.len); fclose(fp); Py_Finalize(); return 0; }
void F_print_dict(void) { print_dict(); pc++; }