main() {
	srand(time(NULL));
	int error;
	int i;
	int pagenum,*buf;
	int fd;

	/* create a new file to be used as input file */


	if ((error=PF_CreateFile(FILE))!= PFE_OK){
		PF_PrintError("Cannot create file");
		exit(1);
	}
	printf("Input File created\n");

	/* create a temporary file */
	if ((error=PF_CreateFile(TMPFILE))!= PFE_OK){
		PF_PrintError("Cannot create file");
		exit(1);
	}
	printf("Temporary File created\n");

	/* this method creates a sample input file filled with random numbers */
	writefile(FILE);

	copyfiles();		// copy the contents of the input file to a temporary file

	/* this method prints out the sample input file generated above */
	readfile(FILE);
	printf("File read\n");
	printf("Starting the sorting process\n");
	external_sort();
	readfile(FILE);
}
コード例 #2
0
INT_PTR CALLBACK MainDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);

	mdialog = hDlg;

	switch (message)
	{
	case WM_INITDIALOG:
		SetDlgItemText(hDlg, IDC_START_DATE, default_oldestFileDate);
		SetDlgItemText(hDlg, IDC_SEARCH_FOLDER, default_SearchFolder);
		SetDlgItemText(hDlg, IDC_SAVE_FOLDER, default_SaveFolder);
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK)
		{
			char datestr[100];
			char month[3], day[3], year[5];
			int imonth, iday, iyear;
			int shift;

			memset(datestr, 0,20);
			GetDlgItemText(hDlg, IDC_START_DATE, datestr, 100);

			imonth = 0;
			iday = 0;
			iyear = 0;
			strncpy_s (month, 3, datestr, 2);
			imonth = atoi(month);
			shift = 3;
			if (month[1] == '/') shift = 2; 
			strncpy_s (day, 3, datestr + shift, 2);
			iday = atoi(day);
			shift = shift + 3;
			if (day[1] == '/') --shift;
			strncpy_s (year, 5, datestr + shift, 4);
			iyear = atoi(year);
			if (iyear < 50) iyear = iyear + 2000;

			if (imonth < 1 || imonth > 12 || iday < 1 || iday > 31 || iyear < 2000) 
			{
				MessageBox (0, "Invalid Oldest File Date", "INVALID DATE", 0);
				return (INT_PTR) FALSE;
			}

			struct tm fromuser;
			memset (&fromuser, 0, sizeof(fromuser));
			fromuser.tm_year = iyear - 1900;
			fromuser.tm_mday = iday;
			fromuser.tm_mon = imonth - 1;
			BeginTime = mktime(&fromuser);

			time_t now;
			time(&now);
			if (BeginTime > now) 
			{
				MessageBox (0, "Oldest File Date cannot be a future date", "FUTURE DATE", 0);
				return (INT_PTR) FALSE;
			}

			memset(SearchFolder, 0, sizeof(SearchFolder));
			GetDlgItemText(hDlg, IDC_SEARCH_FOLDER, SearchFolder, sizeof(SearchFolder));
			if (!directory_exists(SearchFolder)) 
			{
				MessageBox (0, "Search Folder not Found", "FOLDER NOT FOUND", 0);
				return (INT_PTR) FALSE;
			}

			memset(SaveFolder, 0, sizeof(SaveFolder));
			GetDlgItemText(hDlg, IDC_SAVE_FOLDER, SaveFolder, sizeof(SaveFolder));
			if (!directory_exists(SaveFolder)) 
			{
				MessageBox (0, "Save Folder not Found", "FOLDER NOT FOUND", 0);
				return (INT_PTR) FALSE;
			}

			SetDlgItemText(mdialog, IDC_STATUS, "RUNNING");
			SetDlgItemText(mdialog, IDC_COPY_COUNT, "0");
			copyfiles(SearchFolder, "basename");
			SetDlgItemText(mdialog, IDC_STATUS, "COMPLETE");
			Sleep(3000);

			exit(0);			
		}

		if (LOWORD(wParam) == IDCANCEL)
		{
			exit(0);			
		}
	}
	return (INT_PTR)FALSE;
}
コード例 #3
0
void copyfiles(char *dir, char *dirname) 
{
	char searchdir[MAX_PATH];
	char subdir[MAX_PATH];
	_finddata_t finddata;
	intptr_t handle;
	bool first;

	sprintf_s(searchdir, sizeof(searchdir), "%s\\*.*", dir);

	handle = _findfirst(searchdir, &finddata);
	if (handle == -1)
		return;

	first = true;
	while (true) 
	{
		if (!first) 
		{
			if (_findnext(handle, &finddata) != 0)
				break;
		}
	    first = false;
		if (strcmp(finddata.name, ".") == 0 || strcmp(finddata.name, "..") == 0)
			continue;
		if (finddata.attrib == _A_SUBDIR)
		{
			sprintf_s(subdir, sizeof(subdir), "%s\\%s", dir, finddata.name);
			copyfiles(subdir, finddata.name);
		}
		else if (strncmp(finddata.name, "cal_image", 8) == 0) 
		{
			int image_number = 0;
			if (strlen(finddata.name) >= 10)
				image_number = atoi(finddata.name + 10);

			if (finddata.time_write >= BeginTime && image_number == 1) 
			{
				struct tm filetime;
				if (localtime_s(&filetime, &finddata.time_write) == 0)
				{
					char oldname[MAX_PATH], newname[MAX_PATH];

					sprintf_s(oldname, sizeof(oldname), "%s\\%s", dir, finddata.name);

					sprintf_s(newname, sizeof(newname), 
						"%s\\%04d_%02d_%02d_%02d_%02d__%s.tif", 
						SaveFolder, 
						filetime.tm_year + 1900,
						filetime.tm_mon + 1,
						filetime.tm_mday,
						filetime.tm_hour,
						filetime.tm_min,
						dirname);
					
					if (CopyFile(oldname, newname, false) != 0)
					{
						char buf[10];
						sprintf_s(buf, "%d", ++NFilesCopied);
						SetDlgItemText(mdialog, IDC_COPY_COUNT, buf);
					}
				}
			}
		}
	}
}
コード例 #4
0
ファイル: files.c プロジェクト: billev/literacybridge
int 
copyMovedir(char *fromdir, char *todir) {
// 	copy directory tree below fromdir (all subdirectories and files at all levels)
	int ret, r1, len_from, len_to, len, fret;
	char from[PATH_LENGTH], fromfind[PATH_LENGTH], to[PATH_LENGTH], lastdir[FILE_LENGTH];

	struct f_info fi;
	
	fret = 0;
	
	strcpy(from, fromdir);
	len_from = strlen(from);
	
	if(from[len_from-1] != '/') {
		strcat(from, "/");
		len_from++;
	}
	strcat(from, "*");
	
	strcpy(to, todir);
	len_to = strlen(to);
	ret = mkdir((LPSTR)to);	// just to be safe
	if(to[len_to-1] != '/') {
		strcat(to, "/");
		len_to++;
	}
	strcpy(fromfind,from);
	ret =_findfirst((LPSTR)fromfind, &fi, D_DIR);
	from[len_from] = 0;
	lastdir[0] = 0;
	
	while (ret >= 0) {
		if(! (fi.f_attrib & D_DIR)) {
			ret = _findnext(&fi);
			continue;
		}
	
		if(fi.f_name[0]=='.') {
			ret = _findnext(&fi);
			continue;
		}
		from[len_from] = 0;
		to[len_to]= 0;
				
		strcat(from, fi.f_name);
		strcat(to, fi.f_name);
		
		r1 = mkdir((LPSTR)to);
		fret += copyMovedir (from, to);
		ret = rmdir((LPSTR)from);
				
		fret++;
		ret =_findfirst((LPSTR)fromfind, &fi, D_DIR);  //necessary to reset after rmdir? 
	};
	
	from[len_from] = 0;
	to[len_to]= 0;
	fret += copyfiles(from, to);
	cpyTopicPath(from);
	strcpy(lastdir,from+2);
	if ((len = strlen(lastdir)))
		lastdir[len-1] = 0; // remove last '\'
	if (strstr(fromdir,lastdir))
		fret = 0; // prevents system reset if only copying list files
	
	return(fret);
}
external_sort() {
	int i, j, k, error;
	int * buf;
	int run_size = 1, run_count = B_R;
	int MEM = PF_MAX_BUFS - 1, PREVMEM = MEM;

	int fd_inpfile;
	if ((fd_inpfile=PF_OpenFile(TMPFILE))<0){
		PF_PrintError("Error in opening file");
		exit(1);
	}

	int fd_finalfile;
	if ((fd_finalfile=PF_OpenFile(FILE))<0){
		PF_PrintError("Error in opening file");
		exit(1);
	}

	// We assume that inpfile contains all the runs and then we merge them and put them in finalfile
	// when 1 merge pass is over, we again write back everything to the tmpfile
	// repeat

	while(run_count > 1) {
		int pagenum_finalfile = -1;
		//printf("Run count: %d\n", run_count);
		//printf("Run Size: %d\n", run_size);
		MEM = PREVMEM;
		int number_of_merges = run_count / MEM;
		if(run_count % MEM != 0)
			number_of_merges++;

		//printf("Number of merges: %d\n", number_of_merges);

		for(i = 0 ; i < number_of_merges ; i++) {
			//MEM is the number of runs we merge at once. If say MEM = 4 and run_count = 11, then in the last merge step, we merge just 3 runs. So we set MEM accordingly to 3

			if(run_count % MEM != 0 && i == number_of_merges - 1)
				MEM = run_count % MEM;

			int be[MEM], en[MEM];				// be[j] stores the beginning of j-th run and en[j] = 1 + ending of j-th run. Idea is that when be[j] == en[j], then that run is finished
			for(j = 1; j <= MEM; j++) {
				be[j - 1] = i * PREVMEM * run_size + (j - 1) * run_size;		//simple arithmetic
				en[j - 1] = i * PREVMEM * run_size + j * run_size;
			}

			if(i == number_of_merges - 1)	//Since the extremely last run can be shorter than other runs, we need to take care of it. This run will come into picture only in the very last merging iteration
				en[MEM - 1] = en[MEM - 1] < B_R ? en[MEM - 1] : B_R; 	//the ending of the very last run cannot be more than B_R

			//for(j = 0; j < MEM; j++)
				//printf("be: %d, en: %d\n", be[j], en[j]);

			int blocks_written = run_size * (MEM - 1) + (en[MEM - 1] - be[MEM - 1]);		//in 1 merge of iteration, we count the number of blocks we are going to write. Again, take care of the last run separately
			//printf("blocks written: %d\n", blocks_written);
			int *arr[MEM];					// the buffers for the runs that will be merged

			for(j = 0 ; j < MEM ; j++) {
				//printf("Getting page: %d\n", be[j]);
				error = PF_GetThisPage(fd_inpfile, be[j], &arr[j]);			// get the first page of each run in the buffer
				if(error != PFE_OK)
					printf("Error in getting page\n");
				//printf("Page value: %d\n", *arr[j]);
			}

			//start writing blocks one by one
			for(k = 0 ; k < blocks_written ; k++) {							// here goes the actual merge step
				int least = (1 << 30), least_index = -1 ;					// least_index -> the run whose head is the smallest. least -> the value of the corresponding head

				// Note -> be[j] also acts as a pointer to the starting of each run. during the merging process, the value of be[j] will increment
				// if be[j] == en[j], then that particular run is finished
				//printf("Yo\n");
				for(j = 0 ; j < MEM ; j++) {								// this for loop finds the run whose head is the smallest and also the smallest value of the head
					if(en[j] != be[j] && *arr[j] < least) {
						least = *arr[j];
						least_index = j;
					}
					//if(en[j] == be[j])
						//printf("Ignoring\n");
				}

				//printf("Least: %d\n", least);

				//get the next page of the finalfile where the least block is to be written
				//note that pagenum_finalfile will automatically be incremented by 1 after the line below

				//printf("Getting page number %d of finalfile\n", pagenum_finalfile + 1);
				if((error=PF_GetNextPage(fd_finalfile,&pagenum_finalfile,&buf))!= PFE_OK) {
					PF_PrintError("Can't get page");
					exit(1);
				}
				//printf("Value of pagenum_finalfile now: %d\n", pagenum_finalfile);
				//printf("Writing it by changing buffer\n");
				*((int *)buf) = least;				// assign the buffer the least value

				//printf("unfixing the written page\n");
				//unfix the written page
				if ((error=PF_UnfixPage(fd_finalfile,pagenum_finalfile,TRUE))!= PFE_OK){
					PF_PrintError("unfix buffer\n");
					exit(1);
				}
				//printf("Unfixing the least page\n");
				//unfix the least page as well
				if ((error=PF_UnfixPage(fd_inpfile,be[least_index],TRUE))!= PFE_OK){
					PF_PrintError("unfix buffer\n");
					exit(1);
				}
				be[least_index]++;
				if(be[least_index] < en[least_index]) {			// if the run which had the *least* head isn't finished, get its next page
					be[least_index]--;
					if((error=PF_GetNextPage(fd_inpfile , &be[least_index] , &arr[least_index]))!= PFE_OK) {
						PF_PrintError("Can't get page");
						exit(1);
					}
					//printf("Fetching next page: %d\n", *arr[least_index]);
				}
			}
			//printf("End of 1 merge iteration\n");
		}
		copyfiles();

		//printf("\n");

		run_count = number_of_merges;
		run_size = run_size * PREVMEM;
		//printf("End of iteration\n");
		//readfile(FILE);
	}
	//swap files here

	if ((error=PF_CloseFile(fd_inpfile))!= PFE_OK){
		PF_PrintError("Error in closing input file\n");
		exit(1);
	}

	if ((error=PF_CloseFile(fd_finalfile))!= PFE_OK){
		PF_PrintError("Error in closing temporary file\n");
		exit(1);
	}
}