Exemplo n.º 1
0
        public void AnalyzeFile(string filename)
        {
            System.IO.StreamReader sr = null;
            try
            {
                for (int i = 0; i < 30; i++)
                {
                    try
                    {
                        sr = new System.IO.StreamReader(filename, Encoding.Default);
                        break;
                    }
                    catch (Exception ee)
                    {
                        System.Diagnostics.Debug.WriteLine(ee.Message);
                        System.Threading.Thread.Sleep(1000);
                    }
                }
                if (sr == null)
                {
                    throw new ApplicationException("Could not open input file, copy error?");
                }
                string header = sr.ReadLine();

                string[] fields        = header.Split(SplitChars);
                bool     isOs2010Files = fields[0].StartsWith("OS");

                /*Detect OS format*/
                int fldID;
                int fldSI;
                int fldFName;
                int fldEName;
                int fldClub;
                int fldClass;
                int fldStart;
                int fldTime;
                int fldStatus;
                int fldFirstPost;
                int fldLeg;
                int fldFinish;
                int fldTxt1, fldTxt2, fldTxt3;
                int fldTotalTime;
                OXTools.DetectOXCSVFormat(OXTools.SourceProgram.OS, fields, out fldID, out fldSI, out fldFName, out fldEName, out fldClub, out fldClass, out fldStart, out fldTime, out fldStatus, out fldFirstPost, out fldLeg, out fldFinish, out fldTxt1, out fldTxt2, out fldTxt3, out fldTotalTime);

                if (fldID == -1 || fldSI == -1 || fldFName == -1 || fldEName == -1 || fldClub == -1 || fldClass == -1 ||
                    fldStart == -1 || fldTime == -1 ||
                    fldStart == -1 || fldFirstPost == -1 || fldLeg == -1)
                {
                    throw new System.IO.IOException("Not OS-formatted file!");
                }

                string tmp;
                Dictionary <string, int> teamStartTimes = new Dictionary <string, int>();
                Dictionary <string, int> teamStatuses   = new Dictionary <string, int>();
                while ((tmp = sr.ReadLine()) != null)
                {
                    string[] parts = tmp.Split(SplitChars);

                    /* check so that the line is not incomplete*/
                    int id = Convert.ToInt32(parts[fldLeg]) * 1000000 + Convert.ToInt32(parts[fldID]);
                    int si = 0;
                    try
                    {
                        si = Convert.ToInt32(parts[fldSI]);
                    }
                    catch (Exception ee)
                    { }
                    string name  = parts[fldFName].Trim('\"') + " " + parts[fldEName].Trim('\"');
                    string club  = parts[fldClub].Trim('\"');
                    string Class = parts[fldClass].Trim('\"') + "-" + parts[fldLeg].Trim('\"');
                    int    leg   = Convert.ToInt32(parts[fldLeg].Trim('\"'));
                    int    start = strTimeToInt(parts[fldStart]);
                    int    time  = strTimeToInt(parts[fldTime]);

                    int status = 9;
                    try
                    {
                        status = Convert.ToInt32(parts[fldStatus]);
                        if (status == 0 && time < 0)
                        {
                            status = 9;
                        }
                    }
                    catch
                    {
                    }

                    int totalTime   = -1;
                    int totalStatus = status;

                    if (isOs2010Files)
                    {
                        if (fldTotalTime != -1)
                        {
                            //OK We have a totaltimefield..
                            //If totaltime set, use it as time (and status is status)
                            //Else, check if runner on course (status == 0 and FinishTime is empty => Status = 9)
                            //Else, something is not right, set status if <> 0, else set mp

                            if (!string.IsNullOrEmpty(parts[fldTotalTime]))
                            {
                                totalTime = strTimeToInt(parts[fldTotalTime]);
                            }
                            else if (status == 9)
                            {
                                //Runner still on course
                            }
                            else
                            {
                                totalStatus = status != 0 ? status : 3;
                                totalTime   = -3;
                            }
                        }
                        else
                        {
                            string key = parts[fldClass].Trim('\"') + ";" + club;
                            if (!teamStartTimes.ContainsKey(key))
                            {
                                teamStartTimes.Add(key, start);
                            }
                            else if (teamStartTimes[key] > start)
                            {
                                teamStartTimes[key] = start;
                            }

                            if (teamStatuses.ContainsKey(key))
                            {
                                int earlierStatus = teamStatuses[key];
                                if (status == 0 && earlierStatus != 0)
                                {
                                    totalStatus = earlierStatus;
                                }
                            }
                            else if (status != 0)
                            {
                                teamStatuses.Add(key, status);
                            }

                            if (time >= 0)
                            {
                                totalTime = strTimeToInt(parts[fldFinish]) - teamStartTimes[key];
                            }
                        }
                    }



                    List <ResultStruct> splittimes = new List <ResultStruct>();
                    /*parse splittimes*/
                    List <int> codes = new List <int>();
                    for (int i = fldFirstPost; i < parts.Length - 4; i++)
                    {
                        if (parts[i + 1].Length == 0 ||
                            parts[i + 2].Length == 0)
                        {
                            i += 3;
                            continue;
                        }
                        ResultStruct s = new ResultStruct();
                        try
                        {
                            i++;
                            s.ControlCode = Convert.ToInt32(parts[i]);

                            if (s.ControlCode == 999 && status == 0)
                            {
                                i++;
                                if (time == -1)
                                {
                                    time = strTimeToInt(parts[i]);
                                }
                                i++;
                            }
                            else
                            {
                                s.ControlCode += 1000;
                                while (codes.Contains(s.ControlCode))
                                {
                                    s.ControlCode += 1000;
                                }
                                codes.Add(s.ControlCode);
                                i++;
                                s.Time = strTimeToInt(parts[i]);
                                i++;
                                s.Place = 0;
                                try
                                {
                                    s.Place = Convert.ToInt32(parts[i]);
                                }
                                catch
                                { }

                                splittimes.Add(s);
                            }
                        }
                        catch
                        {
                        }
                    }
                    FireOnResult(new RelayResult()
                    {
                        ID            = id,
                        LegNumber     = leg,
                        RunnerName    = name,
                        RunnerClub    = club,
                        Class         = Class,
                        StartTime     = start,
                        Time          = time,
                        Status        = status,
                        SplitTimes    = splittimes,
                        OverallTime   = totalTime,
                        OverallStatus = totalStatus
                    });
                }
            }
            catch (Exception ee)
            {
                FireLogMsg("ERROR in OSParser: " + ee.Message);
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                }
            }
        }
Exemplo n.º 2
0
        public void AnalyzeFile(string filename, bool useExtraFields)
        {
            System.IO.StreamReader sr = null;
            try
            {
                for (int i = 0; i < 30; i++)
                {
                    try
                    {
                        sr = new System.IO.StreamReader(filename, Encoding.Default);
                        break;
                    }
                    catch (Exception ee)
                    {
                        System.Diagnostics.Debug.WriteLine(ee.Message);
                    }
                }
                if (sr == null)
                {
                    throw new ApplicationException("Could not open input file, copy error?");
                }
                string header = sr.ReadLine();

                string[] fields = header.Split(SplitChars);

                /*Detect OE format*/
                int fldID, fldSI, fldFName, fldEName, fldClub, fldClass, fldStart, fldTime, fldStatus, fldFirstPost, fldText1, fldText2, fldText3, fldFinish, fldLeg;
                int fldTotalTime;
                OXTools.DetectOXCSVFormat(OXTools.SourceProgram.OE, fields, out fldID, out fldSI, out fldFName, out fldEName, out fldClub, out fldClass, out fldStart, out fldTime, out fldStatus, out fldFirstPost, out fldLeg, out fldFinish, out fldText1, out fldText2, out fldText3, out fldTotalTime);

                if (fldID == -1 || fldSI == -1 || fldFName == -1 || fldEName == -1 || fldClub == -1 || fldClass == -1 ||
                    fldStart == -1 || fldTime == -1 ||
                    fldStart == -1 || fldFirstPost == -1)
                {
                    throw new System.IO.IOException("Not OE-formatted file!");
                }

                fldText1 = Array.IndexOf(fields, "Text1");
                fldText2 = Array.IndexOf(fields, "Text2");
                fldText3 = Array.IndexOf(fields, "Text3");



                if (fldID == -1 || fldSI == -1 || fldFName == -1 || fldEName == -1 || fldClub == -1 || fldClass == -1 ||
                    fldStart == -1 || fldTime == -1)
                {
                    throw new System.IO.IOException("Not OE-formatted file!");
                }

                string tmp;
                while ((tmp = sr.ReadLine()) != null)
                {
                    string[] parts = tmp.Split(SplitChars);

                    /* check so that the line is not incomplete*/
                    int    id    = Convert.ToInt32(parts[fldID]);
                    string name  = parts[fldFName].Trim('\"') + " " + parts[fldEName].Trim('\"');
                    string club  = parts[fldClub].Trim('\"');
                    string Class = parts[fldClass].Trim('\"');
                    int    start = strTimeToInt(parts[fldStart]);
                    int    time  = strTimeToInt(parts[fldTime]);

                    if (useExtraFields && fldText1 > 0 && fldText2 > 0 && fldText3 > 0)
                    {
                        name += "/" + parts[fldText1].Trim('\"');
                        club  = parts[fldText3].Trim('\"');
                        if (parts[fldText2].Trim('\"') != club)
                        {
                            club += "/" + parts[fldText2].Trim('\"');
                        }
                    }

                    int status = 0;
                    try
                    {
                        status = Convert.ToInt32(parts[fldStatus]);
                    }
                    catch
                    {
                    }

                    List <ResultStruct> splittimes = new List <ResultStruct>();
                    /*parse splittimes*/
                    List <int> codes = new List <int>();
                    if (fldFirstPost >= 0)
                    {
                        for (int i = fldFirstPost; i < parts.Length - 4; i++)
                        {
                            if (parts[i + 1].Length == 0 ||
                                parts[i + 2].Length == 0)
                            {
                                i += 3;
                                continue;
                            }
                            ResultStruct s = new ResultStruct();
                            try
                            {
                                //s.ControlNo = Convert.ToInt32(parts[i]);
                                i++;
                                s.ControlCode = Convert.ToInt32(parts[i]);

                                if (s.ControlCode == 999)
                                {
                                    i++;
                                    if (time == -1 && status == 0)
                                    {
                                        time = strTimeToInt(parts[i]);
                                    }

                                    i++;
                                }
                                else
                                {
                                    s.ControlCode += 1000;
                                    while (codes.Contains(s.ControlCode))
                                    {
                                        s.ControlCode += 1000;
                                    }
                                    codes.Add(s.ControlCode);

                                    i++;
                                    s.Time = strTimeToInt(parts[i]);
                                    i++;

                                    if (s.Time > 0)
                                    {
                                        s.Place = 0;
                                        try
                                        {
                                            s.Place = Convert.ToInt32(parts[i]);
                                        }
                                        catch
                                        { }

                                        splittimes.Add(s);
                                    }
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                    FireOnResult(new Result()
                    {
                        ID         = id,
                        RunnerName = name,
                        RunnerClub = club,
                        Class      = Class,
                        StartTime  = start,
                        Time       = time,
                        Status     = status,
                        SplitTimes = splittimes
                    });
                }
            }
            catch (Exception ee)
            {
                FireLogMsg("ERROR in OEPArser: " + ee.Message);
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                }
            }
        }