Back to index

lightning-sunbird  0.9+nobinonly
nsstrseq.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 //
00038 //  This is a string sequence handling routine to take complex
00039 //  structures and merge them into a chunk of memory.
00040 //
00041 //  Written by: Rich Pizzarro (rhp@netscape.com)
00042 //  November 1997
00043 //
00044 #include <string.h>
00045 #include <stdlib.h>
00046 #include <windows.h>
00047 #include <windowsx.h>
00048 
00049 #include "nsstrseq.h"
00050 
00051 #ifndef NULL
00052 #define NULL '\0'
00053 #endif
00054 
00055 #define MARKER '\377'
00056 
00057 //
00058 // Delete an existing string sequence
00059 //
00060 void NSStrSeqDelete(NSstringSeq seq)
00061 {
00062   if (seq != NULL)
00063     free(seq);
00064   
00065   seq = NULL;
00066 }
00067 
00068 //
00069 // Allocate a new sequence, copying the given strings into it. 
00070 //
00071 NSstringSeq NSStrSeqNew(LPSTR strings[])
00072 {
00073   int size;
00074   if (!strings)
00075   {
00076     return NULL;
00077   }
00078   
00079   {
00080     int i;
00081     for (i=0,size=0; strings[i]; i++)
00082     {
00083       size+=strlen(strings[i])+1;
00084       switch (strings[i][0])
00085       {
00086         // Need to pad "" or anything starting with 255 
00087         // to allow for multiple blank strings in a row
00088       case 0:
00089       case MARKER:
00090         size++;
00091         break;
00092         
00093       default:
00094         break;
00095       }
00096     }
00097   }
00098   
00099   {
00100     NSstringSeq s=(NSstringSeq)malloc(size+1);
00101     if (!s)
00102     {  return NULL;}
00103     
00104     {
00105       int i,offset;
00106       for (i=0,offset=0; strings[i]; i++)
00107       {
00108         switch (strings[i][0])
00109         {
00110           // Need to pad "" or anything starting with 255
00111         case 0:
00112         case MARKER:
00113           s[offset++]=MARKER;
00114           break;
00115           
00116         default:
00117           break;
00118         }
00119         strcpy(s+offset,strings[i]);
00120         offset+=strlen(strings[i])+1;
00121       }
00122       s[offset]=0;
00123     }
00124     
00125     return s;
00126   }
00127 }
00128 
00129 //
00130 // Get the # of bytes required for the sequence 
00131 //
00132 LONG NSStrSeqSize(NSstringSeq seq)
00133 {
00134   const char* s;
00135   if (!seq)
00136     {
00137       return -1;
00138     }
00139 
00140   for (s=seq+1; ((*s) || (*(s-1))); s++)
00141     ;
00142 
00143   // At this point, s points to the second 0 
00144   // of the double 0 at the end 
00145   return (s-seq)+1;
00146 }
00147 
00148 //
00149 // Get the # of strings in the sequence 
00150 //
00151 LONG NSStrSeqNumStrs(NSstringSeq seq)
00152 {
00153   const char* s;
00154   int N;
00155   if (!seq)
00156   {
00157     return -1;
00158   }
00159   
00160   for (s=seq+1,N=0; ((*s) || (*(s-1))); s++)
00161   {
00162     if (!(*s))
00163       N++;
00164   }
00165   
00166   return N;
00167 }
00168 
00169 static LPSTR correct(LPSTR s)
00170 {
00171   if (s[0]==MARKER)
00172     return s+1;
00173   else  // Anup , 4/96
00174     return s;
00175 }
00176 
00177 //
00178 // Extract the index'th string in the sequence
00179 //
00180 LPSTR NSStrSeqGet(NSstringSeq seq, LONG index)
00181 {
00182   char* s;
00183   int N;
00184 
00185   if (!seq)
00186   {
00187     return NULL;
00188   }
00189   
00190   if (index<0)
00191   {
00192     return NULL;
00193   }
00194   
00195   if (!index)
00196     return correct(seq);
00197   
00198   for (s=seq+1,N=0; ((*s) || (*(s-1))) && (N<index); s++)
00199   {
00200     if (!(*s))
00201       N++;
00202   }
00203   
00204   if (N==index)
00205     return correct(s);
00206   
00207   return NULL;
00208 }
00209 
00210 LPSTR * NSStrSeqGetAll(NSstringSeq seq)
00211 {
00212   LONG N=NSStrSeqNumStrs(seq);
00213   if (N<0)
00214     return NULL;
00215   
00216   {
00217     char** res=(char**)malloc( (size_t) ((N+1)*sizeof(char*)) );
00218     int i;
00219     
00220     if (!res)
00221     {
00222       return NULL;
00223     }
00224     for (i=0; i<N; i++)
00225       res[i]=NSStrSeqGet(seq,i);
00226     res[N]=NULL;
00227     
00228     return res;
00229   }
00230 }