示例#1
0
文件: TextTools.cs 项目: NueGy/NgLib
        /// <summary>
        /// Transforme un dictionary de positions en une chaine
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        public static string JoinText(IDictionary <string, string> datas, TEXT.TextStructure schema)
        {
            try
            {
                StringBuilder retour = new StringBuilder();

                int lastPositionPlusSize = 0;
                int partnumber           = 0;
                foreach (var ipart in schema.GetPartsOrdered())
                {
                    TextStructurePart partschema = ipart.Value;


                    // obtenir la donnée
                    string orgnData = null;
                    if (datas.ContainsKey(partschema.NodeName))
                    {
                        orgnData = datas[partschema.NodeName];
                    }
                    if (string.IsNullOrWhiteSpace(orgnData) && partschema != null && partschema.CompleteNumber)
                    {
                        orgnData = "0";                                                                                         // si complete number, ce sera forcément des 0
                    }
                    retour.Append(JoinTextItem(schema, partschema, orgnData, partnumber, lastPositionPlusSize));
                    partnumber++;
                }
                return(retour.ToString());
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("JoinText {0}", ex.Message), ex);
            }
        }
示例#2
0
文件: TextTools.cs 项目: NueGy/NgLib
        private static Dictionary <string, string> SplitTextTXT(string inputString, TEXT.TextStructure schema)
        {
            try
            {
                int sizestring                     = inputString.Length;
                int lastPositionPlusSize           = 0;
                Dictionary <string, string> retour = new Dictionary <string, string>();
                foreach (var ipart in schema.GetPartsOrdered())
                {
                    TextStructurePart partschema = ipart.Value;

                    int itemPosition    = partschema.Position; // Attention position logique et non réel  1=0
                    int itemSize        = partschema.Length;
                    int itemPositionEnd = itemPosition + itemSize;
                    if (itemPositionEnd > sizestring)
                    {
                        itemSize = sizestring - itemPositionEnd;
                    }

                    if (sizestring < itemPosition || itemSize < 1)  // hors limite (la taille de la chaine est plus petite que ce qui a été défini)
                    {
                        retour.Add(partschema.NodeName, null);
                        continue;
                    }

                    // on découpe la partie qui nous interresse
                    string datastr = inputString.Substring(itemPosition, itemSize);

                    if (!schema.ReadDisallowTrim && datastr != null)
                    {
                        datastr = datastr.Trim();
                    }
                    retour.Add(partschema.NodeName, datastr);
                    lastPositionPlusSize = itemPositionEnd;
                }
                return(retour);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("SplitText TXT {0}", ex.Message), ex);
            }
        }
示例#3
0
文件: TextTools.cs 项目: NueGy/NgLib
 private static Dictionary <string, string> SplitTextCSV(string inputString, TEXT.TextStructure schema)
 {
     try
     {
         string[] inputStringT = inputString.Split(schema.TextModeCSVSeparator);
         Dictionary <string, string> retour = new Dictionary <string, string>();
         foreach (var ipart in schema.GetPartsOrdered())
         {
             TextStructurePart partschema = ipart.Value;
             if (inputStringT.Length < partschema.Position)
             {
                 retour.Add(partschema.NodeName, null);
                 continue;
             }
             retour.Add(partschema.NodeName, inputStringT[ipart.Key]);
         }
         return(retour);
     }
     catch (Exception ex)
     {
         throw new Exception(string.Format("SplitText CSV {0}", ex.Message), ex);
     }
 }