| 1 | /** @file\r |
| 2 | File is TargetFile class which is used to generate the new target.txt. \r |
| 3 | \r |
| 4 | Copyright (c) 2006, Intel Corporation\r |
| 5 | All rights reserved. This program and the accompanying materials\r |
| 6 | are licensed and made available under the terms and conditions of the BSD License\r |
| 7 | which accompanies this distribution. The full text of the license may be found at\r |
| 8 | http://opensource.org/licenses/bsd-license.php\r |
| 9 | \r |
| 10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
| 11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r |
| 12 | **/\r |
| 13 | package org.tianocore.context;\r |
| 14 | \r |
| 15 | import java.io.BufferedReader;\r |
| 16 | import java.io.BufferedWriter;\r |
| 17 | import java.io.File;\r |
| 18 | import java.io.FileNotFoundException;\r |
| 19 | import java.io.FileReader;\r |
| 20 | import java.io.FileWriter;\r |
| 21 | import java.io.IOException;\r |
| 22 | \r |
| 23 | public class TargetFile {\r |
| 24 | \r |
| 25 | \r |
| 26 | \r |
| 27 | /** \r |
| 28 | * validate the filename\r |
| 29 | * @param String filename : the name of target file\r |
| 30 | * \r |
| 31 | * @return true or false\r |
| 32 | **/\r |
| 33 | public static boolean validateFilename(String filename) {\r |
| 34 | \r |
| 35 | String workspacePath = System.getenv("WORKSPACE");\r |
| 36 | \r |
| 37 | Fd = new File(workspacePath + File.separator + "Tools" + File.separator + "Conf" + File.separator + filename);\r |
| 38 | \r |
| 39 | if (Fd.exists() == true && Fd.canRead() == true)\r |
| 40 | return true;\r |
| 41 | else\r |
| 42 | return false;\r |
| 43 | }\r |
| 44 | \r |
| 45 | \r |
| 46 | /**\r |
| 47 | * create a empty temp file, which is located at the same directory with target file\r |
| 48 | * @param String filename : the name of target temp file\r |
| 49 | * @return true or false\r |
| 50 | **/\r |
| 51 | public static boolean createTempFile(String filename) {\r |
| 52 | \r |
| 53 | String workspacePath = System.getenv("WORKSPACE");\r |
| 54 | \r |
| 55 | TempFd = new File(workspacePath + File.separator + "Tools" + File.separator + "Conf" + File.separator + filename + "tmp");\r |
| 56 | \r |
| 57 | if (TempFd.exists() == true) {\r |
| 58 | if (TempFd.delete() == false) {\r |
| 59 | System.out.printf("%n%s%n", "target.txttmp has been existed, and failed in deletion!");\r |
| 60 | return false;\r |
| 61 | }\r |
| 62 | }\r |
| 63 | try {\r |
| 64 | TempFd.createNewFile();\r |
| 65 | } catch (IOException e) {\r |
| 66 | System.out.printf("%n%s%n", "Failed in creation of the temp file:target.txttmp!");\r |
| 67 | return false;\r |
| 68 | }\r |
| 69 | \r |
| 70 | return true;\r |
| 71 | }\r |
| 72 | \r |
| 73 | /**\r |
| 74 | * read from target.txt and write to target.txttmp, del target.txt, rename\r |
| 75 | * @param no paremeter\r |
| 76 | * @return true or false\r |
| 77 | **/\r |
| 78 | public static boolean readwriteFile() {\r |
| 79 | \r |
| 80 | if (Fd.canRead() != true)\r |
| 81 | return false;\r |
| 82 | \r |
| 83 | BufferedReader br = null;\r |
| 84 | BufferedWriter bw = null;\r |
| 85 | String textLine = null;\r |
| 86 | \r |
| 87 | try {\r |
| 88 | br = new BufferedReader(new FileReader(Fd));\r |
| 89 | } catch (FileNotFoundException e) {\r |
| 90 | System.out\r |
| 91 | .println("\n# Creating BufferedReader fail!");\r |
| 92 | return false;\r |
| 93 | }\r |
| 94 | try {\r |
| 95 | bw = new BufferedWriter(new FileWriter(TempFd));\r |
| 96 | } catch (IOException e) {\r |
| 97 | System.out.println("\n# Creating the BufferedWriter fail!");\r |
| 98 | return false;\r |
| 99 | }\r |
| 100 | \r |
| 101 | //\r |
| 102 | //TARGET_ARCH must be in front of TARGET!!! according to the target.txt\r |
| 103 | //\r |
| 104 | try {\r |
| 105 | while ((textLine = br.readLine()) != null) {\r |
| 106 | //\r |
| 107 | // the line is composed of Space\r |
| 108 | //\r |
| 109 | if (textLine.trim().compareToIgnoreCase("") == 0) {\r |
| 110 | bw.write(textLine);\r |
| 111 | bw.newLine();\r |
| 112 | } \r |
| 113 | //\r |
| 114 | // the line starts with "#", and no "="\r |
| 115 | //\r |
| 116 | else if ((textLine.trim().charAt(0) == '#') && (textLine.indexOf("=") == -1)){\r |
| 117 | bw.write(textLine);\r |
| 118 | bw.newLine();\r |
| 119 | } else {\r |
| 120 | //\r |
| 121 | //modify at the first time, and there should be "*ACTIVE_PLATFORM*=*" in the line\r |
| 122 | //\r |
| 123 | if (textLine.indexOf("ACTIVE_PLATFORM") != -1) {\r |
| 124 | if(pflag == true){\r |
| 125 | if(textLine.trim().charAt(0) == '#'){\r |
| 126 | if(ParseParameter.npflag == true) {\r |
| 127 | bw.write(ParseParameter.curpstr);\r |
| 128 | }else{\r |
| 129 | bw.write(textLine);\r |
| 130 | }\r |
| 131 | bw.newLine();\r |
| 132 | pflag = false;\r |
| 133 | continue;\r |
| 134 | }\r |
| 135 | if(ParseParameter.npflag == true) {\r |
| 136 | bw.write(ParseParameter.curpstr);\r |
| 137 | } else {\r |
| 138 | bw.write(textLine);\r |
| 139 | }\r |
| 140 | bw.newLine();\r |
| 141 | pflag = false;\r |
| 142 | }\r |
| 143 | } else if (textLine.indexOf("TARGET_ARCH") != -1) {\r |
| 144 | if(aflag == true){\r |
| 145 | if(textLine.trim().charAt(0) == '#'){\r |
| 146 | if(ParseParameter.naflag == true) {\r |
| 147 | bw.write(ParseParameter.curastr);\r |
| 148 | }else{\r |
| 149 | bw.write(textLine);\r |
| 150 | }\r |
| 151 | bw.newLine();\r |
| 152 | aflag = false;\r |
| 153 | continue;\r |
| 154 | }\r |
| 155 | if(ParseParameter.naflag == true) {\r |
| 156 | bw.write(ParseParameter.curastr);\r |
| 157 | } else {\r |
| 158 | bw.write(textLine);\r |
| 159 | }\r |
| 160 | bw.newLine();\r |
| 161 | aflag = false;\r |
| 162 | }\r |
| 163 | } else if (textLine.indexOf("TARGET") != -1) {\r |
| 164 | if(tflag == true){\r |
| 165 | if(textLine.trim().charAt(0) == '#'){\r |
| 166 | if(ParseParameter.ntflag == true) {\r |
| 167 | bw.write(ParseParameter.curtstr);\r |
| 168 | }else{\r |
| 169 | bw.write(textLine);\r |
| 170 | }\r |
| 171 | bw.newLine();\r |
| 172 | tflag = false;\r |
| 173 | continue;\r |
| 174 | }\r |
| 175 | if(ParseParameter.ntflag == true) {\r |
| 176 | bw.write(ParseParameter.curtstr);\r |
| 177 | } else {\r |
| 178 | bw.write(textLine);\r |
| 179 | }\r |
| 180 | bw.newLine();\r |
| 181 | tflag = false;\r |
| 182 | }\r |
| 183 | } else if (textLine.indexOf("TOOL_CHAIN_CONF") != -1) {\r |
| 184 | if(cflag == true){\r |
| 185 | if(textLine.trim().charAt(0) == '#'){\r |
| 186 | if(ParseParameter.ncflag == true) {\r |
| 187 | bw.write(ParseParameter.curcstr);\r |
| 188 | }else{\r |
| 189 | bw.write(textLine);\r |
| 190 | }\r |
| 191 | bw.newLine();\r |
| 192 | cflag = false;\r |
| 193 | continue;\r |
| 194 | }\r |
| 195 | if(ParseParameter.ncflag == true) {\r |
| 196 | bw.write(ParseParameter.curcstr);\r |
| 197 | } else {\r |
| 198 | bw.write(textLine);\r |
| 199 | }\r |
| 200 | bw.newLine();\r |
| 201 | cflag = false;\r |
| 202 | }\r |
| 203 | } else if (textLine.indexOf("TOOL_CHAIN_TAG") != -1) {\r |
| 204 | if(nflag == true){\r |
| 205 | if(textLine.trim().charAt(0) == '#'){\r |
| 206 | if(ParseParameter.nnflag == true) {\r |
| 207 | bw.write(ParseParameter.curnstr);\r |
| 208 | }else{\r |
| 209 | bw.write(textLine);\r |
| 210 | }\r |
| 211 | bw.newLine();\r |
| 212 | nflag = false;\r |
| 213 | continue;\r |
| 214 | }\r |
| 215 | if(ParseParameter.nnflag == true) {\r |
| 216 | bw.write(ParseParameter.curnstr);\r |
| 217 | } else {\r |
| 218 | bw.write(textLine);\r |
| 219 | }\r |
| 220 | bw.newLine();\r |
| 221 | nflag = false;\r |
| 222 | }\r |
| 223 | } else if (textLine.indexOf("MAX_CONCURRENT_THREAD_NUMBER") != -1) {\r |
| 224 | if(mflag == true){\r |
| 225 | if(textLine.trim().charAt(0) == '#'){\r |
| 226 | if(ParseParameter.nmflag == true) {\r |
| 227 | bw.write(ParseParameter.curmstr);\r |
| 228 | }else{\r |
| 229 | bw.write(textLine);\r |
| 230 | }\r |
| 231 | bw.newLine();\r |
| 232 | mflag = false;\r |
| 233 | continue;\r |
| 234 | }\r |
| 235 | if(ParseParameter.nmflag == true) {\r |
| 236 | bw.write(ParseParameter.curmstr);\r |
| 237 | } else {\r |
| 238 | bw.write(textLine);\r |
| 239 | }\r |
| 240 | bw.newLine();\r |
| 241 | mflag = false;\r |
| 242 | }\r |
| 243 | }else if (textLine.indexOf("MULTIPLE_THREAD") != -1) {\r |
| 244 | if(meflag == true){\r |
| 245 | if(textLine.trim().charAt(0) == '#'){\r |
| 246 | if(ParseParameter.nmeflag == true) {\r |
| 247 | bw.write(ParseParameter.curmestr);\r |
| 248 | }else{\r |
| 249 | bw.write(textLine);\r |
| 250 | }\r |
| 251 | bw.newLine();\r |
| 252 | meflag = false;\r |
| 253 | continue;\r |
| 254 | }\r |
| 255 | if(ParseParameter.nmeflag == true) {\r |
| 256 | bw.write(ParseParameter.curmestr);\r |
| 257 | } else {\r |
| 258 | bw.write(textLine);\r |
| 259 | }\r |
| 260 | bw.newLine();\r |
| 261 | meflag = false;\r |
| 262 | }\r |
| 263 | }\r |
| 264 | }\r |
| 265 | }\r |
| 266 | //\r |
| 267 | //user maybe delete the line *ACTIVE_PLATFORM*=*\r |
| 268 | //\r |
| 269 | if( (pflag == true) && (ParseParameter.npflag == true) ){\r |
| 270 | bw.write(ParseParameter.curpstr);\r |
| 271 | bw.newLine();\r |
| 272 | } else if ( (tflag == true) && (ParseParameter.ntflag == true) ){\r |
| 273 | bw.write(ParseParameter.curtstr);\r |
| 274 | bw.newLine();\r |
| 275 | } else if ( (aflag == true) && (ParseParameter.naflag == true) ){\r |
| 276 | bw.write(ParseParameter.curastr);\r |
| 277 | bw.newLine();\r |
| 278 | } else if ( (cflag == true) && (ParseParameter.ncflag == true) ){\r |
| 279 | bw.write(ParseParameter.curcstr);\r |
| 280 | bw.newLine();\r |
| 281 | } else if ( (nflag == true) && (ParseParameter.nnflag == true) ){\r |
| 282 | bw.write(ParseParameter.curnstr);\r |
| 283 | bw.newLine();\r |
| 284 | } else if ( (meflag == true) && (ParseParameter.nmeflag == true) ){\r |
| 285 | bw.write(ParseParameter.curmestr);\r |
| 286 | bw.newLine();\r |
| 287 | } else if ( (mflag == true) && (ParseParameter.nmflag == true) ){\r |
| 288 | bw.write(ParseParameter.curmstr);\r |
| 289 | bw.newLine();\r |
| 290 | }\r |
| 291 | } catch (IOException e) {\r |
| 292 | System.out.println("\n# Reading or Writing file fail!");\r |
| 293 | return false;\r |
| 294 | }\r |
| 295 | \r |
| 296 | try {\r |
| 297 | br.close();\r |
| 298 | bw.close();\r |
| 299 | } catch (IOException e) {\r |
| 300 | System.out\r |
| 301 | .println("\n# Closing BufferedReader&BufferedWriter fail!");\r |
| 302 | return false;\r |
| 303 | }\r |
| 304 | \r |
| 305 | if (Fd.delete() == false) {\r |
| 306 | System.out.println("\n# Deleting file fail!");\r |
| 307 | return false;\r |
| 308 | }\r |
| 309 | if (TempFd.renameTo(Fd) == false) {\r |
| 310 | System.out.println("\n# Renaming file failed!");\r |
| 311 | return false;\r |
| 312 | }\r |
| 313 | \r |
| 314 | return true;\r |
| 315 | }\r |
| 316 | \r |
| 317 | /**\r |
| 318 | * read the file and output the lines which include setting\r |
| 319 | * @param File fd : the File of the target file\r |
| 320 | * @return String: the current setting\r |
| 321 | **/\r |
| 322 | public static boolean readFile() {\r |
| 323 | \r |
| 324 | BufferedReader br = null;\r |
| 325 | String textLine = null;\r |
| 326 | \r |
| 327 | try {\r |
| 328 | br = new BufferedReader(new FileReader(Fd));\r |
| 329 | } catch (FileNotFoundException e) {\r |
| 330 | System.out\r |
| 331 | .println("\n# Creating BufferedReader fail!");\r |
| 332 | return false;\r |
| 333 | }\r |
| 334 | try {\r |
| 335 | while ((textLine = br.readLine()) != null) {\r |
| 336 | //\r |
| 337 | // the line is composed of Space\r |
| 338 | //\r |
| 339 | if (textLine.trim().compareToIgnoreCase("") == 0) {\r |
| 340 | continue;\r |
| 341 | } \r |
| 342 | //\r |
| 343 | // the line starts with "#"\r |
| 344 | //\r |
| 345 | else if ((textLine.trim().charAt(0) == '#')){\r |
| 346 | continue;\r |
| 347 | } else {\r |
| 348 | if (textLine.indexOf("ACTIVE_PLATFORM") != -1) {\r |
| 349 | ParseParameter.curpstr = textLine;\r |
| 350 | } else if (textLine.indexOf("TARGET_ARCH") != -1) {\r |
| 351 | ParseParameter.curastr = textLine;\r |
| 352 | } else if (textLine.indexOf("TARGET") != -1) {\r |
| 353 | ParseParameter.curtstr = textLine;\r |
| 354 | } else if (textLine.indexOf("TOOL_CHAIN_CONF") != -1) {\r |
| 355 | ParseParameter.curcstr = textLine;\r |
| 356 | } else if (textLine.indexOf("TOOL_CHAIN_TAG") != -1) {\r |
| 357 | ParseParameter.curnstr = textLine;\r |
| 358 | } else if (textLine.indexOf("MAX_CONCURRENT_THREAD_NUMBER") != -1) {\r |
| 359 | ParseParameter.curmstr = textLine;\r |
| 360 | } else if (textLine.indexOf("MULTIPLE_THREAD") != -1) {\r |
| 361 | ParseParameter.curmestr = textLine;\r |
| 362 | }\r |
| 363 | }\r |
| 364 | }\r |
| 365 | } catch (IOException e) {\r |
| 366 | System.out.println("\n# Reading file fail!");\r |
| 367 | return false;\r |
| 368 | }\r |
| 369 | \r |
| 370 | try {\r |
| 371 | br.close();\r |
| 372 | } catch (IOException e) {\r |
| 373 | System.out\r |
| 374 | .println("\n# Closing BufferedReader fail!");\r |
| 375 | return false;\r |
| 376 | }\r |
| 377 | return true;\r |
| 378 | }\r |
| 379 | \r |
| 380 | \r |
| 381 | ///\r |
| 382 | /// point to target.txttmp, a temp file, which is created and deleted during the tool's runtime.\r |
| 383 | ///\r |
| 384 | private static File TempFd;\r |
| 385 | \r |
| 386 | ///\r |
| 387 | /// point to target.txt.\r |
| 388 | ///\r |
| 389 | private static File Fd;\r |
| 390 | \r |
| 391 | ///\r |
| 392 | /// when the flag is true, the corresponding str should be add at the end of file.\r |
| 393 | ///\r |
| 394 | private static boolean pflag = true;\r |
| 395 | private static boolean tflag = true;\r |
| 396 | private static boolean aflag = true;\r |
| 397 | private static boolean cflag = true;\r |
| 398 | private static boolean nflag = true;\r |
| 399 | private static boolean mflag = true;\r |
| 400 | private static boolean meflag = true;\r |
| 401 | \r |
| 402 | \r |
| 403 | }\r |