Esempio n. 1
0
        /// <summary>
        /// ファイルから読込(宛先、送信元は読み込まない)
        /// </summary>
        /// <param name="filename">ファイル名</param>
        /// <returns>(件名, 本文)</returns>
        public static (string, string) LoadFromFile(string filename)
        {
            byte[] data = File.ReadAllBytes(filename);

            string subjectText = "";
            string bodyText    = "";

            // ボディ部の先頭を見つける
            int indexBody = 0;

            for (int i = 0; i < data.Length; ++i)
            {
                if (CharCodeUtils.IsCrLf(data, i) && CharCodeUtils.IsCrLf(data, i + 2))
                {
                    indexBody = i + 4;
                    break;
                }
            }
            if (indexBody == 0)
            {
                throw new Exception("形式が違います: ボディ部が存在しません。");
            }

            string headers = Encoding.ASCII.GetString(data, 0, indexBody);

            subjectText = GetSubjectText(headers);

            bodyText = GetBodyText(data, indexBody);

            return(subjectText, bodyText);
        }
Esempio n. 2
0
        /// <summary>
        /// ボディ部データから本文を取得する
        /// NOTE: 自分で出力した形式にしか対応していない
        /// </summary>
        /// <param name="data">ボディ部データ</param>
        /// <param name="offset">オフセット</param>
        /// <returns>本文</returns>
        private static string GetBodyText(byte[] data, int offset)
        {
            bool inJpn = false;

            int dataLength = data.Length;

            StringBuilder result = new StringBuilder();

            for (int i = offset; i < dataLength;)
            {
                if (CharCodeUtils.IsCrLf(data, i))
                {
                    inJpn = false;
                    i    += 2;
                    result.Append("\r\n");
                    continue;
                }
                if (CharCodeUtils.IsBeginEscapeSequence(data, i))
                {
                    inJpn = true;
                    i    += 3;
                    continue;
                }
                if (CharCodeUtils.IsEndEscapeSequence(data, i))
                {
                    inJpn = false;
                    i    += 3;
                    continue;
                }
                if (inJpn)
                {
                    if (i <= dataLength - 2)
                    {
                        int   jiscode = CharCodeUtils.MergeHL(data[i], data[i + 1]);
                        Emoji emoji   = DataBags.Emojis.GetFromJiscode(jiscode);
                        if (emoji != null)
                        {
                            result.Append(emoji.Unicode);
                        }
                        else
                        {
                            string sch = CharCodeUtils.GetCharFromJisCode(jiscode);
                            if (sch != null)
                            {
                                result.Append(sch);
                            }
                        }

                        i += 2;
                    }
                    else
                    {
                        i += 1;
                    }
                }
                else
                {
                    char ascii = (char)data[i];
                    result.Append(ascii);
                    i += 1;
                }
            }

            return(result.ToString());
        }