File: zsconn.cpp

package info (click to toggle)
mozzemberek 0.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 168 kB
  • ctags: 111
  • sloc: cpp: 686; makefile: 111
file content (196 lines) | stat: -rw-r--r-- 4,301 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
/*
  Copyright (c) 2004, TUBITAK/UEKAE

  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.

  Please read the COPYING file.
# Contributor(s): Rail Aliev <rail@i-rs.ru>
*/

#include <iostream>
#include <sstream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>

#include "memory.h"
#include "stdlib.h"

#include "zsconn.h"

#define DEFAULTHOST "localhost"
#define DEFAULTPORT 10444


ZSConn::ZSConn(){
	host = DEFAULTHOST;
	port = DEFAULTPORT;
	init();
}

ZSConn::ZSConn(char *in_host, int in_port)
{
	host = in_host;
	port = in_port;
	init();
}

void ZSConn::init()
{
    struct hostent *he;
    struct sockaddr_in saddr;

    if ( ( he = (struct hostent *)gethostbyname(host) ) == NULL ) {
	    perror( "gethostbyname()" );
	    return; //segfault if cannot resolve, so return from here
    }

    if ( ( _conn = socket(AF_INET, SOCK_STREAM, 0) ) == -1 ) {
	    perror( "socket()" );
	    return;
    }

    saddr.sin_family = AF_INET;
    saddr.sin_port = htons( (uint16_t)port );
    saddr.sin_addr = *( (struct in_addr *)he->h_addr );
    memset( &(saddr.sin_zero), '\0', 8 );

    if ( connect(_conn, (struct sockaddr *)&saddr, sizeof(struct sockaddr)) == -1) {
        perror("zemberek-server hatası");
	    return;
    }
}

ZSConn::~ZSConn()
{
    if ( _conn ) {
        shutdown( _conn, SHUT_RDWR );
        close( _conn );
    }
}


ZString ZSConn::checkString( const string& str, int offset ) const
{
    ZString zstr( str, offset );

    // pislikleri temizle, bunlar ispell'e gönderilen komutlar.
    // şimdilik işimiz yok bunlarla
    // bir de ^ var ama o kullanılıyor bizim için...
    string flags( "*&@+-~#!%`" );
    string::iterator it = flags.begin();
    string::iterator end = flags.end();
    for ( ; it != end; ++it ) {
        if ( str[0] == *it ) {
            zstr.setStatus( Z_UNKNOWN );
            return zstr;
        }
    }
    zstr.setStatus( spellCheck( zstr.str() ) );
    return zstr;
}

enum Z_CHECK_RESULT ZSConn::spellCheck( const string& str ) const
{
    stringstream strstream;
    strstream << str.length()+2 << " * " << str;
    string checkStr = strstream.str();
    if ( send(_conn, checkStr.c_str(), checkStr.length(), 0) == -1) {
	    perror("zemberek-server hatası");
	    return Z_TRUE;
    }

    switch ( recvResult()[0] ) {
    case '*':
        return Z_TRUE;
        break;
    case '#':
        return Z_FALSE;
        break;
    default:
        return Z_UNKNOWN;
        break;
    }
}

vector<string> ZSConn::getSuggestions(const string& str ) const
{
    stringstream strstream;
    vector<string> suggestions;

    strstream << str.length()+2 << " & " << str;
    string checkStr = strstream.str();
    if ( send( _conn, checkStr.c_str(), checkStr.length(), 0 ) == -1 ) {
	    perror("zemberek-server hatası");
	    return suggestions;
    }

    string result = recvResult();

    if ( result[0] != '&' ) {
        return suggestions;
    }

    string::iterator it = result.begin();
    string::iterator end = result.end();
    bool start = false;
    string tmp;
    for ( ; it != end; ++it ) {
        if ( *it == '(' ) {
            start = true;
            continue;
        }

        if ( !start ) continue;


        if ( *it == ',' ) {
            suggestions.push_back( tmp );
            tmp.erase();
            continue;
        } else if ( *it == ')' ) {
            suggestions.push_back( tmp );
            break;
        }

        tmp += *it;
    }

    return suggestions;
}


string ZSConn::recvResult() const
{
    int numbytes = 0;
    string buf("");

    int size = 0;
    while (true) {
        char s;
        numbytes = recv (_conn, &s, 1, 0);

        // ' ' boşluk karakteri hiç gelmezse???
        if (s == ' ') {
            char *endptr;
            size = strtol (buf.c_str() , &endptr, 0);
            buf.erase();
            break;
        }

        buf += s;
    }
    char *ret = new char[size+1];
    numbytes = recv (_conn, ret, size, 0);
    ret[numbytes]='\0';

    string result = ret;
    delete ret;

    return result;
}