Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2005 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 // Handy utility functions and portability code.
     19 //
     20 #ifndef _LIBS_UTILS_MISC_H
     21 #define _LIBS_UTILS_MISC_H
     22 
     23 #include <sys/time.h>
     24 #include <utils/Endian.h>
     25 
     26 namespace android {
     27 
     28 /* get #of elements in a static array */
     29 #ifndef NELEM
     30 # define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
     31 #endif
     32 
     33 /*
     34  * Make a copy of the string, using "new[]" instead of "malloc".  Free the
     35  * string with delete[].
     36  *
     37  * Returns NULL if "str" is NULL.
     38  */
     39 char* strdupNew(const char* str);
     40 
     41 /*
     42  * Concatenate an argument vector into a single string.  If argc is >= 0
     43  * it will be used; if it's < 0 then the last element in the arg vector
     44  * must be NULL.
     45  *
     46  * This inserts a space between each argument.
     47  *
     48  * This does not automatically add double quotes around arguments with
     49  * spaces in them.  This practice is necessary for Win32, because Win32's
     50  * CreateProcess call is stupid.
     51  *
     52  * The caller should delete[] the returned string.
     53  */
     54 char* concatArgv(int argc, const char* const argv[]);
     55 
     56 /*
     57  * Count up the number of arguments in "argv".  The count does not include
     58  * the final NULL entry.
     59  */
     60 int countArgv(const char* const argv[]);
     61 
     62 /*
     63  * Some utility functions for working with files.  These could be made
     64  * part of a "File" class.
     65  */
     66 typedef enum FileType {
     67     kFileTypeUnknown = 0,
     68     kFileTypeNonexistent,       // i.e. ENOENT
     69     kFileTypeRegular,
     70     kFileTypeDirectory,
     71     kFileTypeCharDev,
     72     kFileTypeBlockDev,
     73     kFileTypeFifo,
     74     kFileTypeSymlink,
     75     kFileTypeSocket,
     76 } FileType;
     77 /* get the file's type; follows symlinks */
     78 FileType getFileType(const char* fileName);
     79 /* get the file's modification date; returns -1 w/errno set on failure */
     80 time_t getFileModDate(const char* fileName);
     81 
     82 /*
     83  * Round up to the nearest power of 2.  Handy for hash tables.
     84  */
     85 unsigned int roundUpPower2(unsigned int val);
     86 
     87 void strreverse(char* begin, char* end);
     88 void k_itoa(int value, char* str, int base);
     89 char* itoa(int val, int base);
     90 
     91 }; // namespace android
     92 
     93 #endif // _LIBS_UTILS_MISC_H
     94