1 /*  $Id: srcutil.cpp 512472 2016-08-31 12:38:54Z ivanov $
2 * ===========================================================================
3 *
4 *                            PUBLIC DOMAIN NOTICE
5 *               National Center for Biotechnology Information
6 *
7 *  This software/database is a "United States Government Work" under the
8 *  terms of the United States Copyright Act.  It was written as part of
9 *  the author's official duties as a United States Government employee and
10 *  thus cannot be copyrighted.  This software/database is freely available
11 *  to the public for use. The National Library of Medicine and the U.S.
12 *  Government have not placed any restriction on its use or reproduction.
13 *
14 *  Although all reasonable efforts have been taken to ensure the accuracy
15 *  and reliability of the software and data, the NLM and the U.S.
16 *  Government do not and cannot warrant the performance or results that
17 *  may be obtained by using this software or data. The NLM and the U.S.
18 *  Government disclaim all warranties, express or implied, including
19 *  warranties of performance, merchantability or fitness for any particular
20 *  purpose.
21 *
22 *  Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: Eugene Vasilchenko
27 *
28 * File Description:
29 *   !!! PUT YOUR DESCRIPTION HERE !!!
30 *
31 */
32 
33 #include <ncbi_pch.hpp>
34 #include <corelib/ncbistd.hpp>
35 #include <corelib/ncbistre.hpp>
36 #include "srcutil.hpp"
37 
38 BEGIN_NCBI_SCOPE
39 
Identifier(const string & typeName,bool capitalize)40 string Identifier(const string& typeName, bool capitalize)
41 {
42     string s;
43     s.reserve(typeName.size());
44     string::const_iterator i = typeName.begin();
45     if ( i != typeName.end() ) {
46         if (isalnum((unsigned char)(*i))) {
47             s += capitalize? (char)toupper((unsigned char)(*i)) : *i;
48         } else {
49             string ent(NStr::HtmlEntity((unsigned char)(*i)));
50             if (*i != '_' && !ent.empty()) {
51                 s += ent;
52             } else {
53                 s += '_';
54             }
55         }
56         while ( ++i != typeName.end() ) {
57             char c = *i;
58             if ( c == ':' ) {
59                 continue;
60             }
61             if ( c == '-' || c == '.' || c == '_') {
62                 c = '_';
63             } else if (!isalnum((unsigned char)c)) {
64                 string ent(NStr::HtmlEntity((unsigned char)(*i)));
65                 if (!ent.empty()) {
66                     s += ent;
67                     continue;
68                 }
69                 c = '_';
70             }
71             s += c;
72         }
73     }
74     return s;
75 }
76 
Tabbed(const string & code,const char * tab)77 string Tabbed(const string& code, const char* tab)
78 {
79     string out;
80     SIZE_TYPE size = code.size();
81     if ( size != 0 ) {
82         if ( !tab )
83             tab = "    ";
84         const char* ptr = code.data();
85         while ( size > 0 ) {
86             out += tab;
87             const char* endl =
88                 reinterpret_cast<const char*>(memchr(ptr, '\n', size));
89             if ( !endl ) { // no more '\n'
90                 out.append(ptr, ptr + size);
91                 out += '\n';
92                 break;
93             }
94             ++endl; // skip '\n'
95             size_t lineSize = endl - ptr;
96             out.append(ptr, ptr + lineSize);
97             ptr = endl;
98             size -= lineSize;
99         }
100     }
101     return out;
102 }
103 
WriteTabbed(CNcbiOstream & out,const string & code,const char * tab)104 CNcbiOstream& WriteTabbed(CNcbiOstream& out, const string& code,
105                           const char* tab)
106 {
107     size_t size = code.size();
108     if ( size != 0 ) {
109         if ( !tab )
110             tab = "    ";
111         const char* ptr = code.data();
112         while ( size > 0 ) {
113             out << tab;
114             const char* endl =
115                 reinterpret_cast<const char*>(memchr(ptr, '\n', size));
116             if ( !endl ) { // no more '\n'
117                 out.write(ptr, size) << '\n';
118                 break;
119             }
120             ++endl; // skip '\n'
121             size_t lineSize = endl - ptr;
122             out.write(ptr, lineSize);
123             ptr = endl;
124             size -= lineSize;
125         }
126     }
127     return out;
128 }
129 
PrintASNNewLine(CNcbiOstream & out,int indent)130 CNcbiOstream& PrintASNNewLine(CNcbiOstream& out, int indent)
131 {
132     return out << '\n' << string(2*indent,' ');
133 }
134 
135 END_NCBI_SCOPE
136