status_t CDDBQuery::GetSites(bool (*eachFunc)(const char *site, int port, const char *latitude, const char *longitude, const char *description, void *state), void *passThru) { if (!_IsConnected()) _Connect(); BString tmp; tmp = "sites\n"; STRACE((">%s", tmp.String())); if (fSocket.Send(tmp.String(), tmp.Length()) == -1) return B_ERROR; _ReadLine(tmp); if (tmp.FindFirst("210") == -1) { _Disconnect(); return B_ERROR; } for (;;) { BString site; int32 sitePort; BString latitude; BString longitude; BString description; _ReadLine(tmp); if (tmp == ".") break; const char *scanner = tmp.String(); scanner = _GetToken(scanner, site); BString portString; scanner = _GetToken(scanner, portString); sitePort = atoi(portString.String()); scanner = _GetToken(scanner, latitude); scanner = _GetToken(scanner, longitude); description = scanner; if (eachFunc(site.String(), sitePort, latitude.String(), longitude.String(), description.String(), passThru)) break; } _Disconnect(); return B_OK; }
void CEtsEodManagerDoc::OnAppend(LPCSTR lpBuf, long nSize) { CString strBuf; DWORD dwRes = ERROR_SUCCESS; long nOffset = 0; long nRead = 0; if(m_strScrapsBuf.GetLength()) { strBuf += m_strScrapsBuf; m_strScrapsBuf.Empty(); } strBuf.Append(lpBuf, nSize); nOffset = 0; while(nOffset < strBuf.GetLength()) { if((dwRes = _ReadLine(strBuf, nOffset, &nRead, true)) != ERROR_SUCCESS) { if(ERROR_INSUFFICIENT_BUFFER == dwRes) { m_strScrapsBuf.Append(strBuf.Mid(nOffset), strBuf.GetLength() - nOffset); strBuf.Empty(); break; } else return; } nOffset += nRead; nOffset += 2; // skip CRLF } }
status_t CDDBQuery::_IdentifySelf() { BString username, hostname; username = getenv("USER"); hostname = getenv("HOSTNAME"); if (username.Length() < 1) username = "******"; if (hostname.Length() < 1) hostname = "antares"; BString tmp; tmp << "cddb hello " << username << " " << hostname << " AntaresCDPlayer 1.0\n"; STRACE((">%s", tmp.String())); if (fSocket.Send(tmp.String(), tmp.Length())==-1) { _Disconnect(); return B_ERROR; } _ReadLine(tmp); return B_OK; }
DWORD CEtsEodManagerDoc::_ReadReport() { m_mapSteps.clear(); _InitStepsMap(); CString strBuf; char chBuf[MAX_BUFFER]; DWORD nBufSize = MAX_BUFFER; DWORD dwRes = ERROR_SUCCESS; long nOffset = 0; long nRead = 0; m_strScrapsBuf.Empty(); while(nBufSize) { strBuf.Empty(); if(m_strScrapsBuf.GetLength()) { strBuf += m_strScrapsBuf; m_strScrapsBuf.Empty(); } nBufSize = MAX_BUFFER; if((dwRes = Read(chBuf, nBufSize)) != ERROR_SUCCESS) return dwRes; strBuf.Append(chBuf, nBufSize); nOffset = 0; while(nOffset < strBuf.GetLength()) { if((dwRes = _ReadLine(strBuf, nOffset, &nRead, false)) != ERROR_SUCCESS) { if(ERROR_INSUFFICIENT_BUFFER == dwRes) { m_strScrapsBuf.Append(strBuf.Mid(nOffset), strBuf.GetLength() - nOffset); strBuf.Empty(); break; } else { ASSERT(FALSE); FileClose(); return dwRes; } } nOffset += nRead; nOffset += 2; // skip CRLF } } return ERROR_SUCCESS; }
bool _ReadNumber(const char* prompt, int64& number) { while (true) { BString line; if (!_ReadLine(prompt, line)) return false; char buffer[256]; if (sscanf(line.String(), "%lld%s", &number, buffer) == 1) return true; printf("invalid input\n"); } }
void Run() { // prepare device modifications if (fDevice->IsReadOnly()) { printf("Device is read-only. Can't change anything.\n"); } else { status_t error = fDevice->PrepareModifications(); fPrepared = (error == B_OK); if (error != B_OK) { printf("Error: Failed to prepare device for modifications: " "%s\n", strerror(error)); } } // main input loop while (true) { BString line; if (!_ReadLine("party> ", line)) return; if (line == "") { // do nothing } else if (line == "h" || line == "help") { _PrintHelp(); } else if (line == "i") { _InitializePartition(); } else if (line == "l") { _PrintPartitionsShort(); } else if (line == "ll") { _PrintPartitionsLong(); } else if (line == "n") { _NewPartition(); } else if (line == "q" || line == "quit") { return; } else if (line == "w") { _WriteChanges(); } else { printf("Invalid command \"%s\", type \"help\" for help\n", line.String()); } } }
bool _ReadNumber(const char* prompt, int64 minNumber, int64 maxNumber, int64 abortNumber, const char* invalidNumberMessage, int64& number) { while (true) { BString line; if (!_ReadLine(prompt, line)) return false; char buffer[256]; if (sscanf(line.String(), "%lld%s", &number, buffer) != 1) { printf("invalid input\n"); continue; } if (number == abortNumber) return false; if (number >= minNumber && number <= maxNumber) return true; puts(invalidNumberMessage); } }
status_t CDDBQuery::_Connect() { if (fConnected) _Disconnect(); BNetAddress address; status_t status = address.SetTo(fServerName.String(), fPort); if (status != B_OK) return status; status = fSocket.Connect(address); if (status != B_OK) return status; fConnected = true; BString tmp; _ReadLine(tmp); _IdentifySelf(); return B_OK; }
status_t CDDBQuery::_ReadFromServer(BString &data) { // This function queries the given CDDB server for the existence of the disc's data and // saves the data to file once obtained. // Query for the existence of the disc in the database char idString[10]; sprintf(idString, "%08lx", fCDData.DiscID()); BString query; int32 trackCount = GetTrackCount(&fSCSIData); BString offsetString = OffsetsToString(&fSCSIData); int32 discLength = (fCDData.DiscTime()->GetMinutes() * 60) + fCDData.DiscTime()->GetSeconds(); query << "cddb query " << idString << ' ' << trackCount << ' ' << offsetString << ' ' << discLength << '\n'; STRACE((">%s", query.String())); if (fSocket.Send(query.String(), query.Length()) == -1) { _Disconnect(); return B_ERROR; } BString tmp; _ReadLine(tmp); STRACE(("<%s", tmp.String())); BString category; BString queryDiscID(idString); if (tmp.FindFirst("200") != 0) { if (tmp.FindFirst("211") == 0) { // A 211 means that the query was not exact. To make sure that we don't // have a problem with this in the future, we will choose the first entry that // the server returns. This may or may not be wise, but in my experience, the first // one has been the right one. _ReadLine(tmp); // Get the category from the what the server returned _GetToken(tmp.String(), category); // Now we will get the disc ID for the CD. We will need this when we query for // the track name list. If we send the track name query with the real discID, nothing // will be returned. However, if we send the one from the entry, we'll get the names // and we can take these names attach them to the disc that we have. _GetToken(tmp.String() + category.CountChars(), queryDiscID); // This is to suck up any more search results that the server sends us. BString throwaway; _ReadLine(throwaway); while (throwaway.ByteAt(0) != '.') _ReadLine(throwaway); } else { // We get here for any time the CDDB server does not recognize the CD, amongst other things STRACE(("CDDB lookup error: %s\n", tmp.String())); fCDData.SetGenre("misc"); return B_NAME_NOT_FOUND; } } else { _GetToken(tmp.String() + 3, category); } STRACE(("CDDBQuery::_ReadFromServer: Genre: %s\n", category.String())); if (!category.Length()) { STRACE(("Set genre to 'misc'\n")); category = "misc"; } // Query for the disc's data - artist, album, tracks, etc. query = ""; query << "cddb read " << category << ' ' << queryDiscID << '\n' ; if (fSocket.Send(query.String(), query.Length()) == -1) { _Disconnect(); return B_ERROR; } while (true) { _ReadLine(tmp); if (tmp == "." || tmp == ".\n") break; data << tmp << '\n'; } return B_OK; }
bool _ReadSize(const char* prompt, off_t defaultValue, off_t& size) { while (true) { BString _line; if (!_ReadLine(prompt, _line)) return false; const char* line = _line.String(); // skip whitespace while (isspace(*line)) line++; if (*line == '\0') { size = defaultValue; return true; } // get the number int32 endIndex = 0; while (isdigit(line[endIndex])) endIndex++; if (endIndex == 0) { printf("invalid input\n"); continue; } size = atoll(BString(line, endIndex).String()); // skip whitespace line += endIndex; while (isspace(*line)) line++; // get the size modifier if (*line != '\0') { switch (*line) { case 'K': size *= 1024; break; case 'M': size *= 1024 * 1024; break; case 'G': size *= 1024LL * 1024 * 1024; break; case 'T': size *= 1024LL * 1024 * 1024 * 1024; break; default: line--; } line++; } if (*line == 'B') line++; // skip whitespace while (isspace(*line)) line++; if (*line == '\0') return true; printf("invalid input\n"); } }
void _NewPartition() { if (!fPrepared) { if (fDevice->IsReadOnly()) printf("Device is read-only!\n"); else printf("Sorry, not prepared for modifications!\n"); return; } // get the parent partition BPartition* partition = NULL; int32 partitionIndex; if (!_SelectPartition("parent partition index [-1 to abort]: ", partition, partitionIndex)) { return; } printf("\nselected partition:\n\n"); print_partition_table_header(); print_partition(partition, 0, partitionIndex); if (!partition->ContainsPartitioningSystem()) { printf("The selected partition does not contain a partitioning " "system.\n"); return; } // get supported types BObjectList<BString> supportedTypes(20, true); BString typeBuffer; int32 cookie = 0; while (partition->GetNextSupportedChildType(&cookie, &typeBuffer) == B_OK) { supportedTypes.AddItem(new BString(typeBuffer)); } if (supportedTypes.IsEmpty()) { printf("The partitioning system is not able to create any " "child partition (no supported types).\n"); return; } // get partitioning info BPartitioningInfo partitioningInfo; status_t error = partition->GetPartitioningInfo(&partitioningInfo); if (error != B_OK) { printf("Failed to get partitioning info for partition: %s\n", strerror(error)); return; } int32 spacesCount = partitioningInfo.CountPartitionableSpaces(); if (spacesCount == 0) { printf("There's no space on the partition where a child partition " "could be created\n"); return; } // let the user select the partition type, if there's more than one int64 typeIndex = 0; int32 supportedTypesCount = supportedTypes.CountItems(); if (supportedTypesCount > 1) { // list them printf("Possible partition types:\n"); for (int32 i = 0; i < supportedTypesCount; i++) printf("%2ld %s\n", i, supportedTypes.ItemAt(i)->String()); if (!_ReadNumber("supported type index [-1 to abort]: ", 0, supportedTypesCount - 1, -1, "invalid index", typeIndex)) { return; } } const char* type = supportedTypes.ItemAt(typeIndex)->String(); // list partitionable spaces printf("Unused regions where the new partition could be created:\n"); for (int32 i = 0; i < spacesCount; i++) { off_t _offset; off_t _size; partitioningInfo.GetPartitionableSpaceAt(i, &_offset, &_size); BString offset, size; get_size_string(_offset, offset); get_size_string(_size, size); printf("%2ld start: %8s, size: %8s\n", i, offset.String(), size.String()); } // let the user select the partitionable space, if there's more than one int64 spaceIndex = 0; if (spacesCount > 1) { if (!_ReadNumber("unused region index [-1 to abort]: ", 0, spacesCount - 1, -1, "invalid index", spaceIndex)) { return; } } off_t spaceOffset; off_t spaceSize; partitioningInfo.GetPartitionableSpaceAt(spaceIndex, &spaceOffset, &spaceSize); off_t start; off_t size; BString parameters; while (true) { // let the user enter start, size, and parameters // start while (true) { BString spaceOffsetString; get_size_string(spaceOffset, spaceOffsetString); BString prompt("partition start [default: "); prompt << spaceOffsetString << "]: "; if (!_ReadSize(prompt.String(), spaceOffset, start)) return; if (start >= spaceOffset && start <= spaceOffset + spaceSize) break; printf("invalid partition start\n"); } // size off_t maxSize = spaceOffset + spaceSize - start; while (true) { BString maxSizeString; get_size_string(maxSize, maxSizeString); BString prompt("partition size [default: "); prompt << maxSizeString << "]: "; if (!_ReadSize(prompt.String(), maxSize, size)) return; if (size >= 0 && start + size <= spaceOffset + spaceSize) break; printf("invalid partition size\n"); } // parameters if (!_ReadLine("partition parameters: ", parameters)) return; // validate parameters off_t validatedStart = start; off_t validatedSize = size; // TODO: Support the name parameter! if (partition->ValidateCreateChild(&start, &size, type, NULL, parameters.String()) != B_OK) { printf("Validation of the given values failed. Sorry, can't " "continue.\n"); return; } // did the disk system change offset or size? if (validatedStart == start && validatedSize == size) { printf("Everything looks dandy.\n"); } else { BString startString, sizeString; get_size_string(validatedStart, startString); get_size_string(validatedSize, sizeString); printf("The disk system adjusted the partition start and " "size to %lld (%s) and %lld (%s).\n", validatedStart, startString.String(), validatedSize, sizeString.String()); start = validatedStart; size = validatedSize; } // let the user decide whether to continue, change parameters, or // abort bool changeParameters = false; while (true) { BString line; _ReadLine("[c]ontinue, change [p]arameters, or [a]bort? ", line); if (line == "a") return; if (line == "p") { changeParameters = true; break; } if (line == "c") break; printf("invalid input\n"); } if (!changeParameters) break; } // create child error = partition->CreateChild(start, size, type, NULL, parameters.String()); if (error != B_OK) printf("Creating the partition failed: %s\n", strerror(error)); }
void _InitializePartition() { if (!fPrepared) { if (fDevice->IsReadOnly()) printf("Device is read-only!\n"); else printf("Sorry, not prepared for modifications!\n"); return; } // get the partition int32 partitionIndex; BPartition* partition = NULL; if (!_SelectPartition("partition index [-1 to abort]: ", partition, partitionIndex)) { return; } printf("\nselected partition:\n\n"); print_partition_table_header(); print_partition(partition, 0, partitionIndex); // get available disk systems BObjectList<BDiskSystem> diskSystems(20, true); BDiskDeviceRoster roster; { BDiskSystem diskSystem; while (roster.GetNextDiskSystem(&diskSystem) == B_OK) { if (partition->CanInitialize(diskSystem.PrettyName())) diskSystems.AddItem(new BDiskSystem(diskSystem)); } } if (diskSystems.IsEmpty()) { printf("There are no disk systems, that can initialize this " "partition.\n"); return; } // print the available disk systems printf("\ndisk systems that can initialize the selected partition:\n"); for (int32 i = 0; BDiskSystem* diskSystem = diskSystems.ItemAt(i); i++) printf("%2ld %s\n", i, diskSystem->PrettyName()); printf("\n"); // get the disk system int64 diskSystemIndex; if (!_ReadNumber("disk system index [-1 to abort]: ", 0, diskSystems.CountItems() - 1, -1, "invalid index", diskSystemIndex)) { return; } BDiskSystem* diskSystem = diskSystems.ItemAt(diskSystemIndex); bool supportsName = diskSystem->SupportsContentName(); BString name; BString parameters; while (true) { // let the user enter name and parameters if (supportsName && !_ReadLine("partition name: ", name) || !_ReadLine("partition parameters: ", parameters)) { return; } // validate parameters BString validatedName(name); if (partition->ValidateInitialize(diskSystem->PrettyName(), supportsName ? &validatedName : NULL, parameters.String()) != B_OK) { printf("Validation of the given values failed. Sorry, can't " "continue.\n"); return; } // did the disk system change the name? if (!supportsName || name == validatedName) { printf("Everything looks dandy.\n"); } else { printf("The disk system adjusted the file name to \"%s\".\n", validatedName.String()); name = validatedName; } // let the user decide whether to continue, change parameters, or // abort bool changeParameters = false; while (true) { BString line; _ReadLine("[c]ontinue, change [p]arameters, or [a]bort? ", line); if (line == "a") return; if (line == "p") { changeParameters = true; break; } if (line == "c") break; printf("invalid input\n"); } if (!changeParameters) break; } // initialize status_t error = partition->Initialize(diskSystem->PrettyName(), supportsName ? name.String() : NULL, parameters.String()); if (error != B_OK) printf("Initialization failed: %s\n", strerror(error)); }