Post
#1
|
|
|
Grupa: Przyjaciele php.pl Postów: 1 467 Pomógł: 13 Dołączył: 22.02.2003 Ostrzeżenie: (0%)
|
Kiedyś dawno temu zrobiłem ładny system obsługi drzewek. Teraz leży u mnie na kompie i się marnuje a tutaj na forum conajmniej raz na tydzień ktoś o to pyta. Dlatego zamieszczam. Dodam tylko, że sama struktura nie jest moim pomysłem:
Struktura: Api:
Polecam wszystkim, bo mi naprawdę szkoda, że moja praca się marnuje a sam nie mam co z tym zrobić. Jeżeli jest odpowiedniejsze forum to proszę o przeniesienie. Ojojoj, wyszedłem poza limit długości posta. Teraz jeszcze jakby jacyś linuksiarze z Was chcieli potestować i potrzebowali danych testowych to polecam: Kod #include<stdlib.h> oraz error.c#include<errno.h> #include<dirent.h> #include<stdio.h> #include<fcntl.h> #include<sys/stat.h> #include<sys/types.h> #define FALSE 0 #define TRUE !FALSE int id; int fid; void *xmalloc(size_t size) { void *ptr; if((ptr=malloc(size))==NULL) syserr("malloc"); memset(ptr, 0, size); return ptr; } void xfree(void *ptr) { if(ptr) free(ptr); } void writeFile(const char *path) { FILE *fp; int c; if((fp=fopen(path, "r"))==NULL) syserr("fopen"); while((c=fgetc(fp))!=EOF) { if(c=='\\'||c=='\'') printf("%c", '\\'); printf("%c", c); } if(fclose(fp)!=0) syserr("fclose"); } int isRegular(const char *path) { struct stat st; if( stat( path, &st ) == -1 ) return FALSE; return ( S_ISREG( st.st_mode ) ); } int isDir(const char *path) { struct stat st; if( stat( path, &st ) == -1 ) return FALSE; return ( S_ISDIR( st.st_mode ) ); } unsigned long int insertDir(const char *name, const int pid) { extern int id; id++; printf("INSERT INTO nodes ( id, name, parent) VALUES ( %d, '%s', %d);\n", id, name, pid); printf("INSERT INTO asociations ( first_id, second_id, depth ) SELECT first_id, %d, depth + 1 FROM asociations WHERE second_id = %d;\n", id, pid); printf("INSERT INTO asociations ( first_id, second_id, depth ) VALUES ( %d, %d, 0 );\n", id, id); return id; } void insertFile(const char *path, const char *name, const int pid) { extern int id; extern int fid; id++; fid++; printf("INSERT INTO nodes ( id, name, parent) VALUES ( %d, '%s', %d );\n", id, name, pid); printf("INSERT INTO asociations ( first_id, second_id, depth ) SELECT first_id, %d, depth + 1 FROM asociations WHERE second_id = %d;\n", id, pid); printf("INSERT INTO asociations ( first_id, second_id, depth ) VALUES ( %d, %d, 0 );\n", id, id); printf("INSERT INTO contents ( id, content ) VALUES ( %d, '", fid); writeFile(path); printf("' );\n"); printf("INSERT INTO binds (node_id, content_id) VALUES (%d, %d);", id, fid); } int insert(const char *path, const int pid) { DIR *dir; struct dirent *dent; char *nodepath; unsigned long int npid; if((dir=opendir(path))==NULL) return TRUE; while((dent=readdir(dir))!=NULL) { if((strcmp(dent->d_name, ".")==0)||(strcmp(dent->d_name,"..")==0)||(strcmp(dent->d_name,"dev")==0)||(strcmp( dent->d_name,"proc")==0)) { continue; } nodepath=(char *)xmalloc(strlen(path)+strlen(dent->d_name)+2); sprintf(nodepath, "%s/%s", path, dent->d_name); if(isDir(nodepath)) { npid=insertDir(dent->d_name, pid); insert(nodepath, npid); } else if(isRegular(nodepath)) { insertFile(nodepath, dent->d_name, pid); } xfree(nodepath); } if(closedir(dir)==-1) syserr("closedir"); return TRUE; } int main(int argc, char *argv[]) { extern int id; extern int fid; id=1; fid=0; if(argc<2) syserr("argc"); if(insert(argv[1], id)==FALSE) syserr("insert"); return 0; } Kod #include<stdio.h> Mam nadzieję, że nie muszę dodawać, że framework jest na pgsql (choć łatwo go przerobić, bo pgsql jest prostym językiem).#include<errno.h> #include<fcntl.h> void syserr(char *msg) { fprintf(stderr, "ERROR: %s ( %d", msg, errno); if(errno>0 && errno<sys_nerr) fprintf(stderr, "; %s )\n", strerror(errno)); else fprintf(stderr, " )\n"); exit(1); } I mam też nadzieję, że nie muszę dodawać, że program kompiluje się Kod gcc -o pgins pgins.c error.c a używaKod ./pgins /katalog/do/insertowania | insert.sql
|
|
|
|
![]() |
Post
#2
|
|
|
Grupa: Przyjaciele php.pl Postów: 2 335 Pomógł: 6 Dołączył: 7.03.2002 Ostrzeżenie: (0%)
|
Drzewka to nie tylko XML (a raczej jego zagniezdzona struktura), ale to takze grafy czy inne struktury polaczonych list
Ten post edytował Seth 25.07.2006, 21:33:50 |
|
|
|
Post
#3
|
|
|
Grupa: Zarejestrowani Postów: 352 Pomógł: 0 Dołączył: 22.01.2006 Ostrzeżenie: (0%)
|
Drzewka to nie tylko XML (a raczej jego zagniezdzona struktura), ale to takze grafy czy inne struktury polaczonych list Nie bardzo rozumiem co masz na myśli, czym jest według Ciebie zagnieżdżona struktura XML? Co to są grafy i inne struktury połączonych list, które można zapisać w powyższej bazie a w XML'u już nie (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif) |
|
|
|
Jabol Tree-Framework 24.07.2006, 16:00:50
mariuszn3 Drzewek to znaczy masz na myśli coś takiego jak XM... 25.07.2006, 20:10:38
LBO Jak dla mnie Zyx'owy sposób jest najlepszy... 25.07.2006, 20:35:34 
mariuszn3 Cytat(LBO @ 25.07.2006, 19:35 ) Jak dla m... 25.07.2006, 20:40:15
bela A na matmie w podstawówce spałeś? http://pl.wikipe... 25.07.2006, 21:53:14 
mariuszn3 Cytat(bela @ 25.07.2006, 20:53 ) A na mat... 25.07.2006, 22:00:27
Jabol Że tak to nazwę. Dokładnie jak napisałeś - można z... 25.07.2006, 23:16:06
mariuszn3 Absolutnie tego nie neguję.. nie o to mi chodziło.... 25.07.2006, 23:32:20 ![]() ![]() |
|
Aktualny czas: 27.12.2025 - 19:22 |