AddString() 공개 메소드

public AddString ( string str ) : int
str string
리턴 int
 public static int SetValue(BinaryTable table, string val, string defaultVal)
 {
     if (0 == val.CompareTo(defaultVal))
     {
         return(-1);
     }
     return(table.AddString(val));
 }
예제 #2
0
        public static bool Convert(string file, string outFile, Encoding encoding)
        {
            try {
                string[] lines = File.ReadAllLines(file, encoding);
                if (lines.Length >= 2)
                {
                    string[] types  = lines[0].Split('\t');
                    string[] fields = lines[1].Split('\t');

                    string dirName  = Path.GetDirectoryName(file);
                    string fileName = Path.GetFileName(file);
                    string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);

                    int fieldCount       = 0;
                    int excelColumnCount = types.Length;
                    if (fields.Length != excelColumnCount)
                    {
                        LogSystem.Error("[line:2] “{0}” field count != {1}", lines[1], excelColumnCount);
                        return(false);
                    }
                    for (int ix = 0; ix < excelColumnCount; ++ix)
                    {
                        if (string.IsNullOrEmpty(types[ix]) || string.IsNullOrEmpty(fields[ix]))
                        {
                            continue;
                        }
                        ++fieldCount;
                    }
                    BinaryTable table = new BinaryTable();
                    for (int rowIndex = 2; rowIndex < lines.Length; ++rowIndex)
                    {
                        if (lines[rowIndex].StartsWith("#") || lines[rowIndex].StartsWith("//"))
                        {
                            continue;
                        }
                        int      colIndex    = 0;
                        string[] fieldValues = lines[rowIndex].Split('\t');
                        if (fieldValues.Length != excelColumnCount)
                        {
                            LogSystem.Error("[line:{0}] “{1}” field count != {2}", rowIndex + 1, lines[rowIndex], excelColumnCount);
                            continue;
                        }
                        byte[] record = new byte[fieldCount * sizeof(int)];
                        table.Records.Add(record);
                        for (int ix = 0; ix < excelColumnCount; ++ix)
                        {
                            if (string.IsNullOrEmpty(fields[ix]) || string.IsNullOrEmpty(types[ix]))
                            {
                                continue;
                            }
                            string type = types[ix].Trim();
                            string val  = fieldValues[ix].Trim();
                            try {
                                if (0 == type.CompareTo("int") || 0 == type.CompareTo("int32") || 0 == type.CompareTo("long") || 0 == type.CompareTo("int64"))
                                {
                                    int v = 0;
                                    if (!string.IsNullOrEmpty(val))
                                    {
                                        v = int.Parse(val);
                                    }
                                    WriteIndex(record, colIndex, v);
                                }
                                else if (0 == type.CompareTo("float"))
                                {
                                    float v = 0;
                                    if (!string.IsNullOrEmpty(val))
                                    {
                                        v = float.Parse(val);
                                    }
                                    WriteFloat(record, colIndex, v);
                                }
                                else if (0 == type.CompareTo("bool"))
                                {
                                    bool v = false;
                                    if (!string.IsNullOrEmpty(val))
                                    {
                                        v = (val == "true" || val == "1");
                                    }
                                    WriteIndex(record, colIndex, v ? 1 : 0);
                                }
                                else if (0 == type.CompareTo("string"))
                                {
                                    int index = table.AddString(val);
                                    WriteIndex(record, colIndex, index);
                                }
                                else if (0 == type.CompareTo("int[]") || 0 == type.CompareTo("int32[]") || 0 == type.CompareTo("long[]") || 0 == type.CompareTo("int64[]"))
                                {
                                    int index = -1;
                                    if (!string.IsNullOrEmpty(val))
                                    {
                                        string[] v    = val.Split(',', ';', '|', ' ');
                                        int[]    vals = new int[v.Length];
                                        for (int i = 0; i < v.Length; ++i)
                                        {
                                            vals[i] = int.Parse(v[i]);
                                        }
                                        index = table.AddIntList(vals);
                                    }
                                    WriteIndex(record, colIndex, index);
                                }
                                else if (0 == type.CompareTo("float[]"))
                                {
                                    int index = -1;
                                    if (!string.IsNullOrEmpty(val))
                                    {
                                        string[] v    = val.Split(',', ';', '|', ' ');
                                        float[]  vals = new float[v.Length];
                                        for (int i = 0; i < v.Length; ++i)
                                        {
                                            vals[i] = float.Parse(v[i]);
                                        }
                                        index = table.AddFloatList(vals);
                                    }
                                    WriteIndex(record, colIndex, index);
                                }
                                else if (0 == type.CompareTo("bool[]"))
                                {
                                    int index = -1;
                                    if (!string.IsNullOrEmpty(val))
                                    {
                                        string[] v    = val.Split(',', ';', '|', ' ');
                                        int[]    vals = new int[v.Length];
                                        for (int i = 0; i < v.Length; ++i)
                                        {
                                            vals[i] = (v[i] == "true" || v[i] == "1") ? 1 : 0;
                                        }
                                        index = table.AddIntList(vals);
                                    }
                                    WriteIndex(record, colIndex, index);
                                }
                                else if (0 == type.CompareTo("string[]"))
                                {
                                    int index = -1;
                                    if (!string.IsNullOrEmpty(val))
                                    {
                                        string[] vals = val.Split(',', ';', '|', ' ');
                                        index = table.AddStrList(vals);
                                    }
                                    WriteIndex(record, colIndex, index);
                                }
                            } catch (Exception ex) {
                                LogSystem.Error("[line:{0} col:{1}] “{2}”, exception:{3}\n{4}", rowIndex + 1, colIndex + 1, lines[rowIndex], ex.Message, ex.StackTrace);
                            }
                            ++colIndex;
                        }
                    }
                    table.Save(outFile);
                }
                return(true);
            } catch (Exception ex) {
                LogSystem.Error("exception:{0}\n{1}", ex.Message, ex.StackTrace);
                return(false);
            }
        }
예제 #3
0
        public static bool Convert(string file, string outFile, Encoding encoding)
        {
            try {
                string[] lines = File.ReadAllLines(file, encoding);
                if (lines.Length >= 2) {
                    string[] types = lines[0].Split('\t');
                    string[] fields = lines[1].Split('\t');

                    string dirName = Path.GetDirectoryName(file);
                    string fileName = Path.GetFileName(file);
                    string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);

                    int fieldCount = 0;
                    int excelColumnCount = types.Length;
                    if (fields.Length != excelColumnCount) {
                        LogSystem.Error("[line:2] “{0}” field count != {1}", lines[1], excelColumnCount);
                        return false;
                    }
                    for (int ix = 0; ix < excelColumnCount; ++ix) {
                        if (string.IsNullOrEmpty(types[ix]) || string.IsNullOrEmpty(fields[ix]))
                            continue;
                        ++fieldCount;
                    }
                    BinaryTable table = new BinaryTable();
                    for (int rowIndex = 2; rowIndex < lines.Length; ++rowIndex) {
                        if (lines[rowIndex].StartsWith("#") || lines[rowIndex].StartsWith("//"))
                            continue;
                        int colIndex = 0;
                        string[] fieldValues = lines[rowIndex].Split('\t');
                        if (fieldValues.Length != excelColumnCount) {
                            LogSystem.Error("[line:{0}] “{1}” field count != {2}", rowIndex + 1, lines[rowIndex], excelColumnCount);
                            continue;
                        }
                        byte[] record = new byte[fieldCount * sizeof(int)];
                        table.Records.Add(record);
                        for (int ix = 0; ix < excelColumnCount; ++ix) {
                            if (string.IsNullOrEmpty(fields[ix]) || string.IsNullOrEmpty(types[ix]))
                                continue;
                            string type = types[ix].Trim();
                            string val = fieldValues[ix].Trim();
                            try {
                                if (0 == type.CompareTo("int") || 0 == type.CompareTo("int32") || 0 == type.CompareTo("long") || 0 == type.CompareTo("int64")) {
                                    int v = 0;
                                    if (!string.IsNullOrEmpty(val)) {
                                        v = int.Parse(val);
                                    }
                                    WriteIndex(record, colIndex, v);
                                } else if (0 == type.CompareTo("float")) {
                                    float v = 0;
                                    if (!string.IsNullOrEmpty(val)) {
                                        v = float.Parse(val);
                                    }
                                    WriteFloat(record, colIndex, v);
                                } else if (0 == type.CompareTo("bool")) {
                                    bool v = false;
                                    if (!string.IsNullOrEmpty(val)) {
                                        v = (val == "true" || val == "1");
                                    }
                                    WriteIndex(record, colIndex, v ? 1 : 0);
                                } else if (0 == type.CompareTo("string")) {
                                    int index = table.AddString(val);
                                    WriteIndex(record, colIndex, index);
                                } else if (0 == type.CompareTo("int[]") || 0 == type.CompareTo("int32[]") || 0 == type.CompareTo("long[]") || 0 == type.CompareTo("int64[]")) {
                                    int index = -1;
                                    if (!string.IsNullOrEmpty(val)) {
                                        string[] v = val.Split(',', ';', '|', ' ');
                                        int[] vals = new int[v.Length];
                                        for (int i = 0; i < v.Length; ++i) {
                                            vals[i] = int.Parse(v[i]);
                                        }
                                        index = table.AddIntList(vals);
                                    }
                                    WriteIndex(record, colIndex, index);
                                } else if (0 == type.CompareTo("float[]")) {
                                    int index = -1;
                                    if (!string.IsNullOrEmpty(val)) {
                                        string[] v = val.Split(',', ';', '|', ' ');
                                        float[] vals = new float[v.Length];
                                        for (int i = 0; i < v.Length; ++i) {
                                            vals[i] = float.Parse(v[i]);
                                        }
                                        index = table.AddFloatList(vals);
                                    }
                                    WriteIndex(record, colIndex, index);
                                } else if (0 == type.CompareTo("bool[]")) {
                                    int index = -1;
                                    if (!string.IsNullOrEmpty(val)) {
                                        string[] v = val.Split(',', ';', '|', ' ');
                                        int[] vals = new int[v.Length];
                                        for (int i = 0; i < v.Length; ++i) {
                                            vals[i] = (v[i] == "true" || v[i] == "1") ? 1 : 0;
                                        }
                                        index = table.AddIntList(vals);
                                    }
                                    WriteIndex(record, colIndex, index);
                                } else if (0 == type.CompareTo("string[]")) {
                                    int index = -1;
                                    if (!string.IsNullOrEmpty(val)) {
                                        string[] vals = val.Split(',', ';', '|', ' ');
                                        index = table.AddStrList(vals);
                                    }
                                    WriteIndex(record, colIndex, index);
                                }
                            } catch (Exception ex) {
                                LogSystem.Error("[line:{0} col:{1}] “{2}”, exception:{3}\n{4}", rowIndex + 1, colIndex + 1, lines[rowIndex], ex.Message, ex.StackTrace);
                            }
                            ++colIndex;
                        }
                    }
                    table.Save(outFile);
                }
                return true;
            } catch (Exception ex) {
                LogSystem.Error("exception:{0}\n{1}", ex.Message, ex.StackTrace);
                return false;
            }
        }