Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 //
     18 // Sortable array of strings.  STL-ish, but STL-free.
     19 //
     20 #ifndef _LIBS_UTILS_STRING_ARRAY_H
     21 #define _LIBS_UTILS_STRING_ARRAY_H
     22 
     23 #include <stdlib.h>
     24 #include <string.h>
     25 
     26 namespace android {
     27 
     28 //
     29 // An expanding array of strings.  Add, get, sort, delete.
     30 //
     31 class StringArray {
     32 public:
     33     StringArray();
     34     virtual ~StringArray();
     35 
     36     //
     37     // Add a string.  A copy of the string is made.
     38     //
     39     bool push_back(const char* str);
     40 
     41     //
     42     // Delete an entry.
     43     //
     44     void erase(int idx);
     45 
     46     //
     47     // Sort the array.
     48     //
     49     void sort(int (*compare)(const void*, const void*));
     50 
     51     //
     52     // Pass this to the sort routine to do an ascending alphabetical sort.
     53     //
     54     static int cmpAscendingAlpha(const void* pstr1, const void* pstr2);
     55 
     56     //
     57     // Get the #of items in the array.
     58     //
     59     inline int size(void) const { return mCurrent; }
     60 
     61     //
     62     // Return entry N.
     63     // [should use operator[] here]
     64     //
     65     const char* getEntry(int idx) const {
     66         return (unsigned(idx) >= unsigned(mCurrent)) ? NULL : mArray[idx];
     67     }
     68 
     69     //
     70     // Set entry N to specified string.
     71     // [should use operator[] here]
     72     //
     73     void setEntry(int idx, const char* str);
     74 
     75 private:
     76     int     mMax;
     77     int     mCurrent;
     78     char**  mArray;
     79 };
     80 
     81 }; // namespace android
     82 
     83 #endif // _LIBS_UTILS_STRING_ARRAY_H
     84