Ejemplo n.º 1
0
int main(int argc, char** argv)
{
  printf("hi");
  if (!indexerValidateArgs(argc, argv))
    {
      DICTIONARY* index = buildIndexFromDirectory(argv[1]);
      printf("directory: %s \n", argv[1]);
      printf("filename: %s \n", argv[2]);
      saveIndexToFile(index, argv[2]);
      cleanIndex(index);

      if (argc == 5)
	{
	  //    filename= argv[2];
	  //newFile = argv[argc -1]; 
	  //sprintf(path, "%s/%s", targetDir, filename);
	  index = rebuild(argv[3]);
	  saveIndexToFile(index, argv[4]);
	  cleanIndex(index);
    
	}
      return 0;
    }
  return 1; 
}
Ejemplo n.º 2
0
int main(int argc, char* argv[]) 
{

  //local variables
    HashTable *inverted_index, *test_index;
    char *target_dir, *results_file1, *results_file2, *rewritten_file;
    target_dir = results_file1 = results_file2 = rewritten_file=NULL;

    
  // 1. Program parameter processing
  //./indexer  [TARGET_DIRECTORY] [RESULTS FILENAME1] [RESULTS FILENAME2] [REWRITEN FILENAME]
  //or
  // ./indexer  [TARGET_DIRECTORY] [RESULTS FILENAME]
    if(argc == 3 || argc == 5) {
        target_dir = calloc(strlen(argv[1])+1, sizeof(char));
        if(target_dir == NULL) {
            LOG(stdout,"Error: failed to calloc the variable target_dir\n");
            return 1;
        }
        if( strcpy(target_dir, argv[1]) == NULL) {
            LOG(stdout,"Error: failed to strcpy to the variable target_dir\n");
            return 1;
        }
      //check if the directory is valid
        if(IsDir(target_dir)) {
            printf("%s is a directory\n", target_dir);
        }
        else {
            printf("Error: %s is NOT a directory\n", target_dir);
            free(target_dir);
            return 1;
        }
     
        results_file1 = calloc(strlen(argv[2])+1, sizeof(char));
        if(results_file1 == NULL) {
            LOG(stdout,"Error: failed to calloc the variable results_file1\n");
            return 1;
        }
        
        if( strcpy(results_file1, argv[2]) == NULL) {
            LOG(stdout,"Error: failed to strcpy to the variable results_file1\n");
            return 1;
        }
        
        if(argc == 5) {
            results_file2 = calloc(strlen(argv[3])+1, sizeof(char));
            if(results_file2 == NULL) {
                LOG(stdout,"Error: failed to calloc the varaible results_file2\n");
                free(target_dir);
                free(results_file1);
                return 1;
            }
            
            if( strcpy(results_file2, argv[3]) == NULL) {
                LOG(stdout,"Error: failed to strcpy to variable results_file2\n");
                free(target_dir);
                free(results_file1);
                return 1;
            }
            
            rewritten_file = calloc(strlen(argv[4])+1, sizeof(char));
            if(rewritten_file == NULL) {
                LOG(stdout,"Error: failed to calloc rewritten_file\n");
                free(target_dir);
                free(results_file1);
                free(results_file2);
                return 1;
            }
            
            if( strcpy(rewritten_file, argv[4]) == NULL) {
                LOG(stdout,"Error: failed to strcpy to variable rewritten_file\n");
                free(target_dir);
                free(results_file1);
                free(results_file2);
                return 1;
            }
        }
    }
    else {
      //wrong input parameters, inform the user about it.
        printf("Usage: ./indexer [TARGET_DIRECTORY] [RESULTS FILENAME]\n");
        printf("   or  ./indexer [TARGET_DIRECTORY] [RESULTS FILENAME1] [RESULTS FILENAME2] [REWRITEN FILENAME]\n");
        printf("You have entered %d parameters\n", (argc - 1));
        return 1;
    }
    
    
    
  // 2. Initialize data structures
  // allocate Inverted_index, zero it, and set links to NULL.
    inverted_index = calloc(1, sizeof(HashTable));
    if(inverted_index == NULL) {
        LOG(stdout,"Error: failed to calloc the variable inverted_index\n");
        return 1;
    }
    

  // 3.print to stdout that the building of index is starting
    //printf("\nStarting to build the index\n");
    LOG(stdout,"Starting to build the index\n");

  // 4. Build the inverted index
    if(buildIndexFromDirectory (inverted_index, target_dir)) {
        LOG(stdout, "Error: buildIndexFromDirectory() failed\n");
        return 1;
    }
    LOG(stdout,"Building the inverted index\n");

  // 5. Save the inverted index
    if(!saveFile(inverted_index, results_file1)) {
        LOG(stdout,"Successfully saved the inverted index\n");
    }
    else {
      LOG(stdout, "Error: saveFile() failed\n");
    }
    
    
  // 6. Free appropriate memory
    destroyHash(inverted_index);
    free(target_dir);
    free(results_file1);
    

  // 7. If 4 input parameters are detected
    if(5 == argc) {
        test_index = calloc(1, sizeof(HashTable));
        
        if(test_index == NULL) {
            LOG(stdout, "Error: failed to calloc the variable test_index\n");
            free(results_file2);
            free(rewritten_file);
            destroyHash(test_index);
            return 1;
        }
        
        if(!IsFile(results_file2)) {
            printf("Error: %s is not a file\n", results_file2);
            free(results_file2);
            free(rewritten_file);
            destroyHash(test_index);
            return 1;
        }
        
        
        
  // 8. Inform user that testing has started
        LOG(stdout, "Starting the testing process\n");
  
      
      
  // 9. Reload the index from the file and rewrite it to a new file
        if(readFile(test_index, results_file2)) {
            LOG(stdout,"Error: readFile failed\n");
            free(results_file2);
            free(rewritten_file);
            destroyHash(test_index);
            return 1;
        }
          

  // 10. save the reconstructed index
        if(saveFile(test_index, rewritten_file)) {
            LOG(stdout,"Error: saveFile(test_index, rewritten_file) failed\n");
            free(results_file2);
            free(rewritten_file);
            destroyHash(test_index);
            return 1;
        }
        else {
            LOG(stdout,"Successfully saved the inverted index\n");
        }
        
  // 11. free appropriate memory
        free(results_file2);
        free(rewritten_file);
        destroyHash(test_index);
      
        LOG(stdout,"Test is complete\n");
    }
  
  // 12. Finished
    LOG(stdout,"indexer.c has finished\n");
    return 0;
}
Ejemplo n.º 3
0
int main(int argc, char *argv[]) {

	//Check validity of parameters
	if (argc != 3 && argc != 4) {
		fprintf(stderr, "Usage Error; This program has two methods of use as follows:\n");
		fprintf(stderr, "./indexer [DIRECTORY] [FILENAME]\n");
		fprintf(stderr, "./indexer [DIRECTORY] [FILENAME 1] [FILENAME 2]\n");
		return -1;
	}

	//Check that the directory exists and is readable
   DIR *directory = opendir(argv[1]);
   if(directory == NULL) {  
        if(errno == EACCES) 
            fprintf(stderr, "Error: Directory has incompatible permissions for this program\n");
        else if(errno == ENOENT) 
            fprintf(stderr, "Error: Directory does not exist\n");
        else if (errno == ENOTDIR)
            fprintf(stderr, "Error: Given directory is not a directory\n");
        else 
            fprintf(stderr, "Error: Incompatible directory.\n");
           
        closedir(directory);
        return -1;
    }
    closedir(directory); 

    logfile = fopen("IndexerLogFile", "w");
	
	HashTable *inverted_index = initializeHashTable();
	fprintf(logfile, "Building the Index\n");

	char *dir_name = malloc(strlen(argv[1]) + 1);
	strcpy(dir_name, argv[1]);

	if (buildIndexFromDirectory(dir_name, inverted_index) != 1) {
		fprintf(logfile, "Error: Unable to build index\n");
		fprintf(stderr, "Error: Unable to build index\n");
		free(dir_name);
		fclose(logfile);
		return -1;
	}
	fprintf(logfile, "Index Built!\n");

	if (saveFile(argv[2], inverted_index) != 1){
		fprintf(stderr, "Unable to save the index into %s\n", argv[2]);
		cleanupHashTable(inverted_index);
		free(dir_name);
		fclose(logfile);
		return -1;
	}

	fprintf(logfile, "Index written to %s\n", argv[2]);

	cleanupHashTable(inverted_index);
	free(dir_name);

	//if in testing mode
	if (argc == 4) {
		fprintf(logfile, "Testing the index\n");
		HashTable *reindex = readFile(argv[2]);
		saveFile(argv[3], reindex);
		fprintf(logfile, "Finished Testing\n");
		cleanupHashTable(reindex);
	}
	fclose(logfile);
	return 0;
}