Пример #1
0
int main() {
    char studentName[40];
    int studentGrade;
    Student * curStudent = NULL;
    FILE * studentFile = fopen("test.txt","r");
    Tree theTree;
    int first = 0;
    int shouldContinue = 1;

    /* initialize tree */
    Initialize (&theTree, (&copyStudent), (&destroyStudent), (&compareStudents));
    printf("Initialize()\n");
    getTreeInfo(&theTree);

    while (fscanf(studentFile,"%s %d",studentName,&studentGrade)==2) {
        curStudent = malloc(sizeof(Student));
        InitializeStudent(studentName,studentGrade,curStudent);
        printf("Insert(%s,%d)\n", studentName,studentGrade);
        Insert(&theTree,curStudent);
        getTreeInfo(&theTree);
    }
    fclose(studentFile);

    while (shouldContinue == 1) {
        void * theStudent = malloc(sizeof(Student));
        Student * printStudent;

        if (first == 0) {
            shouldContinue = Minimum(&theTree,theStudent);
            if (shouldContinue == 0)
                break;
            first++;
        } else {
            shouldContinue = Successor(&theTree,theStudent);
            if (shouldContinue == 0)
                break;
        }

        printStudent = (Student *)theStudent;
        printf("%s \t%d%%\n", NameOfStudent(*printStudent),GradeOfStudent(*printStudent));

        free(theStudent);
    }
    Destroy(&theTree);

    return 0;
}
Пример #2
0
void saveTreeToDsik()
{
	//write TreeRecord
	int fd = -1;
	int buf[2014] = {0};

	char treeInfo[2048]={0};
	getTreeInfo(treeInfo);
	printf("%s\n", treeInfo);

	fd = open("TreeRecord", O_RDWR);
	if (fd == -1)
	{
		printf("Failed to open 'TreeRecord'!\n");
		return;
	}
	strcpy(buf,treeInfo);
	printf("buf :%s\n", buf);

	write(fd, buf, 1024);
	close(fd);
}
Пример #3
0
void TestB()
{
	char tty_name[] = "/dev_tty1";

	int fd_stdin  = open(tty_name, O_RDWR);
	assert(fd_stdin  == 0);
	int fd_stdout = open(tty_name, O_RDWR);
	assert(fd_stdout == 1);

	char rdbuf[128];
	char cmd[8];
	char filename[120];
	char buf[1024];
	int m,n;

	initFSTree();


	printf("                        ==================================\n");
	printf("                                    File Manager           \n");
	printf("                                 Kernel on Orange's \n\n");
	printf("                        ==================================\n");
	while (1) {
		printPath();
		printf("$ :");
		int r = read(fd_stdin, rdbuf, 70);
		rdbuf[r] = 0;

		if (strcmp(rdbuf, "i") == 0)
		{
			char treeInfo[2048] = {0};
			getTreeInfo(treeInfo);
			printf("%s\n", treeInfo);
		}
		else if(strcmp(rdbuf, "s") == 0)
		{
			showTree();
		}
		else if(strcmp(rdbuf, "p") == 0)
		{
			printPath();
			printf("\n");
		}
		else if (strcmp(rdbuf, "help") == 0)
		{
			printf("=============================================================================\n");
			printf("Command List     :\n");
			printf("1. create [filename]       : Create a new file \n");
			printf("2. read [filename]         : Read the file\n");
			printf("3. write [filename]        : Write at the end of the file\n");
			printf("4. delete [filename]       : Delete the file\n");
			printf("5. help                    : Display the help message\n");
			printf("==============================================================================\n");		
		}
		else if (strcmp(rdbuf, "dir") == 0)
		{
			printCurrFile();
			continue;
		}
		else
		{
			int fd;
			int i = 0;
			int j = 0;
			char temp = -1;
			while(rdbuf[i]!=' ' && rdbuf[i] != 0)
			{
				cmd[i] = rdbuf[i];
				i++;
			}
			cmd[i++] = 0;
			while(rdbuf[i] != 0)
			{
				filename[j] = rdbuf[i];
				i++;
				j++;
			}
			filename[j] = 0;

			if(strcmp(cmd, "mkdir") == 0)
			{
				createDir(filename);

				saveTreeToDsik();
				// //write TreeRecord
				// char treeInfo[2048]={0};
				// getTreeInfo(treeInfo);
				// printf("%s\n", treeInfo);

				// fd = open("TreeRecord", O_RDWR);
				// if (fd == -1)
				// {
				// 	printf("Failed to open 'TreeRecord'!\n");
				// 	continue ;
				// }
				// strcpy(buf,treeInfo);
				// printf("buf :%s\n", buf);

				// n = write(fd, buf, 1024);
				// close(fd);

			}
			else if(strcmp(cmd, "cd") == 0)
			{
				//open dir
				if(strcmp(filename, "..") == 0)
				{
					backDir();
				}
				else
				{
					openDir(filename);
				}
			}
			else if(strcmp(cmd,"rmdir") == 0)
			{
				//delete dir
				if(isThereFile(filename) == 0)
				{
					printf("fail to remove dir, not here!\n");
					continue;
				}
				deleteDirRecord(filename);

				saveTreeToDsik();
				// //write TreeRecord
				// char treeInfo[2048]={0};
				// getTreeInfo(treeInfo);
				// printf("%s\n", treeInfo);

				// fd = open("TreeRecord", O_RDWR);
				// if (fd == -1)
				// {
				// 	printf("Failed to open 'TreeRecord'!\n");
				// 	continue ;
				// }
				// strcpy(buf,treeInfo);
				// printf("buf :%s\n", buf);

				// n = write(fd, buf, 1024);
				// close(fd);
			}
			else if (strcmp(cmd, "create") == 0)
			{
				fd = open(filename, O_CREAT | O_RDWR);
				if (fd == -1)
				{
					printf("Failed to create file! Please check the fileaname!\n");
					continue ;
				}
				buf[0] = 0;
				write(fd, buf, 1);
				printf("File created: %s (fd %d)\n", filename, fd);
				close(fd);

				//add to tree currFatherDir
				fTreeArray[treeCurrSize].currDir = treeCurrSize;
				fTreeArray[treeCurrSize].fatherDir = currFatherDir;
				fTreeArray[treeCurrSize].isDir = 0;  //not a dir
				strcpy(fTreeArray[treeCurrSize].fileName, filename);
				treeCurrSize++;
				treeCount++;

				// showTree();

				// char* treeInfo;
				// treeInfo = getTreeInfo();
				// printf("%d\n", treeCurrSize);

				saveTreeToDsik();
				// //write TreeRecord
				// char treeInfo[2048]={0};
				// getTreeInfo(treeInfo);
				// printf("%s\n", treeInfo);

				// fd = open("TreeRecord", O_RDWR);
				// if (fd == -1)
				// {
				// 	printf("Failed to open 'TreeRecord'!\n");
				// 	continue ;
				// }
				// strcpy(buf,treeInfo);
				// printf("buf :%s\n", buf);

				// n = write(fd, buf, 1024);
				// close(fd);

			}
			else if (strcmp(cmd, "read") == 0)
			{
				if(isThereFile(filename) == 0)
				{
					printf("fail to read file, not here!\n");
					continue;
				}
				fd = open(filename, O_RDWR);
				if (fd == -1)
				{
					printf("Failed to open file! Please check the fileaname!\n");
					continue ;
				}
				
				n = read(fd, buf, 1024);
				
				printf("%s\n", buf);
				close(fd);

			}
			else if (strcmp(cmd, "write") == 0)
			{
				// if(isThereFile(filename) == 0)
				// {
				// 	printf("fail to write file, not here!\n");
				// 	continue;
				// }
				fd = open(filename, O_RDWR);
				if (fd == -1)
				{
					printf("Failed to open file! Please check the fileaname!\n");
					continue ;
				}

				m = read(fd_stdin, rdbuf,80);
				rdbuf[m] = 0;
				
				n = write(fd, rdbuf, m+1);
				close(fd);


			}
			else if (strcmp(cmd, "delete") == 0)
			{
				if(isThereFile(filename) == 0)
				{
					printf("fail to delete file, not here!\n");
					continue;
				}

				m=unlink(filename);
				if (m == 0)
				{
					printf("File deleted!\n");

					//delete file from tree records, not a dir
					int i = 0;
					for (; i < treeCurrSize; ++i)
					{
						if(strcmp(fTreeArray[i].fileName,filename) == 0)
						{
							fTreeArray[i].currDir = -1;
							continue;
						}
					}
					treeCount--;
					// showTree();

					saveTreeToDsik();
					// //write TreeRecord
					// char treeInfo[2048]={0};
					// getTreeInfo(treeInfo);
					// printf("%s\n", treeInfo);

					// fd = open("TreeRecord", O_RDWR);
					// if (fd == -1)
					// {
					// 	printf("Failed to open 'TreeRecord'!\n");
					// 	continue ;
					// }
					// strcpy(buf,treeInfo);
					// printf("buf :%s\n", buf);

					// n = write(fd, buf, 1024);
					// close(fd);

					continue;
				}
				else
				{
					printf("Failed to delete file! Please check the fileaname!\n");
					continue;
				}

			}
			else 
			{
				printf("Command not found, Please check!\n");
				continue;
			}

			
			
		}
		
			
	}

	assert(0); /* never arrive here */
}