File: myx_ser_aux_functions.c

package info (click to toggle)
mysql-query-browser 1.2.5beta-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 63,792 kB
  • ctags: 46,485
  • sloc: pascal: 249,299; ansic: 80,111; cpp: 72,467; sh: 25,271; objc: 20,015; yacc: 10,755; java: 9,917; xml: 4,580; php: 2,806; python: 1,566; sql: 1,563; makefile: 1,452; perl: 3
file content (110 lines) | stat: -rw-r--r-- 2,571 bytes parent folder | download | duplicates (5)
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
/* Copyright (C) 2004 MySQL AB

   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#include "myx_ser_aux_functions.h"
#include <glib.h>
#include <string.h>
#include <stdio.h>
#include <sys/socket.h>

struct _MYX_SFD
{
  int fd;
  char *in_buffer;
  size_t in_buffer_used;
  size_t in_buffer_size;
};

int ser_string(MYX_SFD *fd, const char *s)
{
  return send(fd->fd, s, strlen(s)+1, 0);
}

int ser_int(MYX_SFD *fd, int value)
{
  char buffer[32];
  g_snprintf(buffer, sizeof(buffer), "%i", value);
  return send(fd->fd, buffer, strlen(buffer)+1, 0);
}


int unser_string(MYX_SFD *fd, char **s)
{
  char *ptr;
  int rc;

  if (!fd->in_buffer)
  {
    fd->in_buffer_size= 1024;
    fd->in_buffer_used= 0;
    fd->in_buffer= g_malloc(fd->in_buffer_size);
  }

  while (fd->in_buffer_used==0 || (ptr= memchr(fd->in_buffer, 0, fd->in_buffer_used))==NULL)
  {
    if (fd->in_buffer_used > fd->in_buffer_size - 128)
    {
      fd->in_buffer_size+= 1024;
      fd->in_buffer= g_realloc(fd->in_buffer, fd->in_buffer_size);
    }
    
    rc= recv(fd->fd, fd->in_buffer+fd->in_buffer_used, fd->in_buffer_size-fd->in_buffer_used, 0);
    if (rc <= 0)
      return rc;
    fd->in_buffer_used+= rc;
  }
  ptr++;

  *s= g_strdup(fd->in_buffer);
  memmove(fd->in_buffer, ptr,
          fd->in_buffer_used-(ptr-fd->in_buffer));
  fd->in_buffer_used-= (ptr-fd->in_buffer);

  return ptr-fd->in_buffer;
}


int unser_int(MYX_SFD *fd, int *value)
{
  char *tmp;
  int rc;
  
  rc= unser_string(fd, &tmp);
  if (rc > 0)
  {
    sscanf(tmp, "%i", value);
    g_free(tmp);
  }
  return rc;
}



MYX_SFD *myx_sfd_from_fd(int fd)
{
  MYX_SFD *sfd= g_malloc0(sizeof(MYX_SFD));
  sfd->fd= fd;
  return sfd;
}


int myx_free_sfd(MYX_SFD *sfd)
{
  if (sfd->in_buffer)
    g_free(sfd->in_buffer);
  g_free(sfd);
  return 0;
}