1f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// found in the LICENSE file.
4f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
5f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "crazy_linker_util.h"
6f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
7f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include <stdio.h>
8f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
9f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)namespace crazy {
10f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
11f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// Return the base name from a file path. Important: this is a pointer
12f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// into the original string.
13f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// static
14f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)const char* GetBaseNamePtr(const char* path) {
15f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  const char* p = strrchr(path, '/');
16f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (!p)
17f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return path;
18f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  else
19f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    return p + 1;
20f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
21f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
22f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)// static
23f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)const char String::kEmpty[] = "";
24f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
25f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)String::String() { Init(); }
26f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
27f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)String::String(const String& other) {
28f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  Init();
29f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  Assign(other.ptr_, other.size_);
30f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
31f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
32f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)String::String(const char* str) {
33f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  Init();
34f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  Assign(str, strlen(str));
35f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
36f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
37f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)String::String(char ch) {
38f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  Init();
39f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  Assign(&ch, 1);
40f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
41f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
42f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)String::~String() {
43f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (ptr_ != const_cast<char*>(kEmpty)) {
44f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    free(ptr_);
45f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    ptr_ = const_cast<char*>(kEmpty);
46f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
47f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
48f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
49f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)String::String(const char* str, size_t len) {
50f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  Init();
51f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  Assign(str, len);
52f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
53f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
54f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void String::Assign(const char* str, size_t len) {
55f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  Resize(len);
56f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (len > 0) {
57f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    memcpy(ptr_, str, len);
58f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    ptr_[len] = '\0';
59f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    size_ = len;
60f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
61f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
62f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
63f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void String::Append(const char* str, size_t len) {
64f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (len > 0) {
65f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    size_t old_size = size_;
66f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    Resize(size_ + len);
67f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    memcpy(ptr_ + old_size, str, len);
68f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
69f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
70f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
71f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void String::Resize(size_t new_size) {
72f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (new_size > capacity_) {
73f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    size_t new_capacity = capacity_;
74f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    while (new_capacity < new_size) {
75f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      new_capacity += (new_capacity >> 1) + 16;
76f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    }
77f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    Reserve(new_capacity);
78f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
79f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
80f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (new_size > size_)
81f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    memset(ptr_ + size_, '\0', new_size - size_);
82f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
83f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  size_ = new_size;
84f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (ptr_ != kEmpty)
85f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    ptr_[size_] = '\0';
86f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
87f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
88f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void String::Reserve(size_t new_capacity) {
89f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  char* old_ptr = (ptr_ == const_cast<char*>(kEmpty)) ? NULL : ptr_;
90f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Always allocate one more byte for the trailing \0
91f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  ptr_ = reinterpret_cast<char*>(realloc(old_ptr, new_capacity + 1));
92f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  ptr_[new_capacity] = '\0';
93f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  capacity_ = new_capacity;
94f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
95f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (size_ > new_capacity)
96f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    size_ = new_capacity;
97f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
98f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
99f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#if 0
100f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)String Format(const char* fmt, ...) {
101f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  va_list args;
102f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  va_start(args, fmt);
103f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  String result(FormatArgs(fmt, args));
104f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  va_end(args);
105f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return result;
106f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
107f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
108f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)String FormatArgs(const char* fmt, va_list args) {
109f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  String result;
110f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  for (;;) {
111f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    va_list args2;
112f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    va_copy(args2, args);
113f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    int ret = vsnprintf(&result[0], result.capacity(), fmt, args2);
114f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    va_end(args2);
115f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    if (static_cast<size_t>(ret) <= result.capacity())
116f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      break;
117f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
118f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    result.Resize(static_cast<size_t>(ret));
119f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
120f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return result;
121f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
122f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif
123f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
124f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}  // namespace crazy
125