Example #1
0
        /// <summary>
        ///   改行まで読み取る。
        /// </summary>
        /// <remarks>
        ///   <para>
        ///     返り値に改行文字は含まない。
        ///     改行文字はLFまたはCRLF。
        ///   </para>
        /// </remarks>
        public string ReadLine()
        {
            byte[] buf = new byte[1024];
            int    ptr = 0;
            int    r;

            while ((r = fs.ReadByte()) >= 0)
            {
                if ((char)r == '\n')
                {
                    break;
                }
                if ((char)r == '\r')
                {
                    continue;
                }
                if (ptr >= buf.Length)
                {
                    byte[] tmp = new byte[buf.Length + 1024];
                    for (int i = 0; i < ptr; i++)
                    {
                        tmp[i] = buf[i];
                    }
                    buf = tmp;
                }
                buf[ptr] = (byte)r;
                ptr++;
            }
            return(SJISDictionary.GetString(buf, 0, ptr));
        }
Example #2
0
 /// <summary>
 ///   指定文字列を書き出す。
 /// </summary>
 /// <remarks>
 ///   <para>
 ///     txtがnullの場合は何も書き出さない。
 ///   </para>
 /// </remarks>
 public void Write(string txt)
 {
     if (txt == null)
     {
         return;
     }
     byte[] b = SJISDictionary.GetBytes(txt);
     fs.Write(b, 0, b.Length);
 }
Example #3
0
 private void write(int len, bool rightAlign, string txt, byte fillbyte, bool fillFlag, bool cutFlag)
 {
     if (len <= 0)
     {
         return;
     }
     if (txt == null)
     {
         txt = "";
     }
     byte[] b = SJISDictionary.GetBytes(txt);
     if (b.Length >= len)
     {
         if (cutFlag)
         {
             while (b.Length > len)
             {
                 txt = txt.Substring(0, txt.Length - 1);
                 b   = SJISDictionary.GetBytes(txt);
             }
         }
         fs.Write(b, 0, b.Length);
         int blen = b.Length;
         b    = new byte[1];
         b[0] = 0x20;
         for (int i = 0; i < len - blen; i++)
         {
             fs.Write(b, 0, 1);
         }
         return;
     }
     if (fillFlag)
     {
         byte[] fill = new byte[len - b.Length];
         for (int i = 0; i < fill.Length; i++)
         {
             fill[i] = 0x20;
         }
         if (rightAlign)
         {
             fs.Write(fill, 0, fill.Length);
             fs.Write(b, 0, b.Length);
         }
         else
         {
             fs.Write(b, 0, b.Length);
             fs.Write(fill, 0, fill.Length);
         }
     }
     else
     {
         fs.Write(b, 0, b.Length);
     }
 }
Example #4
0
        /// <summary>
        ///   最大nバイト読み取る。
        /// </summary>
        /// <param name="n">最大読み取りバイト数</param>
        /// <param name="str">読み取った文字列を格納するstring</param>
        /// <returns>読み取ったバイト数。改行文字はバイト数に含まれない</returns>
        /// <remarks>
        ///   <para>
        ///     nバイト読み取る前にファイル末尾になるか改行文字が読み取られた場合、
        ///     そこで読み取りを打ち切る。改行文字は結果に含まれない。
        ///   </para>
        /// </remarks>
        public int Read(int n, out string str)
        {
            byte[] buf = new byte[n];
            int    ptr = 0;
            int    r;

            while ((ptr < n) && ((r = fs.ReadByte()) >= 0))
            {
                if ((char)r == '\n')
                {
                    break;
                }
                if ((char)r == '\r')
                {
                    continue;
                }
                buf[ptr] = (byte)r;
                ptr++;
            }
            str = SJISDictionary.GetString(buf, 0, ptr);
            return(ptr);
        }
Example #5
0
        private List <string[]> readAll(bool linenoflag)
        {
            if ((m_list != null) && (m_linenoflag == linenoflag))
            {
                return(m_list);
            }
            m_list       = new List <string[]>();
            m_linenoflag = linenoflag;
            using (FileStream fs = FileUtil.BinaryReader(m_filename)) {
                if (fs == null)
                {
                    return(m_list);
                }
                byte[] linebuf = new byte[4096];
                int    linelen = 0;
                bool   eof     = false;
                int    lineno  = 0;
                while (!eof)
                {
                    int ch = fs.ReadByte();
                    if (ch < 0)
                    {
                        eof = true;
                        if (linelen == 0)
                        {
                            break;
                        }
                    }
                    else if (ch == 0x0d) // ignore CR
                    {
                        continue;
                    }
                    else if (ch == 0x0a) // LF
                    // go through
                    {
                    }
                    else if (linelen < linebuf.Length)
                    {
                        linebuf[linelen] = (byte)ch;
                        linelen++;
                        continue;
                    }

                    lineno++;
                    string line;
                    if (m_enc == null)
                    {
                        line = SJISDictionary.GetString(linebuf, 0, linelen);
                    }
                    else
                    {
                        line = m_enc.GetString(linebuf, 0, linelen);
                    }
                    linelen = 0;

                    line = line.Trim();
                    if (line == "")
                    {
                        continue;
                    }
                    if (line.StartsWith("#"))
                    {
                        if (lineno == 1)
                        {
                            m_columns = line.Substring(1).Split(",".ToCharArray());
                            for (int i = 0; i < m_columns.Length; i++)
                            {
                                m_columns[i] = m_columns[i].Trim();
                            }
                        }
                        continue;
                    }
                    List <string> data = new List <string>();
                    if (m_linenoflag)
                    {
                        data.Add(lineno.ToString());
                    }
                    int ptr = 0;
                    int nptr;
                    while (ptr < line.Length)
                    {
                        if (line[ptr] == ' ')
                        {
                            ptr++;
                            continue;
                        }
                        if (line[ptr] == '"')
                        {
                            ptr++;
                            nptr = line.IndexOf('"', ptr);
                            if (nptr < 0)
                            {
                                nptr = line.Length;
                            }
                            data.Add(line.Substring(ptr, nptr - ptr));
                            ptr = line.IndexOf(',', nptr);
                            if (ptr < 0)
                            {
                                ptr = line.Length;
                            }
                            else
                            {
                                ptr++;
                            }
                            continue;
                        }
                        if (line[ptr] == ',')
                        {
                            data.Add("");
                            ptr++;
                            continue;
                        }
                        nptr = line.IndexOf(',', ptr);
                        if (nptr < 0)
                        {
                            nptr = line.Length;
                        }
                        data.Add(line.Substring(ptr, nptr - ptr).Trim());
                        ptr = nptr + 1;
                    }
                    if (line[line.Length - 1] == ',')
                    {
                        data.Add("");
                    }
                    m_list.Add(data.ToArray());
                }
            }
            return(m_list);
        }
Example #6
0
 /// <summary>
 ///   SQLを実行して実行結果をテキストストリームにHumanReadableな形で書き出す
 /// </summary>
 public void Execute(string sql, TextWriter w)
 {
     try {
         if (sql.StartsWith("select", StringComparison.InvariantCultureIgnoreCase))
         {
             List <string[]> values = new List <string[]>();
             string[]        columns;
             int[]           fieldLen;
             using (DBReader reader = Query(sql)) {
                 columns  = reader.GetColumns();
                 fieldLen = new int[columns.Length];
                 for (int i = 0; i < fieldLen.Length; i++)
                 {
                     fieldLen[i] = SJISDictionary.GetByteCount(columns[i]);
                 }
                 string[] value;
                 while ((value = reader.Get()) != null)
                 {
                     for (int i = 0; i < fieldLen.Length; i++)
                     {
                         if (value[i] == null)
                         {
                             value[i] = "NULL";
                         }
                         int len = SJISDictionary.GetByteCount(value[i]);
                         if (fieldLen[i] < len)
                         {
                             fieldLen[i] = len;
                         }
                     }
                     values.Add(value);
                 }
             }
             for (int i = 0; i < fieldLen.Length; i++)
             {
                 w.Write('|');
                 string v = columns[i];
                 w.Write(' ');
                 w.Write(v);
                 for (int j = 0; j < fieldLen[i] - SJISDictionary.GetByteCount(v); j++)
                 {
                     w.Write(' ');
                 }
                 w.Write(' ');
             }
             w.WriteLine('|');
             for (int i = 0; i < fieldLen.Length; i++)
             {
                 w.Write('|');
                 for (int j = 0; j < fieldLen[i] + 2; j++)
                 {
                     w.Write('-');
                 }
             }
             w.WriteLine('|');
             foreach (string[] val in values)
             {
                 for (int i = 0; i < fieldLen.Length; i++)
                 {
                     w.Write('|');
                     string v = val[i];
                     w.Write(' ');
                     w.Write(v);
                     for (int j = 0; j < fieldLen[i] - SJISDictionary.GetByteCount(v); j++)
                     {
                         w.Write(' ');
                     }
                     w.Write(' ');
                 }
                 w.WriteLine('|');
             }
             w.WriteLine("Total {0} records.", values.Count);
         }
         else
         {
             int res = Execute(sql);
             w.WriteLine("{0} records affected.", res);
         }
     } catch (DbException ex) {
         w.WriteLine("{0}: {1}", ex.GetType().Name, ex.Message);
     }
 }
Example #7
0
        /// <summary>
        ///   固定長フィールドを読み取り、DataArrayとして返す。
        /// </summary>
        /// <param name="columns">読み取る項目名一覧(DataArrayのColumnsになる)</param>
        /// <param name="lengths">columnsの各項目のバイト数</param>
        /// <param name="readout">指定項目を読み取った後、改行までを読み捨てるかどうか</param>
        /// <param name="checkcr">改行チェックをするかどうか</param>
        /// <remarks>
        ///   <para>
        ///     readoutがtrueの場合、指定項目を読み取った後、改行コードまでを読み捨てる。
        ///     readoutがfalseでcheckcrがtrueの場合、指定項目を読み取ったすぐ後に改行コードまたはファイル末尾が無い場合にはIOExceptionを発生する。
        ///
        ///     指定項目を読み取っている最中に改行コードまたはファイル末尾になった場合にはIOExceptionを発生する。
        ///     ただし、1項目も読み取らないでファイル末尾になった場合は例外は発生せず、nullを返す。
        ///   </para>
        /// </remarks>
        public DataArray GetRecordData(string[] columns, int[] lengths, bool readout = true, bool checkcr = true)
        {
            if (columns == null)
            {
                throw new ArgumentNullException("columns must not be null.");
            }
            if (lengths == null)
            {
                throw new ArgumentNullException("lengths must not be null.");
            }
            if (columns.Length != lengths.Length)
            {
                throw new ArgumentException("columns.Length and lengths.Length must equal.");
            }
            int n = 0;

            for (int i = 0; i < lengths.Length; i++)
            {
                n += lengths[i];
            }
            byte[] buf = new byte[n];
            int    ptr = 0;
            int    r   = 0;

            while ((ptr < n) && ((r = fs.ReadByte()) >= 0))
            {
                if ((char)r == '\n')
                {
                    break;
                }
                if ((char)r == '\r')
                {
                    continue;
                }
                buf[ptr] = (byte)r;
                ptr++;
            }
            if ((ptr == 0) && (r < 0))
            {
                return(null);
            }
            if (ptr < n)
            {
                throw new IOException(String.Format("Short line (must be longer than {0} bytes)", n));
            }
            DataArray rec = new DataArray(columns, null);

            ptr = 0;
            for (int i = 0; i < columns.Length; i++)
            {
                rec[i] = SJISDictionary.GetString(buf, ptr, lengths[i]);
                ptr   += lengths[i];
            }
            if (readout)
            {
                ReadLine();
            }
            else if (checkcr)
            {
                string val;
                if (Read(1, out val) != 0)
                {
                    throw new IOException(String.Format("Illegal line length (must be {0} bytes).", n));
                }
            }
            return(rec);
        }