Quartett!文本插入程序

年初写的[url=http://rednaxelafx.iteye.com/blog/168505]Quartett!的文本提取程序[/url]的下文就在此:文本插入程序。
荒废了N久的ScriptInserter终于做了 OTL

使用方法:
把编过号的*.tkn文件、ScriptInserter.exe与前面提到的文本提取程序提取出来的*.txt文件放在同一目录下(下面假设目录路径为D:\test\)。然后执行命令:
[code]ScriptInserter textfile.txt[/code]
这样就会在名为new的子目录(假设是D:\test\new\)里得到目标*.tkn文件。

接下来……直接发代码吧。这代码基本上就是提取程序加了十来行代码的样子吧,基本逻辑几乎一模一样所以也没代码什么需要详细说明的。
主要的逻辑是:
0、校验相关文件是否存在,并创建一个名为new的子目录
1、校验*.tkn文件的signature,正确的话将signature也写入新的文件
2、获取总的token数,并写入新的文件
3、对每个token,
a、读入行号,并写入新文件
b、读入token类型,并写入新文件
c、读入token的值,并根据token类型来转换当前的程序状态;依据状态来决定写入原tkn的token值还是从txt读入文本写入新文件
4、结束

// ScriptInserter.cs, 2008/06/21
// by RednaxelaFX

/*
* Copyright (c) 2008 著作权由RednaxelaFX所有。著作权人保留一切权利。
*
* 这份授权条款,在使用者符合以下三条件的情形下,授予使用者使用及再散播本
* 软件包装原始码及二进位可执行形式的权利,无论此包装是否经改作皆然:
*
* * 对于本软件源代码的再散播,必须保留上述的版权宣告、此三条件表列,以
* 及下述的免责声明。
* * 对于本套件二进位可执行形式的再散播,必须连带以文件以及/或者其他附
* 于散播包装中的媒介方式,重制上述之版权宣告、此三条件表列,以及下述
* 的免责声明。
* * 未获事前取得书面许可,不得使用RednaxelaFX之名称,
* 来为本软件之衍生物做任何表示支持、认可或推广、促销之行为。
*
* 免责声明:本软件是由RednaxelaFX以现状("as is")提供,
* 本软件包装不负任何明示或默示之担保责任,包括但不限于就适售性以及特定目
* 的的适用性为默示性担保。RednaxelaFX无论任何条件、
* 无论成因或任何责任主义、无论此责任为因合约关系、无过失责任主义或因非违
* 约之侵权(包括过失或其他原因等)而起,对于任何因使用本软件包装所产生的
* 任何直接性、间接性、偶发性、特殊性、惩罚性或任何结果的损害(包括但不限
* 于替代商品或劳务之购用、使用损失、资料损失、利益损失、业务中断等等),
* 不负任何责任,即在该种使用已获事前告知可能会造成此类损害的情形下亦然。
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace FFDSystemAnalysis
{
enum TokenType
{
Decimal = 0x080,
Identifier = 0x081,
Hexadecimal = 0x082,
String = 0x083,
Operator = 0x085
}

enum OutputState
{
Searching,
FoundText,
FoundEqual
}

sealed class ScriptInserter
{
private static readonly byte[ ] SIGNATURE = {
( byte )0x54, ( byte )0x4F, ( byte )0x4B, ( byte )0x45,
( byte )0x4E, ( byte )0x53, ( byte )0x45, ( byte )0x54,
( byte )0x64, ( byte )0x0, ( byte )0x0, ( byte )0x0
};

static void Main( string[ ] args ) {
string infile = args[ 0 ];
if ( !infile.EndsWith( ".txt" ) ) return;
if ( !File.Exists( infile ) ) return;

string srcfile = Path.GetFileNameWithoutExtension( infile ) + ".tkn";
if ( !File.Exists( srcfile ) ) return;
string outfile = "./new/" + Path.GetFileNameWithoutExtension( infile ).Substring( 4 ) + ".tkn";
Directory.CreateDirectory( "./new" );

Encoding utf16le = new UnicodeEncoding( false, true );
Encoding jis = Encoding.GetEncoding( 932 );
Encoding gbk = Encoding.GetEncoding( 936 );

using ( BinaryReader srcreader = new BinaryReader( File.OpenRead( srcfile ), jis ) ) {
using ( StreamReader inreader = new StreamReader( infile, utf16le) ) {
using ( BinaryWriter writer = new BinaryWriter( File.Create( outfile ), gbk ) ) {
byte[ ] sig = srcreader.ReadBytes( SIGNATURE.Length );
if ( !Equals( sig, SIGNATURE ) ) {
Console.WriteLine( "Wrong signature" );
return;
}

// write UTF-16LE BOM
writer.Write( SIGNATURE );

// process each token
int lineNum = 1;
OutputState state = OutputState.Searching;
int tokenCount = srcreader.ReadInt32( );
writer.Write( tokenCount );

for ( int tokenNum = 0; tokenNum < tokenCount; ++tokenNum ) {
// get line number
lineNum = srcreader.ReadInt32( );
writer.Write( lineNum );

// get tokenType
TokenType tokenType = ( TokenType ) ( srcreader.ReadByte( ) & 0x0FF );
writer.Write( ( byte ) tokenType );

// get token value
string tokenString = ReadCString( srcreader );

switch ( tokenType ) {

case TokenType.Identifier:
if ( tokenString.Equals("text") ) {
state = OutputState.FoundText;
} else {
state = OutputState.Searching;
}
writer.Write( gbk.GetBytes( tokenString ) );
writer.Write( ( byte ) 0 );
break;

case TokenType.String:
if (OutputState.FoundEqual == state)
{
int inLinenum = Convert.ToInt32( inreader.ReadLine( ) );
if ( lineNum != inLinenum ) throw new Exception( "Line number doesn't match." );
string inText = inreader.ReadLine( );
writer.Write( gbk.GetBytes( inText ) );
writer.Write( ( byte ) 0 );
state = OutputState.Searching;
} else {
writer.Write( gbk.GetBytes( tokenString ) );
writer.Write( ( byte ) 0 );
}
break;

case TokenType.Operator:
if ((OutputState.FoundText == state)
&& tokenString.Equals("=")) {
state = OutputState.FoundEqual;
} else {
state = OutputState.Searching;
}
writer.Write( gbk.GetBytes( tokenString ) );
writer.Write( ( byte ) 0 );
break;

case TokenType.Decimal:
case TokenType.Hexadecimal:
writer.Write( gbk.GetBytes( tokenString ) );
writer.Write( ( byte ) 0 );
break;

default:
Console.WriteLine( "Unexpected token type {0} at 0x{1}.",
tokenType.ToString( "X" ),
srcreader.BaseStream.Position.ToString( "X" ) );
return;
} // switch tokenType
} // for
}
}
}
}

static bool Equals( byte[ ] a, byte[ ] b ) {
int len = a.Length;
if ( len != b.Length ) return false;
for ( int i = 0; i < len; i++ ) {
if ( a[ i ] != b[ i ] ) return false;
}
return true;
}

static string ReadCString( BinaryReader srcreader ) {
StringBuilder builder = new StringBuilder( );
char c = '\0';

while ( ( c = srcreader.ReadChar( ) ) != '\0' ) {
builder.Append( c );
}

return builder.ToString( );
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值