Exemple #1
0
        /*
         *  The XTag function is need to add extra functionality to JCR Dir and is called by JCR_Dir if these extra functions are actually used.
         *
         *  (Well, the need of this XTag function lead to JCR5 becoming really really ugly, and that eventually lead to me realizing that if JCR5 would ever be able to do more a more flexible format was needed, hence the birth of JCR6. ;)
         *
         */
        void XTag(QuickStream BT, TJCRDIR JCR, bool LoadComments, string JCRFile)
        {
            string XTC;  // Stands for 'XTag Command'. I'm not promoting any sorts of stuff here(in fact I recommend not to use that) :P
            var    Length = BT.ReadInt();

            XTC = BT.ReadString(Length).ToUpper();
            string  F;
            TJCRDIR J;
            var     D = System.IO.Path.GetDirectoryName(JCRFile).Replace(@"\", "/"); //Replace(ExtractDir(JCRFile),"\","/"); If D And Right(D,1)<>"/" D:+"/"

            if (D != "" && qstr.Right(D, 1) != "/")
            {
                D += "/";
            }
            //'JCRD_Print "XTag: "+XTC
            switch (XTC)
            {
            case "IMP":
            case "IMPORT":
                Length = BT.ReadInt();
                F      = BT.ReadString(Length).Replace("\\", "/");
                if (qstr.Left(F, 1) != "/" && qstr.Mid(F, 2, 1) != ":" && System.IO.File.Exists(D + F))
                {
                    F = D + F;       //' If the required file to import is found in the same directory than hit that file.
                }
                J = JCR6.Dir(F);     //' ,LoadComments)
                if (J == null)
                {
                    //'JCRD_Print "WARNING! Could not import "+F+"~n= Report: "+JCRD_DumpError
                }
                else
                {
                    //'JCRD_Print "Importing: "+F
                    //'For Local K$=EachIn MapKeys(J)
                    //'   MapInsert JCR,K,MapValueForKey(J,K)
                    //'   Next
                    JCR.Patch(J);
                }
                break;

            default:
                JCR_JAMERR("WARNING! Unknown XTag in JCR file!", "?", "?", "XTAG");
                break;
            }
        }
Exemple #2
0
        public override TJCRDIR Dir(string file)
        {
            QuickStream BT = null;

            try {
                BT = QuickStream.ReadFile(file);
                var    ret = new TJCRDIR();
                string s;
                do
                {
                    if (BT.EOF)
                    {
                        throw new Exception("JQL heading not found!");
                    }
                    s = RL(BT);
                } while (s == "" || qstr.Prefixed(s, "#"));
                if (s != "JQL")
                {
                    throw new Exception("JQL not properly headed!");
                }
                var optional = true;
                var author   = "";
                var notes    = "";
                while (!BT.EOF)
                {
                    s = RL(BT);
                    var c = new QP(s);
                    if (s != "" && (!qstr.Prefixed(s, "#")))
                    {
                        switch (c.commando)
                        {
                        case "REQUIRED":
                        case "REQ":
                            optional = false;
                            break;

                        case "OPTIONAL":
                        case "OPT":
                            optional = true;
                            break;

                        case "PATCH": {
                            var to = c.parameter.IndexOf('>');
                            if (to < 0)
                            {
                                var p = JCR6.Dir(c.parameter);
                                if (p == null)
                                {
                                    if (optional)
                                    {
                                        break;
                                    }
                                    throw new Exception($"Patch error {JCR6.JERROR}");
                                }
                                ret.Patch(p);
                            }
                            else
                            {
                                var rw = c.parameter.Substring(0, to).Trim().Replace("\\", "/");
                                var tg = c.parameter.Substring(to + 1).Trim().Replace("\\", "/");
                                var p  = JCR6.Dir(rw);
                                if (p == null)
                                {
                                    if (optional)
                                    {
                                        break;
                                    }
                                    throw new Exception($"Patch error {JCR6.JERROR}");
                                }
                                ret.Patch(p, tg);
                            }
                            break;
                        }

                        case "AUTHOR":
                        case "AUT":
                            author = c.parameter;
                            break;

                        case "NOTES":
                        case "NTS":
                            notes = c.parameter;
                            break;

                        case "RAW": {
                            var p  = c.parameter.IndexOf('>');
                            var rw = c.parameter.Replace("\\", "/");
                            var tg = rw;
                            if (p >= 0)
                            {
                                rw = c.parameter.Substring(0, p).Trim().Replace("\\", "/");
                                tg = c.parameter.Substring(p + 1).Trim().Replace("\\", "/");
                            }
                            if (tg.Length > 1 && tg[1] == ':')
                            {
                                tg = tg.Substring(2);
                            }
                            while (tg[1] == '/')
                            {
                                tg = tg.Substring(1);
                            }
                            if (rw == "")
                            {
                                throw new Exception("RAW no original");
                            }
                            if (tg == "")
                            {
                                throw new Exception("RAW no target");
                            }
                            if (!File.Exists(rw))
                            {
                                if (optional)
                                {
                                    break;
                                }
                                throw new Exception($"Required raw file \"{rw}\" doesn't exist!");
                            }
                            var e = new TJCREntry();
                            e.Entry                   = tg;
                            e.MainFile                = rw;
                            e.Storage                 = "Store";
                            e.Offset                  = 0;
                            e.Size                    = (int)new FileInfo(rw).Length;
                            e.CompressedSize          = e.Size;
                            e.Notes                   = notes;
                            e.Author                  = author;
                            ret.Entries[tg.ToUpper()] = e;
                            break;
                        }

                        case "TEXT":
                        case "TXT": {
                            var tg = c.parameter.Trim().Replace("\\", "/");
                            if (tg.Length > 1 && tg[1] == ':')
                            {
                                tg = tg.Substring(2);
                            }
                            while (tg[1] == '/')
                            {
                                tg = tg.Substring(1);
                            }
                            if (tg == "")
                            {
                                throw new Exception("TEXT no target");
                            }
                            var e   = new TJCREntry();
                            var buf = new byte[5];
                            e.Entry    = tg;
                            e.MainFile = file;
                            e.Storage  = "Store";
                            e.Offset   = (int)BT.Position;
                            e.Notes    = notes;
                            e.Author   = author;
                            do
                            {
                                if (BT.EOF)
                                {
                                    throw new Exception("Unexpected end of file (TXT Block not ended)");
                                }
                                for (int i = 0; i < 4; i++)
                                {
                                    buf[i] = buf[i + 1];
                                }
                                buf[4] = BT.ReadByte();
                                //Console.WriteLine(Encoding.UTF8.GetString(buf, 0, buf.Length));
                            } while (Encoding.UTF8.GetString(buf, 0, buf.Length) != "@END@");
                            RL(BT);
                            e.Size                    = (int)(BT.Position - 7) - e.Offset;
                            e.CompressedSize          = e.Size;
                            ret.Entries[tg.ToUpper()] = e;
                            break;
                        }

                        case "COMMENT":
                        case "CMT": {
                            if (c.parameter == "")
                            {
                                throw new Exception("Comment without a name");
                            }
                            var cmt = new StringBuilder("");
                            var l   = "";
                            do
                            {
                                if (BT.EOF)
                                {
                                    throw new Exception("Unexpected end of file (COMMENT block not ended)");
                                }
                                l = RL(BT, false);
                                if (l.Trim() != "@END@")
                                {
                                    cmt.Append($"{l}\n");
                                }
                            } while (l.Trim() != "@END@");
                            ret.Comments[c.parameter] = cmt.ToString();
                            break;
                        }

                        case "IMPORT":
                            ret.PatchFile(c.parameter);
                            break;

                        case "END":
                            return(ret);

                        default: throw new Exception($"Unknown instruction! {c.commando}");
                        }
                    }
                }
                return(ret);
            } catch (Exception e) {
                JCR6.JERROR = $"JQL error: {e.Message}";
#if DEBUG
                Console.WriteLine(e.StackTrace);
#endif
                return(null);
            } finally {
                if (BT != null)
                {
                    BT.Close();
                }
            }
        }