• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • 飲水思源收藏夾多級目錄的實現細節

    發表于:2007-07-04來源:作者:點擊數: 標簽:
    這一個多周說起來做的事情還蠻多的,回家呆了3天,飛來飛去,然后寫了2天web的收藏夾,寫好了本來想偷懶了,結果最后還是把任務攤到自己頭上了,結果又寫了2天的te .net 代碼,感覺多寫些代碼實在有好處,至少寫完了不會有語法錯誤,也不會出現什么沒定義,或
    這一個多周說起來做的事情還蠻多的,回家呆了3天,飛來飛去,然后寫了2天web的收藏夾,寫好了本來想偷懶了,結果最后還是把任務攤到自己頭上了,結果又寫了2天的te.net代碼,感覺多寫些代碼實在有好處,至少寫完了不會有語法錯誤,也不會出現什么沒定義,或者函數用錯的情況,對了有個很郁悶的事情,就是關于calloc函數,我從網上down下來的一個資料時說calloc(n,size),結果錯誤不斷,排除了各種問題之后,才知道原來竟然是calloc(size,n),浪費半天時間啊,沒有任何進展,很是郁悶。廢話不多說,說一下我是如何實現任意文件夾以及任意討論區的。

    收藏家的struct
    //added by kongyang 2006-06-28
    struct boardcollection
    {
        int subnum;//the number of subdirectories not includes dirs in extends
        int boardnum;//the number of boards
        int isextends;//0 stands for being not an extends,1 not
        char subdirsfile[20][32];//one dir can have 20 sub dirs, this stores the
    filename of each subs
        char subdirname[20][32];//names
        char boards[20][32];//can have 20 boards
        char dirname[32];//filename of current dir
        char updir[32];//the filename of father dir
        char extends[32];//extends of subdirectories.
    };
    subnum是當前文件的子目錄數目,但是不包含擴展目錄下面的子目錄數目
    也就是說,當前文件的子目錄總數應該是它現在的包含的子目錄數目還有擴展
    以及可能的擴展的擴展的包含的子目錄數目總和。
    boardnum是當前文件的版面數目,當然也不包含擴展文件下的字文件夾數目
    也就是說,如果要統計當前的總目錄或者文件夾數目的話,用這個函數:
    int getgood_dirnum(struct boardcollection currentdir)
    {
        int i=0;
        i+=currentdir.subnum;
        while(currentdir.extends[0]!=0)
        {
            f_get_file(currentdir.extends, &currentdir, 1);
            i+=currentdir.subnum;
        }
        return i;
    }
    isextends代表當前的這個文件是否是某一個文件的擴展

    f_get_file為從一個文件中獲得一個結構體
    subdirsfile為對應子目錄名字的文件名
    subdisname為子目錄的顯示給用戶的名字
    boards是討論區的名字
    dirname是當前文件所在的文件名
    updir是父文件夾的文件名
    extends為除了20各目錄和20各討論區后,是否要添加擴展,以實現任意多的討論區
    和任意多的文件夾

    這只是一個實現策略,沒有去參考別人的,本來水母也實現了這個功能,但是還是想有點自己的知識產權,所以全部自己來構造。一開始思維會比較混亂,覺得extends的文件和當前目錄的關系不好把握。

    后來慢慢寫得多了,思路也清晰了。目錄確定了以后,就是來進行添加刪除目錄了。其實添加也很簡單,看看當前目錄有沒有空余的空間,有的話就直接架上,沒有的話就放到extends里面,extends滿了再放到

    extends的extends里面...貼一段函數如下

    int do_adddir(char *boardfile,char* newdir)
    {
     char boardfilename[80];
     struct boardcollection currentdir;
     struct boardcollection newstruct;
     time_t now;
     get(boardfilename);
     f_get_file(boardfile, &currentdir, 1);
     if(currentdir.subnum<20)
     {
      now=getfilename();
      currentdir.subnum++;
      snprintf(currentdir.subdirsfile[currentdir.subnum-1],80,"%d",now);
      snprintf(currentdir.subdirname[currentdir.subnum-1],80,"%s",newdir);
      f_replace_file(currentdir);
      init_dir(&newstruct,currentdir.subdirsfile[currentdir.subnum-1],boardfile);
      f_replace_file(newstruct);
     }
     else
     {
      if(currentdir.extends[0]==0)
      {
       now=getfilename();
       snprintf(currentdir.extends,80,"%d",now);
       f_replace_file(currentdir);
       init_dir(&newstruct,currentdir.extends,boardfile);
       newstruct.isextends=1;
       f_replace_file(newstruct);
       do_adddir(currentdir.extends,newdir);
      }
      else
      {
       do_adddir(currentdir.extends,newdir);
      }
     }
     return 0;
     
    }

    寫起來的一個很大的感覺就是要多用函數,把相同的操作盡量封裝到漢書里面,這樣寫起程序來也會感覺很帥,而且也很容易定位錯誤。更重要的是大大的降低了代碼的復雜度,提高了代碼的可讀性。

    然后刪除的時候會比較麻煩一點,因為刪除一個目錄,已開始想得很多,結果走了半個小時的彎路,后來猛一回神,發現是如此簡單,只要把想刪除的文件里的信息讀出來,然后刪除子目錄對應的文件,

    當然在芟除這些文件之前,要先讀出子目錄對應的文件里包含的子目錄的文件,如此下去,就ok了,已開始總是在想,刪除了子目錄,父目錄會受到什么影響?比如是extends的話有可能不包含子目錄了那么這個extends

    的生命就結束了等等,后來才發現不需要每個刪除都要考慮這一點的,已開始得知需要刪除no care就好了,要考慮的只是最上層這個的情況,貼函數如下

    int do_deldir(struct boardcollection updir,char *deldir)
    {
     struct boardcollection dirr;
     struct boardcollection upupdir;
     f_get_file(deldir, &dirr,1);
     do_del_nocare(dirr);
     struct_del_dir(&updir,deldir);
     if(updir.isextends==1&&updir.boardnum==0&&updir.subnum==0)
     {
      if(updir.extends[0]==0)
      {
       f_get_file(updir.updir,&upupdir,1);
       upupdir.extends[0]=0;
       f_replace_file(upupdir);
      }
      else
      {
       f_get_file(updir.updir,&upupdir,1);
       snprintf(upupdir.extends,80,"%s",updir.extends);
       f_replace_file(upupdir);
      }
      removefile(updir.dirname);
     }
     else
     {
      f_replace_file(updir);
     }
     return 0;
     
    }

    大體來說就是這些了,其他的如果要向編寫一個好的程序的話,良好的編程風格是需要的,還有,頭腦一定要清楚你干的是什么?要多思考,因為,很有可能,某些架構已開始就錯誤了,后來有可能就走不下去了

    一定要想得細致周到

    大概就獲得這些經驗吧,對了,還研究了一些javascript的語法,php的相關知識,配置了一下apache服務器,感覺還是蠻清楚地

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>