1#!/usr/local/bin/ruby 2 3# quotes.rb 4# 5# Copyright (C) 2001-2011 Free Software Foundation, Inc. 6# 7# Copying and distribution of this file, with or without modification, 8# are permitted in any medium without royalty provided the copyright 9# notice and this notice are preserved. 10# 11# Contact: bug-gnu-chess@gnu.org 12 13# This script replaces inner double quotes in fi by single quotes and writes 14# the result on fo. This is necessary because command 'book add' does not 15# accept more than two matching double quotes in PGN files. 16 17# The program expects exactly two arguments in the command-line. 18# The first argument is the name of the input file, i.e. the file that is 19# going to be processed. 20# The second argument is the name of the output file, i.e. the file that will 21# be written as a result of processing the input file. 22 23# Check the number of command-line arguments: if other than two, display the 24# usage message and exit. 25if ARGV.size != 2 then 26 puts "Usage: ./quotes <pgn_file_input> <pgn_file_output>" 27 exit 28end 29 30# 'fiName' takes the first command-line argument, i.e. the input file name 31fiName = ARGV[0] 32 33# 'foName' takes the second command-line argument, i.e. the output file name 34foName = ARGV[1] 35 36# If the input does not exists, print an error message and exit. 37if not File.exists? fiName then 38 puts "File #{fiName} does not exist." 39 exit 40end 41 42# If the input and output file names are identical, print an error message 43# and exit. 44if foName == fiName then 45 puts "Provide different input and output file names" 46 exit 47end 48 49# If the output file already exists, ask the user if she or he wants to 50# replace it. Repeat the question in an infinite loop until the user enters 51# a valid answer: 52# if the user answers 'y' or 'Y', the program will continue; 53# if the user answers 'n' or 'N', the program will exit. 54if File.exists? foName then 55 answer = "" 56 while answer != "y" and answer != "n" 57 puts "File #{foName} already exists. Replace it (y/n)?" 58 answer = STDIN.gets.chop 59 answer.downcase! 60 end 61 if answer == "n" then 62 exit 63 end 64end 65 66# Open the input file in read-only mode 67fi = File.new( fiName, 'r' ) 68 69# Open the output file in read-write mode 70fo = File.new( foName, 'w' ) 71 72# Loop over each line of the input file 73fi.each {|line| 74 75 nQuotes = line.count( '\"' ) # Get the number of double quotes that are 76 # present in the line. 77 oLine = '' # Initialise the output line 78 oLine << line # By default, the output line is equal to the input line 79 80 # If the number of double quotes in the line is greater than 2, the output 81 # line requires special processing. 82 if nQuotes > 2 then 83 84 # The line will be scanned character by character... 85 86 nq = 0 # Initialize the count of double quotes in the line. 87 oLine = '' # Overrides previous value of the output line 88 89 # Loop over each character of the line 90 line.each_char {|c| 91 # If the character is a double quote, do special processing; 92 # otherwise, append it to the output line. 93 if c == "\"" then 94 # Two special cases are considered (enough to cover book_1.01.pgn): 95 # a) 3 double quotes => replace the 2nd double quote by a single quote 96 # b) 4 double quotes => replace the 2nd and 3rd double quotes by a 97 # single quote 98 # Otherwise, append the character to the output line. 99 if ( nQuotes == 3 and nq == 1 ) or ( nQuotes == 4 and ( nq == 1 or nq == 2 ) ) then 100 oLine << '\'' 101 else 102 oLine << c 103 end 104 nq += 1 105 else 106 oLine << c 107 end 108 } # End of looping over the characters of a line 109 110 end 111 112 fo.puts oLine # Write output line to the output file 113 114} # End of looping over the lines of the input file 115 116# Close the output file 117fo.close 118 119# Close the input file 120fi.close 121