상대경로를 절대 경로로 바꾸어 주는 함수
글쓴이: 익명 사용자 / 작성시간: 목, 2005/05/19 - 10:27오후
상대경로를 절대 경로로 바꾸어 주는 함수 입니다.
검증이 안되어 버그가 있을지도 모릅니다.
#include <stdio.h> #include <malloc.h> #include <string.h> typedef struct path_element_t{ char *element; struct path_element_t *prev; struct path_element_t *next; } path_element; int rel2abspath(char *,char *,char **); int main(int argc,char **argv){ char *buf=NULL; if(argc==3){ rel2abspath(argv[1],argv[2],&buf); printf("RESULT: %s\n",buf); free(buf); }else{ printf("Usage: %s [abs_path] [rel_path]\n",argv[0]); printf("\nExample: %s 'http://unstable.elemental.com/mozilla/build/latest/mozilla/caps/dox/nsBasePrincipal_8cpp.html#a0' '.././../////./../'\n",argv[0]); return 1; } return 0; } int rel2abspath(char *abs_path,char *rel_path,char **buf){ char *path_n=NULL; char *abs_path_n=NULL; char *first_sep=NULL; char *abs_second_sep=NULL; char *rel_second_sep=NULL; char *last_offset=NULL; char *temp_path=NULL; char *abs_doc=NULL; char *rel_doc=NULL; char chr; int len=0; int count=0; path_element *head=NULL,*cur=NULL,*prev=NULL,*temp=NULL; //printf("%s\n",abs_path); //printf("%s\n",rel_path); if((first_sep=(char *)strchr((const char *)abs_path,(int)':'))){ if(first_sep[1]=='/'&&first_sep[2]=='/'){ first_sep+=3; if((first_sep=(char *)strchr((const char *)first_sep,(int)'/'))){ ++first_sep; }else{ //printf("Error.Not found/.\n"); return 1; } }else{ //printf("Error.Not found //.\n"); return 2; } }else{ //printf("Error.Not found :.\n"); return 3; } if((abs_second_sep=(char *)strrchr((const char *)first_sep,(int)'/'))){ ++abs_second_sep; abs_doc=abs_second_sep; //printf("%s\n",abs_second_sep); len=abs_second_sep-first_sep; if((rel_second_sep=(char *)strrchr((const char *)rel_path,(int)'/'))){ len+=strlen(rel_path); } path_n=(char *)malloc(sizeof(char)*len+1); memcpy(path_n,first_sep,abs_second_sep-first_sep); if(rel_second_sep){ rel_doc=rel_second_sep+1; memcpy(path_n+(abs_second_sep-first_sep),rel_path,rel_second_sep-rel_path); }else{ rel_doc=rel_path; } path_n[len]='\0'; //printf("%s\n",path_n); }else{ //printf("Error.Not found /.\n"); return 4; } last_offset=path_n; while('\0'!=(chr=*(path_n+count))){ switch(chr){ case '/': cur=(path_element *)malloc(sizeof(path_element)); memset(cur,0x0,sizeof(path_element)); cur->next=NULL; if(head==NULL){ head=cur; }else{ prev->next=cur; } cur->prev=prev; prev=cur; len=sizeof(char)*((path_n+count)-last_offset); cur->element=(char *)malloc(sizeof(char)*len+1); memcpy(cur->element,last_offset,len); cur->element[len]='\0'; //printf("%s\n",cur->element); last_offset=path_n+count+1; break; default: } ++count; } if((path_n+count)-last_offset>0){ cur=(path_element *)malloc(sizeof(path_element)); memset(cur,0x0,sizeof(path_element)); cur->next=NULL; if(head==NULL){ head=cur; }else{ prev->next=cur; } cur->prev=prev; prev=cur; len=sizeof(char)*((path_n+count)-last_offset); cur->element=(char *)malloc(sizeof(char)*len+1); memcpy(cur->element,last_offset,len); cur->element[len]='\0'; //printf("%s\n",cur->element); } cur=head; while(cur!=NULL){ temp=cur; if(memcmp(cur->element,".",1)==0&&strlen(cur->element)==1){ if(cur->prev){ (cur->prev)->next=cur->next; } if(cur->next){ (cur->next)->prev=cur->prev; } cur=cur->prev; free(temp); }else if(memcmp(cur->element,"..",2)==0&&strlen(cur->element)==2){ if(cur->prev){ ((cur->prev)->prev)->next=cur->next; } if(cur->next){ (cur->next)->prev=(cur->prev)->prev; } cur=(cur->prev)->prev; free(temp->prev); free(temp); } cur=cur->next; } cur=head; while(cur!=NULL){ temp=cur; if(strlen(cur->element)==0){ (cur->prev)->next=cur->next; if(cur->next){ (cur->next)->prev=cur->prev; } cur=cur->prev; free(temp); } cur=cur->next; } cur=head; len=0; while(cur!=NULL){ //printf("0x%08x 0x%08x 0x%08x %s\n",cur->prev,cur,cur->next,cur->element); len+=strlen(cur->element); ++len; cur=cur->next; } len+=first_sep-abs_path; if(rel_doc==NULL||strlen(rel_doc)==0){ len+=strlen(abs_doc); }else{ len+=strlen(rel_doc); } abs_path_n=(char *)malloc(len+1); temp_path=abs_path_n; memcpy(abs_path_n,abs_path,first_sep-abs_path); abs_path_n+=(first_sep-abs_path); cur=head; while(cur!=NULL){ memcpy(abs_path_n,cur->element,strlen(cur->element)); abs_path_n+=strlen(cur->element); abs_path_n[0]='/'; ++abs_path_n; cur=cur->next; } if(rel_doc==NULL||strlen(rel_doc)==0){ memcpy(abs_path_n,abs_doc,strlen(abs_doc)); abs_path_n+=strlen(abs_doc); }else{ memcpy(abs_path_n,rel_doc,strlen(rel_doc)); abs_path_n+=strlen(rel_doc); } abs_path_n[0]='\0'; (*buf)=temp_path; return 0; }
Forums:
이 글 삭제 좀 부탁 드립니다.
삭제가 안되네요..삭제좀 부탁 드리겠습니다...버그가 수두룩 하네요.
ㅎㅎ, 손님모드의 단점^^;하지만, 버그없는 코드를 자랑하는것만이 아
ㅎㅎ, 손님모드의 단점^^;
하지만, 버그없는 코드를 자랑하는것만이 아니라, fix해나가는 과정도
같이 해나가보자는 것도 이 코너의 목적이기도 하답니다^^;
--------------------------------------------------------------------------------
\(´∇`)ノ \(´∇`)ノ \(´∇`)ノ \(´∇`)ノ
def ed():neTdiVeR in range(thEeArTh)
댓글 달기