Ejemplo n.º 1
0
        public bool LoadFromBinary(string file)
        {
            long        t1     = TimeUtility.GetElapsedTimeUs();
            bool        result = true;
            BinaryTable table  = new BinaryTable();

            table.Load(HomePath.GetAbsolutePath(file));
            long t2 = TimeUtility.GetElapsedTimeUs();

            long t3 = TimeUtility.GetElapsedTimeUs();

            for (int index = 0; index < table.Records.Count; ++index)
            {
                try {
                    TData data = new TData();
                    bool  ret  = data.ReadFromBinary(table, index);
                    if (ret && !m_DataContainer.ContainsKey(data.GetId()))
                    {
                        m_DataContainer.Add(data.GetId(), data);
                    }
                    else
                    {
                        string info = string.Format("DataTempalteMgr.CollectDataFromBinary collectData Row:{0} failed in {1}!", index, file);
                        LogSystem.Error(info);
                        Helper.LogCallStack(true);
                        result = false;
                    }
                } catch (System.Exception ex) {
                    LogSystem.Error("CollectData failed. file:{0} rowIndex:{1}\nException:{2}\n{3}", file, index, ex.Message, ex.StackTrace);
                }
            }
            long t4 = TimeUtility.GetElapsedTimeUs();

            LogSystem.Info("binary load {0} parse {1}, file {2}", t2 - t1, t4 - t3, file);
            return(result);
        }
 public static int ExtractInt(BinaryTable table, int recordVal, int defaultVal)
 {
     return(recordVal);
 }
 public static bool ExtractBool(BinaryTable table, int recordVal, bool defaultVal)
 {
     return(recordVal != 0);
 }
 public static float ExtractFloat(BinaryTable table, float recordVal, float defaultVal)
 {
     return(recordVal);
 }
 public static float SetValue(BinaryTable table, float val, float defaultVal)
 {
     return(val);
 }
 public static long ExtractLong(BinaryTable table, int recordVal, long defaultVal)
 {
     return(recordVal);
 }
 public static int SetValue(BinaryTable table, long val, long defaultVal)
 {
     return((int)val);
 }
 public static int SetValue(BinaryTable table, int val, int defaultVal)
 {
     return(val);
 }
 public static int SetValue(BinaryTable table, bool val, bool defaultVal)
 {
     return(val ? 1 : 0);
 }
Ejemplo n.º 10
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);
            }
        }
Ejemplo n.º 11
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;
            }
        }