[1316] | 1 | // |
---|
| 2 | // ******************************************************************** |
---|
| 3 | // * License and Disclaimer * |
---|
| 4 | // * * |
---|
| 5 | // * The Geant4 software is copyright of the Copyright Holders of * |
---|
| 6 | // * the Geant4 Collaboration. It is provided under the terms and * |
---|
| 7 | // * conditions of the Geant4 Software License, included in the file * |
---|
| 8 | // * LICENSE and available at http://cern.ch/geant4/license . These * |
---|
| 9 | // * include a list of copyright holders. * |
---|
| 10 | // * * |
---|
| 11 | // * Neither the authors of this software system, nor their employing * |
---|
| 12 | // * institutes,nor the agencies providing financial support for this * |
---|
| 13 | // * work make any representation or warranty, express or implied, * |
---|
| 14 | // * regarding this software system or assume any liability for its * |
---|
| 15 | // * use. Please see the license in the file LICENSE and URL above * |
---|
| 16 | // * for the full disclaimer and the limitation of liability. * |
---|
| 17 | // * * |
---|
| 18 | // * This code implementation is the result of the scientific and * |
---|
| 19 | // * technical work of the GEANT4 collaboration. * |
---|
| 20 | // * By using, copying, modifying or distributing the software (or * |
---|
| 21 | // * any work based on the software) you agree to acknowledge its * |
---|
| 22 | // * use in resulting scientific publications, and indicate your * |
---|
| 23 | // * acceptance of all terms of the Geant4 Software license. * |
---|
| 24 | // ******************************************************************** |
---|
| 25 | // |
---|
| 26 | // |
---|
| 27 | // G4UIcmdPargList.cc |
---|
| 28 | // |
---|
| 29 | // Implementation of a (generic) list argument for G4UIcmdWithPargs |
---|
| 30 | // |
---|
| 31 | |
---|
| 32 | #include "G4UIcmdPargList.hh" |
---|
| 33 | |
---|
| 34 | #include <sstream> |
---|
| 35 | #include <iomanip> |
---|
| 36 | |
---|
| 37 | // |
---|
| 38 | // Constructor |
---|
| 39 | // |
---|
| 40 | G4UIcmdPargList::G4UIcmdPargList( const G4String &theName, G4int theMaxItem ) |
---|
| 41 | : G4UIcmdParg( theName ) |
---|
| 42 | { |
---|
| 43 | nItem = 0; |
---|
| 44 | maxItem = theMaxItem; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | // |
---|
| 49 | // Fetch argument value from input stream |
---|
| 50 | // |
---|
| 51 | std::istream &G4UIcmdPargList::FetchValue( std::istream &istr ) |
---|
| 52 | { |
---|
| 53 | // |
---|
| 54 | // Because of limitations of G4UIcommand, we must avoid |
---|
| 55 | // requiring any spaces. |
---|
| 56 | // |
---|
| 57 | // Require '(' to be the leading character, and read in |
---|
| 58 | // character strings until ',' or ')' are found. If the |
---|
| 59 | // latter is found, input is terminated. |
---|
| 60 | // |
---|
| 61 | // In actual usage, we expect leading blanks to be removed |
---|
| 62 | // before this method is invoked, |
---|
| 63 | // but there is no need to assume that. |
---|
| 64 | // |
---|
| 65 | // Notice that blank items are allowed. It is up to decendent |
---|
| 66 | // classes to decide what to do in this case. To allow an |
---|
| 67 | // empty list (i.e. no items, nItem==0), the special symbol |
---|
| 68 | // "-" is reserved. |
---|
| 69 | // |
---|
| 70 | char buffer[256]; // Buffer for each item |
---|
| 71 | char a; // Single character input |
---|
| 72 | |
---|
| 73 | // |
---|
| 74 | // Remove leading blanks |
---|
| 75 | // |
---|
| 76 | do { |
---|
| 77 | istr.get(a); |
---|
| 78 | if (istr.fail()) return istr; |
---|
| 79 | } while( a == ' ' ); |
---|
| 80 | |
---|
| 81 | // |
---|
| 82 | // Check for empty list ( '-', followed by EOF or blank) |
---|
| 83 | // |
---|
| 84 | if (a == '-') { |
---|
| 85 | istr.get(a); |
---|
| 86 | if (istr.fail() || a == ' ') { |
---|
| 87 | nItem = 0; |
---|
| 88 | return istr; |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | // |
---|
| 94 | // Otherwise: Insist argument begin with a '(' |
---|
| 95 | // |
---|
| 96 | if (a != '(') { |
---|
| 97 | G4cerr << "Listed argument must begin with '(' or consist of only '-'" << G4endl; |
---|
| 98 | istr.clear(std::ios::failbit|istr.rdstate()); // Is there a better way to do this??? |
---|
| 99 | return istr; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | // |
---|
| 103 | // Loop over items in argument |
---|
| 104 | // |
---|
| 105 | G4int nItemFetched = 0; |
---|
| 106 | do { |
---|
| 107 | // |
---|
| 108 | // Next item |
---|
| 109 | // |
---|
| 110 | char *b = buffer; |
---|
| 111 | for(;;) { |
---|
| 112 | // |
---|
| 113 | // Next character |
---|
| 114 | // |
---|
| 115 | istr.get(*b); |
---|
| 116 | if (istr.fail()) return istr; |
---|
| 117 | |
---|
| 118 | // |
---|
| 119 | // Check for delimiter |
---|
| 120 | // |
---|
| 121 | if (*b == ',' || *b == ')') { |
---|
| 122 | a = *b; // Remember |
---|
| 123 | *b = '\0'; |
---|
| 124 | break; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | // |
---|
| 128 | // Go to next character |
---|
| 129 | // |
---|
| 130 | if (++b > buffer+255) { |
---|
| 131 | G4cerr << "Listed argument item must be less than 255 characters" << G4endl; |
---|
| 132 | istr.clear(std::ios::failbit|istr.rdstate()); |
---|
| 133 | return istr; |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | // |
---|
| 138 | // Interpret |
---|
| 139 | // |
---|
| 140 | if (nItemFetched >= maxItem) { |
---|
| 141 | G4cerr << "Maximum of " << maxItem << " items exceeded in listed argument" << G4endl; |
---|
| 142 | istr.clear(std::ios::failbit|istr.rdstate()); |
---|
| 143 | return istr; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | if (!FetchItem( buffer, nItemFetched++ )) { |
---|
| 147 | istr.clear(std::ios::failbit|istr.rdstate()); |
---|
| 148 | return istr; |
---|
| 149 | } |
---|
| 150 | } while( a == ',' ); |
---|
| 151 | |
---|
| 152 | // |
---|
| 153 | // Set nItem only on success |
---|
| 154 | // |
---|
| 155 | nItem = nItemFetched; |
---|
| 156 | |
---|
| 157 | return istr; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | // |
---|
| 162 | // ConvertToString |
---|
| 163 | // |
---|
| 164 | G4String G4UIcmdPargList::ConvertToString() |
---|
| 165 | { |
---|
| 166 | // |
---|
| 167 | // Empty case |
---|
| 168 | // |
---|
| 169 | if (nItem == 0) return "-"; |
---|
| 170 | |
---|
| 171 | // |
---|
| 172 | // Allocate a buffer, big enough to hold all items plus |
---|
| 173 | // a command and '()'. Each item is allowed to take 255 |
---|
| 174 | // characters. |
---|
| 175 | // |
---|
| 176 | // buffSize = 255*nItem+4; |
---|
| 177 | |
---|
| 178 | std::ostringstream os; |
---|
| 179 | |
---|
| 180 | // |
---|
| 181 | // Write out everything in turn |
---|
| 182 | // |
---|
| 183 | os << '('; |
---|
| 184 | |
---|
| 185 | G4int item; |
---|
| 186 | for( item=0; item<nItem; item++) { |
---|
| 187 | if (item) os << ','; |
---|
| 188 | if (!WriteItem(os,item)) return "(error)"; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | os << ')'; |
---|
| 192 | |
---|
| 193 | // |
---|
| 194 | // Convert to G4String |
---|
| 195 | // |
---|
| 196 | G4String answer = os.str(); |
---|
| 197 | |
---|
| 198 | // |
---|
| 199 | // Return G4String |
---|
| 200 | // |
---|
| 201 | return answer; |
---|
| 202 | } |
---|