private static int SaveFestivalBook(string folderName, GCFestivalBook book)
        {
            int nRet = 0;

            using (StreamWriter sw = new StreamWriter(Path.Combine(folderName, book.FileName)))
            {
                sw.WriteLine("{0}:{1}|{2}|{3}", book.GetType().Name, GCFestivalBase.StringToSafe(book.CollectionName), book.CollectionId,
                             (book.Visible ? 1 : 0));
                foreach (GCFestivalBase fb in book.Festivals)
                {
                    if (fb.nDeleted == 0)
                    {
                        nRet++;
                        sw.WriteLine("{0}:{1}", fb.getToken(), fb.EncodedString);
                        if (fb.EventsCount > 0)
                        {
                            foreach (GCFestivalBase fb2 in fb.Events)
                            {
                                sw.WriteLine("{0}:{1}", fb2.getToken(), fb2.EncodedString);
                            }
                        }
                        if (fb is GCFestivalSpecial)
                        {
                            GCFestivalSpecial se = fb as GCFestivalSpecial;
                            sw.WriteLine(GCFestivalSpecial.InstructionTag);
                            sw.WriteLine(se.Script);
                            sw.WriteLine(GCFestivalSpecial.InstructionEndTag);
                        }
                    }
                }
            }

            return(nRet);
        }
        public static void Export(string pszFile, int format)
        {
            String strc;

            using (StreamWriter f = new StreamWriter(pszFile))
            {
                foreach (GCFestivalBook book in GCFestivalBookCollection.Books)
                {
                    switch (format)
                    {
                    case 1:
                        foreach (GCFestivalBase fb in book.Festivals)
                        {
                            if (fb is GCFestivalTithiMasa)
                            {
                                GCFestivalTithiMasa pce = fb as GCFestivalTithiMasa;
                                strc = string.Format("{0}\n\t{1} Tithi,{2} Paksa,{3} Masa\n", pce.Text,
                                                     GCTithi.GetName(pce.nTithi), GCPaksa.GetName(pce.nTithi / 15), GCMasa.GetName(pce.nMasa));
                                f.Write(strc);
                                if (pce.FastID != 0)
                                {
                                    strc = string.Format("\t{0}\n", GCStrings.GetFastingName(pce.FastID));
                                    f.Write(strc);
                                }
                            }
                            else if (fb is GCFestivalSankranti)
                            {
                                GCFestivalSankranti se = fb as GCFestivalSankranti;
                                f.Write(string.Format("{0}\n\t\t{1} Sankranti, {2}\n", se.Text, GCRasi.GetName(se.RasiOfSun), se.DayOffset));
                            }
                        }

                        break;

                    case 2:
                    {
                        f.Write("<xml>\n");
                        foreach (GCFestivalBase fb in book.Festivals)
                        {
                            f.Write("\t<event>\n");
                            if (fb is GCFestivalTithiMasa)
                            {
                                GCFestivalTithiMasa pce = fb as GCFestivalTithiMasa;
                                strc = string.Format("\t\t<name>{0}</name>\n", pce.Text);
                                f.Write(strc);
                                strc = string.Format("\t\t<tithi>{0}</tithi>\n", GCTithi.GetName(pce.nTithi));
                                f.Write(strc);
                                strc = string.Format("\t\t<paksa>{0}</paksa>\n", GCPaksa.GetName(pce.nTithi / 15));
                                f.Write(strc);
                                strc = string.Format("\t\t<masa>{0}</masa>\n", GCMasa.GetName(pce.nMasa));
                                f.Write(strc);
                            }
                            else if (fb is GCFestivalSankranti)
                            {
                                GCFestivalSankranti se = fb as GCFestivalSankranti;
                                f.Write("\t\t<name>" + se.Text + "</name>\n");
                                f.Write("\t\t<sankranti>" + GCRasi.GetName(se.RasiOfSun) + "</sankranti>\n");
                                f.Write("\t\t<rel>" + se.DayOffset + "</rel>\n");
                            }

                            if (fb.EventsCount > 0)
                            {
                                foreach (GCFestivalBase fb2 in fb.Events)
                                {
                                    if (fb2 is GCFestivalRelated)
                                    {
                                        GCFestivalRelated rel = fb2 as GCFestivalRelated;
                                        f.Write("\t\t<relatedEvent>\n");
                                        f.Write("\t\t\t<name>" + rel.Text + "</name>\n");
                                        f.Write("\t\t\t<offset>" + rel.DayOffset + "</offset>\n");
                                        f.Write("\t\t</relatedEvent>\n");
                                    }
                                }
                            }

                            f.Write("\t</event>\n");
                        }

                        f.Write("</xml>\n");
                    }
                    break;

                    case 3:
                        GSScript script = new GSScript();
                        script.Add(new GSToken("book"));
                        script.Add(new GSList(new GSToken("bookId"), new GSNumber(book.CollectionId)));
                        script.Add(new GSList(new GSToken("bookName"), new GSString(book.CollectionName)));
                        foreach (GCFestivalBase fb in book.Festivals)
                        {
                            script.Add(fb.ExecuteMessage("getScript"));
                        }
                        script.writeScript(f);
                        break;

                    case 15:
                    {
                        f.Write("{\n");
                        f.Write("\tname: \"" + book.CollectionName + "\",\n");
                        f.Write("\tbookid: " + book.CollectionId + ",\n");
                        f.Write("\tevents: [\n");
                        foreach (GCFestivalBase fb in book.Festivals)
                        {
                            f.Write("\t\t{\n");
                            if (fb is GCFestivalTithiMasa)
                            {
                                GCFestivalTithiMasa pce = fb as GCFestivalTithiMasa;
                                f.Write("\t\t\ttype: \"T\",\n");
                                f.Write("\t\t\tname: \"{0}\",\n", pce.Text);
                                f.Write("\t\t\ttithi: {0},\n", pce.nTithi);
                                f.Write("\t\t\tmasa: {0}\n", pce.nMasa);
                                f.Write("\t\t\tfastSubj: \"{0}\",\n", pce.FastingSubject);
                                f.Write("\t\t\tfast: {0},\n", pce.FastID);
                                f.Write("\t\t\tstart: {0},\n", pce.StartYear);
                                f.Write("\t\t\tdayoff: {0},\n", pce.DayOffset);
                            }
                            else if (fb is GCFestivalSankranti)
                            {
                                GCFestivalSankranti se = fb as GCFestivalSankranti;
                                f.Write("\t\t\ttype:\"S\",\n");
                                f.Write("\t\t\tname: \"" + se.Text + "\",\n");
                                f.Write("\t\t\tsankranti: " + se.RasiOfSun + ",\n");
                                f.Write("\t\t\tfastSubj: \"{0}\",\n", fb.FastingSubject);
                                f.Write("\t\t\tfast: {0},\n", fb.FastID);
                                f.Write("\t\t\tstart: {0},\n", fb.StartYear);
                                f.Write("\t\t\tdayoff: {0},\n", fb.DayOffset);
                            }
                            else if (fb is GCFestivalSpecial)
                            {
                                GCFestivalSpecial spe = fb as GCFestivalSpecial;
                                f.Write("\t\t\ttype: \"X\",\n");
                                f.Write("\t\t\tname: \"" + spe.Text + "\",\n");
                                f.Write("\t\t\tmasa1: " + spe.nMasaMin + ",\n");
                                f.Write("\t\t\tmasa2: " + spe.nMasaMax + ",\n");
                                f.Write("\t\t\tscript: \"" + spe.Script + "\",\n");
                                f.Write("\t\t\tfastSubj: \"{0}\",\n", fb.FastingSubject);
                                f.Write("\t\t\tfast: {0},\n", fb.FastID);
                                f.Write("\t\t\tstart: {0},\n", fb.StartYear);
                                f.Write("\t\t\tdayoff: {0},\n", fb.DayOffset);
                            }
                            else if (fb is GCFestivalEkadasi)
                            {
                                GCFestivalEkadasi se = fb as GCFestivalEkadasi;
                                f.Write("\t\t\ttype: \"E\",\n");
                                f.Write("\t\t\tname: \"" + se.Text + "\",\n");
                                f.Write("\t\t\tmasa: " + se.nMasa + ",\n");
                                f.Write("\t\t\tpaksa: " + se.nPaksa + ",\n");
                                f.Write("\t\t\tfastSubj: \"{0}\",\n", fb.FastingSubject);
                                f.Write("\t\t\tfast: {0},\n", fb.FastID);
                                f.Write("\t\t\tstart: {0},\n", fb.StartYear);
                                f.Write("\t\t\tdayoff: {0},\n", fb.DayOffset);
                            }

                            if (fb.EventsCount > 0)
                            {
                                f.Write("\t\t\tevents: [\n");
                                foreach (GCFestivalBase fb2 in fb.Events)
                                {
                                    if (fb2 is GCFestivalRelated)
                                    {
                                        GCFestivalRelated rel = fb2 as GCFestivalRelated;
                                        f.Write("\t\t\t\t{\n\t\t\t\t\ttype:\"R\",\n");
                                        f.Write("\t\t\t\t\tname: \"" + rel.Text + "\",\n");
                                        f.Write("\t\t\t\t\tfastSubj: \"{0}\",\n", fb2.FastingSubject);
                                        f.Write("\t\t\t\t\tfast: {0},\n", fb2.FastID);
                                        f.Write("\t\t\t\t\tstart: {0},\n", fb2.StartYear);
                                        f.Write("\t\t\t\t\tdayoff: {0},\n", fb2.DayOffset);
                                        f.Write("\t\t\t\t},\n");
                                    }
                                }
                                f.Write("\t\t\t]");
                            }

                            f.Write("\t\t},\n");
                        }

                        f.Write("\t]\n");
                        f.Write("},\n");
                    }
                    break;

                    default:
                        break;
                    }
                }
            }
        }
        public static int OpenEventsFile(string pszFile)
        {
            int            nMode        = 0;
            int            nRet         = -1;
            StringBuilder  sb           = new StringBuilder();
            GCFestivalBase lastFestival = null;

            using (StreamReader sr = new StreamReader(pszFile))
            {
                nRet++;

                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    string tag  = "";
                    string data = "";

                    if (nMode == 0)
                    {
                        // preparation
                        if (line.Equals(GCFestivalSpecial.InstructionTag))
                        {
                            tag = line;
                        }
                        else if (line.IndexOf(':') > 0)
                        {
                            tag  = line.Substring(0, line.IndexOf(':'));
                            data = line.Substring(line.IndexOf(':') + 1);
                        }

                        // processing of tag and data
                        if (tag.Equals(typeof(GCFestivalBook).Name))
                        {
                            string[] p = data.Split('|');
                            if (p.Length > 2)
                            {
                                GCFestivalBook fb = GCFestivalBookCollection.getSafeBook(GCFestivalBase.StringToInt(p[1], 6));
                                fb.CollectionName = GCFestivalBase.SafeToString(p[0]);
                                if (pszFile.EndsWith(".ev.rl"))
                                {
                                    fb.FileName = Path.GetFileName(pszFile);
                                }
                                fb.Visible = p[2].Equals("1");
                                nRet++;
                            }
                        }
                        else if (tag.Equals(typeof(GCFestivalTithiMasa).Name))
                        {
                            nRet++;
                            GCFestivalTithiMasa pce = new GCFestivalTithiMasa();
                            pce.EncodedString = data;
                            GCFestivalBook book = GCFestivalBookCollection.getSafeBook(pce.BookID);
                            book.Add(pce);
                            lastFestival = pce;
                        }
                        else if (tag.Equals(typeof(GCFestivalSankranti).Name))
                        {
                            nRet++;
                            GCFestivalSankranti se = new GCFestivalSankranti();
                            se.EncodedString = data;
                            GCFestivalBook book = GCFestivalBookCollection.getSafeBook(se.BookID);
                            book.Add(se);
                            lastFestival = se;
                        }
                        else if (tag.Equals(typeof(GCFestivalRelated).Name))
                        {
                            nRet++;
                            GCFestivalRelated re = new GCFestivalRelated();
                            re.EncodedString = data;
                            if (lastFestival != null)
                            {
                                lastFestival.AddRelatedFestival(re);
                            }
                        }
                        else if (tag.Equals(typeof(GCFestivalEkadasi).Name))
                        {
                            nRet++;
                            GCFestivalEkadasi fe = new GCFestivalEkadasi();
                            fe.EncodedString = data;
                            GCFestivalBook book = GCFestivalBookCollection.getSafeBook(fe.BookID);
                            book.Add(fe);
                            lastFestival = fe;
                        }
                        else if (tag.Equals(typeof(GCFestivalMasaDay).Name))
                        {
                            nRet++;
                            GCFestivalMasaDay me = new GCFestivalMasaDay();
                            me.EncodedString = data;
                            GCFestivalBook book = GCFestivalBookCollection.getSafeBook(me.BookID);
                            book.Add(me);
                            lastFestival = me;
                        }
                        else if (tag.Equals(typeof(GCFestivalSpecial).Name))
                        {
                            nRet++;
                            GCFestivalSpecial se = new GCFestivalSpecial();
                            se.EncodedString = data;
                            GCFestivalBook book = GCFestivalBookCollection.getSafeBook(se.BookID);
                            book.Add(se);
                            lastFestival = se;
                        }
                        else if (tag.Equals(GCFestivalSpecial.InstructionTag))
                        {
                            nMode = 1;
                            sb.Clear();
                        }
                    }
                    else if (nMode == 1)
                    {
                        if (line.Equals(GCFestivalSpecial.InstructionEndTag))
                        {
                            if (lastFestival is GCFestivalSpecial)
                            {
                                GCFestivalSpecial se = lastFestival as GCFestivalSpecial;
                                se.Script = sb.ToString();
                            }
                            nMode = 0;
                        }
                        else
                        {
                            sb.AppendLine(line);
                        }
                    }
                }
            }

            return(nRet);
        }