summaryrefslogtreecommitdiff
path: root/trunk/glagen/dll/libraryloader.cc
blob: 9e8b67a7dca822260a975c2b7969d9af8dc80819 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//=============================================================================
//
// Glagen : a planet sized landscape generator
// Copyright (C) 2002  Julien Guertault, Hugues Hiegel, Meng-Tih Lam
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
//=============================================================================
//
// Glagen : GPL LAndscape GENerator
//
// libraryloader.cc for Glagen : made by Hugues HIEGEL
//
// www.glagen.org
//
//=============================================================================

#include <iostream>
#include <string>
#include <list>

#include "includes/errors.hh"
#include "classes/libclass.hh"
#include "libraryloader.hh"

#include "classes/node.hh"
#include "classes/matrix.hh"
#include "classes/Data_string.hh"


using std::cout;
using std::cerr;
using std::endl;

int
getLibID(const std::list<Library>* libids,
	 const string& name)
{
  // Searchs for an ID of the given library name
  
  if (libids->empty())
    return (-1);
  
  int libid = 0;
  std::list<Library>::const_iterator libids_i = NULL;
  
  for (libids_i = libids->begin();
       (libids_i != libids->end()) && ((libids_i->getName()) != name);
       libids_i++)
    libid++;
  
  if (libid == (signed)libids->size())
    return (-1);

  return libid;

}


void
RecursiveAddNode(const Node<int>* Root,
		 Matrix<int>* Adj,
		 const int size)
{
  int curr_node = 0;
  const int nb_childs = Root->get_nb_childs();
  
  if (nb_childs)
    {
      for (curr_node = 0; curr_node < nb_childs; curr_node++)
	{
	  Node<int> node = Root->get_child(curr_node);
	  
	  // Which library is a potentially dependancy ?
	  int curr_lib = node.get_data();
	  
	  // Which libraries need it ?
	  for (int i_ = 0; i_ < size; i_++)
	    if ((*Adj)(i_, curr_lib) == 1)
	      {
		node.add_child(i_);
		(*Adj)(i_, curr_lib) = 2;
	      }
	  RecursiveAddNode(&node, Adj, size);
	}
    }
 
  return;
     
}

void
Int2Datanode(Node<int>* Root,
	     Node<Data_string>* Final,
	     list<Library>* list_libs)
{
  int curr_node = 0;
  const int nb_childs = Root->get_nb_childs();
  std::list<Library>::const_iterator libid_i;
  int i;
  int I;
  
  if (nb_childs)
    {
      for (curr_node = 0; curr_node < nb_childs; curr_node++)
	{
	  Node<int> node = Root->get_child(curr_node);
	  I = node.get_data();
	  
	  libid_i = list_libs->begin();
	  for (i = 0; i < I; i++)
	    libid_i++;

	  Final->add_child(Data_string(libid_i->getFilename()));
	  Node<Data_string> TotoMesCouilles = 
	    Final->get_child(Final->get_nb_childs() - 1);
	  Int2Datanode(&node, &TotoMesCouilles, list_libs); 
	}
    }
 
  return;
     
}

// 
// #
// #         ####     ##    #####   ######  #####
// #        #    #   #  #   #    #  #       #    #
// #        #    #  #    #  #    #  #####   #    #
// #        #    #  ######  #    #  #       #####    ###     ###
// #        #    #  #    #  #    #  #       #   #    ###     ###
// #######   ####   #    #  #####   ######  #    #   ###     ###
// 


Node<Data_string>
LibraryLoader(const list<string>& LibFiles)
{

  /**************************************************************/
  /* Opens all selected libraries and sort them by dependancies */
  /**************************************************************/

  /*\****************\* Declarations *\***************\*/

  /* Pointer needed to manipulate libraries */
  Library*	current_loading (NULL);
  string	libname;

  /* List of loaded libraries needed to construct the deps tree */
  std::list<Library> libids;
  libids.clear();
  int	libid;

  /***********************************/
  /* Loads libraries and stores them */
  /***********************************/
  for (std::list<string>::const_iterator libfile_i = LibFiles.begin();
       libfile_i != LibFiles.end();
       libfile_i++)
    {
      current_loading = new Library (*libfile_i);  // Creates new class
      
      if (! current_loading->LoadLib())
	{

	  /**********************************************
	  ** just for test purposes		       **
	  ** it has nothing to do there		       **
	  ** if it isn't commented.. comment it !      **
	  **********************************************/
	  //current_loading->Initialize();
apao
	  if (getLibID(&libids, current_loading->getName()) == -1)
	    libids.push_back(*current_loading);	// Stores the lib
	  else
	    {
	      cerr << "Library "
		   << current_loading->getName()
		   << " is already loaded !" << endl;
	      delete current_loading;
	    }
	}
      else
	delete current_loading;

    } 



  /*\****************\* Declarations *\***************\*/

  int size = libids.size();

  /* Matrix of dependancies */
  Matrix<int>	adjacence (size, size);
  int i_ = 0, j_ = 0;

  /* List needed to manipulate dependancies of current lib */
  std::list<string> deps;
  deps.clear();

  /* Lists iterators to iterate lists ;o) */
  std::list<Library>::const_iterator libid_i;
  std::list<string>::const_iterator dep_i;

  /**************************************/
  /* Second parsing to get dependancies */
  /**************************************/

  for (libid_i = libids.begin(); libid_i != libids.end(); libid_i++)
    {
      deps = libid_i->getDependancies();
      
      if (! deps.empty())
	for (dep_i = deps.begin();
	     dep_i != deps.end();
	     dep_i++)
	  {
      	    libid = getLibID(&libids, *dep_i);
	    if (!(libid < 0))
	      adjacence(i_, libid) = 1;
	    else
	      {
		cerr
		  << "#ERR " << ERR_LIBMISSING
		  << ": Missing library: "
		  << *dep_i << "" << endl;
		exit(ERR_LIBMISSING);
	      }
	  }
      
      i_++;

    }
  
  cout << "-- " << endl << adjacence << "-- " << endl;

  
  /*\****************\* Declarations *\***************\*/
  /* The root node of sorted libraries tree */
  Node<int> root (-1);
      
  int n_deps;

  /***************************************/
  /* Search for potential root libraries */
  /***************************************/

  for (i_ = 0;
       i_ < size;
       i_++)
    {
      n_deps = 0;
      for (j_ = 0; j_ < size; j_++)
	if (adjacence(i_, j_))
	  n_deps += 1;

      cout << i_ << " : " << n_deps;

      if (n_deps == 0)		/* This is a root library */
	{
	  root.add_child(i_);
	  cout << " Added in Root tree !";
	}
      cout << "" << endl;
    }

  cout << endl << "Root has " << root.get_nb_childs() << " childs.."
       << endl << endl;
  
  if (!root.get_nb_childs())	/* No independant library found.. */
    {
      cerr << "#ERR " << ERR_NOROOTLIB
	   << ": No independant library found.." << endl;
      exit(ERR_NOROOTLIB);
    }

  /***********************************/
  /* Recursive addition of each node */
  /***********************************/

  RecursiveAddNode(&root, &adjacence, size);

  cout << "-- " << endl << adjacence << "-- " << endl;
  
  /******************************************************/
  /* Checks if every Library has been added in the tree */
  /******************************************************/

  for (i_ = 0;
       i_ < size;
       i_++)
    for (j_ = 0;
	 j_ < size;
	 j_++)
      if (adjacence(i_, j_) == 1)
	{
	  cerr << "#Err " << ERR_NOROOTLIB
	       << ": Infinite loop in library dependancies;" << endl;
	  exit(ERR_NOROOTLIB);
	}

  Node<Data_string> FinalResult (Data_string(""));
  Int2Datanode(&root, &FinalResult, &libids);

  // WHEEEE !! IT'S THE END !!! I CAN'T BELIEVE IT !!
  return FinalResult;
}