using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection;
namespace hogehoge
{
///
/// TestDataクラスを使用する例
///
public sealed class TestCall
{
public TestCall(){
}
public void example(){
// データ管理クラスのインスタンス化
TestData testData = new TestData();
// 読込結果を格納するリスト
List> lstTestData1 = null;
List> lstTestData2 = null;
#region 固定長データテスト
// 固定長テストデータ
string filePathDat1 = @"C;\hogehoge1.dat";
string filePathDat2 = @"C;\hogehoge2.dat";
// 固定長データ読込
if(!testData.loadData(TestData.eDataType.テストデータ1_固定長, filePathDat1)) return;
if(!testData.loadData(TestData.eDataType.テストデータ2_固定長, filePathDat2)) return;
// 読込結果をリストで取得
lstTestData1 = testData.getTestData1(false);
lstTestData2 = testData.getTestData2(false);
foreach(List d in lstTestData1){
foreach(string s in d){
System.Diagnostics.Debug.WriteLine(s);
}
}
#endregion
#region CSVデータテスト
// CSVテストデータ
string filePathCsv1 = @"C;\hogehoge1.csv";
string filePathCsv2 = @"C;\hogehoge2.csv";
// CSVデータ読込
if(!testData.loadData(TestData.eDataType.テストデータ1_CSV, filePathCsv1)) return;
if(!testData.loadData(TestData.eDataType.テストデータ2_CSV, filePathCsv2)) return;
// 読込結果をリストで取得
lstTestData1 = testData.getTestData1(false);
lstTestData2 = testData.getTestData2(false);
foreach(List d in lstTestData1){
foreach(string s in d){
System.Diagnostics.Debug.WriteLine(s);
}
}
#endregion
}
}
///
/// 固定長やCSVのファイルを読取る
///
public sealed class TestData
{
#region クラス変数
Encoding _encoding = null;
#endregion
#region データ定義
#region テストデータ1レイアウト
///
/// テストデータ1レイアウト
///
public struct strcTestData1{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=1)] // 1 分類
public byte[] classify;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=20)] // 20 番号
public byte[] no;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=15)] // 15 コード
public byte[] code;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=60)] // 60 名称
public byte[] name;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=10)] // 10 日付
public byte[] date;
}
public List _TestData1 = null;
#endregion
#region テストデータ2レイアウト
///
/// テストデータ2レイアウト
///
public struct strcTestData2{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=200)] // 200 住所
public byte[] address;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=20)] // 20 電話番号
public byte[] telephone;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=20)] // 20 FAX番号
public byte[] fax;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=100)] // 100 メールアドレス
public byte[] mailAddress;
}
public List _TestData2 = null;
#endregion
#region データのタイプ判別用列挙体
///
/// データのタイプ判別用列挙体
///
public enum eDataType{
テストデータ1_固定長
,テストデータ2_固定長
,テストデータ1_CSV
,テストデータ2_CSV
}
#endregion
#endregion
#region コンストラクタ
///
/// コンストラクタ
///
public TestData(){
// エンコードの取得
this._encoding = Encoding.GetEncoding("Shift_JIS");
// テストデータ1用のリストをインスタンス化
this._TestData1 = new List();
// テストデータ2用のリストをインスタンス化
this._TestData2 = new List();
}
#endregion
#region データの読込
///
/// データの読込
///
///
///
///
public bool loadData(eDataType dataType, string filePath){
bool result = false; // Failure
StreamReader sr = null;
try{
// データファイルのストリームを取得
sr = new StreamReader(filePath, this._encoding);
string buf = null;
while(!sr.EndOfStream){
// 1行ずつ読取
buf = sr.ReadLine();
// 読取データを各構造体へセット
switch(dataType){
case eDataType.テストデータ1_固定長:
strcTestData1? testData1 = getTestData1(buf.ToCharArray(), this._encoding);
if(null != testData1) this._TestData1.Add(testData1.Value);
break;
case eDataType.テストデータ2_固定長:
strcTestData2? testData2 = getTestData2(buf.ToCharArray(), this._encoding);
if(null != testData2) this._TestData2.Add(testData2.Value);
break;
case eDataType.テストデータ1_CSV:
strcTestData1? testData1Csv = getTestData1(buf, this._encoding);
if(null != testData1Csv) this._TestData1.Add(testData1Csv.Value);
break;
case eDataType.テストデータ2_CSV:
strcTestData2? testData2Csv = getTestData2(buf, this._encoding);
if(null != testData2Csv) this._TestData2.Add(testData2.Value);
break;
}
}
// 返却値のセット
result = true;
}catch(Exception ex){
result = false;
ex.ToString(); // ログ等にex内容を書く
}finally{
if(null != sr) sr.Close();
}
return result;
}
#endregion
#region 型変換処理
///
/// Byte配列→String
///
///
///
public string convert(byte[] bArray, bool isTrim){
string result = this._encoding.GetString(bArray);
if(isTrim) result = result.Trim();
return result;
}
///
/// String→Byte配列
///
///
///
///
public byte[] convert(string data, bool isTrim){
string result = data;
if(isTrim) result = result.Trim();
return this._encoding.GetBytes(result);
}
#endregion
#region 各構造体の情報を連結文字列で返却
///
/// テストデータ1の情報を連結文字列で返却する
///
///
///
///
///
public string getTestData1(strcTestData1 data, string delimiter, bool isTrim){
StringBuilder result = new StringBuilder();
string X = "";
if(!string.IsNullOrEmpty(delimiter)) X = delimiter;
result.Append(this.convert(data.classify, isTrim) + X);
result.Append(this.convert(data.no, isTrim) + X);
result.Append(this.convert(data.code, isTrim) + X);
result.Append(this.convert(data.name, isTrim) + X);
result.Append(this.convert(data.date, isTrim));
return result.ToString();
}
///
/// テストデータ2の情報を連結文字列で返却する
///
///
///
///
///
public string getTestData2(strcTestData2 data, string delimiter, bool isTrim){
StringBuilder result = new StringBuilder();
string X = "";
if(!string.IsNullOrEmpty(delimiter)) X = delimiter;
result.Append(this.convert(data.address, isTrim) + X);
result.Append(this.convert(data.telephone, isTrim) + X);
result.Append(this.convert(data.fax, isTrim) + X);
result.Append(this.convert(data.mailAddress, isTrim));
return result.ToString();
}
#endregion
#region 各構造体の情報をリストで返却
///
/// テストデータ1の情報をリストで返却する
///
///
///
public List> getTestData1(bool isTrim){
List> result = new List>();
foreach(strcTestData1 j in this._TestData1){
List data = new List();
data.Add(this.convert(j.classify, isTrim));
data.Add(this.convert(j.no, isTrim));
data.Add(this.convert(j.code, isTrim));
data.Add(this.convert(j.name, isTrim));
data.Add(this.convert(j.date, isTrim));
result.Add(data);
}
return result;
}
///
/// テストデータ2の情報をリストで返却する
///
///
///
public List> getTestData2(bool isTrim){
List> result = new List>();
foreach(strcTestData2 j in this._TestData2){
List data = new List();
data.Add(this.convert(j.address, isTrim));
data.Add(this.convert(j.telephone, isTrim));
data.Add(this.convert(j.fax, isTrim));
data.Add(this.convert(j.mailAddress, isTrim));
result.Add(data);
}
return result;
}
#endregion
#region 読取データを各構造体へセット(CSVの場合)
///
/// 読取データをテストデータ1用構造体へセット
///
///
///
///
static strcTestData1? getTestData1(string data, Encoding encode){
string[] sArray = data.Split(',');
if(5 != sArray.Length) return null;
strcTestData1 result = new strcTestData1();
result.classify = encode.GetBytes(sArray[0]);
result.no = encode.GetBytes(sArray[1]);
result.code = encode.GetBytes(sArray[2]);
result.name = encode.GetBytes(sArray[3]);
result.date = encode.GetBytes(sArray[4]);
return result;
}
///
/// 読取データをテストデータ2用構造体へセット
///
///
///
///
static strcTestData2? getTestData2(string data, Encoding encode){
string[] sArray = data.Split(',');
if(4 != sArray.Length) return null;
strcTestData2 result = new strcTestData2();
result.address = encode.GetBytes(sArray[0]);
result.telephone = encode.GetBytes(sArray[1]);
result.fax = encode.GetBytes(sArray[2]);
result.mailAddress = encode.GetBytes(sArray[3]);
return result;
}
#endregion
#region 読取データを各構造体へセット(固定長の場合)
///
/// 読取データをテストデータ1用構造体へセット
///
///
///
///
static strcTestData1? getTestData1(char[] cArray, Encoding encode){
// 返却値
strcTestData1? result = null;
// α:読取データのバイト配列
byte[] bArray = encode.GetBytes(cArray);
// β:読取データをセットする構造体のサイズ
int size = Marshal.SizeOf(typeof(strcTestData1));
// α≠βの場合は終了
if(bArray.Length != size) return result;
// γ:アンマネージメモリからメモリ割当
IntPtr ptr = Marshal.AllocHGlobal(size);
// αをγへコピー
Marshal.Copy(bArray, 0, ptr, size);
// γを返却構造体へセット
result = (strcTestData1)Marshal.PtrToStructure(ptr, typeof(strcTestData1));
// γを開放
Marshal.FreeHGlobal(ptr);
return result;
}
///
/// 読取データをテストデータ2用構造体へセット
///
///
///
///
static strcTestData2? getTestData2(char[] cArray, Encoding encode){
// 返却値
strcTestData2? result = null;
// α:読取データのバイト配列
byte[] bArray = encode.GetBytes(cArray);
// β:読取データをセットする構造体のサイズ
int size = Marshal.SizeOf(typeof(strcTestData2));
// α≠βの場合は終了
if(bArray.Length != size) return result;
// γ:アンマネージメモリからメモリ割当
IntPtr ptr = Marshal.AllocHGlobal(size);
// αをγへコピー
Marshal.Copy(bArray, 0, ptr, size);
// γを返却構造体へセット
result = (strcTestData2)Marshal.PtrToStructure(ptr, typeof(strcTestData2));
// γを開放
Marshal.FreeHGlobal(ptr);
return result;
}
#endregion
#region 各構造体へ情報をセット
///
/// テストデータ1リストへ値をセット
/// (引数nullでリセット)
///
///
public void setTestData1(strcTestData1? data){
if(null == data){
this._TestData1 = new List();
}else{
this._TestData1.Add(data.Value);
}
}
///
/// テストデータ2リストへ値をセット
/// (引数nullでリセット)
///
///
public void setTestData2(strcTestData2? data){
if(null == data){
this._TestData2 = new List();
}else{
this._TestData2.Add(data.Value);
}
}
#endregion
}
}