www.pudn.com > Product_Submit2004.rar > CreateDudeList.cc, change:2004-04-22,size:2131b


 
#include <iostream> 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <dirent.h> 
#include <string.h> 
 
using namespace std; 
 
void GetDudeNames(char* path) {  
  char* dudes[1024]; 
 
  DIR* d; 
  struct dirent* pt_element; 
  struct stat stat_buffer; 
 
  int dudeIndex = 0; 
 
 
  /* Open the directory stream as specified */ 
  if((d = opendir(path)) == NULL) { 
    cout << "Specified path (" << path << ") could not be opened." << endl << flush; 
    return; 
  } 
 
  FILE* outfile = fopen("DUDES/DudeList.h", "wb"); 
 
  while((pt_element = readdir(d)) != NULL) { 
    /* Get information about the device */ 
    lstat(pt_element->d_name, &stat_buffer); 
    int len = strlen(pt_element->d_name); 
    if (pt_element->d_name[len-1] == 'h' && pt_element->d_name[len-2] == '.') { 
      char* str = (char*)malloc(len-1); 
      for (int i = 0; i < len-2; i++) { 
        str[i]=pt_element->d_name[i]; 
      } 
      str[len-2] = '\0'; 
      dudes[dudeIndex++] = str; 
      
    } 
  } 
  fprintf(outfile,"#ifndef _DUDELIST_H_DEFINED\n#define _DUDELIST_H_DEFINED\n\n"); 
  for (int i = 0; i < dudeIndex; i++) { 
    fprintf(outfile,"#include \"%s.h\"\n",dudes[i]); 
  } 
  fprintf(outfile,"\n"); 
  fprintf(outfile,"Dude* CreateDude(char* dudeName) {\n"); 
  printf("Preprocessing dudes:\n"); 
 
  for (int i = 0; i < dudeIndex; i++) { 
    if (strcmp("DudeList",dudes[i]) == 0) continue; 
    printf("  -%s\n",dudes[i]); 
    if (i == 0) { 
      fprintf(outfile,"  if (strcmp(dudeName,\"%s\")==0) {\n",dudes[i]); 
    } else { 
      fprintf(outfile," else if (strcmp(dudeName,\"%s\")==0) {\n",dudes[i]); 
    } 
    fprintf(outfile,"    return new %s();\n",dudes[i]); 
    fprintf(outfile,"  }"); 
  } 
 
  if (dudeIndex == 0) { 
    fprintf(outfile,"  return NULL;"); 
    fprintf(outfile,"}\n"); 
  } else { 
    fprintf(outfile,"\n  return NULL;"); 
    fprintf(outfile,"\n}\n"); 
  } 
  fprintf(outfile,"\n#endif\n\n"); 
  closedir(d); 
  fclose(outfile); 
 
} 
 
 
int main(int argc, char* argv[]) { 
  GetDudeNames("DUDES"); 
  exit(0); 
}