"SfR Fresh" - the SfR Freeware/Shareware Archive

Member "qftp-0.98/hosts.cc" of archive qftp-0.98.tar.gz:


As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
    1 /*
    2  *  qftp
    3  *  Copyright (C) 1997,1998 Peter Strand
    4  *  Distributed under the GNU Pulic Licence
    5  */
    6 
    7 #include <unistd.h>
    8 #include <ctype.h>
    9 
   10 #include "hosts.h"
   11 #include "conf.h"
   12 
   13 
   14 HostEnt *Hosts::Find(char *s)
   15 {
   16 	int i, n;
   17 	n = hosts.size();
   18 	for (i = 0; i < n; i++) {
   19 		if (!strcmp(hosts[i]->Alias(), s))
   20 			return hosts[i];
   21 		if (!strcmp(hosts[i]->Name(), s))
   22 			return hosts[i];
   23 	}
   24 	return NULL;
   25 }
   26 
   27 void Hosts::Add(HostEnt &he)
   28 {
   29 	HostEnt *h = new HostEnt(he);
   30 	hosts.push_back(h);
   31 }
   32 
   33 void Hosts::Del(char *s)
   34 {
   35 	vector<HostEnt *>::iterator iter;
   36 
   37 	iter = hosts.begin();
   38 	while (iter < hosts.end()) {
   39 		if (!strcmp((*iter)->Alias(), s))
   40 			hosts.erase(iter);
   41 		if (!strcmp((*iter)->Name(), s))
   42 			hosts.erase(iter);
   43 	}
   44 	delete *iter;
   45 }
   46 
   47 void Hosts::Parse(int fd, Conf &conf)
   48 {
   49 	HostEnt hent;
   50 
   51 	char buf[10000];
   52 	char *f[10];
   53 	char *p, *c;
   54 	long s;
   55 
   56 	s = read(fd, buf, 10000);
   57 	p = buf;
   58 	do {
   59 		p = parseline(p, f);
   60 		if (f[0] && f[1] && f[2] && f[3] && f[4]) {
   61 			hent.Set(f[0], f[1], f[2], f[3], f[4]);
   62 			Add(hent);
   63 		} else if (f[0] && f[0][0] == '-') {
   64 			c = f[0];
   65 			while (*c)
   66 				conf.set(*c++, f[1]);
   67 		}
   68 	} while ((p - buf) < s);
   69 }
   70 
   71 
   72 char *Hosts::parseline(char *buf, char *f[])
   73 {
   74         char *p;
   75         int i = 0;
   76 
   77 	p = buf;
   78 	if (*p == '#') {
   79 		while (*p++ != '\n');
   80 	} else {
   81 		f[i++] = p;
   82 		do {
   83 			while (!isspace(*p))
   84 				p++;
   85 			if (*p == '\n')
   86 				break;
   87 			*p++ = 0;
   88 			while (isspace(*p))
   89 				p++;
   90 			f[i++] = p;
   91 		} while (*p);
   92 		*p = 0;
   93 	}
   94         while (i < 10)
   95                 f[i++] = 0;
   96 	return p + 1;
   97 }
   98 
   99 
  100 HostEnt::HostEnt()
  101 {
  102 	alias = name = user = password = wd = NULL;
  103 }
  104 
  105 HostEnt::HostEnt(HostEnt &he)
  106 {
  107 	Alias(he.Alias());
  108 	Name(he.Name());
  109 	User(he.User());
  110 	Password(he.Password());
  111 	Wd(he.Wd());
  112 }
  113 
  114 HostEnt::~HostEnt()
  115 {
  116 	if (alias)
  117 		free(alias);
  118 	if (name)
  119 		free(name);
  120 	if (user)
  121 		free(user);
  122 	if (password)
  123 		free(password);
  124 	if (wd)
  125 		free(wd);
  126 }
  127 
  128 void HostEnt::Set(char *a, char *n, char *u, char *p, char *w)
  129 {
  130 	Alias(a);
  131 	Name(n);
  132 	User(u);
  133 	Password(p);
  134 	Wd(w);
  135 }
  136 
  137 void HostEnt::mkstr(char **s, char *n)
  138 {
  139 	if (n) {
  140 		if (*n == '-')
  141 			n = NULL;
  142 		else
  143 			*s = strdup(n);
  144 	}
  145 }
  146 
  147 char *HostEnt::Alias(char *s = NULL)
  148 {
  149 	mkstr(&alias, s);
  150 	return alias;
  151 }
  152 
  153 char *HostEnt::Name(char *s = NULL)
  154 {
  155 	mkstr(&name, s);
  156 	return name;
  157 }
  158 
  159 char *HostEnt::User(char *s = NULL)
  160 {
  161 	mkstr(&user, s);
  162 	return user;
  163 }
  164 
  165 char *HostEnt::Password(char *s = NULL)
  166 {
  167 	mkstr(&password, s);
  168 	return password;
  169 }
  170 
  171 char *HostEnt::Wd(char *s = NULL)
  172 {
  173 	mkstr(&wd, s);
  174 	return wd;
  175 }