blob: ce1766ca9804c04722f5910dc1857d5b87952efe [file] [log] [blame]
srs56943860cbe2011-09-10 20:29:53 -04001/*
2 Implementation of GPTData class derivative with popt-based command
3 line processing
Roderick W. Smith820d1d02014-02-20 13:25:22 -05004 Copyright (C) 2010-2014 Roderick W. Smith
srs56943860cbe2011-09-10 20:29:53 -04005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19*/
20
21#include <string.h>
22#include <string>
23#include <iostream>
24#include <sstream>
25#include <errno.h>
26#include <popt.h>
27#include "gptcl.h"
28
29GPTDataCL::GPTDataCL(void) {
30 attributeOperation = backupFile = partName = hybrids = newPartInfo = NULL;
31 mbrParts = twoParts = outDevice = typeCode = partGUID = diskGUID = NULL;
32 alignment = DEFAULT_ALIGNMENT;
33 deletePartNum = infoPartNum = largestPartNum = bsdPartNum = 0;
34 tableSize = GPT_SIZE;
35} // GPTDataCL constructor
36
37GPTDataCL::GPTDataCL(string filename) {
38} // GPTDataCL constructor with filename
39
40GPTDataCL::~GPTDataCL(void) {
41} // GPTDataCL destructor
42
43void GPTDataCL::LoadBackupFile(string backupFile, int &saveData, int &neverSaveData) {
44 if (LoadGPTBackup(backupFile) == 1) {
45 JustLooking(0);
46 saveData = 1;
47 } else {
48 saveData = 0;
49 neverSaveData = 1;
50 cerr << "Error loading backup file!\n";
51 } // else
srs56940741fa22013-01-09 12:55:40 -050052} // GPTDataCL::LoadBackupFile()
srs56943860cbe2011-09-10 20:29:53 -040053
srs5694d1b11e82011-09-18 21:12:28 -040054// Perform the actions specified on the command line. This is necessarily one
55// monster of a function!
56// Returns values:
57// 0 = success
58// 1 = too few arguments
59// 2 = error when reading partition table
60// 3 = non-GPT disk and no -g option
61// 4 = unable to save changes
62// 8 = disk replication operation (-R) failed
srs56943860cbe2011-09-10 20:29:53 -040063int GPTDataCL::DoOptions(int argc, char* argv[]) {
64 GPTData secondDevice;
65 int opt, numOptions = 0, saveData = 0, neverSaveData = 0;
Roderick W. Smithd28495e2014-03-02 11:37:20 -050066 int partNum = 0, newPartNum = -1, saveNonGPT = 1, retval = 0, pretend = 0;
srs5694e842bc12012-02-03 11:27:05 -050067 uint64_t low, high, startSector, endSector, sSize;
srs56943860cbe2011-09-10 20:29:53 -040068 uint64_t temp; // temporary variable; free to use in any case
69 char *device;
70 string cmd, typeGUID, name;
71 PartType typeHelper;
72
73 struct poptOption theOptions[] =
74 {
75 {"attributes", 'A', POPT_ARG_STRING, &attributeOperation, 'A', "operate on partition attributes", "list|[partnum:show|or|nand|xor|=|set|clear|toggle|get[:bitnum|hexbitmask]]"},
76 {"set-alignment", 'a', POPT_ARG_INT, &alignment, 'a', "set sector alignment", "value"},
77 {"backup", 'b', POPT_ARG_STRING, &backupFile, 'b', "backup GPT to file", "file"},
78 {"change-name", 'c', POPT_ARG_STRING, &partName, 'c', "change partition's name", "partnum:name"},
79 {"recompute-chs", 'C', POPT_ARG_NONE, NULL, 'C', "recompute CHS values in protective/hybrid MBR", ""},
80 {"delete", 'd', POPT_ARG_INT, &deletePartNum, 'd', "delete a partition", "partnum"},
81 {"display-alignment", 'D', POPT_ARG_NONE, NULL, 'D', "show number of sectors per allocation block", ""},
82 {"move-second-header", 'e', POPT_ARG_NONE, NULL, 'e', "move second header to end of disk", ""},
83 {"end-of-largest", 'E', POPT_ARG_NONE, NULL, 'E', "show end of largest free block", ""},
84 {"first-in-largest", 'f', POPT_ARG_NONE, NULL, 'f', "show start of the largest free block", ""},
85 {"first-aligned-in-largest", 'F', POPT_ARG_NONE, NULL, 'F', "show start of the largest free block, aligned", ""},
86 {"mbrtogpt", 'g', POPT_ARG_NONE, NULL, 'g', "convert MBR to GPT", ""},
87 {"randomize-guids", 'G', POPT_ARG_NONE, NULL, 'G', "randomize disk and partition GUIDs", ""},
88 {"hybrid", 'h', POPT_ARG_STRING, &hybrids, 'h', "create hybrid MBR", "partnum[:partnum...]"},
89 {"info", 'i', POPT_ARG_INT, &infoPartNum, 'i', "show detailed information on partition", "partnum"},
90 {"load-backup", 'l', POPT_ARG_STRING, &backupFile, 'l', "load GPT backup from file", "file"},
91 {"list-types", 'L', POPT_ARG_NONE, NULL, 'L', "list known partition types", ""},
92 {"gpttombr", 'm', POPT_ARG_STRING, &mbrParts, 'm', "convert GPT to MBR", "partnum[:partnum...]"},
93 {"new", 'n', POPT_ARG_STRING, &newPartInfo, 'n', "create new partition", "partnum:start:end"},
94 {"largest-new", 'N', POPT_ARG_INT, &largestPartNum, 'N', "create largest possible new partition", "partnum"},
95 {"clear", 'o', POPT_ARG_NONE, NULL, 'o', "clear partition table", ""},
96 {"print", 'p', POPT_ARG_NONE, NULL, 'p', "print partition table", ""},
97 {"pretend", 'P', POPT_ARG_NONE, NULL, 'P', "make changes in memory, but don't write them", ""},
98 {"transpose", 'r', POPT_ARG_STRING, &twoParts, 'r', "transpose two partitions", "partnum:partnum"},
99 {"replicate", 'R', POPT_ARG_STRING, &outDevice, 'R', "replicate partition table", "device_filename"},
100 {"sort", 's', POPT_ARG_NONE, NULL, 's', "sort partition table entries", ""},
101 {"resize-table", 'S', POPT_ARG_INT, &tableSize, 'S', "resize partition table", "numparts"},
102 {"typecode", 't', POPT_ARG_STRING, &typeCode, 't', "change partition type code", "partnum:{hexcode|GUID}"},
103 {"transform-bsd", 'T', POPT_ARG_INT, &bsdPartNum, 'T', "transform BSD disklabel partition to GPT", "partnum"},
104 {"partition-guid", 'u', POPT_ARG_STRING, &partGUID, 'u', "set partition GUID", "partnum:guid"},
105 {"disk-guid", 'U', POPT_ARG_STRING, &diskGUID, 'U', "set disk GUID", "guid"},
106 {"verify", 'v', POPT_ARG_NONE, NULL, 'v', "check partition table integrity", ""},
107 {"version", 'V', POPT_ARG_NONE, NULL, 'V', "display version information", ""},
108 {"zap", 'z', POPT_ARG_NONE, NULL, 'z', "zap (destroy) GPT (but not MBR) data structures", ""},
109 {"zap-all", 'Z', POPT_ARG_NONE, NULL, 'Z', "zap (destroy) GPT and MBR data structures", ""},
110 POPT_AUTOHELP { NULL, 0, 0, NULL, 0 }
111 };
112
113 // Create popt context...
114 poptCon = poptGetContext(NULL, argc, (const char**) argv, theOptions, 0);
srs56940741fa22013-01-09 12:55:40 -0500115
srs56943860cbe2011-09-10 20:29:53 -0400116 poptSetOtherOptionHelp(poptCon, " [OPTION...] <device>");
srs56940741fa22013-01-09 12:55:40 -0500117
srs56943860cbe2011-09-10 20:29:53 -0400118 if (argc < 2) {
119 poptPrintUsage(poptCon, stderr, 0);
srs5694d1b11e82011-09-18 21:12:28 -0400120 return 1;
srs56943860cbe2011-09-10 20:29:53 -0400121 }
srs56940741fa22013-01-09 12:55:40 -0500122
srs56943860cbe2011-09-10 20:29:53 -0400123 // Do one loop through the options to find the device filename and deal
124 // with options that don't require a device filename, to flag destructive
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500125 // (o, z, or Z) options, and to flag presence of a --pretend/-P option
srs56943860cbe2011-09-10 20:29:53 -0400126 while ((opt = poptGetNextOpt(poptCon)) > 0) {
127 switch (opt) {
128 case 'A':
129 cmd = GetString(attributeOperation, 1);
130 if (cmd == "list")
131 Attributes::ListAttributes();
132 break;
133 case 'L':
Roderick W. Smithe3ee7332013-09-24 12:56:11 -0400134 typeHelper.ShowAllTypes(0);
srs56943860cbe2011-09-10 20:29:53 -0400135 break;
136 case 'P':
137 pretend = 1;
138 break;
139 case 'V':
140 cout << "GPT fdisk (sgdisk) version " << GPTFDISK_VERSION << "\n\n";
141 break;
142 default:
143 break;
144 } // switch
145 numOptions++;
146 } // while
srs56940741fa22013-01-09 12:55:40 -0500147
srs56943860cbe2011-09-10 20:29:53 -0400148 // Assume first non-option argument is the device filename....
149 device = (char*) poptGetArg(poptCon);
150 poptResetContext(poptCon);
srs56940741fa22013-01-09 12:55:40 -0500151
srs56943860cbe2011-09-10 20:29:53 -0400152 if (device != NULL) {
153 JustLooking(); // reset as necessary
154 BeQuiet(); // Tell called functions to be less verbose & interactive
155 if (LoadPartitions((string) device)) {
156 if ((WhichWasUsed() == use_mbr) || (WhichWasUsed() == use_bsd))
157 saveNonGPT = 0; // flag so we don't overwrite unless directed to do so
158 sSize = GetBlockSize();
159 while ((opt = poptGetNextOpt(poptCon)) > 0) {
160 switch (opt) {
161 case 'A': {
162 if (cmd != "list") {
163 partNum = (int) GetInt(attributeOperation, 1) - 1;
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500164 if (partNum < 0)
165 partNum = newPartNum;
srs56943860cbe2011-09-10 20:29:53 -0400166 if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
167 switch (ManageAttributes(partNum, GetString(attributeOperation, 2),
168 GetString(attributeOperation, 3))) {
169 case -1:
170 saveData = 0;
171 neverSaveData = 1;
172 break;
173 case 1:
174 JustLooking(0);
175 saveData = 1;
176 break;
177 default:
178 break;
179 } // switch
180 } else {
181 cerr << "Error: Invalid partition number " << partNum + 1 << "\n";
182 saveData = 0;
183 neverSaveData = 1;
184 } // if/else reasonable partition #
185 } // if (cmd != "list")
186 break;
187 } // case 'A':
188 case 'a':
189 SetAlignment(alignment);
190 break;
191 case 'b':
192 SaveGPTBackup(backupFile);
193 free(backupFile);
194 break;
195 case 'c':
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500196 cout << "Setting name!\n";
srs56943860cbe2011-09-10 20:29:53 -0400197 JustLooking(0);
198 partNum = (int) GetInt(partName, 1) - 1;
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500199 if (partNum < 0)
200 partNum = newPartNum;
201 cout << "partNum is " << partNum << "\n";
srs56940741fa22013-01-09 12:55:40 -0500202 if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500203 cout << "REALLY setting name!\n";
srs56940741fa22013-01-09 12:55:40 -0500204 name = GetString(partName, 2);
205 if (SetName(partNum, (UnicodeString) name.c_str())) {
206 saveData = 1;
207 } else {
208 cerr << "Unable to set partition " << partNum + 1
209 << "'s name to '" << GetString(partName, 2) << "'!\n";
210 neverSaveData = 1;
211 } // if/else
212 free(partName);
213 }
srs56943860cbe2011-09-10 20:29:53 -0400214 break;
215 case 'C':
216 JustLooking(0);
217 RecomputeCHS();
218 saveData = 1;
219 break;
220 case 'd':
221 JustLooking(0);
222 if (DeletePartition(deletePartNum - 1) == 0) {
223 cerr << "Error " << errno << " deleting partition!\n";
224 neverSaveData = 1;
225 } else saveData = 1;
226 break;
227 case 'D':
228 cout << GetAlignment() << "\n";
229 break;
230 case 'e':
231 JustLooking(0);
232 MoveSecondHeaderToEnd();
233 saveData = 1;
234 break;
235 case 'E':
236 cout << FindLastInFree(FindFirstInLargest()) << "\n";
237 break;
238 case 'f':
239 cout << FindFirstInLargest() << "\n";
240 break;
241 case 'F':
242 temp = FindFirstInLargest();
243 Align(&temp);
244 cout << temp << "\n";
245 break;
246 case 'g':
247 JustLooking(0);
248 saveData = 1;
249 saveNonGPT = 1;
250 break;
251 case 'G':
252 JustLooking(0);
253 saveData = 1;
254 RandomizeGUIDs();
255 break;
256 case 'h':
257 JustLooking(0);
258 if (BuildMBR(hybrids, 1) == 1)
259 saveData = 1;
260 break;
261 case 'i':
262 ShowPartDetails(infoPartNum - 1);
263 break;
264 case 'l':
265 LoadBackupFile(backupFile, saveData, neverSaveData);
266 free(backupFile);
267 break;
268 case 'L':
269 break;
270 case 'm':
271 JustLooking(0);
272 if (BuildMBR(mbrParts, 0) == 1) {
273 if (!pretend) {
274 if (SaveMBR()) {
275 DestroyGPT();
276 } else
277 cerr << "Problem saving MBR!\n";
278 } // if
279 saveNonGPT = 0;
280 pretend = 1; // Not really, but works around problem if -g is used with this...
281 saveData = 0;
282 } // if
283 break;
284 case 'n':
285 JustLooking(0);
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500286 newPartNum = (int) GetInt(newPartInfo, 1) - 1;
287 if (newPartNum < 0)
288 newPartNum = FindFirstFreePart();
srs56943860cbe2011-09-10 20:29:53 -0400289 low = FindFirstInLargest();
srs5694f5dfbfa2013-02-14 20:47:14 -0500290 Align(&low);
srs56943860cbe2011-09-10 20:29:53 -0400291 high = FindLastInFree(low);
292 startSector = IeeeToInt(GetString(newPartInfo, 2), sSize, low, high, low);
293 endSector = IeeeToInt(GetString(newPartInfo, 3), sSize, startSector, high, high);
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500294 if (CreatePartition(newPartNum, startSector, endSector)) {
srs56943860cbe2011-09-10 20:29:53 -0400295 saveData = 1;
296 } else {
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500297 cerr << "Could not create partition " << newPartNum + 1 << " from "
298 << startSector << " to " << endSector << "\n";
srs56943860cbe2011-09-10 20:29:53 -0400299 neverSaveData = 1;
300 } // if/else
301 free(newPartInfo);
302 break;
303 case 'N':
304 JustLooking(0);
305 startSector = FindFirstInLargest();
srs5694f5dfbfa2013-02-14 20:47:14 -0500306 Align(&startSector);
srs56943860cbe2011-09-10 20:29:53 -0400307 endSector = FindLastInFree(startSector);
308 if (largestPartNum < 0)
309 largestPartNum = FindFirstFreePart();
310 if (CreatePartition(largestPartNum - 1, startSector, endSector)) {
311 saveData = 1;
312 } else {
313 cerr << "Could not create partition " << largestPartNum << " from "
314 << startSector << " to " << endSector << "\n";
315 neverSaveData = 1;
316 } // if/else
317 break;
318 case 'o':
319 JustLooking(0);
320 ClearGPTData();
321 saveData = 1;
322 break;
323 case 'p':
324 DisplayGPTData();
325 break;
326 case 'P':
327 pretend = 1;
328 break;
329 case 'r':
330 JustLooking(0);
331 uint64_t p1, p2;
332 p1 = GetInt(twoParts, 1) - 1;
333 p2 = GetInt(twoParts, 2) - 1;
334 if (SwapPartitions((uint32_t) p1, (uint32_t) p2) == 0) {
335 neverSaveData = 1;
336 cerr << "Cannot swap partitions " << p1 + 1 << " and " << p2 + 1 << "\n";
337 } else saveData = 1;
338 break;
339 case 'R':
340 secondDevice = *this;
341 secondDevice.SetDisk(outDevice);
342 secondDevice.JustLooking(0);
343 if (!secondDevice.SaveGPTData(1))
344 retval = 8;
345 break;
346 case 's':
347 JustLooking(0);
348 SortGPT();
349 saveData = 1;
350 break;
351 case 'S':
352 JustLooking(0);
353 if (SetGPTSize(tableSize) == 0)
354 neverSaveData = 1;
355 else
356 saveData = 1;
357 break;
358 case 't':
359 JustLooking(0);
360 partNum = (int) GetInt(typeCode, 1) - 1;
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500361 if (partNum < 0)
362 partNum = newPartNum;
srs56940741fa22013-01-09 12:55:40 -0500363 if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
364 typeHelper = GetString(typeCode, 2);
365 if ((typeHelper != (GUIDData) "00000000-0000-0000-0000-000000000000") &&
366 (ChangePartType(partNum, typeHelper))) {
367 saveData = 1;
368 } else {
369 cerr << "Could not change partition " << partNum + 1
370 << "'s type code to " << GetString(typeCode, 2) << "!\n";
371 neverSaveData = 1;
372 } // if/else
srs56943860cbe2011-09-10 20:29:53 -0400373 free(typeCode);
srs56940741fa22013-01-09 12:55:40 -0500374 }
srs56943860cbe2011-09-10 20:29:53 -0400375 break;
376 case 'T':
377 JustLooking(0);
378 XFormDisklabel(bsdPartNum - 1);
379 saveData = 1;
380 break;
381 case 'u':
382 JustLooking(0);
383 saveData = 1;
srs56940741fa22013-01-09 12:55:40 -0500384 partNum = (int) GetInt(partGUID, 1) - 1;
Roderick W. Smithd28495e2014-03-02 11:37:20 -0500385 if (partNum < 0)
386 partNum = newPartNum;
srs56940741fa22013-01-09 12:55:40 -0500387 if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
388 SetPartitionGUID(partNum, GetString(partGUID, 2).c_str());
389 }
srs56943860cbe2011-09-10 20:29:53 -0400390 break;
391 case 'U':
392 JustLooking(0);
393 saveData = 1;
394 SetDiskGUID(diskGUID);
395 break;
396 case 'v':
397 Verify();
398 break;
399 case 'z':
400 if (!pretend) {
401 DestroyGPT();
402 } // if
403 saveNonGPT = 0;
404 saveData = 0;
405 break;
406 case 'Z':
407 if (!pretend) {
408 DestroyGPT();
409 DestroyMBR();
410 } // if
411 saveNonGPT = 0;
412 saveData = 0;
413 break;
414 default:
415 cerr << "Unknown option (-" << opt << ")!\n";
416 break;
417 } // switch
418 } // while
419 } else { // if loaded OK
420 poptResetContext(poptCon);
421 // Do a few types of operations even if there are problems....
422 while ((opt = poptGetNextOpt(poptCon)) > 0) {
423 switch (opt) {
424 case 'l':
425 LoadBackupFile(backupFile, saveData, neverSaveData);
426 cout << "Information: Loading backup partition table; will override earlier problems!\n";
427 free(backupFile);
428 retval = 0;
429 break;
430 case 'o':
431 JustLooking(0);
432 ClearGPTData();
433 saveData = 1;
434 cout << "Information: Creating fresh partition table; will override earlier problems!\n";
435 retval = 0;
436 break;
437 case 'v':
438 cout << "Verification may miss some problems or report too many!\n";
439 Verify();
440 break;
441 case 'z':
442 if (!pretend) {
443 DestroyGPT();
444 } // if
445 saveNonGPT = 0;
446 saveData = 0;
447 break;
448 case 'Z':
449 if (!pretend) {
450 DestroyGPT();
451 DestroyMBR();
452 } // if
453 saveNonGPT = 0;
454 saveData = 0;
455 break;
456 } // switch
457 } // while
458 retval = 2;
459 } // if/else loaded OK
460 if ((saveData) && (!neverSaveData) && (saveNonGPT) && (!pretend)) {
461 SaveGPTData(1);
462 }
463 if (saveData && (!saveNonGPT)) {
464 cout << "Non-GPT disk; not saving changes. Use -g to override.\n";
465 retval = 3;
466 } // if
467 if (neverSaveData) {
468 cerr << "Error encountered; not saving changes.\n";
469 retval = 4;
470 } // if
471 } // if (device != NULL)
472 poptFreeContext(poptCon);
473 return retval;
474} // GPTDataCL::DoOptions()
475
476// Create a hybrid or regular MBR from GPT data structures
477int GPTDataCL::BuildMBR(char* argument, int isHybrid) {
478 int numParts, allOK = 1, i, origPartNum;
479 MBRPart newPart;
480 BasicMBRData newMBR;
Roderick W. Smith820d1d02014-02-20 13:25:22 -0500481
srs56943860cbe2011-09-10 20:29:53 -0400482 if (argument != NULL) {
483 numParts = CountColons(argument) + 1;
484 if (numParts <= (4 - isHybrid)) {
485 newMBR.SetDisk(GetDisk());
486 for (i = 0; i < numParts; i++) {
487 origPartNum = GetInt(argument, i + 1) - 1;
Roderick W. Smitha345a922014-02-22 12:12:32 -0500488 if (IsUsedPartNum(origPartNum) && (partitions[origPartNum].IsSizedForMBR() == MBR_SIZED_GOOD)) {
srs56943860cbe2011-09-10 20:29:53 -0400489 newPart.SetInclusion(PRIMARY);
490 newPart.SetLocation(operator[](origPartNum).GetFirstLBA(),
491 operator[](origPartNum).GetLengthLBA());
492 newPart.SetStatus(0);
493 newPart.SetType((uint8_t)(operator[](origPartNum).GetHexType() / 0x0100));
494 newMBR.AddPart(i + isHybrid, newPart);
495 } else {
Roderick W. Smith820d1d02014-02-20 13:25:22 -0500496 cerr << "Original partition " << origPartNum + 1 << " does not exist or is too big! Aborting operation!\n";
srs56943860cbe2011-09-10 20:29:53 -0400497 allOK = 0;
498 } // if/else
499 } // for
500 if (isHybrid) {
501 newPart.SetInclusion(PRIMARY);
502 newPart.SetLocation(1, newMBR.FindLastInFree(1));
503 newPart.SetStatus(0);
504 newPart.SetType(0xEE);
505 newMBR.AddPart(0, newPart);
506 } // if
Roderick W. Smitha345a922014-02-22 12:12:32 -0500507 if (allOK)
508 SetProtectiveMBR(newMBR);
srs56943860cbe2011-09-10 20:29:53 -0400509 } else allOK = 0;
510 } else allOK = 0;
511 if (!allOK)
512 cerr << "Problem creating MBR!\n";
513 return allOK;
514} // GPTDataCL::BuildMBR()
515
516// Returns the number of colons in argument string, ignoring the
517// first character (thus, a leading colon is ignored, as GetString()
518// does).
519int CountColons(char* argument) {
520 int num = 0;
srs56940741fa22013-01-09 12:55:40 -0500521
srs56943860cbe2011-09-10 20:29:53 -0400522 while ((argument[0] != '\0') && (argument = strchr(&argument[1], ':')))
523 num++;
srs56940741fa22013-01-09 12:55:40 -0500524
srs56943860cbe2011-09-10 20:29:53 -0400525 return num;
526} // GPTDataCL::CountColons()
527
528// Extract integer data from argument string, which should be colon-delimited
529uint64_t GetInt(const string & argument, int itemNum) {
530 uint64_t retval;
srs56940741fa22013-01-09 12:55:40 -0500531
srs56943860cbe2011-09-10 20:29:53 -0400532 istringstream inString(GetString(argument, itemNum));
533 inString >> retval;
534 return retval;
535} // GPTDataCL::GetInt()
536
537// Extract string data from argument string, which should be colon-delimited
538// If string begins with a colon, that colon is skipped in the counting. If an
539// invalid itemNum is specified, returns an empty string.
540string GetString(string argument, int itemNum) {
541 size_t startPos = 0, endPos = 0;
542 string retVal = "";
543 int foundLast = 0;
544 int numFound = 0;
srs56940741fa22013-01-09 12:55:40 -0500545
srs56943860cbe2011-09-10 20:29:53 -0400546 if (argument[0] == ':')
547 argument.erase(0, 1);
548 while ((numFound < itemNum) && (!foundLast)) {
549 endPos = argument.find(':', startPos);
550 numFound++;
551 if (endPos == string::npos) {
552 foundLast = 1;
553 endPos = argument.length();
554 } else if (numFound < itemNum) {
555 startPos = endPos + 1;
556 } // if/elseif
557 } // while
558 if ((numFound == itemNum) && (numFound > 0))
559 retVal = argument.substr(startPos, endPos - startPos);
srs56940741fa22013-01-09 12:55:40 -0500560
srs56943860cbe2011-09-10 20:29:53 -0400561 return retVal;
562} // GetString()