~mvo/apt/mvo

« back to all changes in this revision

Viewing changes to apt-pkg/contrib/strutl.cc

  • Committer: Arch Librarian
  • Date: 2004-09-20 16:56:32 UTC
  • Revision ID: Arch-1:apt@arch.ubuntu.com%apt--MAIN--0--patch-614
Join with aliencode
Author: jgg
Date: 2001-02-20 07:03:16 GMT
Join with aliencode

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// -*- mode: cpp; mode: fold -*-
2
2
// Description                                                          /*{{{*/
3
 
// $Id: strutl.cc,v 1.34 2000/01/16 05:36:17 jgg Exp $
 
3
// $Id: strutl.cc,v 1.35 2001/02/20 07:03:17 jgg Exp $
4
4
/* ######################################################################
5
5
 
6
 
   String Util - Some usefull string functions.
 
6
   String Util - Some useful string functions.
7
7
 
8
 
   These have been collected from here and there to do all sorts of usefull
9
 
   things to strings. They are usefull in file parsers, URI handlers and
 
8
   These have been collected from here and there to do all sorts of useful
 
9
   things to strings. They are useful in file parsers, URI handlers and
10
10
   especially in APT methods.   
11
11
   
12
12
   This source is placed in the Public Domain, do with it what you will
21
21
 
22
22
#include <apt-pkg/strutl.h>
23
23
#include <apt-pkg/fileutl.h>
 
24
#include <apt-pkg/error.h>
24
25
 
 
26
#include <apti18n.h>
 
27
    
25
28
#include <ctype.h>
26
29
#include <string.h>
27
30
#include <stdio.h>
28
31
#include <unistd.h>
 
32
#include <regex.h>
29
33
#include <errno.h>
 
34
#include <stdarg.h>
30
35
                                                                        /*}}}*/
31
36
 
32
37
// strstrip - Remove white space from the front and back of a string    /*{{{*/
147
152
                                                                        /*}}}*/
148
153
// ParseCWord - Parses a string like a C "" expression                  /*{{{*/
149
154
// ---------------------------------------------------------------------
150
 
/* This expects a series of space seperated strings enclosed in ""'s. 
 
155
/* This expects a series of space separated strings enclosed in ""'s. 
151
156
   It concatenates the ""'s into a single string. */
152
 
bool ParseCWord(const char *String,string &Res)
 
157
bool ParseCWord(const char *&String,string &Res)
153
158
{
154
159
   // Skip leading whitespace
155
160
   const char *C = String;
180
185
      if (isspace(*C) == 0)
181
186
         return false;
182
187
      *Buf++ = ' ';
183
 
   }   
 
188
   }
184
189
   *Buf = 0;
185
190
   Res = Buffer;
 
191
   String = C;
186
192
   return true;
187
193
}
188
194
                                                                        /*}}}*/
325
331
   
326
332
   return Temp + string(Str,OldPos);
327
333
}
 
334
 
 
335
string SubstVar(string Str,const struct SubstVar *Vars)
 
336
{
 
337
   for (; Vars->Subst != 0; Vars++)
 
338
      Str = SubstVar(Str,Vars->Subst,*Vars->Contents);
 
339
   return Str;
 
340
}
328
341
                                                                        /*}}}*/
329
342
// URItoFileName - Convert the uri into a unique file name              /*{{{*/
330
343
// ---------------------------------------------------------------------
548
561
         return false;
549
562
      
550
563
      // No data
551
 
      if (Res <= 0)
 
564
      if (Res < 0 && errno == EAGAIN)
552
565
         return true;
553
 
 
 
566
      if (Res < 0)
 
567
         return false;
 
568
                              
554
569
      End += Res;
555
570
      
556
571
      // Look for the end of the message
749
764
   return true;
750
765
}
751
766
                                                                        /*}}}*/
 
767
// TokSplitString - Split a string up by a given token                  /*{{{*/
 
768
// ---------------------------------------------------------------------
 
769
/* This is intended to be a faster splitter, it does not use dynamic
 
770
   memories. Input is changed to insert nulls at each token location. */
 
771
bool TokSplitString(char Tok,char *Input,char **List,
 
772
                    unsigned long ListMax)
 
773
{
 
774
   // Strip any leading spaces
 
775
   char *Start = Input;
 
776
   char *Stop = Start + strlen(Start);
 
777
   for (; *Start != 0 && isspace(*Start) != 0; Start++);
 
778
 
 
779
   unsigned long Count = 0;
 
780
   char *Pos = Start;
 
781
   while (Pos != Stop)
 
782
   {
 
783
      // Skip to the next Token
 
784
      for (; Pos != Stop && *Pos != Tok; Pos++);
 
785
      
 
786
      // Back remove spaces
 
787
      char *End = Pos;
 
788
      for (; End > Start && (End[-1] == Tok || isspace(End[-1]) != 0); End--);
 
789
      *End = 0;
 
790
      
 
791
      List[Count++] = Start;
 
792
      if (Count >= ListMax)
 
793
      {
 
794
         List[Count-1] = 0;
 
795
         return false;
 
796
      }
 
797
      
 
798
      // Advance pos
 
799
      for (; Pos != Stop && (*Pos == Tok || isspace(*Pos) != 0 || *Pos == 0); Pos++);
 
800
      Start = Pos;
 
801
   }
 
802
   
 
803
   List[Count] = 0;
 
804
   return true;
 
805
}
 
806
                                                                        /*}}}*/
 
807
// RegexChoice - Simple regex list/list matcher                         /*{{{*/
 
808
// ---------------------------------------------------------------------
 
809
/* */
 
810
unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin,
 
811
                      const char **ListEnd)
 
812
{
 
813
   for (RxChoiceList *R = Rxs; R->Str != 0; R++)
 
814
      R->Hit = false;
 
815
 
 
816
   unsigned long Hits = 0;
 
817
   for (; ListBegin != ListEnd; ListBegin++)
 
818
   {
 
819
      // Check if the name is a regex
 
820
      const char *I;
 
821
      bool Regex = true;
 
822
      for (I = *ListBegin; *I != 0; I++)
 
823
         if (*I == '.' || *I == '?' || *I == '*' || *I == '|')
 
824
            break;
 
825
      if (*I == 0)
 
826
         Regex = false;
 
827
         
 
828
      // Compile the regex pattern
 
829
      regex_t Pattern;
 
830
      if (Regex == true)
 
831
         if (regcomp(&Pattern,*ListBegin,REG_EXTENDED | REG_ICASE |
 
832
                     REG_NOSUB) != 0)
 
833
            Regex = false;
 
834
         
 
835
      // Search the list
 
836
      bool Done = false;
 
837
      for (RxChoiceList *R = Rxs; R->Str != 0; R++)
 
838
      {
 
839
         if (R->Str[0] == 0)
 
840
            continue;
 
841
         
 
842
         if (strcasecmp(R->Str,*ListBegin) != 0)
 
843
         {
 
844
            if (Regex == false)
 
845
               continue;
 
846
            if (regexec(&Pattern,R->Str,0,0,0) != 0)
 
847
               continue;
 
848
         }
 
849
         Done = true;
 
850
         
 
851
         if (R->Hit == false)
 
852
            Hits++;
 
853
         
 
854
         R->Hit = true;
 
855
      }
 
856
      
 
857
      if (Regex == true)
 
858
         regfree(&Pattern);
 
859
      
 
860
      if (Done == false)
 
861
         _error->Warning(_("Selection %s not found"),*ListBegin);
 
862
   }
 
863
   
 
864
   return Hits;
 
865
}
 
866
                                                                        /*}}}*/
 
867
// ioprintf - C format string outputter to C++ iostreams                /*{{{*/
 
868
// ---------------------------------------------------------------------
 
869
/* This is used to make the internationalization strinc easier to translate
 
870
 and to allow reordering of parameters */
 
871
void ioprintf(ostream &out,const char *format,...) 
 
872
{
 
873
   va_list args;
 
874
   va_start(args,format);
 
875
   
 
876
   // sprintf the description
 
877
   char S[400];
 
878
   vsnprintf(S,sizeof(S),format,args);
 
879
   out << S;
 
880
}
 
881
                                                                        /*}}}*/
752
882
 
753
883
// URI::CopyFrom - Copy from an object                                  /*{{{*/
754
884
// ---------------------------------------------------------------------
757
887
{
758
888
   string::const_iterator I = U.begin();
759
889
 
760
 
   // Locate the first colon, this seperates the scheme
 
890
   // Locate the first colon, this separates the scheme
761
891
   for (; I < U.end() && *I != ':' ; I++);
762
892
   string::const_iterator FirstColon = I;
763
893
 
912
1042
   return Res;
913
1043
}
914
1044
                                                                        /*}}}*/
 
1045
// URI::SiteOnly - Return the schema and site for the URI               /*{{{*/
 
1046
// ---------------------------------------------------------------------
 
1047
/* */
 
1048
string URI::SiteOnly(string URI)
 
1049
{
 
1050
   ::URI U(URI);
 
1051
   U.User = string();
 
1052
   U.Password = string();
 
1053
   U.Path = string();
 
1054
   U.Port = 0;
 
1055
   return U;
 
1056
}
 
1057
                                                                        /*}}}*/