int my_store(GDBM_FILE ms,uchar *key,uchar *data,int length) { int my_offset,i; if(length>DL) {fprintf(stderr,"DL\n");exit(1);} my_offset=my_search(key); if(my_offset<0) { my_offset=file_end_offset(fdata); if(my_last!=-1) { if(my_last%(KL+IL+DL))fprintf(stderr,"my_last error\n"); fseek(fdata,my_last+KL,SEEK_SET); fwrite(&my_offset,4,1,fdata); } else { fseek(fhash,4*hashkey,SEEK_SET); fwrite(&my_offset,4,1,fhash); } } if(my_offset%(KL+IL+DL)) fprintf(stderr,"my_offset error %d\n",my_offset); fseek(fdata,my_offset,SEEK_SET); fwrite(key,1,KL,fdata); fwrite(&my_next,4,1,fdata); //fwrite(&length,4,1,fdata); for(i=length;i<DL;i++) data[i]=0; fwrite(data,1,DL,fdata); return 1; }
// // The following function searches for search in result and reports // information about all the partial matches that occur. // void display_partial_matches( const std::string &result, const std::string &search ) { std::string my_search( search ); // Output summary information. std::cout << "Size of string being searched: " << result.size( ) << "\n"; std::cout << "Size of search string: " << search.size( ) << "\n"; // Keep looping while the search string is non-zero sized. while( my_search.size( ) != 0 ) { std::string::size_type match_count = 0; std::string::size_type index = 0; // Keep looping as long as there is a match. std::string::size_type hit; while( ( hit = result.find( my_search, index ) ) != std::string::npos ) { match_count++; index = hit + 1; } // Output results. std::cout << "Match depth: " << std::setw( 2 ) << my_search.size( ) << ", Search = " << my_search << ", Hits = " << match_count << "\n"; my_search.erase( my_search.size( ) - 1, 1 ); } }
/************************************************* Function: int check_putin(LINK *head) Description: 检测用户输入的指令,并执行该功能函数 Calls: my_add(head); my_display(head); my_delete(head); my_search(head); menu(head); Called By: menu(LINK *head) Input: LINK *head 链表头指针 Return: NO *************************************************/ int check_putin(LINK *head) { char check[MAX]; setbuf(stdin,NULL); //清空输入缓冲 printf("\t\t\033[47;31mplease input conmand:\033[0m "); gets(check); //读入输入的命令 if(my_strcmp(check,"ADD") == 0) //若输入的为ADD 则调用my_add增加一个联系人 { my_add(head); } else if(my_strcmp(check,"DISPLAY") == 0) //若输入为DISPLAY 调用my_display函数 { my_display(head); } else if(my_strcmp(check,"DELETE") == 0) //若输入为DELETE 调用my_delete函数 { my_delete(head); } else if(my_strcmp(check,"SEARCH") == 0) //若输入为SEARCH 调用my_search函数 { my_search(head); } else if(my_strcmp(check,"QUIT") == 0) //若输入为QUIT 程序退出 { write_to_file(head); //退出前保存数据进文件 system("clear"); printf("\n"); printf("\t\t\tTHANKS FOR USEING!\n"); getchar(); exit(1); } /*若错误输入 则继续调用menu函数 提示错误并重新输入*/ else { printf("\t\t\033[47;31m输入错误,请重新输入!\033[0m"); // getchar(); scanf("%*[^\n]"); system("clear"); menu(head); } return SUCCESS; }
int longestConsecutive(vector<int> &num) { int n = num.size(); st.clear(); for (int i = 0; i < n; i++) { st.insert(num[i]); } int ans = 1; for (int temp, i = 0; i < n; i++) { temp = my_search(num[i] - 1, -1) + my_search(num[i] + 1, 1) + 1; ans = max(ans, temp); st.erase(num[i]); } return ans; }
int my_fetch(GDBM_FILE ms,uchar *key,uchar *data,int *length) { int my_offset,i; my_offset=my_search(key); if(my_offset>=0) { *length=my_length; fseek(fdata,my_offset+IL+KL,SEEK_SET); fread(data,1,*length,fdata); return 1; } return 0; }
//This tests the link function; int linkTest() { MINODE *test_mip = running->cwd; INODE *ip = NULL; int ino = 0; int actual = 0; int expected1 = 0; int expected2 = 22; strcpy(pathname, ""); cd(pathname); test_mip = running->cwd; printf("\n\n-------- TESTING LINK FUNCTION --------\n\n"); printf("Testing >link tiny new\n"); strcpy(pathname, "tiny"); strcpy(third, "new"); my_link(pathname); //strcpy(pathname, "tiny"); //ino = my_search(pathname); printf("ino of tiny is: %d\n", 24); strcpy(pathname, "new"); actual = my_search(pathname); printf("ino of new is: %d\n", actual); if (24 != actual) { printf("TEST FAILED!\n\n"); return 0; } printf("TEST PASSED!\n\n"); printf("ALL LINK TESTS PASSED!\n\n\n"); return 1; }
//This tests the cp function //It does so by calling cp on two files and checks their INODES size //If the INODES sizes do not match it will return 0. int cpTest() { MINODE *test_mip = running->cwd; INODE *ip = NULL; int ino = 0; int tinysize = 37; int bigsize = 12713; int newsize = 0; strcpy(pathname, ""); cd(pathname); test_mip = running->cwd; printf("\n\n-------- TESTING CP FUNCTION --------\n\n"); strcpy(pathname, "tiny"); strcpy(third, "newtiny"); printf("Testing >cp tiny newtiny\n"); cp_file(pathname); strcpy(pathname, "newtiny"); ino = getino(test_mip, pathname); if(ino != 0) test_mip = iget(dev, ino); ip = &test_mip->INODE; newsize = ip->i_size; printf("Size of tiny: %d\n", tinysize); printf("Size of newtiny: %d\n", newsize); if(tinysize != newsize) { printf("TEST FAILED\n\n"); return 0; } else printf("TEST PASSED\n\n"); newsize = 0; strcpy(pathname, "/Y/bigfile"); strcpy(third, "/newbig"); printf("Testing >cp /Y/bigfile newbig\n"); cp_file(pathname); strcpy(pathname, "newbig"); ino = my_search(pathname); if(ino != 0) test_mip = iget(dev, ino); ip = &test_mip->INODE; newsize = ip->i_size; printf("Size of bigfile: %d\n", bigsize); printf("Size of newbig: %d\n", newsize); if(bigsize != newsize) { printf("TEST FAILED\n\n"); return 0; } else printf("TEST PASSED\n\n"); printf("ALL CP TESTS PASSED\n\n\n"); return 1; }
int searchPort(struct packetInfo *pi){ if( my_search(&db,pi) != NULL) return 1; else return 0; }