File: common_functions.php

package info (click to toggle)
openms 2.4.0-real-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 646,136 kB
  • sloc: cpp: 392,260; xml: 215,373; python: 10,976; ansic: 3,325; php: 2,482; sh: 901; ruby: 399; makefile: 141; perl: 85
file content (537 lines) | stat: -rw-r--r-- 14,617 bytes parent folder | download
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
<?php
# --------------------------------------------------------------------------
#                   OpenMS -- Open-Source Mass Spectrometry
# --------------------------------------------------------------------------
# Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
# ETH Zurich, and Freie Universitaet Berlin 2002-2018.
#
# This software is released under a three-clause BSD license:
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#  * Neither the name of any author or any participating institution
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
# For a full list of authors, refer to the file AUTHORS.
# --------------------------------------------------------------------------
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
# INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# --------------------------------------------------------------------------
# $Maintainer: $
# $Authors: Marc Sturm $
# --------------------------------------------------------------------------
///write array to file line by line
function writeFile($name, $array) {
  $fp = fopen($name, "w");
  foreach($array as $line)
  {
    fwrite($fp, rtrim($line, "\r\n")."\n");
  }
  fclose($fp);
}

/// returns true if $string begins with $begin, return false otherwise
function beginsWith($string, $begin) {
  if(substr($string, 0, strlen($begin)) == $begin)
  {
    return true;
  }
  return false;
}

/// returns true if $string ends with $end, return false otherwise
function endsWith($string, $end) {
  if(substr($string,-1*strlen($end)) == $end)
  {
    return true;
  }
  return false;
}

/// returns the prefix of length $length of string $string
function prefix($string, $length) {
  return substr($string, $length);
}

/// returns the suffix of length $length of string $string
function suffix($string, $length) {
  return substr($string,-1*$length);
}

/**
	returns the include guard for the included file $include
	give only the path and filename i.e. the part between "#include <" and ">"
*/
function includeToGuard($include) {
  return strtoupper(str_replace(".", "_", str_replace("/", "_", $include)));
}

/**
	return true if the given line is a include line, returns false otherwise
	the fielname and path of the include are written into $include if it is given 
*/
function isIncludeLine($line, &$include) {
  if(preg_match('/^#[ \t]*include[ \t]*<(.*)>/', ltrim($line), $parts))
  {
    $include = $parts[1];
    return true;
  }
  return false;
}

function isAlpha($char) {
  $c = ord($char);
  return((($c >= 64) && ($c <= 90)) || (($c >= 97) && ($c <= 122)));
}

function isSpace($char) {
  return($char == " ") || ($char == "\n") || ($char == "\t");
}

function isDigit($char) {
  return('0' <= $char) && ($char <= '9');
}

function isAlnum($char) {
  return isAlpha($char) || isDigit($char);
}

function isIdentifierChar($char) {
  return isAlnum($char) || $char == '_';
}

/// tokenize a line into something like C++-Tokens (not exactly, but good enough)
function tokenize($line) {
  $result = array();
  $start = 0;
  // states: start => whitespace
  //         ident => identifier / number etc.
  $state = 0;
  for($i = 0;$i != strlen($line);$i++)
  {
    $char = $line[$i];
    if($state == 0)
    {
      // start state
      if(isIdentifierChar($char))
      {
        // identifier / number etc.
        $start = $i;
        $state = 1;
      }
      elseif(!isSpace($char))
      {
        // operator
        $result[] = $char;
      }
    }
    else
    {
      // identifier state
      if(!isIdentifierChar($char))
      {
        // end of identifier
        $state = 0;
        $result[] = substr($line, $start, $i-$start);
        
        if(!isSpace($char))
        {
          // additionally, the current char belongs to an operator
          $result[] = $char;
        }
      }
    }
  }
  
  return $result;
}

/// parses out the maintainers from the maintainer line
function parseMaintainerLine($line) {
  $replacements = array(
    "Maintainer" => "",
    ":"          => "",
    "/"          => "",
    "$"          => "",
    "	"          => " ",
    "  "         => " ",
    "#"          => "",
  );
  $result = explode(",", strtr($line, $replacements));
  $result = array_map("trim", $result);
  $result = array_filter($result, "strlen");
  return array_unique($result);
}

/**
	@brief parses the member information form doxygen xml
	
	@param class SimpleXML parsed doxygen file
	@param members Array containing the information. This param is passed per reference, so that it will return changed
	@param classname Name of the class that will be parsed
	@param debug The debug level
	
	@return Nothing
*/
function parseClassInfoFromXML($class, &$members, $classname, $debug, $isNested) {
  
  foreach($class->compounddef->sectiondef as $section)
  {
    foreach($section->memberdef as $member)
    {
      #method
      if($member["kind"] == "function")
      {
        #public methods
        if($member["prot"] == "public")
        {
          #name
          $mem = $member->definition.$member->argsstring;
          
          #template parameters
          if(isset($member->templateparamlist))
          {
            $first = true;
            $template = "";
            foreach($member->templateparamlist->param as $para)
            {
              # edited by Clemens 2009-01-15; this seems to work for doxygen 1.5.7.1
              # (handling of whitespace is horrific, please improve if you know better)
              if($first)
              {
                $template = "template <";
                $first = false;
              }
              else
              {
                $template .= ",";
              }
              if(isset($para->type))
              {
                $template .= " ".$para->type;
                $template = trim($template);
              }
              if(isset($para->type->ref))
              {
                $template .= " ".$para->type->ref;
                $template = trim($template);
              }
              if(isset($para->defname))
              {
                $template .= " ".$para->defname;
                $template = trim($template);
              }
            }
            if(!$first)
            {
              $template .= " > ";
            }
            $mem = $template.$mem;
          }
          
          if($debug > 4)
          {
            print "Exceptions for '$mem':\n";
            var_dump($member->exceptions);
          }
          
          #exceptions
          if(isset($member->exceptions))
          {
            $except = " throw (";
            $first = true;
            foreach($member->exceptions->ref as $ref)
            {
              if(!$first)
              {
                $except .= ", ";
              }
              else
              {
                $first = false;
              }
              $except .= (string) $ref;
            }
            if($except != " throw (")
            {
              $mem .= $except.")";
            }
          }
          # remove namespace stuff
          $mem = strtr($mem, array($classname."::" => ""));
          
          # if it is a nested class, prefix the member name with the appropriate
          # name, to identify the corresponding names later
          if($isNested)
          {
            $members["test-name"][] = "[".$classname."] ".$mem;
          }
          else
          {
            $members["test-name"][] = $mem;
          }
          
          $members["public-long"][] = $mem;
          $members["public"][] = (string) $member->name;
        }
        #non-public methods
        else
        {
          $members["non-public"][] = (string) $member->name;
        }
      }
      elseif($member["kind"] == "variable" && $member["prot"] != "public")
      {
        $members["variables"][] = (string) $member->name;
      }
    }
  }
}

/**
	@brief parses the member information form a file
	
	@param path The OpenMS path
	@param header The header file name
	
	@return Returns the parses information
*/
function getClassInfo($bin_path, $header, $debug) {
  $members = array(
    "classname" => substr(basename($header),
    0,-2),
    "public-long" => array(),
    "public"      => array(),
    "non-public"  => array(),
    "variables"   => array(),
    "test-name"   => array(),
  );
  
  ######################## needed stuff ###############################
  if(!file_exists("$bin_path/doc/xml_output/"))
  {
    print "Error: The directory '$bin_path/doc/xml_output/' is needed!\n";
    print "       Please execute 'make doc_xml' first!\n";
  }
  
  ######################## load file ###############################
  $paths = array(
    "",
    "Internal_1_1",
    "Math_1_1",
    "Logger_1_1",
    "ims_1_1",
  );
  
  $found = false;
  foreach($paths as $p)
  {
    //find class
    $tmp = "$bin_path/doc/xml_output/classOpenMS_1_1".$p.$members["classname"].".xml";
    if(file_exists($tmp))
    {
      $class = simplexml_load_file($tmp);
      $found = true;
      break;
    }
    //find struct
    else
    {
      $tmp = "$bin_path/doc/xml_output/structOpenMS_1_1".$p.$members["classname"].".xml";
      if(file_exists($tmp))
      {
        $class = simplexml_load_file($tmp);
        $found = true;
        break;
      }
    }
  }
  if(!$found)
  {
    print "Error: No XML file found for class '".$members["classname"]."'. Aborting!\n";
    return $members;
  }
  
  ######################## parse ###############################
  $classname = substr($class->compounddef->compoundname, 8);
  parseClassInfoFromXML($class, $members, $classname, $debug, FALSE);
  
  ############## inner class/struct check ######################
  if($class->compounddef->innerclass)
  {
    if($debug > 4)
    {
      print "Need to deal with inner classes/structs for class $classname\n";
    }
    foreach($class->compounddef->innerclass as $innerclass)
    {
      if($innerclass["prot"] == "public") 
        // only check public inner classes
        {
          if($debug > 4)
          {
            print "Handling $innerclass\n";
        }
        $filename = $innerclass["refid"];
        $tmp = "$bin_path/doc/xml_output/".$filename.".xml";
        if(file_exists($tmp))
        {
          if($debug > 4)
          {
            print "INFO: Load inner class data from ".$tmp."\n";
          }
          $innerclass_xml = simplexml_load_file($tmp);
          
          # parse necessary information
          $inner_classname = substr($innerclass_xml->compounddef->compoundname, 8);
          parseClassInfoFromXML($innerclass_xml, $members, $inner_classname, $debug, TRUE);
        }
        elseif($debug > 4) 
          // just for debugging
          {
            print "WARNING: Load inner class data from ".$tmp." failed\n";
        }
      }
    }
  }
  if($debug > 4)
  {
    var_dump($members);
  }
  
  return $members;
}

/// Load information about the tested methods
function parseTestFile($filename) {
  $tests      = array();
  $todo_tests = array();
  $in_test    = false;
  $last_test  = "";
  #load file
  $tmp = file($filename);
  
  foreach($tmp as $line)
  {
    $line = trim($line);
    if(beginsWith($line, "START_SECTION(") || beginsWith($line, "START_SECTION (") || beginsWith($line, "START_SECTION	("))
    {
      # strip brackets
      $function = trim(substr($line, 13));
      //print "Function: $function \n";
      while($function[0] == '(' && $function[strlen($function)-1] == ')')
      {
        $function = trim(substr($function, 1,-1));
      }
      # ignore OPENMS_DLLAPI
      $function = str_replace("OPENMS_DLLAPI ", "", $function);
      
      # ignore extra function tests
      if(!beginsWith($function, "[EXTRA]"))
      {
        $tests[] = $function;
        $last_test = $function;
      }
      $in_test = true;
    }
    elseif(beginsWith($line, "END_SECTION"))
    {
      $in_test = false;
    }
    elseif($in_test)
    {
      if(strpos($line, "TODO") !== FALSE || strpos($line, "????") !== FALSE)
      {
        $todo_tests[] = $function;
      }
    }
  }
  return array("todo" => $todo_tests, "tests" => $tests);
}

/// Comares declared and tested methods
function compareDeclarationsAndTests($declarations, $tests) {
  # Replacements to use when comparing methods
  $method_replacements = array(
    "std::"    => "",
    "OpenMS::" => "",
    " "        => "",
    "\t"       => "",
    "=0"       => "",
    "throw()"  => "",
    "virtual"  => "",
    "static"   => "",
    "/*"       => "",
    "*/"       => "",
  );
  
  $done = array();
  
  $out = array(
    "missing" => array(),
    "unknown" => array(),
    "double"  => array(),
  );
  
  #make a copy without whitespaces
  $tmp = array();
  foreach($declarations as $m)
  {
    $tmp[] = strtr($m, $method_replacements);
  }
  
  #compare tests and declarations
  foreach($tests as $t)
  {
    $stripped = strtr($t, $method_replacements);
    $pos = array_search($stripped, $tmp);
    if($pos === FALSE)
    {
      if(in_array($stripped, $done))
      {
        $out["double"][] = $t;
      }
      else
      {
        $out["unknown"][] = $t;
      }
    }
    else
    {
      unset($tmp[$pos]);
      $done[] = $stripped;
    }
  }
  
  #report extra tests
  if(count($tmp) != 0)
  {
    foreach($tmp as $t)
    {
      # look up test with spaces
      foreach($declarations as $z)
      {
        if(strtr($z, $method_replacements) == $t)
        {
          $out["missing"][] = $z;
          break;
        }
      }
    }
  }
  return $out;
}

?>