Example #1
0
        /// <summary> Parse an array of raw file information returned from the
        /// FTP server
        ///
        /// </summary>
        /// <param name="files">    array of strings
        /// </param>
        /// <returns> array of FTPFile objects
        /// </returns>
        internal virtual FTPFile[] Parse(string[] files)
        {
            FTPFile[] temp = new FTPFile[files.Length];

            // quick check if no files returned
            if (files.Length == 0)
            {
                return(temp);
            }

            int count = 0;

            for (int i = 0; i < files.Length; i++)
            {
                try
                {
                    FTPFile file = parser.Parse(files[i]);
                    // we skip null returns - these are duff lines we know about and don't
                    // really want to throw an exception
                    if (file != null)
                    {
                        temp[count++] = file;
                    }
                }
                catch (FormatException ex)
                {
                    log.Debug(ex.Message);
                    if (rotateParsersOnFail && count == 0)
                    {
                        // first error, let's try swapping parsers
                        RotateParsers();
                        FTPFile file = parser.Parse(files[i]);
                        if (file != null)
                        {
                            temp[count++] = file;
                        }
                    }
                    // rethrow
                    else
                    {
                        throw ex;
                    }
                }
            }
            FTPFile[] result = new FTPFile[count];
            Array.Copy(temp, 0, result, 0, count);
            return(result);
        }
        /// <summary>
        /// Parse an array of raw file information returned from the
        /// FTP server
        /// </summary>
        /// <param name="fileStrings">    array of strings
        /// </param>
        /// <returns> array of FTPFile objects
        /// </returns>
        public virtual FTPFile[] Parse(string[] fileStrings)
        {
            log.Debug("Parse() called using culture: " + parserCulture.EnglishName);
            FTPFile[] files = new FTPFile[fileStrings.Length];

            // quick check if no files returned
            if (fileStrings.Length == 0)
            {
                return(files);
            }

            if (!userSetParser && !parserDetected)
            {
                DetectParser(fileStrings);
            }

            int count = 0;

            for (int i = 0; i < fileStrings.Length; i++)
            {
                if (fileStrings[i] == null || fileStrings[i].Trim().Length == 0)
                {
                    continue;
                }

                try
                {
                    FTPFile file = null;
                    if (parser.IsMultiLine())
                    {
                        // vms uses more than 1 line for some file listings. We must keep going
                        // thru till we've got everything
                        StringBuilder filename = new StringBuilder(fileStrings[i]);
                        while (i + 1 < fileStrings.Length && fileStrings[i + 1].IndexOf(';') < 0)
                        {
                            filename.Append(" ").Append(fileStrings[i + 1]);
                            i++;
                        }
                        file = parser.Parse(filename.ToString());
                    }
                    else
                    {
                        file = parser.Parse(fileStrings[i]);
                    }

                    // we skip null returns - these are duff lines we know about and don't
                    // really want to throw an exception
                    if (file != null)
                    {
                        if (timeDiff.Ticks != 0)
                        {
                            file.ApplyTimeDifference(timeDiff);
                        }
                        files[count++] = file;
                    }
                }
                catch (RestartParsingException)
                {
                    log.Debug("Restarting parsing from first entry in list");
                    i     = -1;
                    count = 0;
                    continue;
                }
            }
            FTPFile[] result = new FTPFile[count];
            Array.Copy(files, 0, result, 0, count);
            return(result);
        }