summaryrefslogtreecommitdiffstats
path: root/ksirc/KSOpenkSirc/serverFileParser.cpp
blob: 8e6ba80eea375960c90f17de07bc381d1ce81a59 (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
#include "serverFileParser.h"
#include "serverDataType.h"
#include <qfile.h>
#include <stdlib.h>

#include <kstandarddirs.h>
#include <kdebug.h>

extern QPtrList<Server> Groups;

//  Opens, reads and parses server information from a server file, 
//  sets the information in the global variable and returns 1 if
//  sucessful, takes a filename as an argument.

int serverFileParser::readDatafile( const char *fileName ) 
{
  Groups.setAutoDelete( TRUE );
  Groups.clear();
  QFile serverFile( fileName );
  if ( !serverFile.open( IO_ReadOnly ) )
    return -1;

  QTextStream fileStream(&serverFile);

  // the file is layed out as follows:
  //  service:servername:serveraddress:ports:script:
  // so we parse it this way

  while( !fileStream.eof() ) {
    QString str = fileStream.readLine();
    const char *strC = str.ascii();
    char *token;
    char groupC[1024], servernameC[1024], serveraddressC[1024], portsC[1024];
    int pos = 0;
    QString group;
    QString servername;
    QString serveraddress;
    QPtrList<port> ports;
    QString script;

    QString buf;
    QString portbuff;

    pos = sscanf(strC, "%1023[^:]:%1024[^:]:%1023[^:]:%1023[^:]:", groupC, servernameC, serveraddressC, portsC);
    if(pos != 4){
      kdWarning() << "Failed to parse servers.txt on line: " << strC << ". Invalid format" << endl;
      return 0;
    }
    group = groupC;
    servername = servernameC;
    serveraddress = serveraddressC;
    token = strtok(portsC, ",");
    while(token != NULL){
      ports.inSort(new port(token));
      token = strtok(NULL, ",");
    }
    
    /*
     for( uint loc = 0; loc <= str.length(); loc++ ) {
      if ( str[loc] == ':' || loc == str.length()) {
        switch(pos) {
          case 0: // service
            group = buf.copy();
            break;
          case 1: // server name
            servername = buf.copy();
            break;
          case 2: // server address
            serveraddress = buf.copy();
            break;
          case 3: // port listing
            for ( uint portloc = 0; portloc <= buf.length(); portloc++ ) {
              if (buf[portloc] == ',' || portloc == buf.length()) {
                if (!portbuff.isEmpty())
                  ports.inSort( new port(portbuff));
                portbuff.truncate( 0 );
              } else {
                portbuff += buf[portloc];
              }
            }
            break;
          default: // script
            script = buf.copy();
        }
        pos++;
        buf.truncate( 0 );
        portbuff.truncate( 0 );
      } else {
        buf += str[loc];
      }
      } // for loop
      */
    

    Groups.inSort( new Server(group, serveraddress, ports, 
                              servername, script) );
 
  } // while loop

  serverFile.close();
  return 1;

}

// Writes the data stored in the global variable to the server file,
// returns 1 if sucessful, takes a filename as an argument, uhh NOT.
// we want to write it ~/.kde/share/apps/ksirc/servers.txt
// 

int writeDataFile() 
{
  QString filename(KGlobal::dirs()->saveLocation("appdata")+"servers.txt");
  return 1;
}