Blame src/StringPool.cc

Packit Service a2489d
/*
Packit Service a2489d
 * lftp - file transfer program
Packit Service a2489d
 *
Packit Service a2489d
 * Copyright (c) 1996-2012 by Alexander V. Lukyanov (lav@yars.free.net)
Packit Service a2489d
 *
Packit Service a2489d
 * This program is free software; you can redistribute it and/or modify
Packit Service a2489d
 * it under the terms of the GNU General Public License as published by
Packit Service a2489d
 * the Free Software Foundation; either version 3 of the License, or
Packit Service a2489d
 * (at your option) any later version.
Packit Service a2489d
 *
Packit Service a2489d
 * This program is distributed in the hope that it will be useful,
Packit Service a2489d
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service a2489d
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service a2489d
 * GNU General Public License for more details.
Packit Service a2489d
 *
Packit Service a2489d
 * You should have received a copy of the GNU General Public License
Packit Service a2489d
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit Service a2489d
 */
Packit Service a2489d
Packit Service a2489d
#include <config.h>
Packit Service a2489d
#include "StringPool.h"
Packit Service a2489d
#include "xarray.h"
Packit Service a2489d
Packit Service a2489d
xarray_m<char> StringPool::strings;
Packit Service a2489d
Packit Service a2489d
const char *StringPool::Get(const char *s)
Packit Service a2489d
{
Packit Service a2489d
   if(!s)
Packit Service a2489d
      return 0;
Packit Service a2489d
Packit Service a2489d
   int l=0;
Packit Service a2489d
   int u=strings.count();
Packit Service a2489d
Packit Service a2489d
   while(l
Packit Service a2489d
   {
Packit Service a2489d
      int m = (l + u) / 2;
Packit Service a2489d
      int cmp = strcmp(strings[m], s);
Packit Service a2489d
Packit Service a2489d
      if(cmp==0)  // found it.
Packit Service a2489d
	 return strings[m];
Packit Service a2489d
Packit Service a2489d
      if(cmp>0)
Packit Service a2489d
	 u=m;
Packit Service a2489d
      else
Packit Service a2489d
	 l=m+1;
Packit Service a2489d
   }
Packit Service a2489d
Packit Service a2489d
   // not found.
Packit Service a2489d
   // l==u points to first elementh greater than s or past end of array.
Packit Service a2489d
   strings.insert(xstrdup(s),u);
Packit Service a2489d
   return strings[u];
Packit Service a2489d
}