1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23package org.graalvm.compiler.debug;
24
25import java.io.PrintStream;
26
27/**
28 * Utilities and global definitions for creating CSV output.
29 */
30public final class CSVUtil {
31    public static final char SEPARATOR = ';';
32    public static final String SEPARATOR_STR = String.valueOf(SEPARATOR);
33    public static final char QUOTE = '"';
34    public static final String QUOTE_STR = String.valueOf(QUOTE);
35    public static final char ESCAPE = '\\';
36    public static final String ESCAPE_STR = String.valueOf(ESCAPE);
37    public static final String ESCAPED_QUOTE_STR = ESCAPE_STR + QUOTE_STR;
38    public static final String ESCAPED_ESCAPE_STR = ESCAPE_STR + ESCAPE_STR;
39
40    public static String buildFormatString(String format, int num) {
41        return buildFormatString(format, SEPARATOR, num);
42    }
43
44    public static String buildFormatString(String... format) {
45        return String.join(SEPARATOR_STR, format);
46    }
47
48    public static String buildFormatString(String format, char separator, int num) {
49        StringBuilder sb = new StringBuilder(num * (format.length() + 1) - 1);
50        sb.append(format);
51        for (int i = 1; i < num; i++) {
52            sb.append(separator).append(format);
53        }
54        return sb.toString();
55    }
56
57    public static final class Escape {
58
59        public static PrintStream println(PrintStream out, String format, Object... args) {
60            return println(out, SEPARATOR, QUOTE, ESCAPE, format, args);
61        }
62
63        public static LogStream println(LogStream out, String format, Object... args) {
64            return println(out, SEPARATOR, QUOTE, ESCAPE, format, args);
65        }
66
67        public static String escape(String str) {
68            return escape(str, SEPARATOR, QUOTE, ESCAPE);
69        }
70
71        public static String escapeRaw(String str) {
72            return escapeRaw(str, QUOTE, ESCAPE);
73        }
74
75        public static PrintStream println(PrintStream out, char separator, char quote, char escape, String format, Object... args) {
76            out.printf(format, escapeArgs(separator, quote, escape, args));
77            out.println();
78            return out;
79        }
80
81        public static LogStream println(LogStream out, char separator, char quote, char escape, String format, Object... args) {
82            out.printf(format, escapeArgs(separator, quote, escape, args));
83            out.println();
84            return out;
85        }
86
87        public static Object[] escapeArgs(Object... args) {
88            return escapeArgs(SEPARATOR, QUOTE, ESCAPE, args);
89        }
90
91        public static Object[] escapeArgs(char separator, char quote, char escape, Object... args) {
92            String separatorStr = String.valueOf(separator);
93            for (int i = 0; i < args.length; i++) {
94                Object obj = args[i];
95                if (obj instanceof String) {
96                    String str = (String) obj;
97                    if (str.contains(separatorStr)) {
98                        args[i] = escapeRaw(str, quote, escape);
99                    }
100                }
101            }
102            return args;
103        }
104
105        public static String escape(String str, char separator, char quote, char escape) {
106            String separatorStr = String.valueOf(separator);
107            if (str.contains(separatorStr)) {
108                return escapeRaw(str, quote, escape);
109            }
110            return str;
111        }
112
113        public static String escapeRaw(String str, char quote, char escape) {
114            String quoteStr = String.valueOf(quote);
115            String escapeStr = String.valueOf(escape);
116            String escapedEscapeStr = escapeStr + escape;
117            String escapedQuoteStr = escapeStr + quote;
118            String str1 = str.replace(escapeStr, escapedEscapeStr);
119            String str2 = str1.replace(quoteStr, escapedQuoteStr);
120            String str3 = quoteStr + str2 + quote;
121            return str3;
122        }
123    }
124}
125