Android培訓班(16)

來源:互聯網
上載者:User

parse_config_file函數是分析*.rc設定檔,並且把裡面的參數組成鏈表的方式。下面來仔細地分析代碼,如下:

#001  int parse_config_file(const char *fn)

#002  {

輸入來的參數是檔案名稱的路徑。

 

#003      char *data;

#004      data = read_file(fn, 0);

#005      if (!data) return -1;

這段代碼是從檔案裡讀取資料,並儲存資料緩衝區的指標在data裡。

 

#006 

#007      parse_config(fn, data);

這行代碼是分析data資料裡,然後把裡面的參數組成鏈表的方式。

 

#008      DUMP();

這行代碼是用來調試時輸出設定檔裡的內容。

 

#009      return 0;

#010  }

 

 

接著下來分析函數read_file的代碼,如下:

#001  /* reads a file, making sure it is terminated with */

#002  void *read_file(const char *fn, unsigned *_sz)

#003  {

#004      char *data;

#005      int sz;

#006      int fd;

#007 

#008      data = 0;

#009      fd = open(fn, O_RDONLY);

#010      if(fd < 0) return 0;

這段代碼是開啟檔案路徑為fn的檔案,使用唯讀方式開啟。

 

#011 

#012      sz = lseek(fd, 0, SEEK_END);

#013      if(sz < 0) goto oops;

這段代碼是移動檔案指標到檔案末尾,然後計算出檔案的長度。

 

#014 

#015      if(lseek(fd, 0, SEEK_SET) != 0) goto oops;

這段代碼是移動到檔案頭開始位置。

 

#016 

#017      data = (char*) malloc(sz + 2);

#018      if(data == 0) goto oops;

這段代碼是分配檔案所需記憶體大小。

 

#019 

#020      if(read(fd, data, sz) != sz) goto oops;

#021      close(fd);

#022      data[sz] = ;

#023      data[sz+1] = 0;

這段代碼是讀取檔案資料到緩衝區,並設定緩衝區最後的結束字元為分行符號和0字元。

 

#024      if(_sz) *_sz = sz;

#025      return data;

這段代碼是返迴文件的長度和檔案緩衝區的指標。

 

#026 

#027  oops:

#028      close(fd);

#029      if(data != 0) free(data);

#030      return 0;

#031  }

這段代碼是讀取檔案出錯,刪除緩衝區。

 

 

 

 

再接著來分析函數parse_config,主要實現從緩衝區裡分析配置資料,產生鏈表。它的代碼如下:

#001  static void parse_config(const char *fn, char *s)

#002  {

#003      struct parse_state state;

#004      char *args[SVC_MAXARGS];

#005      int nargs;

#006 

#007      nargs = 0;

#008      state.filename = fn;

#009      state.line = 1;

#010      state.ptr = s;

#011      state.nexttoken = 0;

#012      state.parse_line = parse_line_no_op;

這段代碼是設定分析開始狀態,其實state.filename指向檔案名稱;state.line是第一行;state.ptr是指向資料緩衝區;state.nexttoken是下一個詞位置;state.parse_line是指向空操作行函數。

 

下面開始迴圈適別所有設定檔。

#013      for (;;) {

 

下面根據三種狀態進行處理。

#014          switch (next_token(&state)) {

#015          case T_EOF:

 

已經到了檔案結尾字元處理。

#016              state.parse_line(&state, 0, 0);

#017              return;

 

新一行設定檔處理。

#018          case T_NEWLINE:

#019              if (nargs) {

 

尋找這一行裡配置的關鍵字。

#020                  int kw = lookup_keyword(args[0]);

#021                  if (kw_is(kw, SECTION)) {

 

是關鍵字處理,分近這一行字元。

#022                      state.parse_line(&state, 0, 0);

 

調用函數parse_new_section來分析這一節設定檔的意思,比如服務或者動作,或者參數等等。

#023                      parse_new_section(&state, kw, nargs, args);

#024                  } else {

 

儲存一行的參數。

#025                      state.parse_line(&state, nargs, args);

#026                  }

#027                  nargs = 0;

#028              }

#029              break;

 

儲存命令配置的參數。

#030          case T_TEXT:

#031              if (nargs < SVC_MAXARGS) {

#032                  args[nargs++] = state.text;

#033              }

#034              break;

#035          }

#036      }

#037  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.