Пример #1
0
        }// -----------------------------------------

        /// <summary>
        /// Load a descriptor file
        /// The CD title is read from the .CUE filename
        /// Also checks if files declared inside the .CUE exist or not
        /// </summary>
        /// <param name="file">Filename to load [.cue]</param>
        /// <returns>Success ( read the ERROR Property )</returns>
        public bool load(string file)
        {
            if (!File.Exists(file))
            {
                ERROR = "File:" + file + "does not exist"; return(false);
            }

            // Vars init
            loadedFile_path = file;
            loadedFile_dir  = Path.GetDirectoryName(loadedFile_path);
            loadedFile_ext  = Path.GetExtension(loadedFile_path).ToLower().Trim('.');

            // This is redudant..
            if (!SUPPORTED_FORMATS.Contains(loadedFile_ext))
            {
                ERROR = "File Type is not supported.- Supported formats : " + SUPPORTED_FORMATS.ToString(); return(false);
            }

            try {
                loadedFile = File.ReadAllLines(file);
            }
            catch (IOException) {
                ERROR = "There was an I/O problem loading the file"; return(false);
            }

            // Init vars
            CD_TOTAL_SIZE = 0;
            CD_TITLE      = "untitled CD"; SECTOR_SIZE = 0; CD_TYPE = null;
            tracks        = new List <CueTrack>();
            openTrack     = null; openFile = null;

            // Try to capture the CD TITLE from the CUE filename
            Match m1 = Regex.Match(loadedFile_path, @"([^\/\\]*)\.(?:" + SUPPORTED_FORMATS + @")$", RegexOptions.IgnoreCase);

            if (m1.Success)
            {
                CD_TITLE = m1.Groups[1].Value;
            }

            // Start Parsing the file based on it's type
            Func <string, int> parser;

            switch (loadedFile_ext)
            {
            case "cue": parser = cue_parser; break;

            case "ccd": parser = ccd_parser; break;

            default: parser = cue_parser; break;
            }

            // Parser ::

            for (int c = 0; c < loadedFile.Length; c++)
            {
                loadedFile[c] = loadedFile[c].Trim();         // Trim whitespaces
                if (loadedFile[c].Length == 0)
                {
                    continue;                                    // Skip blank lines
                }
                if (loadedFile[c] == "\n")
                {
                    continue;
                }

                if (parser(loadedFile[c]) == 0)
                {
                    ERROR = String.Format("Parse Error at line[{0}] : {1}", c + 1, ERROR);
                    return(false);
                }
            }

            // -- POST PARSE CHECK ::

            if (tracks.Count == 0)
            {
                ERROR = "No Tracks in the CUE file"; return(false);
            }

            getCDTypeFromTracks();

            // :: Some Debug Info
            LOG.log("[CUEREADER] : Loading : `{0}`", loadedFile_path);
            LOG.log("[CUEREADER] : Title : `{0}`, Type: `{1}`, NumberOfTracks:{2}", CD_TITLE, CD_TYPE, tracks.Count);
            LOG.indent(1);
            // --

            if (loadedFile_ext == "ccd")
            {
                // TODO: CCD
            }

            // :: Go through each and every track, regardless if multitrack or not,
            //	Check for every single one of the files if exist or not
            //	Also count the number of file images to figure out `multifilecd`

            int cc = 0;     // Number of tracks with DiskFiles found

            foreach (var tr in tracks)
            {
                if (tr.indexes.Count == 0)
                {
                    ERROR = "Track [" + tr.trackNo + "] has no indexes defined"; return(false);
                }

                if (tr.trackFile == null)
                {
                    continue;
                }

                cc++;

                tr.workingFile = Path.Combine(loadedFile_dir, tr.trackFile);

                // Check the diskfiles
                if (!File.Exists(tr.workingFile))
                {
                    ERROR = "Image \"" + tr.trackFile + "\" does not exist"; return(false);
                }

                // Get Sizes
                var finfo = new FileInfo(Path.Combine(loadedFile_dir, tr.trackFile));
                tr.byteSize   = (int)finfo.Length;       // it can't be more than 800MB, so it's safe
                tr.sectorSize = (int)Math.Ceiling((double)(tr.byteSize / SECTOR_SIZE));

                // --
                if (tr.sectorSize <= 0)
                {
                    // Rare but worth checking
                    ERROR = "DiskFile " + tr.trackFile + " is currupt"; return(false);
                }

                CD_TOTAL_SIZE += tr.byteSize;
            }    // -- end each track


            // :: POST PARSE CALCULATIONS AND CHECKS ::


            // - Is it MultiTrack with MultiFiles?
            if (cc == tracks.Count && cc > 1)
            {
                LOG.log("+ MULTI-FILE Image CD");
                MULTIFILE = true;
                // Need to set sectorStart at each tracks;
                calculateTracksSectorStart();
            }
            else if (cc == 1)
            {
                LOG.log("+ SINGLE-FILE Image CD");
                MULTIFILE = false;
                // In case an single was found but not at the first track:
                if (tracks[0].trackFile == null)
                {
                    ERROR = "First track must declare an image file"; return(false);
                }

                var imageSectorSize = tracks[0].sectorSize;
                //Calculate tracks, starting from the end, backwards to 0
                var c = tracks.Count - 1;
                //Calculate last track manually, out of the loop
                tracks[c].calculateStart();
                tracks[c].sectorSize = imageSectorSize - tracks[c].sectorStart;
                while (--c >= 0)
                {
                    tracks[c].calculateStart();
                    tracks[c].sectorSize = tracks[c + 1].sectorStart - tracks[c].sectorStart;
                }

                calculateTracksByteSize();
            }
            else if (cc == 0)
            {
                ERROR = "There are no image files declared in the sheet."; return(false);
            }
            else
            {
                // Rare, and I don't know if anything does this
                ERROR = "Multiple Image sheets are restricted to one Track per Image only"; return(false);
            }

            LOG.log("+ Total CD SIZE : {0}", CD_TOTAL_SIZE);
            foreach (var tt in tracks)
            {
                LOG.log(tt);
            }
            LOG.line(); LOG.indent(0);
            return(true);
        }// -----------------------------------------