Exemple #1
0
        //────────────────────────────────────────
        /// <summary>
        /// Listを作成します。
        /// 
        /// セルのデータ型は全て string です。
        /// </summary>
        /// <param name="csvText"></param>
        /// <returns></returns>
        public List<string[]> Read(
            string string_Csv
            )
        {
            //
            // テーブルを作成します。
            //
            List<string[]> list_ArrayString = new List<string[]>();

            System.IO.StringReader reader = new System.IO.StringReader(string_Csv);
            CsvLineParserImpl csvParser = new CsvLineParserImpl();

            // CSVを解析して、テーブル形式で格納。
            {
                int nRowIndex = 0;
                while (-1 < reader.Peek())
                {
                    string sLine = reader.ReadLine();

                    //
                    // 配列の返却値を、ダイレクトに渡します。
                    //
                    string[] sFields = csvParser.UnescapeLineToFieldList(sLine, this.CharSeparator).ToArray();
                    list_ArrayString.Add(sFields);

                    nRowIndex++;
                }
            }

            // ストリームを閉じます。
            reader.Close();

            return list_ArrayString;
        }
Exemple #2
0
        //────────────────────────────────────────
        #endregion



        #region アクション
        //────────────────────────────────────────

        /// <summary>
        /// Listを作成します。
        ///
        /// セルのデータ型は全て string です。
        /// </summary>
        /// <param name="csvText"></param>
        /// <returns></returns>
        public List <string[]> Read(
            string string_Csv
            )
        {
            //
            // テーブルを作成します。
            //
            List <string[]> list_ArrayString = new List <string[]>();

            System.IO.StringReader reader    = new System.IO.StringReader(string_Csv);
            CsvLineParserImpl      csvParser = new CsvLineParserImpl();

            // CSVを解析して、テーブル形式で格納。
            {
                int nRowIndex = 0;
                while (-1 < reader.Peek())
                {
                    string sLine = reader.ReadLine();

                    //
                    // 配列の返却値を、ダイレクトに渡します。
                    //
                    string[] sFields = csvParser.UnescapeLineToFieldList(sLine, this.CharSeparator).ToArray();
                    list_ArrayString.Add(sFields);

                    nRowIndex++;
                }
            }

            // ストリームを閉じます。
            reader.Close();

            return(list_ArrayString);
        }
Exemple #3
0
        private void Form1_Load(object sender, EventArgs e)
        {
            CsvLineParserImpl csvParser = new CsvLineParserImpl();

            List<string> list = csvParser.UnescapeLineToFieldList("\"    if ( $s4 == \"\",\"\" ){\"", ',');

            int num = 0;
            foreach (string field in list)
            {
                System.Console.Write("("+num+") field=["+field+"]");
                num++;
            }
        }
Exemple #4
0
        private void Form1_Load(object sender, EventArgs e)
        {
            CsvLineParserImpl csvParser = new CsvLineParserImpl();

            List <string> list = csvParser.UnescapeLineToFieldList("\"    if ( $s4 == \"\",\"\" ){\"", ',');

            int num = 0;

            foreach (string field in list)
            {
                System.Console.Write("(" + num + ") field=[" + field + "]");
                num++;
            }
        }
Exemple #5
0
        //────────────────────────────────────────
        /// <summary>
        /// Listを作成します。
        /// 
        /// セルのデータ型は全て string です。
        /// 
        /// 【仕様変更 2011-03-01】空行、スペースだけの行は、トークンに入れません。
        /// 【仕様変更 2011-03-01】行の最後が「,」で終わる場合、最後のトークンは空白が入っているのではなく、追加しません。
        /// </summary>
        /// <param name="csvText"></param>
        /// <returns></returns>
        public List<string> Read(
            string string_Csv
            )
        {
            // テーブルを作成します。
            List<string> list_String = new List<string>();

            System.IO.StringReader reader = new System.IO.StringReader(string_Csv);
            CsvLineParserImpl csvParser = new CsvLineParserImpl();

            // CSVを解析して、テーブル形式で格納。
            {
                string[] fields;
                while (-1 < reader.Peek())
                {
                    string sLine = reader.ReadLine();
                    // 空行、スペースだけの行を拾うこともある。

                    if ("" != sLine.Trim())
                    {
                        //
                        // 「空行、スペースだけの行」ではない場合。

                        fields = csvParser.UnescapeLineToFieldList(sLine, this.CharSeparator).ToArray();

                        //essageBox.Show("ttbwIndex=[" + ttbwIndex + "]行目ループ", "デバッグ2");

                        for (int nColumnIndex = 0; nColumnIndex < fields.Length; nColumnIndex++)
                        {
                            if (nColumnIndex + 1 <= fields.Length && "" == fields[nColumnIndex].Trim())
                            {
                                // 行の最後が「,」で終わる場合、最後のトークンは空白が入っているのではなく、追加しません。
                                break;
                            }
                            list_String.Add(fields[nColumnIndex]);
                        }
                    }

                }
            }

            // ストリームを閉じます。
            reader.Close();

            return list_String;
        }
Exemple #6
0
        //────────────────────────────────────────
        #endregion



        #region アクション
        //────────────────────────────────────────

        /// <summary>
        /// Listを作成します。
        ///
        /// セルのデータ型は全て string です。
        ///
        /// 【仕様変更 2011-03-01】空行、スペースだけの行は、トークンに入れません。
        /// 【仕様変更 2011-03-01】行の最後が「,」で終わる場合、最後のトークンは空白が入っているのではなく、追加しません。
        /// </summary>
        /// <param name="csvText"></param>
        /// <returns></returns>
        public List <string> Read(
            string string_Csv
            )
        {
            // テーブルを作成します。
            List <string> list_String = new List <string>();

            System.IO.StringReader reader    = new System.IO.StringReader(string_Csv);
            CsvLineParserImpl      csvParser = new CsvLineParserImpl();

            // CSVを解析して、テーブル形式で格納。
            {
                string[] fields;
                while (-1 < reader.Peek())
                {
                    string sLine = reader.ReadLine();
                    // 空行、スペースだけの行を拾うこともある。

                    if ("" != sLine.Trim())
                    {
                        //
                        // 「空行、スペースだけの行」ではない場合。

                        fields = csvParser.UnescapeLineToFieldList(sLine, this.CharSeparator).ToArray();

                        //essageBox.Show("ttbwIndex=[" + ttbwIndex + "]行目ループ", "デバッグ2");

                        for (int nColumnIndex = 0; nColumnIndex < fields.Length; nColumnIndex++)
                        {
                            if (nColumnIndex + 1 <= fields.Length && "" == fields[nColumnIndex].Trim())
                            {
                                // 行の最後が「,」で終わる場合、最後のトークンは空白が入っているのではなく、追加しません。
                                break;
                            }
                            list_String.Add(fields[nColumnIndex]);
                        }
                    }
                }
            }

            // ストリームを閉じます。
            reader.Close();

            return(list_String);
        }
Exemple #7
0
        //────────────────────────────────────────
        #endregion



        #region アクション
        //────────────────────────────────────────

        /// <summary>
        /// TODO:「,」「"」に対応したい。
        ///
        ///
        /// 縦、横がひっくり返っていて、
        /// 型定義レコードがないCSVテーブルの読取。
        /// </summary>
        /// <param name="csvText"></param>
        /// <returns>列名情報も含むテーブル。</returns>
        public Table_Humaninput Read(
            string string_Csv,
            Request_ReadsTable forTable_Request,
            Format_Table_Humaninput forTable_Format,
            Log_Reports log_Reports
            )
        {
            Log_Method log_Method = new Log_MethodImpl();

            log_Method.BeginMethod(Info_Table.Name_Library, this, "Read", log_Reports);

            //
            //
            //
            //
            CsvLineParserImpl csvParser = new CsvLineParserImpl();

            Table_Humaninput xenonTable = new Table_HumaninputImpl(
                forTable_Request.Name_PutToTable, forTable_Request.Expression_Filepath, forTable_Request.Expression_Filepath.Cur_Configuration);

            xenonTable.Tableunit               = forTable_Request.Tableunit;
            xenonTable.Typedata                = forTable_Request.Typedata;
            xenonTable.IsDatebackupActivated   = forTable_Request.IsDatebackupActivated;
            xenonTable.Format_Table_Humaninput = forTable_Format;


            //
            // 一旦、テーブルを全て読み込みます。
            //
            List <List <string> > lines = new List <List <string> >();

            {
                // CSVテキストを読み込み、型とデータのバッファーを作成します。
                System.IO.StringReader reader = new System.IO.StringReader(string_Csv);


                string[] sFields;
                while (-1 < reader.Peek())
                {
                    string        sLine  = reader.ReadLine();
                    List <string> tokens = new List <string>();

                    sFields = csvParser.UnescapeLineToFieldList(sLine, this.charSeparator).ToArray();

                    int nColumnIndex = 0;
                    foreach (string sToken in sFields)
                    {
                        if (nColumnIndex == 0 && ToCsv_Table_Humaninput_RowColRegularImpl.S_END == sToken.Trim().ToUpper())
                        {
                            // 1列目にENDがある場合、その手前までの列が有効データです。
                            // END以降の行は無視します。
                            goto row_end;
                        }


                        tokens.Add(sToken);

                        nColumnIndex++;
                    }
                    lines.Add(tokens);
                }
row_end:

                // ストリームを閉じます。
                reader.Close();
            }



            //
            // 型定義部
            //
            // (※NO,ID,EXPL,NAME など、フィールドの定義を持つテーブル)
            //
            RecordFielddefinition recordFielddefinition = new RecordFielddefinitionImpl();

            //
            // データ・テーブル部
            //
            List <List <string> > rows = new List <List <string> >();

            //
            // まず、0列目、1列目のデータを読み取ります。
            //
            int nRowIndex = 0;

            foreach (List <string> tokens in lines)
            {
                Fielddefinition fieldDefinition = null;



                int nColumnIndex = 0;
                foreach (string sToken in tokens)
                {
                    if (0 == nColumnIndex)
                    {
                        //
                        // 0列目は、フィールド名です。
                        //
                        string sFieldName = sToken;//.Trim().ToUpper();

                        // テーブルのフィールドを追加します。フィールドの型は、intに固定です。
                        fieldDefinition = new FielddefinitionImpl(sFieldName, EnumTypeFielddefinition.Int);
                        recordFielddefinition.Add(fieldDefinition);
                    }
                    else if (1 == nColumnIndex)
                    {
                        //
                        // 1列目は、フィールドのコメントとします。
                        //
                        nColumnIndex = 1;
                        {
                            fieldDefinition.Comment = sToken;
                        }
                    }
                    else
                    {
                        //
                        // 2列目から右側は、データ・テーブル部。
                        //

                        if (0 == nRowIndex)
                        {
                            //
                            // 先頭行
                            //

                            //
                            // 「EOF」というトークンが出てくるまで。
                            //
                            if (ToCsv_Table_Humaninput_RowColRegularImpl.S_EOF == sToken.Trim().ToUpper())
                            {
                                goto column_end;
                            }

                            List <string> record = new List <string>();

                            // 1番目のフィールド_データを追加。
                            record.Add(sToken);

                            rows.Add(record);
                        }
                        else
                        {
                            //
                            // 2番目以降のフィールド_データを追加。
                            //

                            //
                            // 先頭の2つのレコード分、切り詰めます。
                            //
                            int nDataIndex = nColumnIndex - 2;
                            if (nDataIndex < rows.Count)
                            {
                                List <string> record = rows[nDataIndex];

                                record.Add(sToken);
                            }
                            else
                            {
                                // 無視
                            }
                        }
                    }


                    nColumnIndex++;
                }//c
column_end:


                nRowIndex++;
            }



            //essageBox.Show("CSV読取終わり1 rows.Count=[" + rows.Count + "]", "TableCsvLibデバッグ");

            // テーブル作成。テーブルのフィールド型定義と、データ本体をセットします。
            xenonTable.CreateTable(recordFielddefinition, log_Reports);
            if (log_Reports.Successful)
            {
                xenonTable.AddRecordList(rows, recordFielddefinition, log_Reports);
                //essageBox.Show("CSV読取後のテーブル作成終わり", "TableCsvLibデバッグ");
            }

            goto gt_EndMethod;
            //
            //
gt_EndMethod:
            log_Method.EndMethod(log_Reports);
            return(xenonTable);
        }
Exemple #8
0
        //────────────────────────────────────────
        #endregion



        #region アクション
        //────────────────────────────────────────

        /// <summary>
        /// 文字列エスケープされます。
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="comment"></param>
        public void AddWithEscape(string name, string type, string comment)
        {
            this.name_.Add(CsvLineParserImpl.EscapeCell(name));
            this.type_.Add(CsvLineParserImpl.EscapeCell(type));
            this.comment_.Add(CsvLineParserImpl.EscapeCell(comment));
        }
        //────────────────────────────────────────
        public string ToCsvText(
            Table_Humaninput tableH,
            Log_Reports log_Reports
            )
        {
            Log_Method log_Method = new Log_MethodImpl(0);
            log_Method.BeginMethod(Info_Table.Name_Library, this, "ToCsvText",log_Reports);

            Log_TextIndented result = new Log_TextIndentedImpl();

            RecordFielddefinition error_RecordFielddefinition;
            Exception err_Excep;
            int error_IndexColumn;
            Fielddefinition error_Fielddefinition;
            object error_Item;

            if (null == tableH)
            {
                // エラー
                goto gt_Error_NullTable;
            }

            CsvLineParserImpl csvParser = new CsvLineParserImpl();

            // フィールド名をカンマ区切りで出力します。最後にENDを付加します。

            // フィールド定義部
            if (tableH.RecordFielddefinition.Count < 1)
            {
                //エラー。
                error_RecordFielddefinition = tableH.RecordFielddefinition;
                goto gt_Error_FieldZero;
            }

            // フィールド定義部:名前
            tableH.RecordFielddefinition.ForEach(delegate(Fielddefinition fielddefinition, ref bool isBreak, Log_Reports log_Reports2)
            {
                if (this.ExceptedFields.TryExceptedField(fielddefinition.Name_Trimupper))
                {
                    // 出力しないフィールドの場合、無視します。
                }
                else
                {
                    result.Append(csvParser.EscapeCell(fielddefinition.Name_Humaninput));
                    result.Append(",");
                }
            }, log_Reports);
            result.Append(ToCsv_Table_Humaninput_RowColRegularImpl.S_END);
            result.Append(Environment.NewLine);//改行

            // フィールド定義部:型
            tableH.RecordFielddefinition.ForEach(delegate(Fielddefinition fielddefinition, ref bool isBreak, Log_Reports log_Reports2)
            {
                if (this.ExceptedFields.TryExceptedField(fielddefinition.Name_Trimupper))
                {
                    // 出力しないフィールドの場合、無視します。
                }
                else
                {
                    switch(fielddefinition.Type_Field)
                    {
                        case EnumTypeFielddefinition.String:
                            {
                                result.Append(FielddefinitionImpl.S_STRING);
                            }
                            break;
                        case EnumTypeFielddefinition.Int:
                            {
                                result.Append(FielddefinitionImpl.S_INT);
                            }
                            break;
                        case EnumTypeFielddefinition.Bool:
                            {
                                result.Append(FielddefinitionImpl.S_BOOL);
                            }
                            break;
                        default:
                            {
                                // TODO エラー対応。

                                // 未定義の型があった場合、そのまま出力します。
                                // C#のメッセージになるかと思います。
                                result.Append(fielddefinition.ToString_Type());
                            }
                            break;
                    }

                    result.Append(",");
                }
            }, log_Reports);
            result.Append(ToCsv_Table_Humaninput_RowColRegularImpl.S_END);
            result.Append(Environment.NewLine);//改行

            // フィールド定義部:コメント
            tableH.RecordFielddefinition.ForEach(delegate(Fielddefinition fielddefinition, ref bool isBreak, Log_Reports log_Reports2)
            {
                if (this.ExceptedFields.TryExceptedField(fielddefinition.Name_Trimupper))
                {
                    // 出力しないフィールドの場合、無視します。
                }
                else
                {
                    result.Append(csvParser.EscapeCell(fielddefinition.Comment));
                    result.Append(",");
                }
            }, log_Reports);
            result.Append(ToCsv_Table_Humaninput_RowColRegularImpl.S_END);
            result.Append(Environment.NewLine);//改行

            // 0行目から数えて3行目以降はデータ・テーブル部。

            // データ・テーブル部
            DataTable dataTable = tableH.DataTable;

            // 各行について
            for (int nRowIndex = 0; nRowIndex < dataTable.Rows.Count; nRowIndex++)
            {
                DataRow dataRow = dataTable.Rows[nRowIndex];

                //
                // 各フィールドについて
                //
                object[] itemArray = dataRow.ItemArray;// ItemArrayは1回の呼び出しが重い。
                for (int indexColumn = 0; indexColumn < itemArray.Length; indexColumn++)
                {

                    // TODO:範囲 リストサイズが0の時がある←プログラムミス?
                    Fielddefinition fielddefinition;
                    try
                    {
                        fielddefinition = tableH.RecordFielddefinition.ValueAt(indexColumn);
                    }
                    catch (Exception e)
                    {
                        // エラー。
                        err_Excep = e;
                        error_RecordFielddefinition = tableH.RecordFielddefinition;
                        error_IndexColumn = indexColumn;
                        goto gt_Error_OutOfIndex;
                    }

                    if (this.ExceptedFields.TryExceptedField(fielddefinition.Name_Trimupper))
                    {
                        // 出力しないフィールドの場合、無視します。
                    }
                    else
                    {
                        string value_Cell;
                        object item = itemArray[indexColumn];

                        if (item is Value_Humaninput)
                        {
                            value_Cell = ((Value_Humaninput)item).Text;
                        }
                        else if (item is string)
                        {
                            //フィールド定義部など。
                            value_Cell = (string)item;
                        }
                        else if (item is DBNull)
                        {
                            //空欄。
                            value_Cell = "";
                        }
                        else
                        {
                            // エラー
                            error_Item = item;
                            error_Fielddefinition = fielddefinition;
                            goto gt_Error_UndefinedFieldType;
                        }

                        result.Append(csvParser.EscapeCell(value_Cell));
                        result.Append(this.charSeparator);
                    }
                }
                result.Append(ToCsv_Table_Humaninput_RowColRegularImpl.S_END);
                result.Append(Environment.NewLine);//改行
            }
            result.Append(ToCsv_Table_Humaninput_RowColRegularImpl.S_EOF);
            // 最後に一応、改行を付けておきます。
            result.Append(Environment.NewLine);//改行

            goto gt_EndMethod;
            //
            //
            #region 異常系
            //────────────────────────────────────────
            gt_Error_FieldZero:
            if (log_Reports.CanCreateReport)
            {
                Log_RecordReports r = log_Reports.BeginCreateReport(EnumReport.Error);
                r.SetTitle("▲エラー854!", log_Method);

                Log_TextIndented s = new Log_TextIndentedImpl();

                s.Append("(プログラム内部エラー)テーブルの列定義が0件です。 error_RecordFielddefinition.Count[");
                s.Append(error_RecordFielddefinition.Count);
                s.Append("] テーブル名=[");
                s.Append(tableH.Name);
                s.Append("]");
                s.Newline();

                // ヒント

                r.Message = s.ToString();
                log_Reports.EndCreateReport();
            }
            goto gt_EndMethod;
            //────────────────────────────────────────
            gt_Error_OutOfIndex:
            if (log_Reports.CanCreateReport)
            {
                Log_RecordReports r = log_Reports.BeginCreateReport(EnumReport.Error);
                r.SetTitle("▲エラー853!", log_Method);

                Log_TextIndented s = new Log_TextIndentedImpl();

                s.Append("(プログラム内部エラー)err_NColIndex=[");
                s.Append(error_IndexColumn);
                s.Append("] error_RecordFielddefinition.Count[");
                s.Append(error_RecordFielddefinition.Count);
                s.Append("]");
                s.Newline();

                // ヒント
                s.Append(r.Message_SException(err_Excep));

                r.Message = s.ToString();
                log_Reports.EndCreateReport();
            }
            goto gt_EndMethod;
            //────────────────────────────────────────
            gt_Error_UndefinedFieldType:
            if (log_Reports.CanCreateReport)
            {
                Log_RecordReports r = log_Reports.BeginCreateReport(EnumReport.Error);
                r.SetTitle("▲エラー855!", log_Method);

                Log_TextIndented s = new Log_TextIndentedImpl();

                s.Append("(プログラム内部エラー)CSVを出力しようとしたとき、未定義のフィールド型=[");
                s.Append(error_Fielddefinition.ToString_Type());
                s.Append("]がありました。");
                s.Newline();

                s.Append("型名=[");
                s.Append(error_Item.GetType().Name);
                s.Append("]");
                s.Newline();

                s.Append("型は[");
                s.Append(typeof(String_HumaninputImpl));
                s.Append("],[");
                s.Append(typeof(Int_HumaninputImpl));
                s.Append("],[");
                s.Append(typeof(Bool_HumaninputImpl));
                s.Append("]が使えます。");
                s.Newline();

                // ヒント

                r.Message = s.ToString();
                log_Reports.EndCreateReport();
            }
            goto gt_EndMethod;
            //────────────────────────────────────────
            gt_Error_NullTable:
            if (log_Reports.CanCreateReport)
            {
                Log_RecordReports r = log_Reports.BeginCreateReport(EnumReport.Error);
                r.SetTitle("▲エラー852!", log_Method);

                Log_TextIndented s = new Log_TextIndentedImpl();

                s.Append("(プログラム内部エラー)tableがヌルでした。");
                s.Newline();

                // ヒント

                r.Message = s.ToString();
                log_Reports.EndCreateReport();
            }
            goto gt_EndMethod;
            //────────────────────────────────────────
            #endregion
            //
            //
            gt_EndMethod:
            log_Method.EndMethod(log_Reports);
            return result.ToString();
        }
Exemple #10
0
        //────────────────────────────────────────
        #endregion



        #region アクション
        //────────────────────────────────────────

        /// <summary>
        /// TODO:「,」「"」に対応したい。
        ///
        ///
        /// 縦と横が逆のテーブル。
        ///
        /// CSVを読取り、テーブルにして返します。
        ///
        ///
        /// SRS仕様の実装状況
        /// ここでは、先頭行を[0]行目と数えるものとします。
        /// (1)CSVの[0]行目は列名です。
        /// (2)CSVの[1]行目は型名です。
        /// (3)CSVの[2]行目はコメントです。
        ///
        /// (4)データ・テーブル部で、0列目に「EOF」と入っていれば終了。大文字・小文字は区別せず。
        ///    それ以降に、コメントのようなデータが入力されていることがあるが、フィールドの型に一致しないことがあるので無視。
        ///    TODO EOF以降の行も、コメントとして残したい。
        ///
        /// (5)列名にENDがある場合、その手前までの列が有効データです。
        ///    END以降の列は無視します。
        ///    TODO END以降の行も、コメントとして残したい。
        ///
        /// (6)int型として指定されているフィールドのデータ・テーブル部に空欄があった場合、DBNull(データベース用のヌル)とします。
        /// </summary>
        /// <param name="csvText"></param>
        /// <returns>列名情報も含むテーブル。列の型は文字列型とします。</returns>
        public Table_Humaninput Read(
            string string_Csv,
            Request_ReadsTable forTable_Request,
            Format_Table forTable_Format,
            Log_Reports log_Reports
            )
        {
            Log_Method log_Method = new Log_MethodImpl();

            log_Method.BeginMethod(Info_Table.Name_Library, this, "Read", log_Reports);

            //
            //
            //
            //
            CsvLineParserImpl csvParser = new CsvLineParserImpl();


            Table_Humaninput xenonTable = new Table_HumaninputImpl(
                forTable_Request.Name_PutToTable, forTable_Request.Expression_Filepath, forTable_Request.Expression_Filepath.Conf);

            xenonTable.Tableunit               = forTable_Request.Tableunit;
            xenonTable.Typedata                = forTable_Request.Typedata;
            xenonTable.IsDatebackupActivated   = forTable_Request.IsDatebackupActivated;
            xenonTable.Format_Table_Humaninput = forTable_Format;


            //
            // 一旦、テーブルを全て読み込みます。
            //
            List <List <string> > lines = new List <List <string> >();

            {
                // CSVテキストを読み込み、型とデータのバッファーを作成します。
                System.IO.StringReader reader = new System.IO.StringReader(string_Csv);


                while (-1 < reader.Peek())
                {
                    string        sLine  = reader.ReadLine();
                    List <string> tokens = new List <string>();

                    string[] sFields;
                    sFields = csvParser.UnescapeLineToFieldList(sLine, this.charSeparator).ToArray();

                    int nColumnIndex = 0;
                    foreach (string sToken in sFields)
                    {
                        if (nColumnIndex == 0 &&
                            (
                                ToCsv_Table_RowColRegularImpl_.S_EOL == sToken.Trim().ToUpper() ||
                                ToCsv_Table_RowColRegularImpl_.S_END == sToken.Trim().ToUpper()
                            )
                            )
                        {
                            // 1列目にENDがある場合、その手前までの列が有効データです。
                            // END以降の行は無視します。
                            goto row_end;
                        }


                        tokens.Add(sToken);

                        nColumnIndex++;
                    }
                    lines.Add(tokens);
                }
row_end:

                // ストリームを閉じます。
                reader.Close();
            }



            //
            // 型定義部
            //
            // (※NO,ID,EXPL,NAME など、フィールドの定義を持つテーブル)
            //
            RecordFielddef recordFielddef = new RecordFielddefImpl();

            //
            // データ・テーブル部
            //
            List <List <string> > rows = new List <List <string> >();

            //
            // まず、0列目、1列目、2列目のデータを読み取ります。
            //
            int nRowIndex = 0;

            foreach (List <string> tokens in lines)
            {
                Fielddef fieldDefinition = null;



                int nColumnIndex = 0;
                foreach (string sToken in tokens)
                {
                    if (0 == nColumnIndex)
                    {
                        //
                        // 0列目は、フィールド名です。
                        //
                        string sFieldName = sToken;//.Trim().ToUpper();

                        // テーブルのフィールドを追加します。型の既定値は文字列型とします。
                        fieldDefinition = new FielddefImpl(sFieldName, EnumTypeFielddef.String);
                        recordFielddef.Add(fieldDefinition);
                    }
                    else if (1 == nColumnIndex)
                    {
                        //
                        // 1列目は、フィールドの型名です。
                        //
                        nColumnIndex = 1;
                        string sFieldTypeNameLower = sToken.Trim().ToLower();

                        // テーブルのフィールドを追加します。型の既定値は文字列型とします。
                        // TODO int型とboolean型にも対応したい。
                        if (FielddefImpl.S_STRING.Equals(sFieldTypeNameLower))
                        {
                            fieldDefinition.Type_Field = EnumTypeFielddef.String;
                        }
                        else if (FielddefImpl.S_INT.Equals(sFieldTypeNameLower))
                        {
                            fieldDefinition.Type_Field = EnumTypeFielddef.Int;
                        }
                        else if (FielddefImpl.S_BOOL.Equals(sFieldTypeNameLower))
                        {
                            fieldDefinition.Type_Field = EnumTypeFielddef.Bool;
                        }
                        else
                        {
                            // 型が未定義の列は、文字列型として読み取ります。

                            // TODO: 警告。(エラーではない)

                            Log_TextIndented t = new Log_TextIndentedImpl();
                            t.Append("▲エラー45!(" + Info_Table.Name_Library + ")");
                            t.Newline();

                            t.Append("型の名前を記入してください。");
                            t.Append(Environment.NewLine);
                            t.Append(Environment.NewLine);
                            t.Append("※縦と横がひっくり返っているテーブルと指定されています。");
                            t.Append(Environment.NewLine);
                            t.Append(Environment.NewLine);
                            t.Append("1列目(先頭を0とする)に、型の名前は必須です。");
                            t.Append(Environment.NewLine);
                            t.Append(Environment.NewLine);
                            t.Append("[");

                            t.Append(fieldDefinition.Name_Humaninput);

                            t.Append("]フィールド ([");
                            t.Append(nRowIndex);
                            t.Append("]行目)に、");
                            t.Append(Environment.NewLine);
                            t.Append("型名が[");
                            t.Append(sFieldTypeNameLower);
                            t.Append("]と入っています。この名前には、未対応です。");
                            t.Append(Environment.NewLine);
                            t.Append(Environment.NewLine);
                            t.Append("文字列型として続行します。");
                            t.Append(Environment.NewLine);
                            t.Append(Environment.NewLine);
                            t.Append("テーブル名=[");
                            t.Append(forTable_Request.Name_PutToTable);
                            t.Append("]");
                            t.Append(Environment.NewLine);
                            t.Append("ファイル・パス=[");
                            t.Append(forTable_Request.Expression_Filepath.Humaninput);
                            t.Append("]");
                            t.Append(Environment.NewLine);
                            t.Append(Environment.NewLine);

                            string sWarning = t.ToString();

                            MessageBox.Show(sWarning, "▲警告!(L02)");

                            fieldDefinition.Type_Field = EnumTypeFielddef.String;
                        }
                    }
                    else if (2 == nColumnIndex)
                    {
                        //
                        // 2列目は、フィールドのコメントとします。
                        //
                        nColumnIndex = 2;
                        {
                            fieldDefinition.Comment = sToken;
                        }
                    }
                    else
                    {
                        //
                        // 3列目から右側は、データ・テーブル部。
                        //

                        if (0 == nRowIndex)
                        {
                            //
                            // 先頭行
                            //

                            //
                            // 「EOF」というトークンが出てくるまで。
                            //
                            if (ToCsv_Table_RowColRegularImpl_.S_EOF == sToken.Trim().ToUpper())
                            {
                                goto column_end;
                            }

                            List <string> record = new List <string>();

                            // 1番目のフィールド_データを追加。
                            record.Add(sToken);

                            rows.Add(record);
                        }
                        else
                        {
                            //
                            // 2番目以降のフィールド_データを追加。
                            //

                            //
                            // 先頭の3つのレコード分、切り詰めます。
                            //
                            int nDataIndex = nColumnIndex - 3;
                            if (nDataIndex < rows.Count)
                            {
                                List <string> record = rows[nDataIndex];

                                record.Add(sToken);
                            }
                            else
                            {
                                // 無視
                            }
                        }
                    }


                    nColumnIndex++;
                }//c
column_end:


                nRowIndex++;
            }



            //essageBox.Show("CSV読取終わり1 rows.Count=[" + rows.Count + "]", "TableCsvLibデバッグ");

            // テーブル作成。テーブルのフィールド型定義と、データ本体をセットします。
            xenonTable.CreateTable(recordFielddef, log_Reports);
            if (log_Reports.Successful)
            {
                xenonTable.AddRecordList(rows, recordFielddef, log_Reports);
                //essageBox.Show("CSV読取後のテーブル作成終わり", "TableCsvLibデバッグ");
            }

            goto gt_EndMethod;
            //
            //
gt_EndMethod:
            log_Method.EndMethod(log_Reports);
            return(xenonTable);
        }
Exemple #11
0
        //────────────────────────────────────────
        #endregion



        #region アクション
        //────────────────────────────────────────

        public string ToCsvText(
            Table_Humaninput xTable,
            Log_Reports log_Reports
            )
        {
            Log_Method log_Method = new Log_MethodImpl(0);

            log_Method.BeginMethod(Info_Table.Name_Library, this, "ToCsvText", log_Reports);

            Log_TextIndented result = new Log_TextIndentedImpl();

            RecordFielddef error_RecordFielddef;
            Exception      err_Excep;
            int            error_IndexColumn;
            Fielddef       error_Fielddef;
            object         error_Item;

            if (null == xTable)
            {
                // エラー
                goto gt_Error_NullTable;
            }

            CsvLineParserImpl csvParser = new CsvLineParserImpl();

            // フィールド名をカンマ区切りで出力します。最後にEOLを付加します。

            // フィールド定義部
            if (xTable.RecordFielddef.Count < 1)
            {
                //エラー。
                error_RecordFielddef = xTable.RecordFielddef;
                goto gt_Error_FieldZero;
            }


            // フィールド定義部:名前
            xTable.RecordFielddef.ForEach(delegate(Fielddef fielddefinition, ref bool isBreak, Log_Reports log_Reports2)
            {
                if (this.ExceptedFields.TryExceptedField(fielddefinition.Name_Trimupper))
                {
                    // 出力しないフィールドの場合、無視します。
                }
                else
                {
                    result.Append(CsvLineParserImpl.EscapeCell(fielddefinition.Name_Humaninput));
                    result.Append(",");
                }
            }, log_Reports);
            result.Append(
                ToCsv_Table_RowColRegularImpl_.S_EOL //ToCsv_Table_Humaninput_RowColRegularImpl.S_END
                );
            result.Append(Environment.NewLine);      //改行

            // フィールド定義部:型
            xTable.RecordFielddef.ForEach(delegate(Fielddef fielddefinition, ref bool isBreak, Log_Reports log_Reports2)
            {
                if (this.ExceptedFields.TryExceptedField(fielddefinition.Name_Trimupper))
                {
                    // 出力しないフィールドの場合、無視します。
                }
                else
                {
                    switch (fielddefinition.Type_Field)
                    {
                    case EnumTypeFielddef.String:
                        {
                            result.Append(FielddefImpl.S_STRING);
                        }
                        break;

                    case EnumTypeFielddef.Int:
                        {
                            result.Append(FielddefImpl.S_INT);
                        }
                        break;

                    case EnumTypeFielddef.Bool:
                        {
                            result.Append(FielddefImpl.S_BOOL);
                        }
                        break;

                    default:
                        {
                            // TODO エラー対応。

                            // 未定義の型があった場合、そのまま出力します。
                            // C#のメッセージになるかと思います。
                            result.Append(fielddefinition.ToString_Type());
                        }
                        break;
                    }

                    result.Append(",");
                }
            }, log_Reports);
            result.Append(
                ToCsv_Table_RowColRegularImpl_.S_EOL
                //ToCsv_Table_Humaninput_RowColRegularImpl.S_END
                );
            result.Append(Environment.NewLine);//改行

            // フィールド定義部:コメント
            xTable.RecordFielddef.ForEach(delegate(Fielddef fielddefinition, ref bool isBreak, Log_Reports log_Reports2)
            {
                if (this.ExceptedFields.TryExceptedField(fielddefinition.Name_Trimupper))
                {
                    // 出力しないフィールドの場合、無視します。
                }
                else
                {
                    result.Append(CsvLineParserImpl.EscapeCell(fielddefinition.Comment));
                    result.Append(",");
                }
            }, log_Reports);
            result.Append(
                ToCsv_Table_RowColRegularImpl_.S_EOL
                //ToCsv_Table_Humaninput_RowColRegularImpl.S_END
                );
            result.Append(Environment.NewLine);//改行

            // 0行目から数えて3行目以降はデータ・テーブル部。

            // データ・テーブル部
            DataTable dataTable = xTable.DataTable;

            // 各行について
            for (int nRowIndex = 0; nRowIndex < dataTable.Rows.Count; nRowIndex++)
            {
                DataRow dataRow = dataTable.Rows[nRowIndex];

                //
                // 各フィールドについて
                //
                object[] itemArray = dataRow.ItemArray;// ItemArrayは1回の呼び出しが重い。
                for (int indexColumn = 0; indexColumn < itemArray.Length; indexColumn++)
                {
                    // TODO:範囲 リストサイズが0の時がある←プログラムミス?
                    Fielddef fielddefinition;
                    try
                    {
                        fielddefinition = xTable.RecordFielddef.ValueAt(indexColumn);
                    }
                    catch (Exception e)
                    {
                        // エラー。
                        err_Excep            = e;
                        error_RecordFielddef = xTable.RecordFielddef;
                        error_IndexColumn    = indexColumn;
                        goto gt_Error_OutOfIndex;
                    }

                    if (this.ExceptedFields.TryExceptedField(fielddefinition.Name_Trimupper))
                    {
                        // 出力しないフィールドの場合、無視します。
                    }
                    else
                    {
                        string value_Cell;
                        object item = itemArray[indexColumn];

                        if (item is Cell)
                        {
                            value_Cell = ((Cell)item).Text;
                        }
                        else if (item is string)
                        {
                            //フィールド定義部など。
                            value_Cell = (string)item;
                        }
                        else if (item is DBNull)
                        {
                            //空欄。
                            value_Cell = "";
                        }
                        else
                        {
                            // エラー
                            error_Item     = item;
                            error_Fielddef = fielddefinition;
                            goto gt_Error_UndefinedFieldType;
                        }

                        result.Append(CsvLineParserImpl.EscapeCell(value_Cell));
                        result.Append(this.charSeparator);
                    }
                }
                result.Append(
                    ToCsv_Table_RowColRegularImpl_.S_EOL
                    //ToCsv_Table_Humaninput_RowColRegularImpl.S_END
                    );
                result.Append(Environment.NewLine);//改行
            }
            result.Append(ToCsv_Table_RowColRegularImpl_.S_EOF);
            // 最後に一応、改行を付けておきます。
            result.Append(Environment.NewLine);//改行

            goto gt_EndMethod;
            //
            //
            #region 異常系
            //────────────────────────────────────────
gt_Error_FieldZero:
            if (log_Reports.CanCreateReport)
            {
                Log_RecordReports r = log_Reports.BeginCreateReport(EnumReport.Error);
                r.SetTitle("▲エラー854!", log_Method);

                Log_TextIndented s = new Log_TextIndentedImpl();

                s.Append("(プログラム内部エラー)テーブルの列定義が0件です。 error_RecordFielddef.Count[");
                s.Append(error_RecordFielddef.Count);
                s.Append("] テーブル名=[");
                s.Append(xTable.Name);
                s.Append("]");
                s.Newline();

                // ヒント

                r.Message = s.ToString();
                log_Reports.EndCreateReport();
            }
            goto gt_EndMethod;
            //────────────────────────────────────────
gt_Error_OutOfIndex:
            if (log_Reports.CanCreateReport)
            {
                Log_RecordReports r = log_Reports.BeginCreateReport(EnumReport.Error);
                r.SetTitle("▲エラー853!", log_Method);

                Log_TextIndented s = new Log_TextIndentedImpl();

                s.Append("(プログラム内部エラー)err_NColIndex=[");
                s.Append(error_IndexColumn);
                s.Append("] error_RecordFielddef.Count[");
                s.Append(error_RecordFielddef.Count);
                s.Append("]");
                s.Newline();

                // ヒント
                s.Append(r.Message_SException(err_Excep));

                r.Message = s.ToString();
                log_Reports.EndCreateReport();
            }
            goto gt_EndMethod;
            //────────────────────────────────────────
gt_Error_UndefinedFieldType:
            if (log_Reports.CanCreateReport)
            {
                Log_RecordReports r = log_Reports.BeginCreateReport(EnumReport.Error);
                r.SetTitle("▲エラー855!", log_Method);

                Log_TextIndented s = new Log_TextIndentedImpl();

                s.Append("(プログラム内部エラー)CSVを出力しようとしたとき、未定義のフィールド型=[");
                s.Append(error_Fielddef.ToString_Type());
                s.Append("]がありました。");
                s.Newline();

                s.Append("型名=[");
                s.Append(error_Item.GetType().Name);
                s.Append("]");
                s.Newline();

                s.Append("型は[");
                s.Append(typeof(StringCellImpl));
                s.Append("],[");
                s.Append(typeof(IntCellImpl));
                s.Append("],[");
                s.Append(typeof(BoolCellImpl));
                s.Append("]が使えます。");
                s.Newline();

                // ヒント

                r.Message = s.ToString();
                log_Reports.EndCreateReport();
            }
            goto gt_EndMethod;
            //────────────────────────────────────────
gt_Error_NullTable:
            if (log_Reports.CanCreateReport)
            {
                Log_RecordReports r = log_Reports.BeginCreateReport(EnumReport.Error);
                r.SetTitle("▲エラー852!", log_Method);

                Log_TextIndented s = new Log_TextIndentedImpl();

                s.Append("(プログラム内部エラー)tableがヌルでした。");
                s.Newline();

                // ヒント

                r.Message = s.ToString();
                log_Reports.EndCreateReport();
            }
            goto gt_EndMethod;
            //────────────────────────────────────────
            #endregion
            //
            //
gt_EndMethod:
            log_Method.EndMethod(log_Reports);
            return(result.ToString());
        }
Exemple #12
0
        //────────────────────────────────────────
        #endregion



        #region アクション
        //────────────────────────────────────────

        /// <summary>
        /// DataTableを作成します。
        ///
        /// セルのデータ型は全て string です。
        /// </summary>
        /// <param name="csvText"></param>
        /// <returns></returns>
        public DataTable Read(
            string string_Csv
            )
        {
            // テーブルを作成します。
            DataTable dataTable = new DataTable();

            System.IO.StringReader reader = new System.IO.StringReader(string_Csv);

            //
            // CSVを解析して、テーブル形式で格納。
            //

            int index_Row = 0;

            string[]          array_Field;
            DataRow           datarow;
            CsvLineParserImpl csvParser = new CsvLineParserImpl();

            if (-1 < reader.Peek())
            {
                // 1行ずつ読み取ります。

                //
                // 0 行目の読取。 列名データが入っている行です。
                //

                // 読み取った返却値を、変数に入れ直さずにスプリット。
                array_Field = csvParser.UnescapeLineToFieldList(reader.ReadLine(), this.CharSeparator).ToArray();

                // 行を作成します。
                datarow = dataTable.NewRow();

                int indexColumn = 0;
                while (indexColumn < array_Field.Length)
                {
                    // 列情報を追加します。 型は文字列型とします。
                    dataTable.Columns.Add(array_Field[indexColumn], typeof(string));

                    // データとしても早速格納します。
                    datarow[indexColumn] = array_Field[indexColumn];

                    indexColumn++;
                }

                dataTable.Rows.Add(datarow);
                index_Row++;



                //
                // 1行目以降の読取。
                //
                while (-1 < reader.Peek())
                {
                    // 1行ずつ読み取ります。

                    // 読み取った返却値を、変数に入れ直さずにスプリット。
                    array_Field = reader.ReadLine().Split(this.CharSeparator);

                    // 行を作成します。
                    datarow = dataTable.NewRow();

                    //
                    // 追加する列数
                    //
                    object[] itemArray         = datarow.ItemArray;//ItemArrayは1回の呼び出しが重い。
                    int      count_AddsColumns = array_Field.Length - itemArray.Length;
                    for (int count = 0; count < count_AddsColumns; count++)
                    {
                        // 0行目で数えた列数より多い場合。

                        // 列を追加します。
                        // 列定義を追加しています。型は文字列型、名前は空文字列です。
                        dataTable.Columns.Add("", typeof(string));
                    }

                    int indexColumn3 = 0;
                    while (indexColumn3 < array_Field.Length)
                    {
                        datarow[indexColumn3] = array_Field[indexColumn3];
                        indexColumn3++;
                    }

                    dataTable.Rows.Add(datarow);
                    index_Row++;
                }
            }



            // ストリームを閉じます。
            reader.Close();

            return(dataTable);
        }
Exemple #13
0
        //────────────────────────────────────────
        #endregion



        #region アクション
        //────────────────────────────────────────

        /// <summary>
        /// CSVを読取り、テーブルにして返します。
        ///
        ///
        /// SRS仕様の実装状況
        /// ここでは、先頭行を[0]行目と数えるものとします。
        /// (1)CSVの[0]行目は列名です。
        /// (2)CSVの[1]行目は型名です。
        /// (3)CSVの[2]行目はコメントです。
        ///
        /// (4)データ・テーブル部で、0列目に「EOF」と入っていれば終了。大文字・小文字は区別せず。
        ///    それ以降に、コメントのようなデータが入力されていることがあるが、フィールドの型に一致しないことがあるので無視。
        ///    TODO: EOF以降の行も、コメントとして残したい。
        ///
        /// (5)列名に ”END”(半角) がある場合、その手前までの列が有効データです。
        ///    ”END”以降の列は無視します。
        ///    TODO: ”END”以降の行も、コメントとして残したい。
        ///
        /// (6)int型として指定されているフィールドのデータ・テーブル部に空欄があった場合、DBNull(データベース用のヌル)とします。
        /// </summary>
        /// <param name="csvText"></param>
        /// <returns>列名情報も含むテーブル。列の型は文字列型とします。</returns>
        public Table_Humaninput Read(
            string string_Csv,
            Request_ReadsTable forTable_request,
            Format_Table_Humaninput forTable_puts,
            Log_Reports log_Reports
            )
        {
            Log_Method log_Method = new Log_MethodImpl();

            log_Method.BeginMethod(Info_Table.Name_Library, this, "Read(1)", log_Reports);

            Table_Humaninput xenonTable = new Table_HumaninputImpl(
                forTable_request.Name_PutToTable, forTable_request.Expression_Filepath, forTable_request.Expression_Filepath.Cur_Configuration);

            xenonTable.Tableunit               = forTable_request.Tableunit;
            xenonTable.Typedata                = forTable_request.Typedata;
            xenonTable.IsDatebackupActivated   = forTable_request.IsDatebackupActivated;
            xenonTable.Format_Table_Humaninput = forTable_puts;


            Exception err_Excp;
            int       error_Count_Index;

            string[] error_Fields_Cur;


            //
            // 型定義部
            //
            // (※NO,ID,EXPL,NAME など、フィールドの定義を持つテーブル)
            //
            RecordFielddefinition recordFielddefinition = new RecordFielddefinitionImpl();

            //
            // データ・テーブル部
            //
            List <List <string> > dataTableRows = new List <List <string> >();

            // CSVテキストを読み込み、型とデータのバッファーを作成します。
            System.IO.StringReader reader    = new System.IO.StringReader(string_Csv);
            CsvLineParserImpl      csvParser = new CsvLineParserImpl();

            // CSVを解析して、テーブル形式で格納。
            {
                // データとして認識する列の総数です。
                int nDataColumnsCount = 0;

                int      nRowIndex = 0;
                string[] fields_Cur;
                while (-1 < reader.Peek())
                {
                    string line = reader.ReadLine();

                    fields_Cur = csvParser.UnescapeLineToFieldList(line, this.charSeparator).ToArray();


                    if (0 == nRowIndex)
                    {
                        // 0行目

                        // 列名の行とします。

                        for (int nColumnIx = 0; nColumnIx < fields_Cur.Length; nColumnIx++)
                        {
                            string sColumnName = fields_Cur[nColumnIx];

                            // 列名を読み込みました。

                            // トリム&大文字
                            string sCellValueTU = sColumnName.Trim().ToUpper();
                            if (ToCsv_Table_Humaninput_RowColRegularImpl.S_END == sCellValueTU)
                            {
                                // 列名に ”END” がある場合、その手前までの列が有効データです。
                                // ”END” 以降の列は無視します。
                                goto field_name_reading_end;
                            }

                            // テーブルのフィールドを追加します。型の既定値は文字列型とします。
                            FielddefinitionImpl fieldDef = new FielddefinitionImpl(sColumnName, EnumTypeFielddefinition.String);
                            recordFielddefinition.Add(fieldDef);
                            nDataColumnsCount++;
                        }


                        // 0行目は、テーブルのデータとしては持ちません。
                    }
                    else if (1 == nRowIndex)
                    {
                        // 1行目

                        // フィールド型名の行。

                        for (int nColumnIx = 0; nColumnIx < nDataColumnsCount; nColumnIx++)
                        {
                            string name_FieldType_Lower;
                            try
                            {
                                name_FieldType_Lower = fields_Cur[nColumnIx].ToLower();
                            }
                            catch (IndexOutOfRangeException e)
                            {
                                err_Excp = e;
                                goto gt_Error_FdIndexOutOfRangeException;
                            }

                            // 列の型名を読み込みました。

                            // テーブルのフィールドを追加します。型の既定値は文字列型とします。
                            // TODO int型とboolean型にも対応したい。
                            if (FielddefinitionImpl.S_STRING.Equals(name_FieldType_Lower))
                            {
                                recordFielddefinition.ValueAt(nColumnIx).Type_Field = EnumTypeFielddefinition.String;
                            }
                            else if (FielddefinitionImpl.S_INT.Equals(name_FieldType_Lower))
                            {
                                recordFielddefinition.ValueAt(nColumnIx).Type_Field = EnumTypeFielddefinition.Int;
                            }
                            else if (FielddefinitionImpl.S_BOOL.Equals(name_FieldType_Lower))
                            {
                                // 2009-11-11修正:SRS仕様では「bool」が正しい。「boolean」は間違い。
                                recordFielddefinition.ValueAt(nColumnIx).Type_Field = EnumTypeFielddefinition.Bool;
                            }
                            else
                            {
                                // 型が未定義の列は、文字列型として読み取ります。

                                // TODO:警告を出すか?

                                recordFielddefinition.ValueAt(nColumnIx).Type_Field = EnumTypeFielddefinition.String;
                            }
                        }

                        // 1行目は、テーブルのデータとしては持ちません。
                    }
                    else if (2 == nRowIndex)
                    {
                        // 2行目

                        // フィールドのコメントの行。
                        // TODO: フィールドのコメントの行は省略されることがある。

                        for (int column = 0; column < nDataColumnsCount; column++)
                        {
                            if (fields_Cur.Length <= column)
                            {
                                error_Fields_Cur = fields_Cur;
                                //error_Count_Columns = fields_Cur.Length;
                                error_Count_Index = column;
                                goto gt_Error_CommentFieldCount;
                            }

                            string comment_Field = fields_Cur[column];//todo:bug:境界線エラーをキャッチしてない。

                            recordFielddefinition.ValueAt(column).Comment = comment_Field;
                        }

                        // 2行目は、テーブルのデータとしては持ちません。
                    }
                    else
                    {
                        // 3行目以降のループ。
                        List <string> sList_Column = new List <string>();

                        // データ・テーブル部で、0列目に「EOF」と入っていれば終了。大文字・小文字は区別せず。

                        if (fields_Cur.Length < 1)
                        {
                            // 空行は無視。
                            goto end_recordAdd;
                        }
                        //ystem.Console.WriteLine(InfxenonTable.LibraryName + ":" + this.GetType().Name + "#UnescapeToList: sFields[0]=[" + sFields[0] + "] sLine=[" + sLine + "]");

                        string sCellValueTrimUpper = fields_Cur[0].Trim().ToUpper();
                        if (ToCsv_Table_Humaninput_RowColRegularImpl.S_EOF == sCellValueTrimUpper)
                        {
                            goto reading_end;
                        }

                        int nColumnCount;
                        if (fields_Cur.Length < nDataColumnsCount)
                        {
                            // 「実際にデータとして存在する列数」
                            nColumnCount = fields_Cur.Length;
                        }
                        else
                        {
                            // 「データとして存在する筈の列数」(これ以降の列は無視)
                            nColumnCount = nDataColumnsCount;
                        }


                        for (int nColumnIx = 0; nColumnIx < nColumnCount; nColumnIx++)
                        {
                            string sValue;

                            sValue = fields_Cur[nColumnIx];

                            if (recordFielddefinition.Count <= nColumnIx)
                            {
                                // 0行目で数えた列数より多い場合。

                                // テーブルのフィールドを追加します。型は文字列型とします。名前は空文字列です。
                                recordFielddefinition.Add(new FielddefinitionImpl("", EnumTypeFielddefinition.String));
                            }

                            sList_Column.Add(sValue);
                        }

                        dataTableRows.Add(sList_Column);
end_recordAdd:
                        ;
                    }
field_name_reading_end:

                    //essageBox.Show("ttbwIndex=[" + ttbwIndex + "]行目ループ終わり", "TableCsvLibデバッグ");
                    nRowIndex++;
                }
            }
reading_end:

            // ストリームを閉じます。
            reader.Close();

            //essageBox.Show("CSV読取終わり1 rows.Count=[" + rows.Count + "]", "TableCsvLibデバッグ");


            // テーブルのフィールド定義。
            xenonTable.CreateTable(recordFielddefinition, log_Reports);
            if (log_Reports.Successful)
            {
                // データ本体のセット。
                xenonTable.AddRecordList(dataTableRows, recordFielddefinition, log_Reports);
            }

            goto gt_EndMethod;
            //
            //
            #region 異常系
            //────────────────────────────────────────
gt_Error_CommentFieldCount:
            if (log_Reports.CanCreateReport)
            {
                Log_RecordReports r = log_Reports.BeginCreateReport(EnumReport.Error);
                r.SetTitle("▲エラー1356!", log_Method);

                Log_TextIndented s = new Log_TextIndentedImpl();

                s.Append("「フィールド・コメント」行のフィールド数が合いませんでした。");
                s.Append(Environment.NewLine);
                s.Append(Environment.NewLine);

                s.Append("index=[");
                s.Append(error_Count_Index);
                s.Append("]");
                s.Append(Environment.NewLine);

                s.Append("列数=[");
                s.Append(error_Fields_Cur.Length);
                s.Append("]");
                s.Append(Environment.NewLine);
                s.Append(Environment.NewLine);

                s.Append("──────────fields ここから");
                s.Append(Environment.NewLine);
                foreach (string field in error_Fields_Cur)
                {
                    s.Append("field=[");
                    s.Append(field);
                    s.Append("]");
                    s.Append(Environment.NewLine);
                }
                s.Append("──────────fields ここまで");
                s.Append(Environment.NewLine);

                //
                // ヒント
                s.Append(Log_RecordReportsImpl.ToText_Configuration(xenonTable));

                r.Message = s.ToString();
                log_Reports.EndCreateReport();
            }
            goto gt_EndMethod;
            //────────────────────────────────────────
gt_Error_FdIndexOutOfRangeException:
            if (log_Reports.CanCreateReport)
            {
                Log_RecordReports r = log_Reports.BeginCreateReport(EnumReport.Error);
                r.SetTitle("▲エラー132!", log_Method);

                Log_TextIndented s = new Log_TextIndentedImpl();
                s.Newline();

                s.Append("フィールド定義の数が合いませんでした。");
                s.Append(Environment.NewLine);
                s.Append(Environment.NewLine);

                string sFpatha = forTable_request.Expression_Filepath.Execute4_OnExpressionString(
                    EnumHitcount.Unconstraint, log_Reports);
                s.Append("ファイルパス=[");
                s.Append(sFpatha);
                s.Append("]");
                s.Append(Environment.NewLine);
                s.Append(Environment.NewLine);

                //
                // ヒント
                s.Append(err_Excp.Message);

                r.Message = s.ToString();
                log_Reports.EndCreateReport();
            }
            goto gt_EndMethod;
            //────────────────────────────────────────
            #endregion
            //
            //
gt_EndMethod:
            log_Method.EndMethod(log_Reports);
            return(xenonTable);
        }
        //────────────────────────────────────────
        /// <summary>
        /// CSVを読取り、テーブルにして返します。
        /// 
        /// 
        /// SRS仕様の実装状況
        /// ここでは、先頭行を[0]行目と数えるものとします。
        /// (1)CSVの[0]行目は列名です。
        /// (2)CSVの[1]行目は型名です。
        /// (3)CSVの[2]行目はコメントです。
        /// 
        /// (4)データ・テーブル部で、0列目に「EOF」と入っていれば終了。大文字・小文字は区別せず。
        ///    それ以降に、コメントのようなデータが入力されていることがあるが、フィールドの型に一致しないことがあるので無視。
        ///    TODO: EOF以降の行も、コメントとして残したい。
        /// 
        /// (5)列名に ”END”(半角) がある場合、その手前までの列が有効データです。
        ///    ”END”以降の列は無視します。
        ///    TODO: ”END”以降の行も、コメントとして残したい。
        /// 
        /// (6)int型として指定されているフィールドのデータ・テーブル部に空欄があった場合、DBNull(データベース用のヌル)とします。
        /// </summary>
        /// <param name="csvText"></param>
        /// <returns>列名情報も含むテーブル。列の型は文字列型とします。</returns>
        public Table_Humaninput Read(
            string string_Csv,
            Request_ReadsTable forTable_request,
            Format_Table_Humaninput forTable_puts,
            Log_Reports log_Reports
            )
        {
            Log_Method log_Method = new Log_MethodImpl();
            log_Method.BeginMethod(Info_Table.Name_Library, this, "Read(1)",log_Reports);

            Table_Humaninput xenonTable = new Table_HumaninputImpl(
                forTable_request.Name_PutToTable, forTable_request.Expression_Filepath, forTable_request.Expression_Filepath.Cur_Configuration );
            xenonTable.Tableunit = forTable_request.Tableunit;
            xenonTable.Typedata = forTable_request.Typedata;
            xenonTable.IsDatebackupActivated = forTable_request.IsDatebackupActivated;
            xenonTable.Format_Table_Humaninput = forTable_puts;

            Exception err_Excp;
            int error_Count_Index;
            string[] error_Fields_Cur;

            //
            // 型定義部
            //
            // (※NO,ID,EXPL,NAME など、フィールドの定義を持つテーブル)
            //
            RecordFielddefinition recordFielddefinition = new RecordFielddefinitionImpl();

            //
            // データ・テーブル部
            //
            List<List<string>> dataTableRows = new List<List<string>>();

            // CSVテキストを読み込み、型とデータのバッファーを作成します。
            System.IO.StringReader reader = new System.IO.StringReader(string_Csv);
            CsvLineParserImpl csvParser = new CsvLineParserImpl();

            // CSVを解析して、テーブル形式で格納。
            {
                // データとして認識する列の総数です。
                int nDataColumnsCount = 0;

                int nRowIndex = 0;
                string[] fields_Cur;
                while (-1 < reader.Peek())
                {
                    string line = reader.ReadLine();

                    fields_Cur = csvParser.UnescapeLineToFieldList(line, this.charSeparator).ToArray();

                    if (0 == nRowIndex)
                    {
                        // 0行目

                        // 列名の行とします。

                        for (int nColumnIx = 0; nColumnIx < fields_Cur.Length; nColumnIx++)
                        {
                            string sColumnName = fields_Cur[nColumnIx];

                            // 列名を読み込みました。

                            // トリム&大文字
                            string sCellValueTU = sColumnName.Trim().ToUpper();
                            if (ToCsv_Table_Humaninput_RowColRegularImpl.S_END == sCellValueTU)
                            {
                                // 列名に ”END” がある場合、その手前までの列が有効データです。
                                // ”END” 以降の列は無視します。
                                goto field_name_reading_end;
                            }

                            // テーブルのフィールドを追加します。型の既定値は文字列型とします。
                            FielddefinitionImpl fieldDef = new FielddefinitionImpl(sColumnName, EnumTypeFielddefinition.String);
                            recordFielddefinition.Add(fieldDef);
                            nDataColumnsCount++;
                        }

                        // 0行目は、テーブルのデータとしては持ちません。
                    }
                    else if (1 == nRowIndex)
                    {
                        // 1行目

                        // フィールド型名の行。

                        for (int nColumnIx = 0; nColumnIx < nDataColumnsCount; nColumnIx++)
                        {
                            string name_FieldType_Lower;
                            try
                            {
                                name_FieldType_Lower = fields_Cur[nColumnIx].ToLower();
                            }
                            catch (IndexOutOfRangeException e)
                            {
                                err_Excp = e;
                                goto gt_Error_FdIndexOutOfRangeException;
                            }

                            // 列の型名を読み込みました。

                            // テーブルのフィールドを追加します。型の既定値は文字列型とします。
                            // TODO int型とboolean型にも対応したい。
                            if (FielddefinitionImpl.S_STRING.Equals(name_FieldType_Lower))
                            {
                                recordFielddefinition.ValueAt(nColumnIx).Type_Field = EnumTypeFielddefinition.String;
                            }
                            else if (FielddefinitionImpl.S_INT.Equals(name_FieldType_Lower))
                            {
                                recordFielddefinition.ValueAt(nColumnIx).Type_Field = EnumTypeFielddefinition.Int;
                            }
                            else if (FielddefinitionImpl.S_BOOL.Equals(name_FieldType_Lower))
                            {
                                // 2009-11-11修正:SRS仕様では「bool」が正しい。「boolean」は間違い。
                                recordFielddefinition.ValueAt(nColumnIx).Type_Field = EnumTypeFielddefinition.Bool;
                            }
                            else
                            {
                                // 型が未定義の列は、文字列型として読み取ります。

                                // TODO:警告を出すか?

                                recordFielddefinition.ValueAt(nColumnIx).Type_Field = EnumTypeFielddefinition.String;
                            }
                        }

                        // 1行目は、テーブルのデータとしては持ちません。
                    }
                    else if (2 == nRowIndex)
                    {
                        // 2行目

                        // フィールドのコメントの行。
                        // TODO: フィールドのコメントの行は省略されることがある。

                        for (int column = 0; column < nDataColumnsCount; column++)
                        {
                            if (fields_Cur.Length<=column)
                            {
                                error_Fields_Cur = fields_Cur;
                                //error_Count_Columns = fields_Cur.Length;
                                error_Count_Index = column;
                                goto gt_Error_CommentFieldCount;
                            }

                            string comment_Field = fields_Cur[column];//todo:bug:境界線エラーをキャッチしてない。

                            recordFielddefinition.ValueAt(column).Comment = comment_Field;
                        }

                        // 2行目は、テーブルのデータとしては持ちません。
                    }
                    else
                    {
                        // 3行目以降のループ。
                        List<string> sList_Column = new List<string>();

                        // データ・テーブル部で、0列目に「EOF」と入っていれば終了。大文字・小文字は区別せず。

                        if (fields_Cur.Length < 1)
                        {
                            // 空行は無視。
                            goto end_recordAdd;
                        }
                        //ystem.Console.WriteLine(InfxenonTable.LibraryName + ":" + this.GetType().Name + "#UnescapeToList: sFields[0]=[" + sFields[0] + "] sLine=[" + sLine + "]");

                        string sCellValueTrimUpper = fields_Cur[0].Trim().ToUpper();
                        if (ToCsv_Table_Humaninput_RowColRegularImpl.S_EOF == sCellValueTrimUpper)
                        {
                            goto reading_end;
                        }

                        int nColumnCount;
                        if (fields_Cur.Length < nDataColumnsCount)
                        {
                            // 「実際にデータとして存在する列数」
                            nColumnCount = fields_Cur.Length;
                        }
                        else
                        {
                            // 「データとして存在する筈の列数」(これ以降の列は無視)
                            nColumnCount = nDataColumnsCount;
                        }

                        for (int nColumnIx = 0; nColumnIx < nColumnCount; nColumnIx++)
                        {
                            string sValue;

                            sValue = fields_Cur[nColumnIx];

                            if (recordFielddefinition.Count <= nColumnIx)
                            {
                                // 0行目で数えた列数より多い場合。

                                // テーブルのフィールドを追加します。型は文字列型とします。名前は空文字列です。
                                recordFielddefinition.Add(new FielddefinitionImpl("", EnumTypeFielddefinition.String));
                            }

                            sList_Column.Add(sValue);
                        }

                        dataTableRows.Add(sList_Column);
                    end_recordAdd:
                        ;
                    }
                field_name_reading_end:

                    //essageBox.Show("ttbwIndex=[" + ttbwIndex + "]行目ループ終わり", "TableCsvLibデバッグ");
                    nRowIndex++;
                }
            }
            reading_end:

            // ストリームを閉じます。
            reader.Close();

            //essageBox.Show("CSV読取終わり1 rows.Count=[" + rows.Count + "]", "TableCsvLibデバッグ");

            // テーブルのフィールド定義。
            xenonTable.CreateTable(recordFielddefinition,log_Reports);
            if(log_Reports.Successful)
            {
                // データ本体のセット。
                xenonTable.AddRecordList(dataTableRows, recordFielddefinition, log_Reports);
            }

            goto gt_EndMethod;
            //
            //
            #region 異常系
            //────────────────────────────────────────
            gt_Error_CommentFieldCount:
            if (log_Reports.CanCreateReport)
            {
                Log_RecordReports r = log_Reports.BeginCreateReport(EnumReport.Error);
                r.SetTitle("▲エラー1356!", log_Method);

                Log_TextIndented s = new Log_TextIndentedImpl();

                s.Append("「フィールド・コメント」行のフィールド数が合いませんでした。");
                s.Append(Environment.NewLine);
                s.Append(Environment.NewLine);

                s.Append("index=[");
                s.Append(error_Count_Index);
                s.Append("]");
                s.Append(Environment.NewLine);

                s.Append("列数=[");
                s.Append(error_Fields_Cur.Length);
                s.Append("]");
                s.Append(Environment.NewLine);
                s.Append(Environment.NewLine);

                s.Append("──────────fields ここから");
                s.Append(Environment.NewLine);
                foreach (string field in error_Fields_Cur)
                {
                    s.Append("field=[");
                    s.Append(field);
                    s.Append("]");
                    s.Append(Environment.NewLine);
                }
                s.Append("──────────fields ここまで");
                s.Append(Environment.NewLine);

                //
                // ヒント
                s.Append(Log_RecordReportsImpl.ToText_Configuration(xenonTable));

                r.Message = s.ToString();
                log_Reports.EndCreateReport();
            }
            goto gt_EndMethod;
            //────────────────────────────────────────
            gt_Error_FdIndexOutOfRangeException:
            if (log_Reports.CanCreateReport)
            {
                Log_RecordReports r = log_Reports.BeginCreateReport(EnumReport.Error);
                r.SetTitle("▲エラー132!", log_Method);

                Log_TextIndented s = new Log_TextIndentedImpl();
                s.Newline();

                s.Append("フィールド定義の数が合いませんでした。");
                s.Append(Environment.NewLine);
                s.Append(Environment.NewLine);

                string sFpatha = forTable_request.Expression_Filepath.Execute4_OnExpressionString(
                    EnumHitcount.Unconstraint, log_Reports);
                s.Append("ファイルパス=[");
                s.Append(sFpatha);
                s.Append("]");
                s.Append(Environment.NewLine);
                s.Append(Environment.NewLine);

                //
                // ヒント
                s.Append(err_Excp.Message);

                r.Message = s.ToString();
                log_Reports.EndCreateReport();
            }
            goto gt_EndMethod;
            //────────────────────────────────────────
            #endregion
            //
            //
            gt_EndMethod:
            log_Method.EndMethod(log_Reports);
            return xenonTable;
        }
Exemple #15
0
        /// <summary>
        /// 大雑把にfunctionを一覧。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Log_ReportsImpl.BDebugmode_Static = true;
            Log_Reports log_Reports;
            //(2)メソッド開始
            Log_Method log_Method = new Log_MethodImpl();
            // デバッグモード静的設定の後で。
            log_Method.BeginMethod(Info_Actorslist.Name_Library, this, "button2_Click", out log_Reports);

            Exception error_Exception;

            //コンフィグファイル
            ConfigxmlImpl configxml = new ConfigxmlImpl();
            configxml.Read(log_Reports);

            //CSV→テーブル
            Table_Humaninput tableH;
            if (log_Reports.Successful)
            {
                if(!File.Exists(configxml.FilepathExportLualist))
                {
                    goto gt_Error_File1;
                }

                string csvtext = File.ReadAllText(configxml.FilepathExportLualist);
                CsvTo_Table_HumaninputImpl trans = new CsvTo_Table_HumaninputImpl();
                tableH = trans.Read(
                    csvtext,
                    new Request_ReadsTableImpl(),
                    new Format_Table_HumaninputImpl(),
                    //true,
                    log_Reports
                    );
            }
            else
            {
                tableH = null;
            }

            //各.luaファイル
            List<string> listFile = new List<string>();
            List<string> listRow = new List<string>();
            List<string> listFunction = new List<string>();
            if (log_Reports.Successful)
            {
                Regex regex2 = new Regex(@"^\s*function\s+(.*)$", RegexOptions.Compiled);
                tableH.ForEach_Datapart(
                    (Record_Humaninput recordH, ref bool isBreak2, Log_Reports log_Reports2) =>
                    {
                        string filepathRelational = recordH.TextAt("FILE");
                        string filepath = Path.Combine(configxml.FolderpathProject, filepathRelational);
                        //ystem.Console.WriteLine("filepath=[" + filepath + "]");

                        string luatext = File.ReadAllText(filepath, Encoding.GetEncoding("Shift-JIS"));
                        string[] lines = luatext.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

                        int row = 1;//行番号
                        foreach (string line in lines)
                        {
                            Match m2 = regex2.Match(line);
                            if (m2.Success)
                            {
                                listFile.Add(filepathRelational);
                                listRow.Add(row.ToString());
                                listFunction.Add(line);
                                //System.Console.WriteLine("ファイル=[" + filepathRelational + "] [" + row + "]行 関数line=[" + line + "]");
                            }
                            else
                            {
                            }

                            row++;
                        }

                    }, log_Reports);
            }

            //配列(空回し);buffer
            Dictionary<string, string> dictionary1 = new Dictionary<string, string>();// 関数名の問題文字列:["シーン","クラスネーム"]
            if (log_Reports.Successful)
            {
                Regex regex4 = new Regex(@"^\s*function\s+(.*?)(_OnClose|_OnVanish)\s*\(.*$", RegexOptions.Compiled);

                //「_OnClose」ならシーン、
                //「_OnVanish」ならクラスネームに確定します。

                for (int i = 0; i < listFunction.Count; i++)
                {
                    string function = listFunction[i];

                    Match m4 = regex4.Match(function);
                    if (m4.Success)
                    {
                        if ("_OnClose" == m4.Groups[2].Value)
                        {
                            //シーン
                            dictionary1.Add(m4.Groups[1].Value, "シーン");
                        }
                        else if ("_OnVanish" == m4.Groups[2].Value)
                        {
                            //クラスネーム
                            dictionary1.Add(m4.Groups[1].Value, "クラスネーム");
                        }
                        else
                        {
                        }
                    }
                }
            }

            //配列→CSV
            if (log_Reports.Successful)
            {
                Regex regex3 = new Regex(@"^\s*function\s+(.*?)(?:_OnStart|_OnStep|_OnClose|_OnVanish|_OnDraw)\s*\(.*$", RegexOptions.Compiled);
                Regex regex4 = new Regex(@"^\s*function\s+(?:OnLoad|OnVanish)\s*\(.*$", RegexOptions.Compiled);
                Regex regex5 = new Regex(@"^\s*function\s+(.*?)_init\s*\(.*$", RegexOptions.Compiled);
                Regex regex6 = new Regex(@"^\s*function\s+(.*?)Thread\s*\(.*$", RegexOptions.Compiled);
                Regex regex7 = new Regex(@"^\s*function\s+thread_(.*?)\s*\(.*$", RegexOptions.Compiled);

                CsvLineParserImpl parser = new CsvLineParserImpl();

                StringBuilder sb = new StringBuilder();
                sb.Append("NO,FILE,ROW,INITIALIZER,SCENE,THREAD,CLASS_NAME,FUNCTION,END");//,EOL
                sb.Append(Environment.NewLine);
                sb.Append("int,string,int,string,string,string,string,string,");
                sb.Append(Environment.NewLine);
                sb.Append("-1,ファイルパス,行番号,suica32用ローダースレッド・ハンドラ?,シーン,スレッド?,クラス名,関数シグネチャー,");
                sb.Append(Environment.NewLine);
                int row = 0;
                for (int i = 0; i < listFunction.Count; i++)
                {
                    string file = listFile[i];
                    string numberRow = listRow[i];
                    string function = listFunction[i];

                    string initializer = "";
                    string scene = "";
                    string thread = "";
                    string classname = "";

                    Match m3 = regex3.Match(function);
                    if (m3.Success)
                    {
                        string functionname2 = m3.Groups[1].Value;

                        if (dictionary1.ContainsKey(functionname2))
                        {
                            switch (dictionary1[functionname2])
                            {
                                case "シーン":
                                    scene = functionname2;
                                    break;
                                case "クラスネーム":
                                    classname = functionname2;
                                    break;
                                default:
                                    break;
                            }
                        }

                        goto gt_Csv;
                    }

                    Match m4 = regex4.Match(function);
                    if (m4.Success)
                    {
                        scene = Path.GetFileNameWithoutExtension(file);
                        goto gt_Csv;
                    }

                    Match m5 = regex5.Match(function);
                    if (m5.Success)
                    {
                        initializer = m5.Groups[1].Value;
                        goto gt_Csv;
                    }

                    Match m6 = regex6.Match(function);
                    if (m6.Success)
                    {
                        thread = m6.Groups[1].Value;
                        goto gt_Csv;
                    }

                    Match m7 = regex7.Match(function);
                    if (m7.Success)
                    {
                        thread = m7.Groups[1].Value;
                        goto gt_Csv;
                    }

                gt_Csv:
                    sb.Append(row);
                    sb.Append(",");
                    sb.Append(file);
                    sb.Append(",");
                    sb.Append(numberRow);
                    sb.Append(",");
                    sb.Append(initializer);
                    sb.Append(",");
                    sb.Append(scene);
                    sb.Append(",");
                    sb.Append(thread);
                    sb.Append(",");
                    sb.Append(classname);
                    sb.Append(",");
                    sb.Append(parser.EscapeCell(function));
                    sb.Append(",");
                    sb.Append(Environment.NewLine);
                    //ystem.Console.WriteLine("row=[" + row + "] file=[" + file + "] numberRow=[" + numberRow + "] initializer=[" + initializer + "] scene=[" + scene + "] thread=[" + thread + "] classname=[" + classname + "] classname=[" + classname + "] function=[" + parser.EscapeCell(function) + "]");
                    row++;
                }

                sb.Append("EOF,,,,,,,,");
                sb.Append(Environment.NewLine);

                string csvfile = Path.Combine(Application.StartupPath, configxml.FilepathExportFunctionlist);
                System.Console.WriteLine("csvfile=[" + csvfile + "]");
                File.WriteAllText(csvfile, sb.ToString(), Encoding.UTF8);
            }

            goto gt_EndMethod;
            #region 異常系
            //────────────────────────────────────────
            gt_Error_File1:
            if (log_Reports.CanCreateReport)
            {
                Log_RecordReports r = log_Reports.BeginCreateReport(EnumReport.Error);
                r.SetTitle("▲エラー13007!", log_Method);

                Log_TextIndented s = new Log_TextIndentedImpl();
                s.Append("ファイルが足りません。");
                s.Append(Environment.NewLine);
                s.Append("readme.txtを読んで、手順を踏んでください。");
                s.Append(Environment.NewLine);
                s.Append("  .lua一覧CSVファイル=[");
                s.Append(configxml.FilepathExportLualist);
                s.Append("]");

                r.Message = s.ToString();
                log_Reports.EndCreateReport();
            }
            goto gt_EndMethod;
            //────────────────────────────────────────
            #endregion

            gt_EndMethod:
            log_Method.EndMethod(log_Reports);
            log_Reports.EndLogging(log_Method);
        }
Exemple #16
0
        //────────────────────────────────────────
        /// <summary>
        /// DataTableを作成します。
        /// 
        /// セルのデータ型は全て string です。
        /// </summary>
        /// <param name="csvText"></param>
        /// <returns></returns>
        public DataTable Read(
            string string_Csv
            )
        {
            // テーブルを作成します。
            DataTable dataTable = new DataTable();

            System.IO.StringReader reader = new System.IO.StringReader(string_Csv);

            //
            // CSVを解析して、テーブル形式で格納。
            //

            int index_Row = 0;
            string[] array_Field;
            DataRow datarow;
            CsvLineParserImpl csvParser = new CsvLineParserImpl();

            if (-1 < reader.Peek())
            {
                // 1行ずつ読み取ります。

                //
                // 0 行目の読取。 列名データが入っている行です。
                //

                // 読み取った返却値を、変数に入れ直さずにスプリット。
                array_Field = csvParser.UnescapeLineToFieldList(reader.ReadLine(), this.CharSeparator).ToArray();

                // 行を作成します。
                datarow = dataTable.NewRow();

                int indexColumn = 0;
                while (indexColumn < array_Field.Length)
                {
                    // 列情報を追加します。 型は文字列型とします。
                    dataTable.Columns.Add(array_Field[indexColumn], typeof(string));

                    // データとしても早速格納します。
                    datarow[indexColumn] = array_Field[indexColumn];

                    indexColumn++;
                }

                dataTable.Rows.Add(datarow);
                index_Row++;

                //
                // 1行目以降の読取。
                //
                while (-1 < reader.Peek())
                {
                    // 1行ずつ読み取ります。

                    // 読み取った返却値を、変数に入れ直さずにスプリット。
                    array_Field = reader.ReadLine().Split(this.CharSeparator);

                    // 行を作成します。
                    datarow = dataTable.NewRow();

                    //
                    // 追加する列数
                    //
                    object[] itemArray = datarow.ItemArray;//ItemArrayは1回の呼び出しが重い。
                    int count_AddsColumns = array_Field.Length - itemArray.Length;
                    for (int count = 0; count < count_AddsColumns; count++)
                    {
                        // 0行目で数えた列数より多い場合。

                        // 列を追加します。
                        // 列定義を追加しています。型は文字列型、名前は空文字列です。
                        dataTable.Columns.Add("", typeof(string));
                    }

                    int indexColumn3 = 0;
                    while (indexColumn3 < array_Field.Length)
                    {
                        datarow[indexColumn3] = array_Field[indexColumn3];
                        indexColumn3++;
                    }

                    dataTable.Rows.Add(datarow);
                    index_Row++;
                }
            }

            // ストリームを閉じます。
            reader.Close();

            return dataTable;
        }
        //────────────────────────────────────────
        /// <summary>
        /// TODO:「,」「"」に対応したい。
        /// 
        /// 
        /// 縦と横が逆のテーブル。
        /// 
        /// CSVを読取り、テーブルにして返します。
        /// 
        /// 
        /// SRS仕様の実装状況
        /// ここでは、先頭行を[0]行目と数えるものとします。
        /// (1)CSVの[0]行目は列名です。
        /// (2)CSVの[1]行目は型名です。
        /// (3)CSVの[2]行目はコメントです。
        /// 
        /// (4)データ・テーブル部で、0列目に「EOF」と入っていれば終了。大文字・小文字は区別せず。
        ///    それ以降に、コメントのようなデータが入力されていることがあるが、フィールドの型に一致しないことがあるので無視。
        ///    TODO EOF以降の行も、コメントとして残したい。
        /// 
        /// (5)列名にENDがある場合、その手前までの列が有効データです。
        ///    END以降の列は無視します。
        ///    TODO END以降の行も、コメントとして残したい。
        /// 
        /// (6)int型として指定されているフィールドのデータ・テーブル部に空欄があった場合、DBNull(データベース用のヌル)とします。
        /// </summary>
        /// <param name="csvText"></param>
        /// <returns>列名情報も含むテーブル。列の型は文字列型とします。</returns>
        public Table_Humaninput Read(
            string string_Csv,
            Request_ReadsTable forTable_Request,
            Format_Table_Humaninput forTable_Format,
            Log_Reports log_Reports
            )
        {
            Log_Method log_Method = new Log_MethodImpl();
            log_Method.BeginMethod(Info_Table.Name_Library, this, "Read",log_Reports);

            //
            //
            //
            //
            CsvLineParserImpl csvParser = new CsvLineParserImpl();

            Table_Humaninput xenonTable = new Table_HumaninputImpl(
                forTable_Request.Name_PutToTable, forTable_Request.Expression_Filepath, forTable_Request.Expression_Filepath.Cur_Configuration );
            xenonTable.Tableunit = forTable_Request.Tableunit;
            xenonTable.Typedata = forTable_Request.Typedata;
            xenonTable.IsDatebackupActivated = forTable_Request.IsDatebackupActivated;
            xenonTable.Format_Table_Humaninput = forTable_Format;

            //
            // 一旦、テーブルを全て読み込みます。
            //
            List<List<string>> lines = new List<List<string>>();

            {
                // CSVテキストを読み込み、型とデータのバッファーを作成します。
                System.IO.StringReader reader = new System.IO.StringReader(string_Csv);

                while (-1 < reader.Peek())
                {
                    string sLine = reader.ReadLine();
                    List<string> tokens = new List<string>();

                    string[] sFields;
                    sFields = csvParser.UnescapeLineToFieldList(sLine, this.charSeparator).ToArray();

                    int nColumnIndex = 0;
                    foreach (string sToken in sFields)
                    {
                        if (nColumnIndex == 0 && ToCsv_Table_Humaninput_RowColRegularImpl.S_END == sToken.Trim().ToUpper())
                        {
                            // 1列目にENDがある場合、その手前までの列が有効データです。
                            // END以降の行は無視します。
                            goto row_end;
                        }

                        tokens.Add(sToken);

                        nColumnIndex++;
                    }
                    lines.Add(tokens);
                }
            row_end:

                // ストリームを閉じます。
                reader.Close();
            }

            //
            // 型定義部
            //
            // (※NO,ID,EXPL,NAME など、フィールドの定義を持つテーブル)
            //
            RecordFielddefinition recordFielddefinition = new RecordFielddefinitionImpl();

            //
            // データ・テーブル部
            //
            List<List<string>> rows = new List<List<string>>();

            //
            // まず、0列目、1列目、2列目のデータを読み取ります。
            //
            int nRowIndex=0;
            foreach (List<string> tokens in lines)
            {
                Fielddefinition fieldDefinition = null;

                int nColumnIndex = 0;
                foreach(string sToken in tokens)
                {

                    if(0==nColumnIndex)
                    {
                        //
                        // 0列目は、フィールド名です。
                        //
                        string sFieldName = sToken;//.Trim().ToUpper();

                        // テーブルのフィールドを追加します。型の既定値は文字列型とします。
                        fieldDefinition = new FielddefinitionImpl(sFieldName, EnumTypeFielddefinition.String);
                        recordFielddefinition.Add(fieldDefinition);
                    }
                    else if(1==nColumnIndex)
                    {
                        //
                        // 1列目は、フィールドの型名です。
                        //
                        nColumnIndex = 1;
                        string sFieldTypeNameLower = sToken.Trim().ToLower();

                        // テーブルのフィールドを追加します。型の既定値は文字列型とします。
                        // TODO int型とboolean型にも対応したい。
                        if (FielddefinitionImpl.S_STRING.Equals(sFieldTypeNameLower))
                        {
                            fieldDefinition.Type_Field = EnumTypeFielddefinition.String;
                        }
                        else if (FielddefinitionImpl.S_INT.Equals(sFieldTypeNameLower))
                        {
                            fieldDefinition.Type_Field = EnumTypeFielddefinition.Int;
                        }
                        else if (FielddefinitionImpl.S_BOOL.Equals(sFieldTypeNameLower))
                        {
                            fieldDefinition.Type_Field = EnumTypeFielddefinition.Bool;
                        }
                        else
                        {
                            // 型が未定義の列は、文字列型として読み取ります。

                            // TODO: 警告。(エラーではない)

                            Log_TextIndented t = new Log_TextIndentedImpl();
                            t.Append("▲エラー45!(" + Info_Table.Name_Library + ")");
                            t.Newline();

                            t.Append("型の名前を記入してください。");
                            t.Append(Environment.NewLine);
                            t.Append(Environment.NewLine);
                            t.Append("※縦と横がひっくり返っているテーブルと指定されています。");
                            t.Append(Environment.NewLine);
                            t.Append(Environment.NewLine);
                            t.Append("1列目(先頭を0とする)に、型の名前は必須です。");
                            t.Append(Environment.NewLine);
                            t.Append(Environment.NewLine);
                            t.Append("[");

                            t.Append(fieldDefinition.Name_Humaninput);

                            t.Append("]フィールド ([");
                            t.Append(nRowIndex);
                            t.Append("]行目)に、");
                            t.Append(Environment.NewLine);
                            t.Append("型名が[");
                            t.Append(sFieldTypeNameLower);
                            t.Append("]と入っています。この名前には、未対応です。");
                            t.Append(Environment.NewLine);
                            t.Append(Environment.NewLine);
                            t.Append("文字列型として続行します。");
                            t.Append(Environment.NewLine);
                            t.Append(Environment.NewLine);
                            t.Append("テーブル名=[");
                            t.Append(forTable_Request.Name_PutToTable);
                            t.Append("]");
                            t.Append(Environment.NewLine);
                            t.Append("ファイル・パス=[");
                            t.Append(forTable_Request.Expression_Filepath.Humaninput);
                            t.Append("]");
                            t.Append(Environment.NewLine);
                            t.Append(Environment.NewLine);

                            string sWarning = t.ToString();

                            MessageBox.Show(sWarning, "▲警告!(L02)");

                            fieldDefinition.Type_Field = EnumTypeFielddefinition.String;
                        }
                    }
                    else if(2==nColumnIndex)
                    {
                        //
                        // 2列目は、フィールドのコメントとします。
                        //
                        nColumnIndex = 2;
                        {
                            fieldDefinition.Comment = sToken;
                        }

                    }
                    else
                    {
                        //
                        // 3列目から右側は、データ・テーブル部。
                        //

                        if(0==nRowIndex)
                        {
                            //
                            // 先頭行
                            //

                            //
                            // 「EOF」というトークンが出てくるまで。
                            //
                            if(ToCsv_Table_Humaninput_RowColRegularImpl.S_EOF==sToken.Trim().ToUpper())
                            {
                                goto column_end;
                            }

                            List<string> record = new List<string>();

                            // 1番目のフィールド_データを追加。
                            record.Add( sToken);

                            rows.Add(record);
                        }
                        else
                        {
                            //
                            // 2番目以降のフィールド_データを追加。
                            //

                            //
                            // 先頭の3つのレコード分、切り詰めます。
                            //
                            int nDataIndex = nColumnIndex - 3;
                            if (nDataIndex < rows.Count)
                            {
                                List<string> record = rows[nDataIndex];

                                record.Add(sToken);
                            }
                            else
                            {
                                // 無視
                            }
                        }
                    }

                    nColumnIndex ++;
                }//c
            column_end:

                nRowIndex++;
            }

            //essageBox.Show("CSV読取終わり1 rows.Count=[" + rows.Count + "]", "TableCsvLibデバッグ");

            // テーブル作成。テーブルのフィールド型定義と、データ本体をセットします。
            xenonTable.CreateTable(recordFielddefinition, log_Reports);
            if (log_Reports.Successful)
            {
                xenonTable.AddRecordList(rows, recordFielddefinition, log_Reports);
                //essageBox.Show("CSV読取後のテーブル作成終わり", "TableCsvLibデバッグ");
            }

            goto gt_EndMethod;
            //
            //
            gt_EndMethod:
            log_Method.EndMethod(log_Reports);
            return xenonTable;
        }
        //────────────────────────────────────────
        /// <summary>
        /// TODO:「,」「"」に対応したい。
        /// 
        /// 
        /// 縦、横がひっくり返っていて、
        /// 型定義レコードがないCSVテーブルの読取。
        /// </summary>
        /// <param name="csvText"></param>
        /// <returns>列名情報も含むテーブル。</returns>
        public Table_Humaninput Read(
            string string_Csv,
            Request_ReadsTable forTable_Request,
            Format_Table_Humaninput forTable_Format,
            Log_Reports log_Reports
            )
        {
            Log_Method log_Method = new Log_MethodImpl();
            log_Method.BeginMethod(Info_Table.Name_Library, this, "Read",log_Reports);

            //
            //
            //
            //
            CsvLineParserImpl csvParser = new CsvLineParserImpl();

            Table_Humaninput xenonTable = new Table_HumaninputImpl(
                forTable_Request.Name_PutToTable, forTable_Request.Expression_Filepath, forTable_Request.Expression_Filepath.Cur_Configuration);
            xenonTable.Tableunit = forTable_Request.Tableunit;
            xenonTable.Typedata = forTable_Request.Typedata;
            xenonTable.IsDatebackupActivated = forTable_Request.IsDatebackupActivated;
            xenonTable.Format_Table_Humaninput = forTable_Format;

            //
            // 一旦、テーブルを全て読み込みます。
            //
            List<List<string>> lines = new List<List<string>>();

            {
                // CSVテキストを読み込み、型とデータのバッファーを作成します。
                System.IO.StringReader reader = new System.IO.StringReader(string_Csv);

                string[] sFields;
                while (-1 < reader.Peek())
                {
                    string sLine = reader.ReadLine();
                    List<string> tokens = new List<string>();

                    sFields = csvParser.UnescapeLineToFieldList(sLine, this.charSeparator).ToArray();

                    int nColumnIndex = 0;
                    foreach (string sToken in sFields)
                    {
                        if (nColumnIndex == 0 && ToCsv_Table_Humaninput_RowColRegularImpl.S_END == sToken.Trim().ToUpper())
                        {
                            // 1列目にENDがある場合、その手前までの列が有効データです。
                            // END以降の行は無視します。
                            goto row_end;
                        }

                        tokens.Add(sToken);

                        nColumnIndex++;
                    }
                    lines.Add(tokens);
                }
            row_end:

                // ストリームを閉じます。
                reader.Close();
            }

            //
            // 型定義部
            //
            // (※NO,ID,EXPL,NAME など、フィールドの定義を持つテーブル)
            //
            RecordFielddefinition recordFielddefinition = new RecordFielddefinitionImpl();

            //
            // データ・テーブル部
            //
            List<List<string>> rows = new List<List<string>>();

            //
            // まず、0列目、1列目のデータを読み取ります。
            //
            int nRowIndex=0;
            foreach (List<string> tokens in lines)
            {
                Fielddefinition fieldDefinition = null;

                int nColumnIndex = 0;
                foreach(string sToken in tokens)
                {

                    if(0==nColumnIndex)
                    {
                        //
                        // 0列目は、フィールド名です。
                        //
                        string sFieldName = sToken;//.Trim().ToUpper();

                        // テーブルのフィールドを追加します。フィールドの型は、intに固定です。
                        fieldDefinition = new FielddefinitionImpl(sFieldName, EnumTypeFielddefinition.Int);
                        recordFielddefinition.Add(fieldDefinition);
                    }
                    else if(1==nColumnIndex)
                    {
                        //
                        // 1列目は、フィールドのコメントとします。
                        //
                        nColumnIndex = 1;
                        {
                            fieldDefinition.Comment = sToken;
                        }

                    }
                    else
                    {
                        //
                        // 2列目から右側は、データ・テーブル部。
                        //

                        if(0==nRowIndex)
                        {
                            //
                            // 先頭行
                            //

                            //
                            // 「EOF」というトークンが出てくるまで。
                            //
                            if(ToCsv_Table_Humaninput_RowColRegularImpl.S_EOF==sToken.Trim().ToUpper())
                            {
                                goto column_end;
                            }

                            List<string> record = new List<string>();

                            // 1番目のフィールド_データを追加。
                            record.Add( sToken);

                            rows.Add(record);
                        }
                        else
                        {
                            //
                            // 2番目以降のフィールド_データを追加。
                            //

                            //
                            // 先頭の2つのレコード分、切り詰めます。
                            //
                            int nDataIndex = nColumnIndex - 2;
                            if (nDataIndex < rows.Count)
                            {
                                List<string> record = rows[nDataIndex];

                                record.Add(sToken);
                            }
                            else
                            {
                                // 無視
                            }
                        }
                    }

                    nColumnIndex ++;
                }//c
            column_end:

                nRowIndex++;
            }

            //essageBox.Show("CSV読取終わり1 rows.Count=[" + rows.Count + "]", "TableCsvLibデバッグ");

            // テーブル作成。テーブルのフィールド型定義と、データ本体をセットします。
            xenonTable.CreateTable(recordFielddefinition,log_Reports);
            if( log_Reports.Successful)
            {
                xenonTable.AddRecordList(rows, recordFielddefinition, log_Reports);
                //essageBox.Show("CSV読取後のテーブル作成終わり", "TableCsvLibデバッグ");
            }

            goto gt_EndMethod;
            //
            //
            gt_EndMethod:
            log_Method.EndMethod(log_Reports);
            return xenonTable;
        }