NCBI C++ ToolKit
publication_edit.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: publication_edit.cpp 93572 2021-04-30 13:48:31Z stakhovv $
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: Igor Filippov, Andrea Asztalos
27 *
28 * File Description:
29 *
30 */
31 
32 #include <ncbi_pch.hpp>
35 
39 
40 string GetFirstInitial(string input, bool skip_rest)
41 {
42  char letter;
43  string inits;
44  string::iterator p = input.begin();
45 
46  // skip leading punct
47  while (p != input.end() && !isalpha(*p))
48  p++;
49 
50  while (p != input.end()) {
51  if (p != input.end() && isalpha(*p)) {
52  letter = *p;
53  inits += toupper(letter);
54  inits += '.';
55  p++;
56  }
57 
58  // skip rest of name
59  if (skip_rest) {
60  while (p != input.end() && isalpha(*p))
61  p++;
62  }
63  else { // skip only subsequent low case letters
64  while (p != input.end() && isalpha(*p) && *p == tolower(*p))
65  p++;
66  }
67 
68  bool dash = false;
69  while (p != input.end() && !isalpha(*p)) {
70  if (*p == '-')
71  dash = true;
72  p++;
73  }
74  if (dash)
75  inits += '-';
76  }
77  return NStr::ToUpper(inits);
78 }
79 
81 {
82  string first_init;
83  if (name.IsSetFirst()) {
84  string first = name.GetFirst();
85  first_init = GetFirstInitial(first, true);
86  }
87 
88  string original_init = (name.IsSetInitials()) ? name.GetInitials() : kEmptyStr;
89  if (!NStr::IsBlank(original_init)) {
90  if (NStr::IsBlank(first_init)) {
91  first_init += ".";
92  }
93  first_init += original_init;
94  }
95  if (first_init.empty()) {
96  return false;
97  }
98 
99  name.SetInitials(first_init);
100  FixInitials(name);
101  return true;
102 }
103 
105 {
106  if (!name.IsSetInitials())
107  return false;
108 
109  string first_init;
110  if (name.IsSetFirst()) {
111  string first = name.GetFirst();
112  first_init = GetFirstInitial(first, true);
113  }
114 
115  string original_init = name.GetInitials();
116  string middle_init = GetFirstInitial(original_init, false);
117 
118  if (!NStr::IsBlank(first_init) && NStr::StartsWith(middle_init, first_init, NStr::eNocase)) {
119  middle_init = middle_init.substr(first_init.length());
120  }
121 
122  string init(first_init);
123  if (!NStr::IsBlank(middle_init)) {
124  init.append(middle_init);
125  }
126  if (!NStr::IsBlank(init) && init != original_init) {
127  name.SetInitials(init);
128  return true;
129  }
130  return false;
131 }
132 
134 {
135  if (!name.IsSetInitials())
136  return false;
137 
138  string initials = name.GetInitials();
139  string first = (name.IsSetFirst()) ? name.GetFirst() : kEmptyStr;
140  SIZE_TYPE dot = NStr::FindNoCase(initials, ".");
141  if (dot != NPOS) {
142  SIZE_TYPE cp = dot;
143  while (isalpha((unsigned char)initials[++cp])) {}
144  string middle = initials.substr(++dot, cp - 2);
145  if (middle.size() > 1) {
146  name.SetFirst(first + " " + middle);
147  return true;
148  }
149  }
150  return false;
151 }
152 
153 
@Name_std.hpp User-defined methods of the data storage class.
Definition: Name_std.hpp:56
static void DLIST_NAME() init(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:40
static DLIST_TYPE *DLIST_NAME() first(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:46
string
Definition: cgiapp.hpp:687
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
NCBI_NS_STD::string::size_type SIZE_TYPE
Definition: ncbistr.hpp:132
#define kEmptyStr
Definition: ncbistr.hpp:123
static SIZE_TYPE FindNoCase(const CTempString str, const CTempString pattern, SIZE_TYPE start, SIZE_TYPE end, EOccurrence which=eFirst)
Find the pattern in the specified range of a string using a case insensitive search.
Definition: ncbistr.cpp:2984
static bool IsBlank(const CTempString str, SIZE_TYPE pos=0)
Check if a string is blank (has no text).
Definition: ncbistr.cpp:106
#define NPOS
Definition: ncbistr.hpp:133
static bool StartsWith(const CTempString str, const CTempString start, ECase use_case=eCase)
Check if a string starts with a specified prefix value.
Definition: ncbistr.hpp:5414
static string & ToUpper(string &str)
Convert string to upper case – string& version.
Definition: ncbistr.cpp:424
@ eNocase
Case insensitive compare.
Definition: ncbistr.hpp:1206
void SetInitials(const TInitials &value)
Assign a value to Initials data member.
Definition: Name_std_.hpp:619
const TInitials & GetInitials(void) const
Get the Initials member data.
Definition: Name_std_.hpp:610
void SetFirst(const TFirst &value)
Assign a value to First data member.
Definition: Name_std_.hpp:478
bool IsSetInitials(void) const
first + middle initials Check if a value has been assigned to Initials data member.
Definition: Name_std_.hpp:598
const TFirst & GetFirst(void) const
Get the First member data.
Definition: Name_std_.hpp:469
bool IsSetFirst(void) const
Check if a value has been assigned to First data member.
Definition: Name_std_.hpp:457
static int input()
Definition: fix_pub.hpp:45
int isalpha(Uchar c)
Definition: ncbictype.hpp:61
int tolower(Uchar c)
Definition: ncbictype.hpp:72
int toupper(Uchar c)
Definition: ncbictype.hpp:73
bool GenerateInitials(CName_std &name)
string GetFirstInitial(string input, bool skip_rest)
bool MoveMiddleToFirst(CName_std &name)
bool FixInitials(CName_std &name)
static Uint4 letter(char c)
Modified on Thu Jul 11 17:50:42 2024 by modify_doxy.py rev. 669887