JAVA版StarDict星际译王简单实现(五)
Supported())
{
System.out.println("This stream do not support mark....!");
}
}
catch (Exception e)
{
System.out.println("Open files error!");
e.printStackTrace();
}
while (true)
{
System.out.println("INPUT A WORD OR PHRASE: ");
int count[] = new int[1];
String word = get_input(MAX_WORD, count);
long skips1, skips2;
if (count[0] > 0)// 从控制台得到输入单词字符
{
try
{
// 从文件开头跳到单词大致索引所在位置
// isidx.mark(0);
isidx.reset();
skips1 = locate_idx(word, idx);
// skips2 = isidx.skip(skips1);
skips2 = skipBytesFromStream(isidx, skips1);
System.out
.println("skips1:" + skips1 + " skips2:" + skips2);
}
catch (Exception e)
{
System.out.println("locate_idx run error");
e.printStackTrace();
}
if (search_word(word, offset, length))
{
data = get_data(offset, length);
display_data(data, length);
data = null;
} else
System.out.println("SORRY " + word + " CANNOT BE FOUND!\n");
System.out
.println("\n----------------------------------------\n\n");
} else
break;
}
}
/**
* 不区分大小写比较两个字符串
*
* @param s1
* @param s2
* @return
*/
public static int strsEqualsIgnoreCase(String s1, String s2)
{
int n1 = s1.length(), n2 = s2.length();
for (int i1 = 0, i2 = 0; i1 < n1 && i2 < n2; i1++, i2++)
{
char c1 = s1.charAt(i1);
char c2 = s2.charAt(i2);
if (c1 != c2)
{
// 源字符串全部都转为大写字符串
c1 = Character.toUpperCase(c1);
c2 = Character.toUpperCase(c2);
if (c1 != c2)
{
// 源字符串全部都转为小写字符串
c1 = Character.toLowerCase(c1);
c2 = Character.toLowerCase(c2);
if (c1 != c2)
{
return c1 - c2;
}
}
}
}
return n1 - n2;// 如果其中一个或者两个String都比较完了还没有同样的char的话,那就return两个String的长度差距
}
/**
* 重写了Inpustream 中的skip(long n) 方法,将数据流中起始的n 个字节跳过
* 参考:http://blog.csdn.net/ranxiedao/article/details/7787342
* @param inputStream
* @param n
* @return
*/
private static long skipBytesFromStream(InputStream inputStream, long n)
{
long remaining = n; // SKIP_BUFFER_SIZE is used to determine the size of
// skipBuffer
int SKIP_BUFFER_SIZE = 2048; // skipBuffer is initialized in
// skip(long), if needed.
byte[] skipBuffer = null;
int nr = 0;
if (skipBuffer == null)
{
skipBuffer = new byte[SKIP_BUFFER_SIZE];
}
byte[] localSkipBuffer = skipBuffer;
if (n <= 0)
{
return 0;
}
while (remaining > 0)
{
try
{
nr = inputStream.read(localSkipBuffer, 0, (int) Math.min(
SKIP_BUFFER_SIZE, remaining));
}
catch (IOException e)
{
e.printStackTrace();
}
if (nr < 0)
{
break;
}
remaining -= nr;
}
return n - remaining;
}
/**
* 主函数
* @param args
*/
public static void main(String args[])
{
consult();
try
{
isidx.close();
isdict.close();
}
catch (Exception e)
{
System.out.println("Close files error!");
e.printStackTrace();
}
}
}
如果要在
windows平台下编译http://blog.chinaunix.net/uid-20454005-id-1675913.html文章中的程序代码最好保存为cpp文件以C++项目编译执行,而且strcasecmp函数应该换为stricmp函数,并且上面作者原来的程序是在linux平台下的,字符编码本身就是UTF8的不需要进行编码转换,但在windows平台下中文为gb232编码,就需要进行编码的转换,下面为需要添加修改上的字符编码转换后的程序。
//UTF-8到GB2312的转换
char* U2G(const char* utf8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
wchar_t* wstr