bool GPA::importStudents(string mapFileName, string setFileName) { bool imported = true; // Will be set to false if there is an error // Open the set and map files fstream set_file; fstream map_file; set_file.open(setFileName.c_str()); map_file.open(mapFileName.c_str()); // Check for invalid filenames if (set_file.fail() || map_file.fail()) { imported = false; } // Make sure both files are the right length else if (check_file_length(mapFileName, 4) && check_file_length(setFileName, 4)) { string id, name, address, phone; unsigned long long int id_as_ulli; // Import the map file while (getline(map_file, id) && getline(map_file, name) && getline(map_file, address) && getline(map_file, phone) ) { id_as_ulli = string_to_ulli(id); student_map[id_as_ulli] = new Student(id_as_ulli, name, address, phone); } // Import the set file while (getline(set_file, id) && getline(set_file, name) && getline(set_file, address) && getline(set_file, phone) ) { id_as_ulli = string_to_ulli(id); student_set.insert( new Student(id_as_ulli, name, address, phone) ); } } else { imported = false; } set_file.close(); map_file.close(); return imported; }
bool GPA::importGrades(string fileName) { bool imported = false; fstream grade_file; grade_file.open(fileName.c_str()); // if the file exists if( !grade_file.fail() ) { // Check the file length if (check_file_length(fileName, 3)) { // Read in 1 grade/id/class string id, class_name, grade; while( getline(grade_file, id) && getline(grade_file, class_name) && getline(grade_file, grade)) { // Convert the grade and id double grade_as_double = convert_grade(grade); unsigned long long int id_as_ulli = string_to_ulli(id); // Check for the student in the map for ( pair<unsigned long long int,StudentInterface*> student : student_map ) { if (student.first == id_as_ulli) { student.second->addGPA(grade_as_double); imported = true; } } // Check for the student in the set for ( StudentInterface* student : student_set ) { if (student->getID() == id_as_ulli ) { student->addGPA(grade_as_double); imported = true; } } } } } return imported; }
int main(int argc, char** argv) { uint8_t *image_buf; int fd; struct bpb33* bpb; if (argc != 2) { usage(); } image_buf = mmap_file(argv[1], &fd); bpb = check_bootsector(image_buf); int total_clusters = bpb->bpbSectors / bpb->bpbSecPerClust; int clust_size = bpb->bpbSecPerClust * bpb->bpbBytesPerSec; int used_clusters[total_clusters]; check_lost_files(used_clusters, 0, image_buf, bpb); int i; int shownPrefix = 0; for (i = 2; i < total_clusters; i++) { if (used_clusters[i] == 0 && get_fat_entry(i, image_buf, bpb) != CLUST_FREE) { if (!shownPrefix) { printf("Unreferenced:"); shownPrefix = 1; } printf(" %i", i); } if (i == total_clusters - 1 && shownPrefix) { printf("\n"); } } int foundCount = 1; shownPrefix = 0; for (i = 2; i < total_clusters; i++) { if (used_clusters[i] == 0 && get_fat_entry(i, image_buf, bpb) != CLUST_FREE) { if (!shownPrefix) { printf("Lost File: "); } uint16_t size = get_file_length(i, image_buf, bpb); printf("%i %i\n", i, size); struct direntry *dirent = (struct direntry*) cluster_to_addr(0, image_buf, bpb); uint32_t size_bytes = size * clust_size; const char base[] = "found"; const char extension[] = ".dat"; char filename [13]; sprintf(filename, "%s%i%s", base, foundCount++, extension); create_dirent(dirent, filename, i, size_bytes, image_buf, bpb); check_lost_files(used_clusters, 0, image_buf, bpb); } if (i == total_clusters - 1 && shownPrefix) { printf("\n"); } } check_file_length(0, image_buf, bpb); close(fd); exit(0); }
void check_file_length(uint16_t cluster, uint8_t *image_buf, struct bpb33* bpb) { struct direntry *dirent; int d, i; dirent = (struct direntry*) cluster_to_addr(cluster, image_buf, bpb); int clust_size = bpb->bpbBytesPerSec * bpb->bpbSecPerClust; while (1) { for (d = 0; d < clust_size; d += sizeof(struct direntry)) { char name[9]; char extension[4]; uint32_t size; uint16_t file_cluster; name[8] = ' '; extension[3] = ' '; memcpy(name, &(dirent->deName[0]), 8); memcpy(extension, dirent->deExtension, 3); if (name[0] == SLOT_EMPTY) return; /* skip over deleted entries */ if (((uint8_t)name[0]) == SLOT_DELETED) continue; /* names are space padded - remove the spaces */ for (i = 8; i > 0; i--) { if (name[i] == ' ') name[i] = '\0'; else break; } /* remove the spaces from extensions */ for (i = 3; i > 0; i--) { if (extension[i] == ' ') extension[i] = '\0'; else break; } /* don't print "." or ".." directories */ if (strcmp(name, ".") == 0) { dirent++; continue; } if (strcmp(name, "..") == 0) { dirent++; continue; } if ((dirent->deAttributes & ATTR_VOLUME) != 0) { continue; } else if ((dirent->deAttributes & ATTR_DIRECTORY) != 0) { file_cluster = getushort(dirent->deStartCluster); check_file_length(file_cluster, image_buf, bpb); } else { size = getulong(dirent->deFileSize); file_cluster = getushort(dirent->deStartCluster); uint16_t fat_size_clusters = get_file_length(file_cluster, image_buf, bpb); uint32_t size_clusters = (size + (clust_size - 1)) / clust_size; uint32_t fat_size = fat_size_clusters * clust_size; if (size_clusters != fat_size_clusters) { printf("%s.%s %u %u\n", name, extension, size, fat_size); uint16_t begin_cluster = file_cluster + size_clusters - 1; uint16_t end_cluster = file_cluster + fat_size_clusters; free_clusters(begin_cluster, end_cluster, image_buf, bpb); } } dirent++; } /* We've reached the end of the cluster for this directory. Where's the next cluster? */ if (cluster == 0) { // root dir is special dirent++; } else { cluster = get_fat_entry(cluster, image_buf, bpb); dirent = (struct direntry*) cluster_to_addr(cluster, image_buf, bpb); } } }