home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / nsstrseq.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.8 KB  |  212 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. //
  19. //  This is a string sequence handling routine to take complex
  20. //  structures and merge them into a chunk of memory.
  21. //
  22. //  Written by: Rich Pizzarro (rhp@netscape.com)
  23. //  November 1997
  24. //
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <windows.h>
  28. #include <windowsx.h>
  29.  
  30. #include "nsstrseq.h"
  31.  
  32. #ifndef NULL
  33. #define NULL '\0'
  34. #endif
  35.  
  36. #define MARKER '\377'
  37.  
  38. //
  39. // Delete an existing string sequence
  40. //
  41. void NSStrSeqDelete(NSstringSeq seq)
  42. {
  43.   if (seq != NULL)
  44.     free(seq);
  45.   
  46.   seq = NULL;
  47. }
  48.  
  49. //
  50. // Allocate a new sequence, copying the given strings into it. 
  51. //
  52. NSstringSeq NSStrSeqNew(LPSTR strings[])
  53. {
  54.   int size;
  55.   if (!strings)
  56.   {
  57.     return NULL;
  58.   }
  59.   
  60.   {
  61.     int i;
  62.     for (i=0,size=0; strings[i]; i++)
  63.     {
  64.       size+=strlen(strings[i])+1;
  65.       switch (strings[i][0])
  66.       {
  67.         // Need to pad "" or anything starting with 255 
  68.         // to allow for multiple blank strings in a row
  69.       case 0:
  70.       case MARKER:
  71.         size++;
  72.         break;
  73.         
  74.       default:
  75.         break;
  76.       }
  77.     }
  78.   }
  79.   
  80.   {
  81.     NSstringSeq s=(NSstringSeq)malloc(size+1);
  82.     if (!s)
  83.     {    return NULL;}
  84.     
  85.     {
  86.       int i,offset;
  87.       for (i=0,offset=0; strings[i]; i++)
  88.       {
  89.         switch (strings[i][0])
  90.         {
  91.           // Need to pad "" or anything starting with 255
  92.         case 0:
  93.         case MARKER:
  94.           s[offset++]=MARKER;
  95.           break;
  96.           
  97.         default:
  98.           break;
  99.         }
  100.         strcpy(s+offset,strings[i]);
  101.         offset+=strlen(strings[i])+1;
  102.       }
  103.       s[offset]=0;
  104.     }
  105.     
  106.     return s;
  107.   }
  108. }
  109.  
  110. //
  111. // Get the # of bytes required for the sequence 
  112. //
  113. LONG NSStrSeqSize(NSstringSeq seq)
  114. {
  115.   const char* s;
  116.   if (!seq)
  117.     {
  118.       return -1;
  119.     }
  120.  
  121.   for (s=seq+1; ((*s) || (*(s-1))); s++)
  122.     ;
  123.  
  124.   // At this point, s points to the second 0 
  125.   // of the double 0 at the end 
  126.   return (s-seq)+1;
  127. }
  128.  
  129. //
  130. // Get the # of strings in the sequence 
  131. //
  132. LONG NSStrSeqNumStrs(NSstringSeq seq)
  133. {
  134.   const char* s;
  135.   int N;
  136.   if (!seq)
  137.   {
  138.     return -1;
  139.   }
  140.   
  141.   for (s=seq+1,N=0; ((*s) || (*(s-1))); s++)
  142.   {
  143.     if (!(*s))
  144.       N++;
  145.   }
  146.   
  147.   return N;
  148. }
  149.  
  150. static LPSTR correct(LPSTR s)
  151. {
  152.   if (s[0]==MARKER)
  153.     return s+1;
  154.   else  // Anup , 4/96
  155.     return s;
  156. }
  157.  
  158. //
  159. // Extract the index'th string in the sequence
  160. //
  161. LPSTR NSStrSeqGet(NSstringSeq seq, LONG index)
  162. {
  163.   char* s;
  164.   int N;
  165.  
  166.   if (!seq)
  167.   {
  168.     return NULL;
  169.   }
  170.   
  171.   if (index<0)
  172.   {
  173.     return NULL;
  174.   }
  175.   
  176.   if (!index)
  177.     return correct(seq);
  178.   
  179.   for (s=seq+1,N=0; ((*s) || (*(s-1))) && (N<index); s++)
  180.   {
  181.     if (!(*s))
  182.       N++;
  183.   }
  184.   
  185.   if (N==index)
  186.     return correct(s);
  187.   
  188.   return NULL;
  189. }
  190.  
  191. LPSTR * NSStrSeqGetAll(NSstringSeq seq)
  192. {
  193.   LONG N=NSStrSeqNumStrs(seq);
  194.   if (N<0)
  195.     return NULL;
  196.   
  197.   {
  198.     char** res=(char**)malloc( (size_t) ((N+1)*sizeof(char*)) );
  199.     int i;
  200.     
  201.     if (!res)
  202.     {
  203.       return NULL;
  204.     }
  205.     for (i=0; i<N; i++)
  206.       res[i]=NSStrSeqGet(seq,i);
  207.     res[N]=NULL;
  208.     
  209.     return res;
  210.   }
  211. }
  212.