//------------------------------------------------------------------------------ int main(int, char**) { Print("one", 2, 3.0); std::cout << std::endl; const std::string s = Format("this is the $2nd paramenter and this is the $1.\n", "first", 2); std::cout << s; auto t = std::make_tuple(1., 2., 3, "4"); Apply(2, PrintToStream(std::cout), t); std::cout << std::endl; return 0; }
std::string Format(const char* f, const Args&... params) { std::ostringstream oss; while(*f) { if(*f == '$') { ++f; if(std::isdigit(*f)) { //positional values go from $1 to $9 const int pos = *f - 48 - 1; //0 in standard ASCII Apply(pos, PrintToStream(oss), params...); } else oss << "$" << *f; ++f; } else oss << *f++; } return oss.str(); }
status_t SettingsFile::Save() const { status_t ret; BFile file(path.Path(), B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE); ret = file.InitCheck(); if (ret != B_OK) { // try to create the parent directory if creating the file fails the first time BPath parent; ret = path.GetParent(&parent); if (ret != B_OK) { return ret; } ret = create_directory(parent.Path(), 0777); if (ret != B_OK) { return ret; } ret = file.SetTo(path.Path(), B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE); } if (ret != B_OK) { return ret; } ret = file.Lock(); // lock the file to do atomic attribute transactions on it. if (ret != B_OK) { return ret; } if (vision_app->fDebugSettings) PrintToStream(); ret = Flatten(&file); if (ret != B_OK) { file.Unlock(); return ret; } /* ret=_StoreAttributes(this,&file); if (ret!=B_OK) { file.Unlock(); return ret; } */ file.Unlock(); return B_OK; }
status_t Model::OpenNodeCommon(bool writable) { #if xDEBUG PRINT(("opening node for %s\n", Name())); #endif #ifdef CHECK_OPEN_MODEL_LEAKS if (writableOpenModelList) writableOpenModelList->RemoveItem(this); if (readOnlyOpenModelList) readOnlyOpenModelList->RemoveItem(this); #endif if (fBaseType == kUnknownNode) SetupBaseType(); switch (fBaseType) { case kPlainNode: case kExecutableNode: case kQueryNode: case kQueryTemplateNode: // open or reopen delete fNode; fNode = new BFile(&fEntryRef, (uint32)(writable ? O_RDWR : O_RDONLY)); break; case kDirectoryNode: case kVolumeNode: case kRootNode: if (!IsNodeOpen()) fNode = new BDirectory(&fEntryRef); if (fBaseType == kDirectoryNode && static_cast<BDirectory *>(fNode)->IsRootDirectory()) { // promote from directory to volume fBaseType = kVolumeNode; } break; case kLinkNode: if (!IsNodeOpen()) { BEntry entry(&fEntryRef); fNode = new BSymLink(&entry); } break; default: #if DEBUG PrintToStream(); #endif TRESPASS(); // this can only happen if GetStat failed before, in which case // we shouldn't be here // ToDo: Obviously, we can also be here if the type could not be determined, // for example for block devices (so the TRESPASS() macro shouldn't be // used here)! return fStatus = B_ERROR; } fStatus = fNode->InitCheck(); if (fStatus != B_OK) { delete fNode; fNode = NULL; // original code snoozed an error here and returned B_OK return fStatus; } fWritable = writable; if (!fMimeType.Length()) FinishSettingUpType(); #ifdef CHECK_OPEN_MODEL_LEAKS if (fWritable) { if (!writableOpenModelList) { TRACE(); writableOpenModelList = new BObjectList<Model>(100); } writableOpenModelList->AddItem(this); } else { if (!readOnlyOpenModelList) { TRACE(); readOnlyOpenModelList = new BObjectList<Model>(100); } readOnlyOpenModelList->AddItem(this); } #endif return fStatus; }
status_t SettingsFile::Load() { status_t ret; BFile file(path.Path(), B_READ_ONLY); ret = file.InitCheck(); if (ret != B_OK) { return ret; } ret = file.Lock(); if (ret != B_OK) { return ret; } ret = Unflatten(&file); if (ret != B_OK) { file.Unlock(); MakeEmpty(); return ret; } if (vision_app->fDebugSettings) PrintToStream(); /* ret=file.RewindAttrs(); if (ret!=B_OK) { file.Unlock(); MakeEmpty(); return ret; } char attr_name[B_ATTR_NAME_LENGTH]; while ((ret=file.GetNextAttrName(attr_name))!=B_ENTRY_NOT_FOUND) { // walk all the attributes of the settings file if (ret!=B_OK) { file.Unlock(); return ret; } // found an attribute attr_info ai; ret=file.GetAttrInfo(attr_name,&ai); if (ret!=B_OK) { file.Unlock(); return ret; } switch (ai.type) { case B_CHAR_TYPE : case B_STRING_TYPE : case B_BOOL_TYPE : case B_INT8_TYPE : case B_INT16_TYPE : case B_INT32_TYPE : case B_INT64_TYPE : case B_UINT8_TYPE : case B_UINT16_TYPE : case B_UINT32_TYPE : case B_UINT64_TYPE : case B_FLOAT_TYPE : case B_DOUBLE_TYPE : case B_OFF_T_TYPE : case B_SIZE_T_TYPE : case B_SSIZE_T_TYPE : case B_POINT_TYPE : case B_RECT_TYPE : case B_RGB_COLOR_TYPE : case B_TIME_TYPE : case B_MIME_TYPE : { char*partial_name=strdup(attr_name); if (partial_name==NULL) { file.Unlock(); return B_NO_MEMORY; } ret=_ExtractAttribute(this,&file,attr_name,partial_name,&ai); free(partial_name); if (ret!=B_OK) { file.Unlock(); return ret; } break; } } } */ file.Unlock(); return B_OK; }