multimap_iterator_t multimap_insert_hint( multimap_t* pt_multimap, multimap_iterator_t t_hint, const pair_t* cpt_pair) { pair_t t_elempair; assert(pt_multimap != NULL && cpt_pair != NULL); assert(_same_multimap_pair_type(&pt_multimap->_t_pair, cpt_pair)); /* initialize the new pair */ t_elempair = _create_pair( pt_multimap->_t_pair._t_firsttypesize, pt_multimap->_t_pair._sz_firsttypename, pt_multimap->_t_pair._t_secondtypesize, pt_multimap->_t_pair._sz_secondtypename); pair_init(&t_elempair); memcpy(t_elempair.first, cpt_pair->first, t_elempair._t_firsttypesize); memcpy(t_elempair.second, cpt_pair->second, t_elempair._t_secondtypesize); /* insert pair into tree */ #ifdef CSTL_MULTIMAP_AVL_TREE t_hint = _avl_tree_insert_equal(_GET_MULTIMAP_AVL_TREE(pt_multimap), &t_elempair); #else t_hint = _rb_tree_insert_equal(_GET_MULTIMAP_RB_TREE(pt_multimap), &t_elempair); #endif /* * There is no need for destroy the elempair, because the pair is copied * to the tree, and it is destroied by tree when tree is desroied. */ _GET_CONTAINER(&t_hint) = pt_multimap; _GET_MULTIMAP_CONTAINER_TYPE(&t_hint) = _MULTIMAP_CONTAINER; _GET_MULTIMAP_ITERATOR_TYPE(&t_hint) = _BIDIRECTIONAL_ITERATOR; return t_hint; }
/** * Initialize pair container with pair. */ void pair_init_copy(pair_t* ppair_dest, const pair_t* cppair_src) { bool_t b_result = false; assert(ppair_dest != NULL); assert(cppair_src != NULL); assert(_pair_is_created(ppair_dest)); assert(_pair_is_inited(cppair_src)); assert(_pair_same_type(ppair_dest, cppair_src)); /* initialize dest pair */ pair_init(ppair_dest); /* copy element */ b_result = _GET_PAIR_FIRST_TYPE_SIZE(ppair_dest); _GET_PAIR_FIRST_TYPE_COPY_FUNCTION(ppair_dest)(ppair_dest->_pv_first, cppair_src->_pv_first, &b_result); assert(b_result); b_result = _GET_PAIR_SECOND_TYPE_SIZE(ppair_dest); _GET_PAIR_SECOND_TYPE_COPY_FUNCTION(ppair_dest)(ppair_dest->_pv_second, cppair_src->_pv_second, &b_result); assert(b_result); ppair_dest->_bfun_mapkeycompare = cppair_src->_bfun_mapkeycompare; ppair_dest->_bfun_mapvaluecompare = cppair_src->_bfun_mapvaluecompare; }
/* pair_t */ void _type_init_pair(const void* cpv_input, void* pv_output) { bool_t b_result = false; assert(cpv_input != NULL && pv_output != NULL); b_result = _create_pair_auxiliary((pair_t*)cpv_input, (char*)pv_output); assert(b_result); /* initialize pair */ pair_init((pair_t*)cpv_input); }
Pair_ptr Pair_create(void* first, void* second) { Pair_ptr self = ALLOC(Pair, 1); PAIR_CHECK_INSTANCE(self); pair_init(self); Pair_set_values(self, first, second); return self; }
map_iterator_t map_insert(map_t* pt_map, const pair_t* cpt_pair) { pair_t t_elempair; map_iterator_t t_result; #ifdef CSTL_MAP_AVL_TREE avl_tree_result_pair_t t_avlresult; #else rb_tree_result_pair_t t_rbresult; #endif assert(pt_map != NULL && cpt_pair != NULL); assert(_same_map_pair_type(&pt_map->_t_pair, cpt_pair)); /* initialize the new pair */ t_elempair = _create_pair( pt_map->_t_pair._t_firsttypesize, pt_map->_t_pair._sz_firsttypename, pt_map->_t_pair._t_secondtypesize, pt_map->_t_pair._sz_secondtypename); pair_init(&t_elempair); memcpy(t_elempair.first, cpt_pair->first, t_elempair._t_firsttypesize); memcpy(t_elempair.second, cpt_pair->second, t_elempair._t_secondtypesize); /* insert pair into tree */ #ifdef CSTL_MAP_AVL_TREE t_avlresult = _avl_tree_insert_unique(_GET_MAP_AVL_TREE(pt_map), &t_elempair); if(t_avlresult._t_second._t_bool) { t_result = t_avlresult._t_first; } else { t_result = map_end(pt_map); pair_destroy(&t_elempair); } #else t_rbresult = _rb_tree_insert_unique(_GET_MAP_RB_TREE(pt_map), &t_elempair); if(t_rbresult._t_second._t_bool) { t_result = t_rbresult._t_first; } else { t_result = map_end(pt_map); pair_destroy(&t_elempair); } #endif _GET_CONTAINER(&t_result) = pt_map; _GET_MAP_CONTAINER_TYPE(&t_result) = _MAP_CONTAINER; _GET_MAP_ITERATOR_TYPE(&t_result) = _BIDIRECTIONAL_ITERATOR; return t_result; }
/* map function */ void map_init(map_t* pt_map) { assert(pt_map != NULL); /* initialize the pair */ pair_init(&pt_map->_t_pair); /* initialize the tree */ #ifdef CSTL_MAP_AVL_TREE _avl_tree_init(_GET_MAP_AVL_TREE(pt_map), _map_cmp, _map_destroy_elem); #else _rb_tree_init(_GET_MAP_RB_TREE(pt_map), _map_cmp, _map_destroy_elem); #endif }
/** * Initialize map container. */ void map_init(map_t* pmap_map) { assert(pmap_map != NULL); assert(_pair_is_created(&pmap_map->_pair_temp)); /* initialize the pair */ pair_init(&pmap_map->_pair_temp); /* initialize the tree */ #ifdef CSTL_MAP_AVL_TREE _avl_tree_init(&pmap_map->_t_tree, _map_value_compare); #else _rb_tree_init(&pmap_map->_t_tree, _map_value_compare); #endif }
/** * Initialize map container with user define compare function. */ void map_init_ex(map_t* pmap_map, binary_function_t bfun_keycompare) { assert(pmap_map != NULL); assert(_pair_is_created(&pmap_map->_pair_temp)); pair_init(&pmap_map->_pair_temp); pmap_map->_bfun_keycompare = bfun_keycompare; pmap_map->_pair_temp._bfun_mapkeycompare = bfun_keycompare; #ifdef CSTL_MAP_AVL_TREE _avl_tree_init(&pmap_map->_t_tree, _map_value_compare); #else _rb_tree_init(&pmap_map->_t_tree, _map_value_compare); #endif }
/** * Initialize hash_multimap container with user define compare function. */ void hash_multimap_init_ex(hash_multimap_t* phmmap_map, size_t t_bucketcount, unary_function_t ufun_hash, binary_function_t bfun_compare) { unary_function_t ufun_default_hash = NULL; assert(phmmap_map != NULL); assert(_pair_is_created(&phmmap_map->_pair_temp)); /* initialize the pair */ pair_init(&phmmap_map->_pair_temp); phmmap_map->_bfun_keycompare = bfun_compare; phmmap_map->_pair_temp._bfun_mapkeycompare = bfun_compare; ufun_default_hash = ufun_hash != NULL ? ufun_hash : _hash_multimap_default_hash; /* initialize the hashtable */ _hashtable_init(&phmmap_map->_t_hashtable, t_bucketcount, ufun_default_hash, _hash_multimap_value_compare); }
void* _map_at_varg(map_t* pt_map, va_list val_elemlist) { pair_t t_elempair; bool_t t_result = false; void* pv_value = NULL; #ifdef CSTL_MAP_AVL_TREE avl_tree_result_pair_t t_avlresult; #else rb_tree_result_pair_t t_rbresult; #endif assert(pt_map != NULL); t_elempair = _create_pair( pt_map->_t_pair._t_firsttypesize, pt_map->_t_pair._sz_firsttypename, pt_map->_t_pair._t_secondtypesize, pt_map->_t_pair._sz_secondtypename); pair_init(&t_elempair); _get_varg_value( t_elempair.first, val_elemlist, t_elempair._t_firsttypesize, t_elempair._sz_firsttypename); memset(t_elempair.second, 0x00, t_elempair._t_secondtypesize); #ifdef CSTL_MAP_AVL_TREE t_avlresult = _avl_tree_insert_unique(_GET_MAP_AVL_TREE(pt_map), &t_elempair); pv_value = ((pair_t*)((avlnode_t*) _GET_AVL_TREE_COREPOS(&t_avlresult._t_first))->_pc_data)->second; t_result = t_avlresult._t_second._t_bool; #else t_rbresult = _rb_tree_insert_unique(_GET_MAP_RB_TREE(pt_map), &t_elempair); pv_value = ((pair_t*)((rbnode_t*) _GET_RB_TREE_COREPOS(&t_rbresult._t_first))->_pc_data)->second; t_result = t_rbresult._t_second._t_bool; #endif assert(pv_value != NULL); /* destroy the pair when inserting failed */ if(!t_result) { pair_destroy(&t_elempair); } return pv_value; }
pair_t _multimap_equal_range_varg( const multimap_t* cpt_multimap, va_list val_elemlist) { multimap_iterator_t t_first; multimap_iterator_t t_second; pair_t t_pair; #ifdef CSTL_MULTIMAP_AVL_TREE avl_tree_result_pair_t t_avlresult; #else rb_tree_result_pair_t t_rbresult; #endif assert(cpt_multimap != NULL); _get_varg_value( cpt_multimap->_t_pair.first, val_elemlist, cpt_multimap->_t_pair._t_firsttypesize, cpt_multimap->_t_pair._sz_firsttypename); #ifdef CSTL_MULTIMAP_AVL_TREE t_avlresult = _avl_tree_equal_range( _GET_MULTIMAP_AVL_TREE(cpt_multimap), &cpt_multimap->_t_pair); t_first = t_avlresult._t_first; t_second = t_avlresult._t_second._t_iterator; #else t_rbresult = _rb_tree_equal_range( _GET_MULTIMAP_RB_TREE(cpt_multimap), &cpt_multimap->_t_pair); t_first = t_rbresult._t_first; t_second = t_rbresult._t_second._t_iterator; #endif _GET_CONTAINER(&t_first) = (multimap_t*)cpt_multimap; _GET_MULTIMAP_CONTAINER_TYPE(&t_first) = _MULTIMAP_CONTAINER; _GET_MULTIMAP_ITERATOR_TYPE(&t_first) = _BIDIRECTIONAL_ITERATOR; _GET_CONTAINER(&t_second) = (multimap_t*)cpt_multimap; _GET_MULTIMAP_CONTAINER_TYPE(&t_second) = _MULTIMAP_CONTAINER; _GET_MULTIMAP_ITERATOR_TYPE(&t_second) = _BIDIRECTIONAL_ITERATOR; t_pair = create_pair(multimap_iterator_t, multimap_iterator_t); pair_init(&t_pair); memcpy(t_pair.first, &t_first, t_pair._t_firsttypesize); memcpy(t_pair.second, &t_second, t_pair._t_secondtypesize); return t_pair; }
void Pair_init(Pair_ptr self, void* first, void* second) { pair_init(self); Pair_set_values(self, first, second); }
/** * @brief main function for the copyright agent * * The copyright agent is used to automatically locate copyright statements * found in code. * * There are 3 ways to use the copyright agent: * 1. Command Line Analysis :: test a file from the command line * 2. Agent Based Analysis :: waits for commands from stdin * 3. Accuracy Test :: tests the accuracy of the copyright agent * * +-----------------------+ * | Command Line Analysis | * +-----------------------+ * * To analyze a file from the command line: * -C <filename> :: run copyright agent from command line * -d :: turn on debugging information * -T <Copyright Statements | URLs| Emails> :: Copyright Statements | URLs |Emails * * example: * $ ./copyright -C myfiletoscan * * +----------------------+ * | Agent Based Analysis | * +----------------------+ * * To run the copyright agent as an agent simply run with no command line args * -i :: initialize a connection to the database * -d :: turn on debugging information * * example: * $ upload_pk | ./copyright * * +---------------+ * | Accuracy Test | * +---------------+ * * To test the accuracy of the copyright agent run with a -t. Make sure to run the * accuracy tests in the source directory with the testdata directory: * -t :: run the accuracy analysis * * example: * $ ./copyright -t * * Running the tests will create 3 files: * 1. Matches: contains all of the matches found by the copyright agent, information * includes what file the match was found in, the dictionary element * that it matched, the name that it matched and the text that was found * 2. False_Positives: contains all of the false positives found by the agent, * information in the file includes the file the false positive was * in, the dictionary match, the name match, and the text * 3. Flase_Negatives: contains all of the false negatives found by the agent, * information in the file includes the file the false negative was * in, and the text of the false negative * * NOTE: -d will produces the exact same style of Matches file that the accuracy * testing does. Currently this is the only thing that -d will produce * * @param argc the number of command line arguments * @param argv the command line arguments * @return 0 on a successful program execution */ int main(int argc, char** argv) { /* primitives */ char sql[512]; // buffer for database access int c, i = -1; // temporary int containers int num_files = 0; // the number of rows in a job int ars_pk = 0; // the args primary key int user_pk = 0; long upload_pk = 0; // the upload primary key long agent_pk = 0; // the agents primary key char *SVN_REV = NULL; char *VERSION = NULL; char agent_rev[myBUFSIZ]; char copy_buf[FILENAME_MAX]; char name_buf[FILENAME_MAX]; int report_type = 7; // defaul as all. binary xxx 1st number as email, 2nd number as url, 3rd number as statement int cli_run = 0; // when run from command line, that mean -C option is set; 1: yes, 0: no /* Database structs */ PGconn* pgConn = NULL; // the connection to Database PGresult* pgResult = NULL; // result of a database access /* copyright structs */ copyright copy; // the work horse of the copyright agent pair curr; // pair to push into the file list /* verbose data */ FILE* mout = NULL; /* set the output streams */ cout = stdout; cerr = stdout; cin = stdin; /* connect to the scheduler */ fo_scheduler_connect(&argc, argv, &pgConn); /* initialize complex data strcutres */ memset(copy_buf, '\0', sizeof(copy_buf)); memset(name_buf, '\0', sizeof(copy_buf)); snprintf(copy_buf, sizeof(copy_buf), "%s/mods-enabled/copyright/agent/copyright.dic", sysconfigdir); snprintf(name_buf, sizeof(name_buf), "%s/mods-enabled/copyright/agent/names.dic", sysconfigdir); if(!copyright_init(©, copy_buf, name_buf)) { fprintf(cerr, "FATAL %s.%d: copyright initialization failed\n", __FILE__, __LINE__); fprintf(cerr, "FATAL %s\n", strerror(errno)); fflush(cerr); return 1; } /* parse the command line options */ while((c = getopt(argc, argv, "T:dc:C:tiVvh")) != -1) { switch(c) { case 'v': /* debugging */ mout = fopen("Matches", "w"); if(!mout) { fprintf(cerr, "ERROR could not open Matches for logging\n"); fflush(cerr); } else { verbose = 1; } break; case 'C': /* run from command line */ cli_run = 1; pair_init(&curr, string_function_registry(), int_function_registry()); pair_set_first(curr, optarg); pair_set_second(curr, &i); num_files++; break; case 'T': /* report type, Copyright Statements | URLs| Emails */ report_type = atoi(optarg); printf("report_type is:%d\n", report_type); break; case 't': /* run accuracy testing */ run_test_files(copy); copyright_destroy(copy); return 0; case 'i': /* initialize database connections */ copyright_destroy(copy); PQfinish(pgConn); return 0; case 'V': printf("%s", BuildVersion); copyright_destroy(copy); PQfinish(pgConn); return 0; default: /* error, print usage */ copyright_usage(argv[0]); return 3; } } /** run from command line */ if (1 == cli_run) { perform_analysis(pgConn, copy, curr, agent_pk, mout, report_type); pair_destroy(curr); } /* if there are no files in the file list then the agent is begin run from */ /* the scheduler, open the database and grab the files to be analyzed */ if(num_files == 0) { /* create the sql copy structure */ sqlcpy = fo_sqlCopyCreate(pgConn, "copyright", 32768, 7, "agent_fk", "pfile_fk", "copy_startbyte", "copy_endbyte", "content", "hash", "type"); /* book keeping */ pair_init(&curr, string_function_registry(), int_function_registry()); db_connected = 1; SVN_REV = fo_sysconfig("copyright", "SVN_REV"); VERSION = fo_sysconfig("copyright", "VERSION"); sprintf(agent_rev, "%s.%s", VERSION, SVN_REV); agent_pk = fo_GetAgentKey(pgConn, AGENT_NAME, 0, agent_rev, AGENT_DESC); /* make sure that we are connected to the database */ if(!check_copyright_table(pgConn)) { return 5; } user_pk = fo_scheduler_userID(); /* get user_pk for user who queued the agent */ /* enter the main agent loop */ while(fo_scheduler_next()) { upload_pk = atol(fo_scheduler_current()); /* Check Permissions */ if (GetUploadPerm(pgConn, upload_pk, user_pk) < PERM_WRITE) { LOG_ERROR("You have no update permissions on upload %ld", upload_pk); continue; } ars_pk = fo_WriteARS(pgConn, 0, upload_pk, agent_pk, AGENT_ARS, NULL, 0); sprintf(sql, fetch_pfile, upload_pk, agent_pk, agent_pk); pgResult = PQexec(pgConn, sql); num_files = PQntuples(pgResult); for(i = 0; i < num_files; i++) { c = atoi(PQgetvalue(pgResult, i, PQfnumber(pgResult, "pfile_pk"))); pair_set_first(curr, PQgetvalue(pgResult, i, PQfnumber(pgResult, "pfilename"))); pair_set_second(curr, &c); perform_analysis(pgConn, copy, curr, agent_pk, mout, REPORTALL); } fo_WriteARS(pgConn, ars_pk, upload_pk, agent_pk, AGENT_ARS, NULL, 1); PQclear(pgResult); } pair_destroy(curr); } if(db_connected) { fo_sqlCopyDestroy(sqlcpy, 1); PQfinish(pgConn); } if(verbose) { fclose(mout); } copyright_destroy(copy); fo_scheduler_disconnect(0); return 0; }