• buildroot中C语言使用libconfig的实例


    首先在buildroot中开启libconfig

    在config中添加

    BR2_PACKAGE_LIBCONFIG=y

    下面是官方给出来的3个实例

    1. /* ----------------------------------------------------------------------------
    2. libconfig - A library for processing structured configuration files
    3. Copyright (C) 2005-2010 Mark A Lindner
    4. This file is part of libconfig.
    5. This library is free software; you can redistribute it and/or
    6. modify it under the terms of the GNU Lesser General Public License
    7. as published by the Free Software Foundation; either version 2.1 of
    8. the License, or (at your option) any later version.
    9. This library is distributed in the hope that it will be useful, but
    10. WITHOUT ANY WARRANTY; without even the implied warranty of
    11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    12. Lesser General Public License for more details.
    13. You should have received a copy of the GNU Library General Public
    14. License along with this library; if not, see
    15. .
    16. ----------------------------------------------------------------------------
    17. */
    18. #include
    19. #include
    20. #include
    21. /* This example reads the configuration file 'example.cfg' and displays
    22. * some of its contents.
    23. */
    24. int main(int argc, char **argv)
    25. {
    26. config_t cfg;
    27. config_setting_t *setting;
    28. const char *str;
    29. config_init(&cfg);
    30. /* Read the file. If there is an error, report it and exit. */
    31. if(! config_read_file(&cfg, "example.cfg"))
    32. {
    33. fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
    34. config_error_line(&cfg), config_error_text(&cfg));
    35. config_destroy(&cfg);
    36. return(EXIT_FAILURE);
    37. }
    38. /* Get the store name. */
    39. if(config_lookup_string(&cfg, "name", &str))
    40. printf("Store name: %s\n\n", str);
    41. else
    42. fprintf(stderr, "No 'name' setting in configuration file.\n");
    43. /* Output a list of all books in the inventory. */
    44. setting = config_lookup(&cfg, "inventory.books");
    45. if(setting != NULL)
    46. {
    47. int count = config_setting_length(setting);
    48. int i;
    49. printf("%-30s %-30s %-6s %s\n", "TITLE", "AUTHOR", "PRICE", "QTY");
    50. for(i = 0; i < count; ++i)
    51. {
    52. config_setting_t *book = config_setting_get_elem(setting, i);
    53. /* Only output the record if all of the expected fields are present. */
    54. const char *title, *author;
    55. double price;
    56. int qty;
    57. if(!(config_setting_lookup_string(book, "title", &title)
    58. && config_setting_lookup_string(book, "author", &author)
    59. && config_setting_lookup_float(book, "price", &price)
    60. && config_setting_lookup_int(book, "qty", &qty)))
    61. continue;
    62. printf("%-30s %-30s $%6.2f %3d\n", title, author, price, qty);
    63. }
    64. putchar('\n');
    65. }
    66. /* Output a list of all movies in the inventory. */
    67. setting = config_lookup(&cfg, "inventory.movies");
    68. if(setting != NULL)
    69. {
    70. unsigned int count = config_setting_length(setting);
    71. unsigned int i;
    72. printf("%-30s %-10s %-6s %s\n", "TITLE", "MEDIA", "PRICE", "QTY");
    73. for(i = 0; i < count; ++i)
    74. {
    75. config_setting_t *movie = config_setting_get_elem(setting, i);
    76. /* Only output the record if all of the expected fields are present. */
    77. const char *title, *media;
    78. double price;
    79. int qty;
    80. if(!(config_setting_lookup_string(movie, "title", &title)
    81. && config_setting_lookup_string(movie, "media", &media)
    82. && config_setting_lookup_float(movie, "price", &price)
    83. && config_setting_lookup_int(movie, "qty", &qty)))
    84. continue;
    85. printf("%-30s %-10s $%6.2f %3d\n", title, media, price, qty);
    86. }
    87. putchar('\n');
    88. }
    89. config_destroy(&cfg);
    90. return(EXIT_SUCCESS);
    91. }
    92. /* eof */
    1. /* ----------------------------------------------------------------------------
    2. libconfig - A library for processing structured configuration files
    3. Copyright (C) 2005-2010 Mark A Lindner
    4. This file is part of libconfig.
    5. This library is free software; you can redistribute it and/or
    6. modify it under the terms of the GNU Lesser General Public License
    7. as published by the Free Software Foundation; either version 2.1 of
    8. the License, or (at your option) any later version.
    9. This library is distributed in the hope that it will be useful, but
    10. WITHOUT ANY WARRANTY; without even the implied warranty of
    11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    12. Lesser General Public License for more details.
    13. You should have received a copy of the GNU Library General Public
    14. License along with this library; if not, see
    15. .
    16. ----------------------------------------------------------------------------
    17. */
    18. #include
    19. #include
    20. #include
    21. /* This example reads the configuration file 'example.cfg', adds a new
    22. * movie record to the movies list, and writes the updated configuration to
    23. * 'updated.cfg'.
    24. */
    25. int main(int argc, char **argv)
    26. {
    27. static const char *output_file = "updated.cfg";
    28. config_t cfg;
    29. config_setting_t *root, *setting, *movie;
    30. config_init(&cfg);
    31. /* Read the file. If there is an error, report it and exit. */
    32. if(! config_read_file(&cfg, "example.cfg"))
    33. {
    34. fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
    35. config_error_line(&cfg), config_error_text(&cfg));
    36. config_destroy(&cfg);
    37. return(EXIT_FAILURE);
    38. }
    39. /* Find the 'movies' setting. Add intermediate settings if they don't yet
    40. * exist.
    41. */
    42. root = config_root_setting(&cfg);
    43. setting = config_setting_get_member(root, "inventory");
    44. if(!setting)
    45. setting = config_setting_add(root, "inventory", CONFIG_TYPE_GROUP);
    46. setting = config_setting_get_member(setting, "movies");
    47. if(!setting)
    48. setting = config_setting_add(setting, "movies", CONFIG_TYPE_LIST);
    49. /* Create the new movie entry. */
    50. movie = config_setting_add(setting, NULL, CONFIG_TYPE_GROUP);
    51. setting = config_setting_add(movie, "title", CONFIG_TYPE_STRING);
    52. config_setting_set_string(setting, "Buckaroo Banzai");
    53. setting = config_setting_add(movie, "media", CONFIG_TYPE_STRING);
    54. config_setting_set_string(setting, "DVD");
    55. setting = config_setting_add(movie, "price", CONFIG_TYPE_FLOAT);
    56. config_setting_set_float(setting, 12.99);
    57. setting = config_setting_add(movie, "qty", CONFIG_TYPE_INT);
    58. config_setting_set_float(setting, 20);
    59. config_set_options(&cfg, 0);
    60. /* Write out the updated configuration. */
    61. if(! config_write_file(&cfg, output_file))
    62. {
    63. fprintf(stderr, "Error while writing file.\n");
    64. config_destroy(&cfg);
    65. return(EXIT_FAILURE);
    66. }
    67. fprintf(stderr, "Updated configuration successfully written to: %s\n",
    68. output_file);
    69. config_destroy(&cfg);
    70. return(EXIT_SUCCESS);
    71. }
    72. /* eof */
    1. /* ----------------------------------------------------------------------------
    2. libconfig - A library for processing structured configuration files
    3. Copyright (C) 2005-2010 Mark A Lindner
    4. This file is part of libconfig.
    5. This library is free software; you can redistribute it and/or
    6. modify it under the terms of the GNU Lesser General Public License
    7. as published by the Free Software Foundation; either version 2.1 of
    8. the License, or (at your option) any later version.
    9. This library is distributed in the hope that it will be useful, but
    10. WITHOUT ANY WARRANTY; without even the implied warranty of
    11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    12. Lesser General Public License for more details.
    13. You should have received a copy of the GNU Library General Public
    14. License along with this library; if not, see
    15. .
    16. ----------------------------------------------------------------------------
    17. */
    18. #include
    19. #include
    20. #include
    21. /* This example constructs a new configuration in memory and writes it to
    22. * 'newconfig.cfg'.
    23. */
    24. int main(int argc, char **argv)
    25. {
    26. static const char *output_file = "newconfig.cfg";
    27. config_t cfg;
    28. config_setting_t *root, *setting, *group, *array;
    29. int i;
    30. config_init(&cfg);
    31. root = config_root_setting(&cfg);
    32. /* Add some settings to the configuration. */
    33. group = config_setting_add(root, "address", CONFIG_TYPE_GROUP);
    34. setting = config_setting_add(group, "street", CONFIG_TYPE_STRING);
    35. config_setting_set_string(setting, "1 Woz Way");
    36. setting = config_setting_add(group, "city", CONFIG_TYPE_STRING);
    37. config_setting_set_string(setting, "San Jose");
    38. setting = config_setting_add(group, "state", CONFIG_TYPE_STRING);
    39. config_setting_set_string(setting, "CA");
    40. setting = config_setting_add(group, "zip", CONFIG_TYPE_INT);
    41. config_setting_set_int(setting, 95110);
    42. array = config_setting_add(root, "numbers", CONFIG_TYPE_ARRAY);
    43. for(i = 0; i < 10; ++i)
    44. {
    45. setting = config_setting_add(array, NULL, CONFIG_TYPE_INT);
    46. config_setting_set_int(setting, 10 * i);
    47. }
    48. /* Write out the new configuration. */
    49. if(! config_write_file(&cfg, output_file))
    50. {
    51. fprintf(stderr, "Error while writing file.\n");
    52. config_destroy(&cfg);
    53. return(EXIT_FAILURE);
    54. }
    55. fprintf(stderr, "New configuration successfully written to: %s\n",
    56. output_file);
    57. config_destroy(&cfg);
    58. return(EXIT_SUCCESS);
    59. }
    60. /* eof */

    使用的时候需要链接库Makefile中添加 -lconfig

  • 相关阅读:
    【算法集训专题攻克篇】第十四篇之栈
    learn编码器
    Java的类与Golang的结构体的区别
    摩尔投票法(Java)
    Linux centos7 bash编程训练
    大一学生《Web编程基础》期末网页制作 基于HTML+CSS+JavaScript响应式个人主页相册介绍模板
    The 2023 ICPC Asia Regionals Online Contest (1) E. Magical Pair(数论 欧拉函数)
    在Vue.js中,什么是mixins?它们的作用是什么?
    吴恩达机器学习课程资源(笔记、中英文字幕视频、课后作业,提供百度云镜像!)
    终极篇章——解释Spring事务,全网最全,看不懂不可能
  • 原文地址:https://blog.csdn.net/wangmeng0804/article/details/132812050