Exemple #1
0
        /// <summary>
        /// 全角文字の文字コード(JIS)から文字を取得する
        /// </summary>
        /// <param name="jiscode">JIS コード</param>
        /// <returns>文字(1文字): 取得できない場合は null</returns>
        public static string GetCharFromJisCode(int jiscode)
        {
            (byte high, byte low) = CharCodeUtils.SplitHL(jiscode);

            if (0x21 <= high && high <= 0x7e)
            {
                // OK
            }
            else
            {
                return(null);
            }

            if (0x21 <= low && low <= 0x7e)
            {
                // OK
            }
            else
            {
                return(null);
            }

            byte[] bistr = new byte[] {
                JIS_BEGIN_ESCAPE_SEQUENCE[0],
                JIS_BEGIN_ESCAPE_SEQUENCE[1],
                JIS_BEGIN_ESCAPE_SEQUENCE[2],
                high, low,
                JIS_END_ESCAPE_SEQUENCE[0],
                JIS_END_ESCAPE_SEQUENCE[1],
                JIS_END_ESCAPE_SEQUENCE[2]
            };

            string result = null;

            try {
                result = jisEncoding.GetString(bistr);
            } catch (Exception ex) {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// 本文を JIS エンコーディングした MemoryStream を返す
        /// NOTE: 半角カタカナは処理できないので渡さないこと
        /// </summary>
        /// <param name="bodyText">本文</param>
        /// <returns>エンコーディング内容を格納した MemoryStream</returns>
        private static MemoryStream GetBodyStream(string bodyText)
        {
            int maxBodyCols;

            if (DataBags.Config.ForceInsertLineFeed)
            {
                maxBodyCols = DataBags.Config.MaxBodyCols * 2;
            }
            else
            {
                maxBodyCols = int.MaxValue;
            }

            MemoryStream stream = new MemoryStream();

            int  cols  = 0;
            bool jisIn = false;

            for (int i = 0; i < bodyText.Length; ++i)
            {
                string sch = bodyText.Substring(i, 1);

                if (StringUtils.IsAsciiWithControl(sch))
                {
                    if (jisIn)
                    {
                        CharCodeUtils.WriteJisEndEscapeSequence(stream);
                    }

                    jisIn = false;

                    if (sch[0] == '\n')
                    {
                        CharCodeUtils.WriteCrLf(stream);
                        cols = 0;
                    }
                    else
                    {
                        char ascii = sch[0];
                        stream.WriteByte((byte)(ascii & 0xFF));
                        ++cols;
                    }
                }
                else
                {
                    if (!jisIn)
                    {
                        CharCodeUtils.WriteJisBeginEscapeSequence(stream);
                    }
                    jisIn = true;

                    int   jiscode;
                    Emoji emoji = DataBags.Emojis.GetFromUnicode(sch[0]);
                    if (emoji != null)
                    {
                        jiscode = emoji.Jiscode;
                    }
                    else
                    {
                        jiscode = CharCodeUtils.GetJisCodeFromChar(sch);
                        if (jiscode == 0)
                        {
                            jiscode = CharCodeUtils.JIS_WHITE_SPACE_CODE;
                        }
                    }

                    (byte high, byte low) = CharCodeUtils.SplitHL(jiscode);

                    stream.WriteByte(high);
                    stream.WriteByte(low);
                    cols += 2;
                }

                if (maxBodyCols <= cols)
                {
                    if (jisIn)
                    {
                        CharCodeUtils.WriteJisEndEscapeSequence(stream);
                    }
                    CharCodeUtils.WriteCrLfWithFlowed(stream);
                    if (jisIn)
                    {
                        CharCodeUtils.WriteJisBeginEscapeSequence(stream);
                    }

                    cols = 0;
                }
            }
            if (jisIn)
            {
                CharCodeUtils.WriteJisEndEscapeSequence(stream);
            }

            return(stream);
        }
Exemple #3
0
        /// <summary>
        /// 件名を固定長に分け、BASE64 エンコーディングして返す
        /// NOTE: 改行は処理できないので渡さないこと
        /// NOTE: 半角カタカナは処理できないので渡さないこと
        /// </summary>
        /// <param name="subjectText">件名</param>
        /// <param name="maxCols">件名を分けるための長さ</param>
        /// <returns>複数の BASE64 文字列の一覧</returns>
        private static List <string> GetBase64EncodingBlocks(string subjectText, int maxCols)
        {
            List <string> base64EncodingBlocks = new List <string>();

            MemoryStream stream = new MemoryStream();

            int  cols  = 0;
            bool jisIn = false;

            for (int i = 0; i < subjectText.Length; ++i)
            {
                string sch = subjectText.Substring(i, 1);

                if (StringUtils.IsAsciiWithControl(sch))
                {
                    if (jisIn)
                    {
                        CharCodeUtils.WriteJisEndEscapeSequence(stream);
                    }

                    jisIn = false;

                    char ascii = sch[0];
                    stream.WriteByte((byte)(ascii & 0xFF));
                    ++cols;
                }
                else
                {
                    if (!jisIn)
                    {
                        CharCodeUtils.WriteJisBeginEscapeSequence(stream);
                    }
                    jisIn = true;

                    int   jiscode;
                    Emoji emoji = DataBags.Emojis.GetFromUnicode(sch[0]);
                    if (emoji != null)
                    {
                        jiscode = emoji.Jiscode;
                    }
                    else
                    {
                        jiscode = CharCodeUtils.GetJisCodeFromChar(sch);
                        if (jiscode == 0)
                        {
                            jiscode = CharCodeUtils.JIS_WHITE_SPACE_CODE;
                        }
                    }

                    (byte high, byte low) = CharCodeUtils.SplitHL(jiscode);

                    stream.WriteByte(high);
                    stream.WriteByte(low);
                    ++cols;
                }

                if (maxCols <= cols)
                {
                    if (jisIn)
                    {
                        CharCodeUtils.WriteJisEndEscapeSequence(stream);
                    }
                    base64EncodingBlocks.Add(GetBase64EncodingBlock(stream));
                    stream = new MemoryStream();
                    if (jisIn)
                    {
                        CharCodeUtils.WriteJisBeginEscapeSequence(stream);
                    }

                    cols = 0;
                }
            }
            if (jisIn)
            {
                CharCodeUtils.WriteJisEndEscapeSequence(stream);
            }
            if (0 < stream.Length)
            {
                base64EncodingBlocks.Add(GetBase64EncodingBlock(stream));
            }

            return(base64EncodingBlocks);
        }