File: Frog-util.cxx

package info (click to toggle)
frog 0.20-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 2,804 kB
  • sloc: cpp: 12,335; sh: 4,282; makefile: 52; ansic: 38
file content (123 lines) | stat: -rw-r--r-- 3,387 bytes parent folder | download | duplicates (2)
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
/* ex: set tabstop=8 expandtab: */
/*
  Copyright (c) 2006 - 2020
  CLST  - Radboud University
  ILK   - Tilburg University

  This file is part of frog:

  A Tagger-Lemmatizer-Morphological-Analyzer-Dependency-Parser for
  several languages

  frog 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 3 of the License, or
  (at your option) any later version.

  frog 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, see <http://www.gnu.org/licenses/>.

  For questions and suggestions, see:
      https://github.com/LanguageMachines/frog/issues
  or send mail to:
      lamasoftware (at ) science.ru.nl

*/

#include "frog/Frog-util.h"

#include <cstring>
#include <set>
#include <string>
#include <ostream>
#include "ticcutils/SocketBasics.h"
#include "config.h"

#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif

using namespace std;

string prefix( const string& path, const string& fn ){
  /// add a full path to the filename \e fn
  /*!
    \param path the path to add
    \param fn the filename to be prefixed
    \return a new path
    If \e fn already contains (relative) path information, it is left unchanged.
   */
  if ( fn.find( "/" ) == string::npos && !path.empty() ){
    // only append prefix when it isn't empty AND
    // NO path is specified in the filename
    return path + "/" + fn;
  }
  return fn;
}

#ifdef HAVE_DIRENT_H
set<string> getFileNames( const string& dirName,
			   const string& ext ){
  /// extract a (sorted) list of file-names matching an extension pattern
  /*!
    \param dirName the search directory
    \param ext the file extension we search
    \return a sorted list og file-names
  */
  set<string> result;
  DIR *dir = opendir( dirName.c_str() );
  if ( !dir ){
    return result;
  }
  else {
    struct stat sb;
    struct dirent *entry = readdir( dir );
    while ( entry ){
      if ( entry->d_name[0] != '.' ) {
	string filename = entry->d_name;
	if ( ext.empty() ||
	     filename.rfind( ext ) != string::npos ) {
	  string fullName = dirName + "/" + filename;
	  if ( stat( fullName.c_str(), &sb ) >= 0 ){
            if ( (sb.st_mode & S_IFMT) == S_IFREG ){
	      result.insert( entry->d_name );
	    }
	  }
	}
      }
      entry = readdir( dir );
    }
    closedir( dir );
    return result;
  }
}
#endif

string check_server( const string& host,
		     const string& port,
		     const string& name ){
  /// check if a certain host:port is available for us
  /*!
    \param host the host we check
    \param port the port we want to access
    \param name extra information (used in diagnostics only)
    \return "" on succes or an error message on failure
  */
  string outline;
  Sockets::ClientSocket client;
  if ( !client.connect( host, port ) ){
    outline = "cannot open connection, " + host + ":" + port;
    if ( !name.empty() ){
      outline += " for " + name + " module";
    }
    outline += "\nmessage: (" + client.getMessage() + ")" ;
  }
  return outline;
}