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 MoveBook(int oldIndex, int newIndex)
        {
            GCFestivalBook fb = Books[oldIndex];

            Books.RemoveAt(oldIndex);
            Books.Insert(newIndex, fb);
            BooksModified = true;
        }
        public static GCFestivalBook CreateBook(string bookName)
        {
            GCFestivalBook fb = getSafeBook(BookCollectionIdCounter);

            fb.CollectionName = bookName;
            SaveFestivalBook(GCGlobal.ConfigurationFolderPath, fb);
            return(fb);
        }
        public static GCFestivalBook getSafeBook(int i)
        {
            foreach (GCFestivalBook book in Books)
            {
                if (book.CollectionId == i)
                {
                    return(book);
                }
            }

            GCFestivalBook newBook = new GCFestivalBook();

            newBook.CollectionId = i;
            newBook.FileName     = string.Format("events{0:00}.ev.rl", i);
            Books.Add(newBook);

            // check NextCollectionId
            BookCollectionIdCounter = Math.Max(i + 1, BookCollectionIdCounter);

            return(newBook);
        }
        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);
        }