Ejemplo n.º 1
0
 //*********************************************************     
 //
 /// <summary>
 /// Create a book from files selected by the user
 /// </summary>
 //  
 //*********************************************************
  public void CreateBookFromFiles() {
     OpenFileDialog          openDlg;
     SaveFileDialog          saveDlg;
     PgnParser               parser;
     Stream                  stream;
     TextReader              reader;
     Book                    book;
     string                  strText;
     List<int[]>             arrMoveList;
     int                     iSkip;
     int                     iTruncated;
     int                     iTotalSkip = 0;
     int                     iTotalTruncated = 0;
     int                     iTotalFiles = 0;
     int                     iBookEntries;
     bool                    bAbort = false;
     
     arrMoveList = new List<int[]>(8192);
     book        = new Book();
     using(openDlg = new OpenFileDialog()) {
         openDlg.AddExtension        = true;
         openDlg.CheckFileExists     = true;
         openDlg.CheckPathExists     = true;
         openDlg.DefaultExt          = "pgn";
         openDlg.Filter              = "Chess PGN Files (*.pgn)|*.pgn";
         openDlg.Multiselect         = true;
         if (openDlg.ShowDialog() == DialogResult.OK) {
             foreach (string strFileName in openDlg.FileNames) {
                 try {
                     stream = File.OpenRead(strFileName);
                 } catch(System.Exception) {
                     MessageBox.Show("Unable to open the file - " + strFileName);
                     stream = null;
                 }
                 if (stream != null) {
                     reader  = new StreamReader(stream);
                     strText = reader.ReadToEnd();
                     parser  = new PgnParser(false);
                     try {
                         parser.Parse(strText, arrMoveList, out iSkip, out iTruncated);
                         iTotalSkip      += iSkip;
                         iTotalTruncated += iTruncated;
                         iTotalFiles++;
                     } catch(PgnParserException exc) {
                         MessageBox.Show("Error processing file '" + strFileName + "'\r\n" + exc.Message + "\r\n" + exc.CodeInError);
                         bAbort =  true;
                     }
                     stream.Close();
                 }
                 if (bAbort) {
                     break;
                 }
             }
             if (!bAbort) {
                 iBookEntries = book.CreateBookList(arrMoveList, 30, 10);
                 MessageBox.Show(iTotalFiles.ToString() + " PNG file(s) read. " + arrMoveList.Count.ToString() + " games processed. " + iTotalTruncated.ToString() + " truncated. " + iTotalSkip.ToString() + " skipped. " + iBookEntries.ToString() + " book entries defined.");
                 using(saveDlg = new SaveFileDialog()) {
                     saveDlg.AddExtension        = true;
                     saveDlg.CheckPathExists     = true;
                     saveDlg.DefaultExt          = "bin";
                     saveDlg.Filter              = "Chess Opening Book (*.bin)|*.bin";
                     saveDlg.OverwritePrompt     = true;
                     if (saveDlg.ShowDialog() == DialogResult.OK) {
                         try {
                             book.SaveBookToFile(saveDlg.FileName);
                         } catch (System.Exception ex) {
                             MessageBox.Show(ex.Message);
                         }
                     }
                 }
             }
         }
     }
 }