1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <stdarg.h> 20 #include <stdio.h> 21 22 #include <private/bionic_macros.h> 23 24 class MallocXmlElem { 25 public: 26 // Name must be valid throughout lifetime of the object. 27 explicit MallocXmlElem(FILE* fp, const char* name, fp_(fp)28 const char* attr_fmt = nullptr, ...) : fp_(fp), name_(name) { 29 fprintf(fp, "<%s", name_); 30 if (attr_fmt != nullptr) { 31 va_list args; 32 va_start(args, attr_fmt); 33 fputc(' ', fp_); 34 vfprintf(fp_, attr_fmt, args); 35 va_end(args); 36 } 37 fputc('>', fp_); 38 } 39 ~MallocXmlElem()40 ~MallocXmlElem() noexcept { 41 fprintf(fp_, "</%s>", name_); 42 } 43 Contents(const char * fmt,...)44 void Contents(const char* fmt, ...) { 45 va_list args; 46 va_start(args, fmt); 47 vfprintf(fp_, fmt, args); 48 va_end(args); 49 } 50 51 private: 52 FILE* fp_; 53 const char* name_; 54 55 BIONIC_DISALLOW_IMPLICIT_CONSTRUCTORS(MallocXmlElem); 56 }; 57