00001 /* 00002 * File Name: search_test.cpp 00003 */ 00004 00005 /* 00006 * This file is part of uds-plugin-plaintext. 00007 * 00008 * uds-plugin-plaintext is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation, either version 2 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * uds-plugin-plaintext is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00020 */ 00021 00022 /** 00023 * Copyright (C) 2008 iRex Technologies B.V. 00024 * All rights reserved. 00025 */ 00026 00027 #include <stdio.h> 00028 #include "text_model.h" 00029 00030 /* Test step 00031 search_next/all Language case_sensitive forward match_whole_word Passed? 00032 search_all ASCII Y N/A Y Y(6) 00033 search_all ASCII Y N/A N Y(11) 00034 search_all ASCII N N/A Y Y(9) 00035 search_all ASCII N N/A N Y(14) 00036 search_all GB2312 Y N/A Y Y(3) 00037 search_all GB2312 Y N/A N Y(76) 00038 search_all GB2312 N N/A Y Y(3) 00039 search_all GB2312 N N/A N Y(76) 00040 search_next ASCII Y Y Y Y 00041 search_next ASCII Y Y N Y 00042 search_next ASCII Y N Y Y 00043 search_next ASCII Y N N Y 00044 search_next ASCII N Y Y Y 00045 search_next ASCII N Y N Y 00046 search_next ASCII N N Y Y 00047 search_next ASCII N N N Y 00048 search_next GB2312 Y Y Y Y 00049 search_next GB2312 Y Y N Y 00050 search_next GB2312 Y N Y Y 00051 search_next GB2312 Y N N Y 00052 search_next GB2312 N Y Y Y 00053 search_next GB2312 N Y N Y 00054 search_next GB2312 N N Y Y 00055 search_next GB2312 N N N Y 00056 */ 00057 00058 using namespace text; 00059 00060 int main(int argc, char* argv[]) 00061 { 00062 if (argc != 2) 00063 { 00064 fprintf(stderr, "Usage: %s <file_name>.\n", argv[0]); 00065 return -1; 00066 } 00067 00068 TextModel model(argv[1]); 00069 model.open(); 00070 model.read_text(); 00071 00072 // Set search criteria. 00073 const char* search_text = "¹ùÐ¥Ìì"; 00074 Position from(16, 0); 00075 bool case_sensitive = false; 00076 bool forward = false; 00077 bool match_whole_word = false; 00078 00079 // Convert the search text to UTF-8 encoded string. 00080 const char* in_p = search_text; 00081 size_t in_bytes_left = strlen(search_text); 00082 char out_buf[1024]; 00083 char *out_p = out_buf; 00084 size_t out_bytes_left = sizeof(out_buf); 00085 00086 iconv_t conv = iconv_open("UTF-8", "GB2312"); 00087 iconv(conv, &in_p, &in_bytes_left, &out_p, &out_bytes_left); 00088 iconv_close(conv); 00089 00090 *out_p = 0; 00091 00092 // Test search all function 00093 /* 00094 std::vector<Range> result_ranges; 00095 model.search_all(result_ranges, out_buf, case_sensitive, match_whole_word); 00096 for (unsigned int i=0; i<result_ranges.size(); i++) 00097 { 00098 printf("Match %d: (%d, %d) ~ (%d, %d)\n", 00099 i+1, 00100 result_ranges[i].start.paragraph, 00101 result_ranges[i].start.offset, 00102 result_ranges[i].end.paragraph, 00103 result_ranges[i].end.offset); 00104 }*/ 00105 00106 // Test search next function. 00107 Range result; 00108 bool ret = model.search_next(result, from, out_buf, case_sensitive, forward, match_whole_word); 00109 if (ret == false) 00110 { 00111 printf("No match.\n"); 00112 } 00113 else 00114 { 00115 printf("Pattern found: (%d, %d) ~ (%d, %d)\n", 00116 result.start.paragraph, 00117 result.start.offset, 00118 result.end.paragraph, 00119 result.end.offset); 00120 } 00121 00122 getchar(); 00123 return 0; 00124 } 00125