コード例 #1
0
        public MapData ReadFromFile(string fileName)
        {
            Func <string, bool> isComment = m => !string.IsNullOrEmpty(commentKey) && m.StartsWith(commentKey);

            var result = new MapData();

            using (StreamReader sr = new StreamReader(fileName))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    if (!isComment(line))
                    {
                        break;
                    }
                }

                var parts = line.Split(this.delimiter);
                if (this.keyIndex == -1)
                {
                    this.keyIndex = Array.IndexOf(parts, this.keyName);
                    if (this.keyIndex == -1)
                    {
                        throw new ArgumentException(string.Format("Cannot find key column {0} in file {1}", this.keyName, fileName));
                    }
                }
                else
                {
                    this.keyName = parts[this.keyIndex];
                }

                if (this.valueIndex == -1)
                {
                    this.valueIndex = Array.IndexOf(parts, this.valueName);
                    if (valueIndex == -1)
                    {
                        throw new ArgumentException(string.Format("Cannot find value column {0} in file {1}", this.valueName, fileName));
                    }
                }
                else
                {
                    this.valueName = parts[this.valueIndex];
                }

                result.KeyName   = this.keyName;
                result.ValueName = this.valueName;
                for (int i = 0; i < parts.Length; i++)
                {
                    if (i != keyIndex && i != valueIndex)
                    {
                        result.InfoNames.Add(parts[i]);
                    }
                }

                while ((line = sr.ReadLine()) != null)
                {
                    if (!string.IsNullOrWhiteSpace(line) && !isComment(line))
                    {
                        if (CheckEnd(line))
                        {
                            break;
                        }

                        var curParts = line.Split(this.delimiter);
                        var item     = new MapDataItem();
                        item.Key              = curParts[keyIndex];
                        item.Value            = curParts[valueIndex];
                        result.Data[item.Key] = item;
                        for (int i = 0; i < curParts.Length; i++)
                        {
                            if (i != keyIndex && i != valueIndex)
                            {
                                item.Informations.Add(curParts[i]);
                            }
                        }
                    }
                }
            }

            return(result);
        }
コード例 #2
0
ファイル: MapDataReader.cs プロジェクト: shengqh/CQS.Core
    public MapData ReadFromFile(string fileName)
    {
      Func<string, bool> isComment = m => !string.IsNullOrEmpty(commentKey) && m.StartsWith(commentKey);

      var result = new MapData();
      using (StreamReader sr = new StreamReader(fileName))
      {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
          if (!isComment(line))
          {
            break;
          }
        }

        var parts = line.Split(this.delimiter);
        if (this.keyIndex == -1)
        {
          this.keyIndex = Array.IndexOf(parts, this.keyName);
          if (this.keyIndex == -1)
          {
            throw new ArgumentException(string.Format("Cannot find key column {0} in file {1}", this.keyName, fileName));
          }
        }
        else
        {
          this.keyName = parts[this.keyIndex];
        }

        if (this.valueIndex == -1)
        {
          this.valueIndex = Array.IndexOf(parts, this.valueName);
          if (valueIndex == -1)
          {
            throw new ArgumentException(string.Format("Cannot find value column {0} in file {1}", this.valueName, fileName));
          }
        }
        else
        {
          this.valueName = parts[this.valueIndex];
        }

        result.KeyName = this.keyName;
        result.ValueName = this.valueName;
        for (int i = 0; i < parts.Length; i++)
        {
          if (i != keyIndex && i != valueIndex)
          {
            result.InfoNames.Add(parts[i]);
          }
        }

        while ((line = sr.ReadLine()) != null)
        {
          if (!string.IsNullOrWhiteSpace(line) && !isComment(line))
          {
            if (CheckEnd(line))
            {
              break;
            }

            var curParts = line.Split(this.delimiter);
            var item = new MapDataItem();
            item.Key = curParts[keyIndex];
            item.Value = curParts[valueIndex];
            result.Data[item.Key] = item;
            for (int i = 0; i < curParts.Length; i++)
            {
              if (i != keyIndex && i != valueIndex)
              {
                item.Informations.Add(curParts[i]);
              }
            }
          }
        }
      }

      return result;
    }