Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

strpool.C

Go to the documentation of this file.
00001 #include "support.H"
00002 
00003 #include <cctype>
00004 
00005 #ifndef WIN32
00006 #include <dirent.h>
00007 #include <sys/stat.h>
00008 #endif
00009 
00010 // Static constructors can be the spawn of satan.  We don't know when
00011 // str_ptr::null will be referenced before it has been initialized, so
00012 // we want to make sure that we protect against this by using
00013 // null_str()
00014 
00015 // useful string constants:
00016 STR         *STR::null = 0;
00017 Cstr_ptr str_ptr::null(str_ptr::null_str());
00018 
00019 // A string pool is just a hash table
00020 HASH *STR::strpool = 0;
00021 
00022 extern "C"
00023 int
00024 compare_words(const void *a, const void *b)
00025 {
00026    str_ptr m1 = *((str_ptr *)a);
00027    str_ptr m2 = *((str_ptr *)b);
00028    return strcmp(**m1, **m2);
00029 }
00030 
00031 //
00032 // Gets a list of files in a certain directory
00033 //
00034 str_list dir_list(Cstr_ptr &path)
00035 {
00036    str_list list;
00037 #ifdef WIN32
00038    WIN32_FIND_DATA file;
00039    HANDLE hFile;
00040    
00041    Cstr_ptr dot(".");
00042    Cstr_ptr dotdot("..");
00043    str_ptr current = path+"*";
00044    
00045    if ((hFile = FindFirstFile(**current, &file)) != INVALID_HANDLE_VALUE) 
00046    {
00047       while (1) 
00048       {
00049          str_ptr fname(file.cFileName);
00050          if ((fname != dot) && (fname != dotdot)) list += fname;
00051          if (!FindNextFile(hFile, &file)) break;
00052       }
00053       FindClose(hFile);
00054    }
00055 #else
00056    DIR *dir = 0;
00057    struct dirent *direntry;
00058    if (!!path && (dir =opendir(**path))) 
00059    {
00060       static Cstr_ptr dot(".");
00061       static Cstr_ptr dotdot("..");
00062       struct stat statbuf;
00063       while ((direntry= readdir(dir))) 
00064       {
00065            str_ptr file(direntry->d_name);
00066             if (file != dot && file != dotdot) 
00067             {
00068               str_ptr path_to_file = path + "/" + file;
00069               if (!stat(**path_to_file, &statbuf) && (statbuf.st_mode & S_IFMT) == S_IFREG) 
00070               {
00071                  list += file;
00072               }
00073            }
00074       }
00075       closedir(dir);
00076    }
00077 #endif
00078    list.sort(compare_words);
00079    return list;
00080 }
00081 
00082 str_list
00083 tokenize(
00084    Cstr_ptr &string,
00085    char      delim
00086    )
00087 {
00088    str_list tokens;
00089    if (string  == NULL_STR) return tokens;
00090    
00091    char *buff = new char[strlen(**string) + 1];
00092    strcpy(buff, **string);
00093    char delimstr[2];
00094    delimstr[0] = delim;
00095    delimstr[1] = '\0';
00096    char *item = strtok(buff, delimstr);
00097    if (item) {
00098       tokens += str_ptr(item);
00099       while ((item = strtok(0, delimstr))) {
00100          tokens += str_ptr(item);
00101       }
00102    }
00103    delete [] buff;
00104    return tokens;
00105 }
00106 
00107 str_ptr
00108 str_ptr::to_upper() const
00109 {
00110    if ((*this) == NULL_STR) return NULL_STR;
00111    char buff[1024];
00112    strcpy(buff, **p_);
00113    const int len = strlen(buff);
00114    for (int i = 0; i < len; i++) {
00115       buff[i] = toupper(buff[i]);
00116    }
00117    return str_ptr(buff);
00118 }
00119 
00120 str_ptr
00121 str_ptr::to_lower() const
00122 {
00123    if ((*this) == NULL_STR) return NULL_STR;
00124    char buff[1024];
00125    strcpy(buff, **p_);
00126    const int len = strlen(buff);
00127    for (int i = 0; i < len; i++) {
00128       buff[i] = tolower(buff[i]);
00129    }
00130    return str_ptr(buff);
00131 }
00132 
00133 
00134 // Does this string contain any of the strings in str_list s?
00135 bool
00136 str_ptr::contains(Cstr_list &s) const
00137 {
00138    for (int i = 0; i < s.num(); i++) {
00139       if (contains(s[i])) return 1;
00140    }
00141    return 0;
00142 }

Generated on Mon Sep 18 11:39:33 2006 for jot by  doxygen 1.4.4