
#ifndef sectline_h__
#define sectline_h__

#include "mfbase1.h"
#include "mfstring.h"

/*========================================================-
 *
 * sectional lines を構成する行たち(一方向リスト)
 */
typedef struct tag_mfSectionLine {

  mfStr                    * m_line;      // 行データ
  int                        m_br_code;   // 改行コード
  struct tag_mfSectionLine * m_next;     // 次の行へ

} mfSectionLine;


// br_code
#define MFSL_NONE 0 // 改行なし
#define MFSL_CR   1
#define MFSL_LF   2
#define MFSL_CRLF 3  // 推奨
#define MFSL_LFCR 4  // 強い "非"推奨


MF_METHOD(mfSectionLine *)
new_mfSectionLine (
    const char * a_line,
    int          a_length,
    int          a_br_code );


MF_METHOD(void)
delete_mfSectionLine (
    mfSectionLine * a_self );

/*========================================================
 *
 *    sectional lines の 1 セクション
 */
typedef struct tag_mfSection {

  mfStr         * m_section_name;
  int             m_br_code;  // セクション名 [section] の後ろの改行コード
  int             m_lines_count;
  mfSectionLine * m_lines;

  struct tag_mfSection * m_next;  // 次のセクションへ

} mfSection;


/**
 *  セクション名を指定して生成
 */
MF_METHOD(mfSection *)
new_mfSection (
    const char * a_section_name,
    int          a_length,
    int          a_br_code );

/**
 *  セクション破棄
 */
MF_METHOD(void)
delete_mfSection(mfSection * a_self);

/**
 *  セクション内のラインデータの数
 */
MF_METHOD(int)
mfSection_get_line_count(const mfSection * a_self) MF_CONST_METHOD;

/**
 *  MF_String にラインデータをコピー
 */
MF_METHOD(int)
mfSection_get_line (
    const mfSection * a_self,
    int               a_index,
    mfStr           * a_out_string,
    int             * a_out_br_code ) MF_CONST_METHOD;


/**
 *  セクションにラインデータを追加
 */
MF_METHOD(int)
mfSection_add_line (
    mfSection  * a_self,
    const char * a_line,
    int          a_length,
    int          a_br_code );


/**
 *  セクションからラインデータを削除
 */
MF_METHOD(int)
mfSection_remove_line (
    mfSection * a_self,
    int         a_index );



/*=============================================================--
 *
 *   sectinal lines クラス
 */
typedef struct tag_mfSectinalLines {

  char        m_constructed;
  mfSection * m_sections;

} mfSectionalLines;


/**
 *  a_self を初期化
 * @retval MF_OK    初期化成功
 * @retval MF_ERROR 失敗
 */
MF_METHOD(int)
mfSectionalLines_construct (mfSectionalLines * a_self);


/**
 * a_self の内容を破棄
 */
MF_METHOD(void)
mfSectionalLines_destruct (mfSectionalLines *a_self);


/**  new MF_SectionalLines
 *  オブジェクト生成
 */
MF_METHOD(mfSectionalLines *)
new_mfSectionalLines ();


/** delete MF_SectionalLines
 *   オブジェクト破棄
 */
MF_METHOD(void)
delete_mfSectionalLines (mfSectionalLines *a_self );


/**
 *  指定したセクションを得る
 *  存在しなければ 0 を返す
 */
MF_METHOD(mfSection *)
mfSectionalLines_get_section (
    const mfSectionalLines * a_self,
    const char             * a_section_name,
    int                      a_length ) MF_CONST_METHOD;


/**
 *  指定したセクションを得る
 *  存在しなければ新規作成される
 */
MF_METHOD(mfSection *)
mfSectionalLines_create_section (
    mfSectionalLines * a_self,
    const char       * a_section_name,
    int                a_length,
    int                a_br_code);



/**
 *  指定したセクションを削除する
 */


/**
 *  クリアする
 */
MF_METHOD(int)
mfSectionalLines_clear (mfSectionalLines * a_self );


/**
 *  ファイルを読む
 */
MF_METHOD(int)
mfSectionalLines_read_file (
    mfSectionalLines * a_self,
    const char *       a_flie_name,
    int                a_length );


/**
 *  ダンプ
 */
MF_METHOD(int)
mfSectionalLines_dump (const mfSectionalLines *) MF_CONST_METHOD;



/**
 *  ファイルに保存
 */
MF_METHOD(int)
mfSectionalLines_write_file (
   const mfSectionalLines * a_self,
   const char *             a_file_name,
   int                      a_length ) MF_CONST_METHOD;



#endif // sectline_h__




