summaryrefslogtreecommitdiffstats
path: root/gn/examples/ios/host/main.cc
blob: 1f244efaf4fd36c723752848507e3b7ebf82481a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdlib.h>
#include <iostream>
#include <string>

namespace {

// Returns the current user username.
std::string Username() {
  const char* username = getenv("USER");
  return username ? std::string(username) : std::string();
}

// Writes |string| to |stream| while escaping all C escape sequences.
void EscapeString(std::ostream* stream, const std::string& string) {
  for (char c : string) {
    switch (c) {
      case 0:
        *stream << "\\0";
        break;
      case '\a':
        *stream << "\\a";
        break;
      case '\b':
        *stream << "\\b";
        break;
      case '\e':
        *stream << "\\e";
        break;
      case '\f':
        *stream << "\\f";
        break;
      case '\n':
        *stream << "\\n";
        break;
      case '\r':
        *stream << "\\r";
        break;
      case '\t':
        *stream << "\\t";
        break;
      case '\v':
        *stream << "\\v";
        break;
      case '\\':
        *stream << "\\\\";
        break;
      case '\"':
        *stream << "\\\"";
        break;
      default:
        *stream << c;
        break;
    }
  }
}

}  // namespace

int main(int argc, char** argv) {
  std::string username = Username();

  std::cout << "{\"username\": \"";
  EscapeString(&std::cout, username);
  std::cout << "\"}" << std::endl;

  return 0;
}