summaryrefslogtreecommitdiffstats
path: root/ksystemlog/src/parsingHelper.cpp
blob: ed15c49bdbff695ac092f605ba44035735a2371e (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
/***************************************************************************
 *   Copyright (C) 2005 by Nicolas Ternisien                               *
 *   nicolas.ternisien@gmail.com                                           *
 *                                                                         *
 *   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.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include <klocale.h>

#include "parsingHelper.h"

TQMap<TQString, int> ParsingHelper::mapMonths;

TQMap<TQString, TQString> ParsingHelper::mapHTTPResponse;

TQDateTime ParsingHelper::parseDateTimeFromHTTP(TQString& logLine) {

	//Format example : [22/May/2005:01:50:34 +0200]
		
	TQString day=logLine.mid(1,2);
	TQString month=logLine.mid(4,3);
	TQString year=logLine.mid(8,4);
	
	TQDate date(year.toInt(), ParsingHelper::parseMonthNumber(month), day.toInt());
	
	TQString hour=logLine.mid(13,2);
	TQString min=logLine.mid(16,2);
	TQString sec=logLine.mid(19,2);

	TQTime time(hour.toInt(), min.toInt(), sec.toInt());
	
	//TQString zone=logLine.mid(22,5);
	
	return(TQDateTime(date, time));
}


/**
 * Improve speed of this method with a static Map in the class
 */
int ParsingHelper::parseMonthNumber(TQString& string) {
	//If this is the first time we call this method, we instanciate the map
	if (ParsingHelper::mapMonths.isEmpty()) {
		ParsingHelper::mapMonths["Jan"]=1;
		ParsingHelper::mapMonths["Feb"]=2;
		ParsingHelper::mapMonths["Mar"]=3;
		ParsingHelper::mapMonths["Apr"]=4;
		ParsingHelper::mapMonths["May"]=5;
		ParsingHelper::mapMonths["Jun"]=6;
		ParsingHelper::mapMonths["Jul"]=7;
		ParsingHelper::mapMonths["Aug"]=8;
		ParsingHelper::mapMonths["Sep"]=9;
		ParsingHelper::mapMonths["Oct"]=10;
		ParsingHelper::mapMonths["Nov"]=11;
		ParsingHelper::mapMonths["Dec"]=12;
	}
	
	TQMap<TQString, int>::Iterator it;
	
	//Search the month name in the map
	it=ParsingHelper::mapMonths.find(string);
	
	if (it!=ParsingHelper::mapMonths.end())
		return(*it);
	else
		return(1);
	
}

TQString ParsingHelper::parseSize(const TQString& stringSize) {
	long size=stringSize.toLong();
	
	if (size<1024)
		return(i18n("Size format", "%1 B").tqarg(size));
	else if (size<1024*1024) {
		double newSize=size / 1024.;
		TQString strNewSize;
		strNewSize.sprintf("%0.2f", newSize);
		return(i18n("Size format", "%1 KB").tqarg(strNewSize));
	}
	else {
		double newSize=size / (1024.*1024.);
		TQString strNewSize;
		strNewSize.sprintf("%0.2f", newSize);
		return(i18n("Size format", "%1 MB").tqarg(strNewSize));
	}
}

TQString ParsingHelper::parseHTTPResponse(const TQString& response) {
	if (ParsingHelper::mapHTTPResponse.isEmpty()) {
		//1xx Responses
		mapHTTPResponse["100"]="Continue";
		mapHTTPResponse["101"]="Switching Protocols";
	
		//2xx Responses
		mapHTTPResponse["200"]="OK";
		mapHTTPResponse["201"]="Created";
		mapHTTPResponse["202"]="Accepted";
		mapHTTPResponse["203"]="Non-Authoritative Information";
		mapHTTPResponse["204"]="No Content";
		mapHTTPResponse["205"]="Reset Content";
		mapHTTPResponse["206"]="Partial Content";
	
		//3xx Responses
		mapHTTPResponse["300"]="OK";
		mapHTTPResponse["301"]="Moved Permanently";
		mapHTTPResponse["302"]="Found";
		mapHTTPResponse["303"]="See Other";
		mapHTTPResponse["304"]="Not Modified";
		mapHTTPResponse["305"]="Use Proxy";
		mapHTTPResponse["306"]="(Unused)";
		mapHTTPResponse["307"]="Temporary Redirect";
		
		//4xx Responses
		mapHTTPResponse["400"]="Bad Request";
		mapHTTPResponse["401"]="Unauthorized";
		mapHTTPResponse["402"]="Payment Required";
		mapHTTPResponse["403"]="Forbidden";
		mapHTTPResponse["404"]="Not Found";
		mapHTTPResponse["405"]="Method Not Allowed";
		mapHTTPResponse["406"]="Not Acceptable";
		mapHTTPResponse["407"]="Proxy Authentication Required";
		mapHTTPResponse["408"]="Request Timeout";
		mapHTTPResponse["409"]="Conflict";
		mapHTTPResponse["410"]="Gone";
		mapHTTPResponse["411"]="Length Required";
		mapHTTPResponse["412"]="Precondition Failed";
		mapHTTPResponse["413"]="Request Entity Too Large";
		mapHTTPResponse["414"]="Request-URI Too Long";
		mapHTTPResponse["415"]="Unsupported Media Type";
		mapHTTPResponse["416"]="Requested Range Not Satisfiable";
		mapHTTPResponse["417"]="Expectation Failed";
		
		//5xx Responses
		mapHTTPResponse["500"]="Internal Server Error";
		mapHTTPResponse["501"]="Not Implemented";
		mapHTTPResponse["502"]="Bad Gateway";
		mapHTTPResponse["503"]="Service Unavailable";
		mapHTTPResponse["504"]="Gateway Timeout";
		mapHTTPResponse["505"]="HTTP Version Not Supported";
	}
	
	//Search the response string in the map
	TQMap<TQString, TQString>::Iterator it=ParsingHelper::mapHTTPResponse.find(response);
	if (it!=ParsingHelper::mapHTTPResponse.end())
		return(TQString("%1 %2").tqarg(response).tqarg(*it));
	else
		return(response);

}

TQString ParsingHelper::parseAgent(const TQString& agent) {
	return(agent);
}