makefile
代码:
plsMaker: table.o node.o plsMaker.o gcc -o plsMaker table.o node.o plsMaker.o
plsMaker.o: plsMaker02.c gcc -c -o plsMaker.o plsMaker02.c
table.o: table.c table.h gcc -c -o table.o table.c
node.o: node.c node.h gcc -c -o node.o node.c
table.c
代码:
#include "table.h"
/* * This program could provide an abstract link table. This table conserns nothing * about the data structure it will contain. With `#define nodeType xxxx' we could make * this program suitable for any data structure. * * struct Node defines a Node which have two pointers pointing to the previous node * and next node and another pointer pointing to the data which should be contained in * this node. * * struct table defines a real link table which has tree pointers. One head, one end * and one operating pointer. Those pointers will point to struct Node which will be nodes * in the program. * */
/* * genNode() and killNode() are two functions which will generate and kill a typNode * from the data provided by users. * */
typNode *genNode(nodeType *node) { typNode *myNode; myNode=malloc(sizeof(typNode)); if ((myNode->thisNode=node)==NULL) { free(myNode); return NULL; } myNode->nextNode=NULL; myNode->preNode=NULL; return myNode; }
typNode *killNode(typNode *node) { node->thisNode=dieNode(node->thisNode); free(node); return NULL; }
/* * In section below, we have creatTable(), insert(), move(), del(), delTable() which * could manage the link table with creation, insertion, op-pointer moving forward and * backward, removing node, and closing the whole table. Also, we have readTable() which * returns a pointer pointing to the content of op-pointer (that is pointer *thisNode in * struct Node). * For convenience, we also have dispTable() which could print all the items in the * table, but because it concerns too much about `NodeType', it may not appear when we reuse * this program. * */
linkTable *creatTable(nodeType *head) { linkTable *myTable; typNode *firstNode; myTable=malloc(sizeof(linkTable)); firstNode=genNode(head); myTable->headerPtr=firstNode; myTable->nowPtr=firstNode; myTable->endPtr=firstNode; // myTable->numOfNodes=1; return myTable; }
void insert(linkTable *myTable, nodeType *thisNode) { typNode *nodePtr; nodePtr=genNode(thisNode); if (myTable->nowPtr==myTable->endPtr) { myTable->endPtr=nodePtr; nodePtr->preNode=myTable->nowPtr; myTable->nowPtr->nextNode=nodePtr; } else { nodePtr->preNode=myTable->nowPtr; move(myTable, 1); myTable->nowPtr->preNode=nodePtr; nodePtr->nextNode=myTable->nowPtr; nodePtr->preNode->nextNode=nodePtr; } myTable->nowPtr=nodePtr; }
/* void search(linkTable *myTable) { } */
// int move(linkTable *, int) will move linkTable op-pointer forward [step] steps. // if step>0, op-pointer will move forward, or it will move backward. once op-pointer // reaches the bottom, or the top, of the table, it will stop. The value move() return // will be ([step]-[steps actually moved]), that is how many steps left -- didn't move.
int move(linkTable *myTable, int step) { if (step<0) while (myTable->nowPtr->preNode!=NULL && step++!=0) myTable->nowPtr=myTable->nowPtr->preNode; else if (step>0) while (myTable->nowPtr->nextNode!=NULL && step--!=0) myTable->nowPtr=myTable->nowPtr->nextNode; return step; }
linkTable *del(linkTable *myTable) { if (myTable->nowPtr==myTable->headerPtr) { if (move(myTable, 1)==1) { myTable->nowPtr=killNode(myTable->nowPtr); myTable->endPtr=NULL; myTable->headerPtr=NULL; free(myTable); return NULL; } else { myTable->nowPtr->preNode=NULL; myTable->headerPtr=killNode(myTable->headerPtr); myTable->headerPtr=myTable->nowPtr; return myTable; } } else if (myTable->nowPtr==myTable->endPtr) { move(myTable, -1); myTable->nowPtr->nextNode=NULL; myTable->endPtr=killNode(myTable->endPtr); myTable->endPtr=myTable->nowPtr; return myTable; } else { typNode *tmp; tmp=myTable->nowPtr; move(myTable, -1); tmp->preNode->nextNode=tmp->nextNode; tmp->nextNode->preNode=tmp->preNode; tmp=killNode(tmp); return myTable; } }
linkTable *delTable(linkTable *myTable) { // del all the nodes; myTable->nowPtr=myTable->headerPtr; while((myTable=del(myTable))!=NULL) ; return NULL; }
nodeType *readNode(linkTable *myTable) { return myTable->nowPtr->thisNode; }
void dispTable(linkTable *myTable) { typNode *tmpPtr; tmpPtr=myTable->nowPtr; myTable->nowPtr=myTable->headerPtr; do { output(readNode(myTable)); } while (move(myTable, 1)!=1); myTable->nowPtr=tmpPtr; }
/* Test for correctness */ /* int main(int argc, char *argv[]) { int i=0; song *mySong; mySong=newNode(argv[0],argv[0]); linkTable *myTable; myTable=creatTable(mySong); for (i=1;i<argc;i++) { mySong=newNode(argv[i],argv[i]); insert(myTable, mySong); } myTable->nowPtr=myTable->headerPtr; do { output(readNode(myTable)); } while (move(myTable, 1)!=1); printf("\n\n************ Here's another version of the table ***************\n\n"); dispTable(myTable);
myTable=delTable(myTable); return 0; } */ /* End of Test */
table.h
代码:
#include <stdlib.h> #include "node.h"
#define nodeType song
typedef struct Node { struct Node *nextNode; struct Node *preNode; nodeType *thisNode; } typNode;
typedef struct table { typNode *headerPtr; typNode *nowPtr; typNode *endPtr;
// int numOfNodes; } linkTable;
typNode *genNode(nodeType *); typNode *killNode(typNode *); linkTable *creatTable(nodeType *); void insert(linkTable *, nodeType *); int move(linkTable *, int); linkTable *del(linkTable *); linkTable *delTable(linkTable *); nodeType *readNode(linkTable *); void dispTable(linkTable *); linkTable *creatTable(nodeType *);
node.c
代码:
#include "node.h"
song *newNode (char *path, char *filename) { song *mySong; mySong=malloc(sizeof(song)); int length; if ((length=strlen(path))>DIR_LENGTH-1) { printf("Error: length of path is too long. %d characters\n", length); free(mySong); return NULL; } else strncpy(mySong->dirPath, path, length); if (getPath(path)==0) strcat(mySong->dirPath, "/");
if ((length=strlen(filename))>NAME_LENGTH-1) { printf("Error: length of file name is too long. %d characters\n", length); free(mySong); return NULL; } else strncpy(mySong->fileName, filename, length);
return mySong; }
song *dieNode(song *node) { if (node!=NULL) free(node); return NULL; }
void output(song *node) { printf("------ Node Information ------\n"); printf("Directory: %s\n",node->dirPath); printf("File Name: %s\n",node->fileName); printf("----- End of Information -----\n"); }
int getPath(const char *path) { for (;*path!='\0';path++); path--; if (*path=='/') return 1; else return 0; }
/* test for correctness */ /* int main(int argc, char *argv[]) { struct playNode *oneSong; oneSong=newNode(argv[1], argv[2]); output(oneSong); oneSong=dieNode(oneSong); return 0; } */ /* end of test */
node.h
代码:
#include <string.h> #include <stdlib.h>
#define DIR_LENGTH 1024 #define NAME_LENGTH 512 #define TITLE_LENGTH 100 #define AUTHOR_LENGTH 50
typedef struct playNode { char dirPath[DIR_LENGTH]; char fileName[NAME_LENGTH]; char title[TITLE_LENGTH]; char author[AUTHOR_LENGTH]; } song;
song *newNode (char *, char *); song *dieNode(song *); void output(song *); int getPath(const char *);
plsMaker02.c
代码:
#include <string.h> #include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <dirent.h> #include "table.h"
/* * In this program, we will search all files in the given directory. Then, play them * in realplayer one by one. * * * * */
int main(int argc, char *argv[]) { if (argc!=3) { printf("Command Usage: %s <dirName> <fileName>\n",argv[0]); exit(-1); } char txtCommand[]="/usr/share/RealPlayer/realplay -q "; char txtLength[]="length=-1"; char txtFile[]="file="; char txtTitle[]="title="; FILE *fileName; DIR *curDir; struct dirent *deDir; struct stat fileStatBuf; if ((curDir=opendir(argv[1]))==NULL) { printf("Can't open direcotry: %s\n",argv[1]); exit (-1); } else { chdir(argv[1]); }
song *mySong=NULL; linkTable *myPls=NULL;
while ((deDir=readdir(curDir))!=NULL) { lstat(deDir->d_name, &fileStatBuf); if (S_ISREG(fileStatBuf.st_mode)) { mySong=newNode(argv[1], deDir->d_name); if (myPls==NULL) myPls=creatTable(mySong); else insert(myPls, mySong); } } mySong=NULL; dispTable(myPls);
char *command; for (myPls->nowPtr=myPls->headerPtr;myPls->nowPtr->nextNode!=NULL;move(myPls,1)) { command=malloc(2048); command=strcpy(command, txtCommand); command=strcat(command, readNode(myPls)->dirPath); command=strcat(command, readNode(myPls)->fileName); printf("Command: %s\n", command); fileName=popen(command, "r"); pclose(fileName); } myPls=delTable(myPls); return 0; }
|