CCCC - C and C++ Code Counter  9999-git
CCCC Development version (post-3.1.4)
cccc_mod.cc
Go to the documentation of this file.
1 /*
2  CCCC - C and C++ Code Counter
3  Copyright (C) 1994-2005 Tim Littlefair (tim_littlefair@hotmail.com)
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 // cccc_mod.cc
20 
21 // implementation file for CCCC_Module class
22 
23 #include "cccc.h"
24 
25 #include "cccc_itm.h"
26 #include "cccc_mod.h"
27 #include "cccc_db.h"
28 
30 {
32 }
33 
34 
35 string CCCC_Module::name(int name_level) const
36 {
37  string retval;
38 
39  switch(name_level)
40  {
41  case nlMODULE_TYPE:
42  retval=module_type;
43  break;
44 
45  case nlMODULE_NAME:
46  retval=module_name;
47  break;
48 
50  retval=module_type;
51  if(retval.size()>0)
52  {
53  retval=retval+" ";
54  }
55  retval=retval+module_name;
56  break;
57 
58  default:
59  retval=module_name;
60  }
61  return retval.c_str();
62 }
63 
64 int CCCC_Module::get_count(const char* count_tag)
65 {
66  int retval=0;
67  if(strcmp(count_tag,"NOM")==0)
68  {
69  if(is_trivial()==FALSE)
70  {
71  retval=1;
72  }
73  }
74  else if(strcmp(count_tag,"CBO")==0)
75  {
76  retval=client_map.size()+supplier_map.size();
77  }
78  else if(strcmp(count_tag,"NOC")==0)
79  {
80  retval=0;
81 
82  relationship_map_t::iterator iter;
83  iter=client_map.begin();
84  while(iter!=client_map.end())
85  {
86  if((*iter).second->get_usetype()==utINHERITS)
87  {
88  retval++;
89  }
90  iter++;
91  }
92  }
93  else if(strcmp(count_tag,"DIT")==0)
94  {
95  retval=0;
96 
97  // cyclical inheritance relationships in code would
98  // never compile, but this is no excuse for us allowing them
99  // to cause us to overflow the stack
100  static int recursion_depth=0;
101  recursion_depth++;
102  if(recursion_depth>100)
103  {
104  cerr << "Recursion overflow attempting to calculate DIT for "
105  << key() << endl;
106  retval=1000;
107  }
108  else
109  {
110  relationship_map_t::iterator iter;
111  iter=supplier_map.begin();
112  while(iter!=supplier_map.end())
113  {
114  if((*iter).second->get_usetype()==utINHERITS)
115  {
116  int parent_depth=
117  (*iter).second->supplier_module_ptr(project)->get_count("DIT");
118  if(retval<parent_depth+1)
119  {
120  retval=parent_depth+1;
121  }
122  }
123  iter++;
124  }
125  }
126  recursion_depth--;
127  }
128  else if(strncmp(count_tag,"FI",2)==0)
129  {
130  relationship_map_t::iterator iter;
131  iter=supplier_map.begin();
132  while(iter!=supplier_map.end())
133  {
134  retval+=(*iter).second->get_count(count_tag);
135  iter++;
136  }
137  }
138  else if(strncmp(count_tag,"FO",2)==0)
139  {
140  relationship_map_t::iterator iter;
141  iter=client_map.begin();
142  while(iter!=client_map.end())
143  {
144  retval+=(*iter).second->get_count(count_tag);
145  iter++;
146  }
147  }
148  else if(strncmp(count_tag,"IF4",3)==0)
149  {
150  char if4_suffix=count_tag[3];
151  string fi_variant="FI", fo_variant="FO";
152  if(if4_suffix!=0)
153  {
154  fi_variant+=if4_suffix;
155  fo_variant+=if4_suffix;
156  }
157  retval=get_count(fi_variant.c_str())*get_count(fo_variant.c_str());
158  retval*=retval;
159  }
160  else
161  {
163  while(extPtr!=NULL)
164  {
165  int extent_count=extPtr->get_count(count_tag);
166  retval+=extent_count;
167  extPtr=extent_table.next_item();
168  }
169 
170  member_map_t::iterator memIter=member_map.begin();
171  while(memIter!=member_map.end())
172  {
173  int member_count=(*memIter).second->get_count(count_tag);
174  retval+=member_count;
175  memIter++;
176  }
177  }
178  return retval;
179 }
180 
182 {
183  int retval=FALSE;
184 
185  if(
186  (module_type=="builtin") ||
187  (module_type=="enum") ||
188  (module_type=="struct") ||
189  (module_type=="trivial")
190  )
191  {
192  retval=TRUE;
193  }
194 
195  return retval;
196 }
197 
198 int CCCC_Module::ToFile(ofstream& ofstr)
199 {
200  int retval=FALSE;
201  CCCC_Item module_line;
202  module_line.Insert(MODULE_PREFIX);
203  module_line.Insert(module_name);
204  module_line.Insert(module_type);
205  module_line.ToFile(ofstr);
206 
207  CCCC_Extent *extent_ptr=extent_table.first_item();
208  while(extent_ptr!=NULL)
209  {
210  CCCC_Item extent_line;
211  extent_line.Insert(MODEXT_PREFIX);
212  extent_line.Insert(module_name);
213  extent_line.Insert(module_type);
214  extent_ptr->AddToItem(extent_line);
215  extent_line.ToFile(ofstr);
216 
217  extent_ptr=extent_table.next_item();
218  }
219 
220  if(ofstr.good())
221  {
222  retval=TRUE;
223  }
224 
225  return retval;
226 }
227 
228 int CCCC_Module::FromFile(ifstream& ifstr)
229 {
230  int retval=RECORD_ERROR;
231 
232  CCCC_Item next_line;
233  next_line.FromFile(ifstr);
234  ifstr_line++;
235 
236  string line_keyword_dummy;
237 
238  CCCC_Module *found_mptr=NULL;
239 
240  if(
241  next_line.Extract(line_keyword_dummy) &&
242  next_line.Extract(this->module_name) &&
243  next_line.Extract(this->module_type)
244  )
245  {
246  found_mptr=
248  if(found_mptr==this)
249  {
250  // the newly created instance of the module is the first
251  // and has taken its place in the database, so we protect
252  // it from deletion
253  retval=RECORD_ADDED;
254  }
255  else
256  {
257  retval=RECORD_TRANSCRIBED;
258  }
259 
260  // process extent records
261  while(PeekAtNextLinePrefix(ifstr,MODEXT_PREFIX))
262  {
263  CCCC_Extent *new_extent=new CCCC_Extent;
264  next_line.FromFile(ifstr);
265  ifstr_line++;
266  string module_name_dummy, module_type_dummy;
267 
268  if(
269  next_line.Extract(line_keyword_dummy) &&
270  next_line.Extract(module_name_dummy) &&
271  next_line.Extract(module_type_dummy) &&
272  new_extent->GetFromItem(next_line)
273  )
274  {
275  // We don't ever expect to find duplicated extent records
276  // but just in case...
277  CCCC_Extent *found_eptr=
278  found_mptr->extent_table.find_or_insert(new_extent);
279  if(found_eptr!=new_extent)
280  {
281  cerr << "Failed to add extent for module "
282  << found_mptr->key() << " at line " << ifstr_line
283  << endl;
284  delete new_extent;
285  }
286  }
287  }
288  }
289  else
290  {
291  // unexpected problem with the input
292  retval=RECORD_ERROR;
293  }
294 
295  // If the import was successful, we will also have imported all dependent
296  // extent records following the main record.
297  // If not, we must skip them.
298  while(PeekAtNextLinePrefix(ifstr,MODEXT_PREFIX))
299  {
300  CCCC_Item next_line;
301  next_line.FromFile(ifstr);
302  ifstr_line++;
303  cerr << "Ignoring member extent on line " << ifstr_line << endl;
304  }
305 
306  return retval;
307 }
CCCC_Table< CCCC_Module > module_table
Definition: cccc_prj.h:54
virtual string key() const
Definition: cccc_rec.cc:76
int ifstr_line
Definition: cccc_db.cc:32
relationship_map_t client_map
Definition: cccc_mod.h:54
int is_trivial()
Definition: cccc_mod.cc:181
bool Insert(const string &s)
Definition: cccc_itm.cc:31
static CCCC_Project * get_active_project()
Definition: cccc_rec.cc:28
static const string MODULE_PREFIX
Definition: cccc_mod.h:35
string module_name
Definition: cccc_mod.h:48
virtual int get_count(const char *count_tag)
Definition: cccc_mod.cc:64
int get_count(const char *count_tag)
Definition: cccc_ext.cc:147
bool Extract(string &s)
Definition: cccc_itm.cc:47
string module_type
Definition: cccc_mod.h:48
CCCC_Project * current_loading_project
Definition: cccc_db.cc:25
T * first_item()
Definition: cccc_tbl.cc:118
int GetFromItem(CCCC_Item &item)
Definition: cccc_ext.cc:84
int ToFile(ofstream &outfile)
Definition: cccc_mod.cc:198
T * find_or_insert(T *new_item_ptr)
Definition: cccc_tbl.cc:78
bool FromFile(ifstream &ifstr)
Definition: cccc_itm.cc:119
T * next_item()
Definition: cccc_tbl.cc:124
Extent_Table extent_table
Definition: cccc_rec.h:45
int AddToItem(CCCC_Item &item)
Definition: cccc_ext.cc:64
string name(int name_level) const
Definition: cccc_mod.cc:35
relationship_map_t supplier_map
Definition: cccc_mod.h:55
static const string MODEXT_PREFIX
Definition: cccc_mod.h:36
bool PeekAtNextLinePrefix(ifstream &ifstr, string pfx)
Definition: cccc_db.cc:51
member_map_t member_map
Definition: cccc_mod.h:51
CCCC_Project * project
Definition: cccc_mod.h:47
int FromFile(ifstream &infile)
Definition: cccc_mod.cc:228
bool ToFile(ofstream &ofstr)
Definition: cccc_itm.cc:112