G3507_And_note_結果登録機能の追加
2025年1月31日
6:09
G3507_andoroid_and_note_result_flg_function
設計
レコード書式
テキストファイルの最大登録件数をグローバル変数に追加します
public static int Note_lin_cnt; // 現在登録件数
本日表示の関数でカウンターを加算します
// 本日表示
private void today_dsp()
{
Sys_cal_get(); // システム日付取得
string fil_path = Get_fil_path(Sys_yy_all, Sys_mm, Sys_dd, "");
string[] cut_tbl = new string[20];
string lin_cst = "";
int lin_no = 0;
int cnt;
bool find_flg;
if (File.Exists(fil_path) == false) return;
Edt_tbl[0].Text = "";
Note_lin_cnt = 0;
// テキストファイル読み込み
System.Text.Encoding.GetEncoding("utf-8");
using (StreamReader red = new StreamReader(fil_path))
{
while (red.EndOfStream == false)
{
// 0 1 2 3 4 5 6 7 8 9 10 11
// yyyy mm dd th tm ts msg GPS th tm ts msg
lin_cst = red.ReadLine();
if (lin_cst == null) continue;
cut_tbl = lin_cst.Split(',');
++lin_no;
++Note_lin_cnt;
// 除外ワードの判定を追加します
find_flg = false;
for (cnt = 0; cnt < Exclu_key_cnt; cnt++)
{
// 先頭文字とキーワードが一致したら除外します
// 一致しない場合(-1) 一致しても先頭以外は1以上の整数になります
if (cut_tbl[6].IndexOf(Exclu_key_tbl[cnt], 0) == 0)
{
find_flg = true;
break;
}
}
if (find_flg == false) Edt_tbl[0].Text += lin_no.ToString() + "." + cut_tbl[6] + "\r\n";
}
}
}
インフォメーションの臨時関数を作成します
// 臨時のインフォメーション
private void dsp_info(string prm_msg)
{
But_tbl[5].Text = prm_msg;
}
// 臨時のインフォメーション 初期化
private void dsp_info_clr()
{
But_tbl[5].Text = "";
}
// 臨時のインフォメーション
private void dsp_info2(string prm_msg)
{
But_tbl[6].Text = prm_msg;
}
コマンドを追加します
フラグ登録の書式判定関数を追加します
// フラグ登録の書式判定をします
private bool note_flg_form_chk(string prm_wrt_cst, string[] prm_flg_tbl)
{
string[] cut_tbl = new string[10];
string flg_lin_cst;
int flg_lin_no;
string flg_cst;
if (prm_wrt_cst == "")
{
dsp_info("**書き込みデータが空白です**");
return (false);
}
// スペースで分解します
cut_tbl = prm_wrt_cst.Split(' ', ' ');
if (cut_tbl.Length < 2)
{
dsp_info("**書き込みデータが不足しています**");
return (false);
}
// 行番号取得
flg_lin_cst = Regex.Replace(cut_tbl[0], @"[^0-9]", "");
flg_lin_no = int.Parse(flg_lin_cst);
if (flg_lin_no < 1 || Note_lin_cnt < flg_lin_no)
{
dsp_info("**行番号が範囲外です**");
return (false);
}
// フラグ名取得
flg_cst = cut_tbl[1].Trim();
if (flg_cst == "")
{
dsp_info("**文字指定がありません**");
return (false);
}
prm_flg_tbl[0] = flg_lin_no.ToString();
prm_flg_tbl[1] = flg_cst;
dsp_info("「実行/保存」ボタンを押してください");
return true;
}
コマンド共通処理を関数化します
// コマンド共通処理
private void command_but_on()
{
string[] cst_tbl = new string[3];
switch (Command_name)
{
case "本日表示":
command_name_set("本日表示");
today_dsp();
break;
case "除外ワード":
command_name_set("除外ワード");
Edt_tbl[0].Text = Voice_cst = "";
voice_call(); // 音声変換呼び出し
break;
case "削除":
case "抹消":
case "消去":
// どの文言でも同じ結果が得られます
command_name_set(Command_name);
Edt_tbl[0].Text = Voice_cst = "";
voice_call(); // 音声変換呼び出し
break;
case "結果登録":
case "フラグ登録":
case "状態登録":
command_name_set(Command_name);
// この処理では、エディットボックスをクリアせず
// 何度も音声変換を受け付けます
// 書式が不正の場合は、音声変換をコールします
if(note_flg_form_chk(Edt_tbl[0].Text, cst_tbl) == false) voice_call(); // 音声変換呼び出し
note_flg_form_chk(Edt_tbl[0].Text, cst_tbl);
break;
}
}
コマンド共通処理のボタン処理を変更します
command_but_on();
実行/保存ボタンの関数を修正します
case "結果登録":
case "フラグ登録":
case "状態登録":
note_flg_wrt(Edt_tbl[0].Text);
break;
ここまでをクリーン、配置して確認します
2行を完成登録します
日単位テキストファイルのレコード書式を構成する関数を追加します
// テキストファイルのレコード配置処理
private string txt_fil_rec_gene(string[] prm_fld_tbl)
{
// 年,月,日,時,分,秒,記事データ,GPS,時,分,秒,結果名称
// 0 1 2 3 4 5 6 7 8 9 10 11
int fld_max = 11;
int cnt;
string ans_cst = "";
for(cnt=0;cnt<fld_max;cnt++)
{
if (cnt == 0) ans_cst = prm_fld_tbl[cnt];
else ans_cst += "," + prm_fld_tbl[cnt];
}
return ans_cst;
}
日単位テキストファイルのレコード書式を増設します
// 音声テキストの日単位保持処理
private async void edit_txt_sav()
{
string wrt_cst;
string fil_path;
string[] fld_tbl = new string[20];
Sys_cal_get(); // システム日付取得
fil_path = Get_fil_path(Sys_yy_all, Sys_mm, Sys_dd, "");
// ファイルを開く
System.Text.Encoding.GetEncoding("utf-8");
using (StreamWriter writer = new StreamWriter(fil_path, true))
{
fld_tbl[0] = Sys_yy_all.ToString("D4"); // 年
fld_tbl[1] = Sys_mm.ToString("D2"); // 月
fld_tbl[2] = Sys_dd.ToString("D2"); // 日
fld_tbl[3] = Sys_th.ToString("D2"); // 時
fld_tbl[4] = Sys_tm.ToString("D2"); // 分
fld_tbl[5] = Sys_ts.ToString("D2"); // 秒
fld_tbl[6] = Edt_tbl[0].Text; // 登録データ
await get_GPS();
fld_tbl[7] = Gps_cst;
fld_tbl[8] = ""; // 時
fld_tbl[9] = ""; // 分
fld_tbl[10] = ""; // 秒
fld_tbl[11] = ""; // フラグデータ
wrt_cst = txt_fil_rec_gene(fld_tbl);
writer.WriteLine(wrt_cst); // テキストを書き込む
writer.Flush(); // 書き込んだ内容をフラッシュする
}
Edt_tbl[0].Text = Voice_cst = "";
}
レコードの転写とコピー処理は削除と同じなので関数名を変更します
// 指示された行の削除やフラグデータの登録処理
private void txt_fil_rec_cpy(
string prm_type, // "D" 削除 "F" フラグ登録
string prm_lin_no_cst, // 対象行
string prm_data_cst) // フラグ登録の書き込みデータ
{
string fil_path;
string fil_path_cpy;
string txt_buf = "";
int lin_no = 0;
int sel_lin_no = 0;
string tmp_cst;
string[] cut_tbl = new string[20];
string[] fld_tbl = new string[20];
int i;
if(prm_type == "D")
{
// 削除処理
// 削除行文字列から、数値を抽出する "9行" -> 9
tmp_cst = Regex.Replace(prm_lin_no_cst, @"[^0-9]", "");
sel_lin_no = int.Parse(tmp_cst);
}
else
{
// フラグ登録処理
sel_lin_no = int.Parse(prm_lin_no_cst);
}
if (sel_lin_no == 0) return; // 削除行 or フラグ書き込み行なし
Sys_cal_get(); // システム日付取得
fil_path = Get_fil_path(Sys_yy_all, Sys_mm, Sys_dd, "");
fil_path_cpy = Get_fil_path(Sys_yy_all, Sys_mm, Sys_dd, "_cpy");
// 前回の残骸があれば削除する
if (File.Exists(fil_path_cpy) == true) File.Delete(fil_path_cpy);
while (File.Exists(fil_path_cpy) == true) { }
System.Text.Encoding.GetEncoding("utf-8");
StreamReader red = new StreamReader(fil_path); // 読み込みOPEN
System.Text.Encoding.GetEncoding("utf-8");
StreamWriter writer = new StreamWriter(fil_path_cpy, true); // 書き込みOPEN
while (red.EndOfStream == false)
{
txt_buf = red.ReadLine();
if (txt_buf == null) continue;
++lin_no;
if (lin_no == sel_lin_no) // 対象行が一致しました
{
if (prm_type == "D") continue; // 書き込みしないことで削除扱いです
else
{
// フラグ登録処理
cut_tbl = txt_buf.Split(','); // 対象レコードを分解します
// 前回データのコピー
for(i=0;i<7+1;i++) fld_tbl[i] = cut_tbl[i]; // 年 - GPS
// フラグデータの代入
fld_tbl[8] = Sys_th.ToString("D2"); // 時
fld_tbl[9] = Sys_tm.ToString("D2"); // 分
fld_tbl[10] = Sys_ts.ToString("D2");// 秒
fld_tbl[11] = prm_data_cst; // フラグデータ
// 新しいフラグデータを付加しレコードの再構成
txt_buf = txt_fil_rec_gene(fld_tbl);
}
}
writer.WriteLine(txt_buf);
}
red.Close(); // 読み込みクローズ
writer.Close(); // 書き込みクローズ
// 原本削除
if (File.Exists(fil_path) == true) File.Delete(fil_path);
while (File.Exists(fil_path) == true) { }
// 転写から原本へコピー
File.Copy(fil_path_cpy, fil_path);
// 今回の作業ファイルを削除する
if (File.Exists(fil_path_cpy) == true) File.Delete(fil_path_cpy);
while (File.Exists(fil_path_cpy) == true) { }
}
ここまでをクリーン、配置し確認します
レコードの構成を確認します
デスクトップ上のテキストファイルをメモ帳で開きます
削除機能が壊れていないか確認します
フラグ登録機能の確認
フラグ書き込みのデバックに相当時間を費やしました
このようにフラグ登録は成功しました
中間の空白を削除する
文字列の中間に存在する空白を除外する
System.Text.RegularExpressions.Regex.Replace(txtReplaceBefore.Text, @"\s", "");
半角 半角文字 全角 全角文字 半角+全角 すべて処理できました
string msg_cst;
msg_cst = Regex.Replace("フラグ 登録", @"\s", "");
msg(msg_cst);
msg_cst = Regex.Replace("フラグ 登録", @"\s", "");
msg(msg_cst);
msg_cst = Regex.Replace("フラグ 登録", @"\s", "");
msg(msg_cst);
msg_cst = Regex.Replace("フラグ 登録", @"\s", "");
msg(msg_cst);
msg_cst = Regex.Replace("フラグ 登録", @"\s", "");
msg(msg_cst);
msg_cst = Regex.Replace("フラグ 登録 削除", @"\s", "");
msg(msg_cst);
コマンド受音後の、「実行/保存ボタン」の改良
case "コマンド":
// コマンドは中間のスペースを削除します
tmp_cst = Edt_tbl[0].Text.Trim();
tmp_cst = Regex.Replace(tmp_cst, @"\s", "");
tmp_cst = command_name_sarch(tmp_cst);
if (tmp_cst != "") command_name_set(tmp_cst);
else command_name_set("----- 未登録です -----");
break;
追記 ログファイルで記録できる、メッセージ関数の移植
メッセージカウンターをグローバル変数に追加します
public static int Msg_cnt = 0; // エラーログ件数
プログラム初期化ループの直後にローカル配置します
// エラーメッセージ ログファイル
private void msg2(string prm_msg1, string prm_msg2, string prm_msg3)
{
// テキストファイル名のみでは処理出来ない
string fil_path = Path_com+@"/999_err_msg.log";
string txt_buf;
DateTime dt = DateTime.Now;
// ファイルを開く
System.Text.Encoding.GetEncoding("utf-8");
using (StreamWriter wrt = new StreamWriter(fil_path, true))
{
txt_buf = dt.ToString("yyyy/MM/dd HH:mm") + ",(" + Msg_cnt.ToString() + "),"
+ prm_msg1 + "," + prm_msg2 + "," + prm_msg3;
wrt.WriteLine(txt_buf);
wrt.Flush(); // 書き込んだ内容をフラッシュする
++Msg_cnt;
}
}
ログファイルの確認