├── .travis.yml ├── COPYING.LESSER ├── README.md ├── SConscript ├── example ├── SConscript └── libcsv_demo.c ├── filestest ├── format_correct.csv ├── format_error.csv └── readme.md ├── libcsv.c ├── libcsv.h ├── libcsv_development_manual.pdf └── script ├── script_bspcfg └── script_scons /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | 3 | notifications: 4 | email: true 5 | 6 | git: 7 | depth: 3 8 | 9 | before_script: 10 | - sudo apt-get update 11 | - "sudo apt-get -qq install gcc-multilib libc6:i386 libgcc1:i386 gcc-4.6-base:i386 libstdc++5:i386 libstdc++6:i386 libsdl-dev || true" 12 | - "[ $RTT_TOOL_CHAIN = 'sourcery-arm' ] && curl -s https://launchpadlibrarian.net/287101520/gcc-arm-none-eabi-5_4-2016q3-20160926-linux.tar.bz2 | sudo tar xjf - -C /opt && export RTT_EXEC_PATH=/opt/gcc-arm-none-eabi-5_4-2016q3/bin && /opt/gcc-arm-none-eabi-5_4-2016q3/bin/arm-none-eabi-gcc --version || true" 13 | 14 | - git clone --depth=3 --branch=master https://github.com/RT-Thread/rt-thread.git ../RT-Thread 15 | 16 | - export RTT_ROOT=/home/travis/build/$USER_NAME/RT-Thread 17 | - "[ x$RTT_CC == x ] && export RTT_CC='gcc' || true" 18 | 19 | - sudo mkdir $RTT_ROOT/bsp/$RTT_BSP/packages 20 | - sudo cp /home/travis/build/$USER_NAME/$REPO_NAME/script/script_bspcfg $RTT_ROOT/bsp/$RTT_BSP/rtconfig.h 21 | - sudo cp /home/travis/build/$USER_NAME/$REPO_NAME/script/script_scons $RTT_ROOT/bsp/$RTT_BSP/packages/SConscript 22 | - sudo cp -r /home/travis/build/$USER_NAME/$REPO_NAME $RTT_ROOT/bsp/$RTT_BSP/packages/$REPO_NAME 23 | 24 | script: 25 | - scons -C $RTT_ROOT/bsp/$RTT_BSP 26 | 27 | env: 28 | - RTT_BSP='imxrt1052-evk' RTT_TOOL_CHAIN='sourcery-arm' USER_NAME='liu2guang' REPO_NAME='libcsv' 29 | -------------------------------------------------------------------------------- /COPYING.LESSER: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.] 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # libcsv pkg # 2 | 3 | [![Build Status](https://travis-ci.org/liu2guang/libcsv.svg?branch=master)](https://travis-ci.org/liu2guang/libcsv) 4 | [![release](https://img.shields.io/badge/Release-v3.0.4-orange.svg)](https://github.com/liu2guang/libcsv/releases) 5 | --- 6 | 7 | ## 1、介绍 8 | 9 | ### 1.1、CSV格式 10 | CSV文件(Comma-separated values), 就是逗号分隔的数据文件, 常用于数据集成的数据交换部分标准部分. CSV没有正式标准,但是国际互联网工程任务组(IETF)给推荐标准RFC 4180描述了CSV文件的结构. 11 | 12 | ### 1.2、libcsv介绍 13 | libcsv是用纯ANSI C89编写的小型、简单、快速的CSV库,可以读写CSV数据。它提供了一个简单的接口,使用回调函数来处理解析的字段和行,并且可以解析不正确格式化的CSV文件. 14 | 15 | ### 1.3、常见CSV格式配置 16 | 17 | ~~~ 18 | MS-DOS-style lines that end with (CR/LF) characters (optional for the last line) 19 | Jiger: {使用回车换行(两个字符)作为行分隔符,最后一行数据可以没有这两个字符。} 20 | 21 | An optional header record (there is no sure way to detect whether it is present, so care is required when importing). 22 | Jiger:{标题行是否需要,要双方显示约定}. 23 | 24 | Each record "should" contain the same number of comma-separated fields. 25 | Jiger:{每行记录的字段数要相同,使用逗号分隔。} 逗号是默认使用的值,双方可以约定别的。 26 | Any field may be quoted (with double quotes). 27 | 28 | Jiger:{任何字段的值都可以使用双引号括起来}. 为简单期间,可以要求都使用双引号。 29 | Fields containing a line-break, double-quote, and/or commas should be quoted. (If they are not, the file will likely be impossible to process correctly). 30 | Jiger:{字段值中如果有换行符,双引号,逗号的,必须要使用双引号括起来。这是必须的。} 31 | 32 | A (double) quote character in a field must be represented by two (double) quote characters. 33 | Jiger:{如果值中有双引号,使用一对双引号来表示原来的一个双引号} 34 | ~~~ 35 | 36 | ### 1.4、依赖 37 | 38 | - 依赖 [libc](https://github.com/RT-Thread/rt-thread/tree/master/components/libc) 组件. 39 | - 依赖 [dfs](https://www.rt-thread.org/document/site/rtthread-development-guide/rtthread-manual-doc/zh/1chapters/12-chapter_filesystem/) 文件系统. 40 | - 依赖 [env](https://www.rt-thread.org/document/site/rtthread-development-guide/rtthread-tool-manual/env/env-user-manual/) 工具. 41 | - RT-Thread 3.0+,对 bsp 无依赖 42 | - demo程序依赖 [optpaser](https://github.com/liu2guang/optparse) (getopt-like for rtt)在线包. 43 | 44 | > 注: 该仓库基于libcsv-3.0.3 【[源仓库地址](https://sourceforge.net/projects/libcsv/ "点击跳转")】版本进行移植. 45 | 46 | ### 1.5、demo程序 47 | libcsv目前提供了一个验证csv文件是否合法的例程. 48 | 49 | ## 2、获取方式 50 | 51 | 请使用 ENV 工具辅助下载: 52 | 53 | 包的路径为:`RT-Thread online package` -> `miscellaneous packages` -> `libcsv` 54 | 55 | ``` 56 | RT-Thread online packages ---> 57 | miscellaneous packages ---> 58 | [*] libcsv: a small, simple and fast CSV library written in pure ANSI C89. ---> 59 | libcsv version(v1.0.0) ---> 60 | [*] libcsv demo example 61 | ``` 62 | 保存 menuconfig 配置后使用 `pkgs --update` 命令下载软件包 63 | 64 | > 注:demo是测试例程, 帮助理解库的使用. 65 | 66 | ## 3、使用说明 67 | 68 | > 注意: 使用说明见源码下的`libcsv_development_manual.pdf`手册. 后期会整理翻译成中文文档. 69 | 70 | ## 4、注意事项 71 | 72 | > libcsv库移植到RT-Thread过程中只对代码格式和目录格式做调整. 73 | 74 | ## 5、许可方式 75 | 76 | GNU Library or Lesser General Public License version 2.0 (LGPLv2). 77 | 更多关于许可信息请浏览`COPYING.LESSER`文件和源作者仓库的许可声明, 或者直接联系原作者本人. 78 | 79 | ## 6、联系方式 & 感谢 80 | * 感谢:[rgamble](https://sourceforge.net/u/rgamble/profile/) 编写了libcsv库. 81 | * 维护:[liu2guang](https://github.com/liu2guang). 82 | * 主页:https://github.com/liu2guang/libcsv. 83 | -------------------------------------------------------------------------------- /SConscript: -------------------------------------------------------------------------------- 1 | import os 2 | from building import * 3 | 4 | # get current dir path 5 | cwd = GetCurrentDir() 6 | 7 | # init src and inc vars 8 | src = [] 9 | inc = [] 10 | 11 | # add libcsv common include 12 | inc = inc + [cwd] 13 | 14 | # add libcsv basic code 15 | src = src + ['./libcsv.c'] 16 | 17 | # add group to IDE project 18 | objs = DefineGroup('libcsv-3.0.3', src, depend = ['PKG_USING_LIBCSV'], CPPPATH = inc) 19 | 20 | # traversal subscript 21 | list = os.listdir(cwd) 22 | if GetDepend('PKG_USING_LIBCSV'): 23 | for d in list: 24 | path = os.path.join(cwd, d) 25 | if os.path.isfile(os.path.join(path, 'SConscript')): 26 | objs = objs + SConscript(os.path.join(d, 'SConscript')) 27 | 28 | Return('objs') 29 | -------------------------------------------------------------------------------- /example/SConscript: -------------------------------------------------------------------------------- 1 | import os 2 | from building import * 3 | 4 | # get current dir path 5 | cwd = GetCurrentDir() 6 | 7 | # init src and inc vars 8 | src = [] 9 | inc = [] 10 | 11 | # add libcsv common include 12 | inc = inc + [cwd] 13 | 14 | # add libcsv basic code 15 | src = src + Glob('./*.c') 16 | 17 | # add group to IDE project 18 | objs = DefineGroup('libcsv-3.0.3', src, depend = ['LIBCSV_USING_DEMO'], CPPPATH = inc) 19 | 20 | Return('objs') 21 | -------------------------------------------------------------------------------- /example/libcsv_demo.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | /* libcsv */ 11 | #include "libcsv.h" 12 | 13 | /* optparse */ 14 | #include "optparse.h" 15 | 16 | #define PROGRAM_NAME "csvcheck" 17 | #define AUTHORS "Robert Gamble" 18 | 19 | static struct optparse_long longopts[] = 20 | { 21 | {"delimiter", 'd', OPTPARSE_REQUIRED}, 22 | {"quote", 'q', OPTPARSE_REQUIRED}, 23 | {"help", CHAR_MAX + 1, OPTPARSE_NONE}, 24 | {NULL, 0, OPTPARSE_NONE} 25 | }; 26 | 27 | /* The name this program was called with */ 28 | char *program_name; 29 | 30 | /* The delimiter character */ 31 | char delimiter = CSV_COMMA; 32 | 33 | /* The delimiter argument */ 34 | char *delimiter_name; 35 | 36 | /* The quote character */ 37 | char quote = CSV_QUOTE; 38 | 39 | /* The quote argument */ 40 | char *quote_name; 41 | 42 | void usage(int status) 43 | { 44 | if (status != RT_EOK) 45 | fprintf (stderr, "Try `%s --help for more information.\n", program_name); 46 | else { 47 | printf("\ 48 | Usage: %s [OPTION]... [FILE]...\n\ 49 | Determine if file(s) are properly formed CSV files and display the position\n\ 50 | of the first offending byte if not.\n\ 51 | \n\ 52 | ", program_name); 53 | printf("\ 54 | -d, --delimiter=DELIM use DELIM as the delimiter instead of comma\n\ 55 | -q, --quote=QUOTE_CHAR use QUOTE_CHAR as the quote character instead of\n\ 56 | double quote\n\ 57 | --help display this help and exit\n\ 58 | "); 59 | } 60 | } 61 | 62 | void check_file(char *filename) 63 | { 64 | size_t pos = 0; 65 | char buf[1024]; 66 | struct csv_parser p; 67 | FILE *fp; 68 | size_t bytes_read; 69 | size_t retval; 70 | 71 | if (csv_init(&p, CSV_STRICT | CSV_STRICT_FINI) != 0) 72 | { 73 | fprintf(stderr, "Failed to initialize csv parser\n"); 74 | return; 75 | } 76 | csv_set_delim(&p, delimiter); 77 | csv_set_quote(&p, quote); 78 | 79 | if (filename == NULL || !strcmp(filename, "-")) 80 | { 81 | fp = stdin; 82 | } 83 | else 84 | { 85 | fp = fopen(filename, "rb"); 86 | if (fp == NULL) 87 | { 88 | fprintf(stderr, "Failed to open file %s: %s\n", filename, strerror(errno)); 89 | csv_free(&p); 90 | return; 91 | } 92 | } 93 | 94 | while ((bytes_read = fread(buf, 1, 1024, fp)) > 0) 95 | { 96 | if ((retval = csv_parse(&p, buf, bytes_read, NULL, NULL, NULL)) != bytes_read) 97 | { 98 | if (csv_error(&p) == CSV_EPARSE) 99 | { 100 | printf("%s: malformed at byte %lu\n", filename ? filename : "stdin", (unsigned long)pos + retval + 1); 101 | goto end; 102 | } 103 | else 104 | { 105 | printf("Error while processing %s: %s\n", filename ? filename : "stdin", csv_strerror(csv_error(&p))); 106 | goto end; 107 | } 108 | } 109 | pos += 1024; 110 | } 111 | 112 | if (csv_fini(&p, NULL, NULL, NULL) != 0) 113 | printf("%s: missing closing quote at end of input\n", filename ? filename : "stdin"); 114 | else 115 | printf("%s well-formed\n", filename ? filename : "data is"); 116 | 117 | end: 118 | fclose(fp); 119 | } 120 | 121 | int csvcheck(int argc, char *argv[]) 122 | { 123 | int optc; 124 | int option_index; 125 | program_name = argv[0]; 126 | struct optparse options; 127 | 128 | if(argc == 1) 129 | { 130 | usage(RT_EOK); 131 | return RT_EOK; 132 | } 133 | 134 | optparse_init(&options, argv); 135 | while ((optc = optparse_long(&options, longopts, &option_index)) != -1) 136 | { 137 | switch (optc) 138 | { 139 | case 'd': 140 | delimiter_name = options.optarg; 141 | if (strlen(delimiter_name) > 1) 142 | printf("delimiter must be exactly one byte long\n"); 143 | else 144 | delimiter = delimiter_name[0]; 145 | break; 146 | 147 | case 'q': 148 | quote_name = options.optarg; 149 | if (strlen(quote_name) > 1) 150 | printf("delimiter must be exactly one byte long\n"); 151 | else 152 | quote = quote_name[0]; 153 | break; 154 | 155 | case CHAR_MAX + 1: 156 | /* --help */ 157 | usage(RT_EOK); 158 | return RT_EOK; 159 | 160 | default: 161 | usage(RT_ERROR); 162 | return RT_ERROR; 163 | } 164 | } 165 | 166 | if (options.optind < argc) 167 | { 168 | while (options.optind < argc) 169 | { 170 | check_file(argv[options.optind++]); 171 | } 172 | } 173 | else 174 | { 175 | /* Process from stdin */ 176 | check_file(NULL); 177 | } 178 | 179 | return RT_EOK; 180 | } 181 | MSH_CMD_EXPORT(csvcheck, The command to check CSV files format based on the libcsv.); 182 | -------------------------------------------------------------------------------- /filestest/format_correct.csv: -------------------------------------------------------------------------------- 1 | 1,2 , 3 ,4,5 2 | -------------------------------------------------------------------------------- /filestest/format_error.csv: -------------------------------------------------------------------------------- 1 | "hello","world" 2 | "rtthread,"libcsv -------------------------------------------------------------------------------- /filestest/readme.md: -------------------------------------------------------------------------------- 1 | ## 测试文件 ## 2 | 3 | - format_correct.csv: 正确格式csv文件. 4 | - format_error.csv: 错误格式csv文件. -------------------------------------------------------------------------------- /libcsv.c: -------------------------------------------------------------------------------- 1 | /* 2 | libcsv - parse and write csv data 3 | Copyright (C) 2008 Robert Gamble 4 | 5 | This library is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 2.1 of the License, or (at your option) any later version. 9 | 10 | This library is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this library; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | */ 19 | 20 | #if ___STDC_VERSION__ >= 199901L 21 | #include 22 | #else 23 | #define SIZE_MAX ((size_t)-1) /* C89 doesn't have stdint.h or SIZE_MAX */ 24 | #endif 25 | 26 | #include "libcsv.h" 27 | 28 | #define VERSION "3.0.3" 29 | 30 | #define ROW_NOT_BEGUN 0 31 | #define FIELD_NOT_BEGUN 1 32 | #define FIELD_BEGUN 2 33 | #define FIELD_MIGHT_HAVE_ENDED 3 34 | 35 | /* 36 | Explanation of states 37 | ROW_NOT_BEGUN There have not been any fields encountered for this row 38 | FIELD_NOT_BEGUN There have been fields but we are currently not in one 39 | FIELD_BEGUN We are in a field 40 | FIELD_MIGHT_HAVE_ENDED 41 | We encountered a double quote inside a quoted field, the 42 | field is either ended or the quote is literal 43 | */ 44 | 45 | #define MEM_BLK_SIZE 128 46 | 47 | #define SUBMIT_FIELD(p) \ 48 | do \ 49 | { \ 50 | if (!quoted) \ 51 | entry_pos -= spaces; \ 52 | if (p->options & CSV_APPEND_NULL) \ 53 | ((p)->entry_buf[entry_pos]) = '\0'; \ 54 | if (cb1 && (p->options & CSV_EMPTY_IS_NULL) && !quoted && entry_pos == 0) \ 55 | cb1(NULL, entry_pos, data); \ 56 | else if (cb1) \ 57 | cb1(p->entry_buf, entry_pos, data); \ 58 | pstate = FIELD_NOT_BEGUN; \ 59 | entry_pos = quoted = spaces = 0; \ 60 | } while (0) 61 | 62 | #define SUBMIT_ROW(p, c) \ 63 | do \ 64 | { \ 65 | if (cb2) \ 66 | cb2(c, data); \ 67 | pstate = ROW_NOT_BEGUN; \ 68 | entry_pos = quoted = spaces = 0; \ 69 | } while (0) 70 | 71 | #define SUBMIT_CHAR(p, c) ((p)->entry_buf[entry_pos++] = (c)) 72 | 73 | static char *csv_errors[] = 74 | { 75 | "success", 76 | "error parsing data while strict checking enabled", 77 | "memory exhausted while increasing buffer size", 78 | "data size too large", 79 | "invalid status code"}; 80 | 81 | int csv_error(struct csv_parser *p) 82 | { 83 | /* Return the current status of the parser */ 84 | return p->status; 85 | } 86 | 87 | char *csv_strerror(int status) 88 | { 89 | /* Return a textual description of status */ 90 | if (status >= CSV_EINVALID || status < 0) 91 | return csv_errors[CSV_EINVALID]; 92 | else 93 | return csv_errors[status]; 94 | } 95 | 96 | int csv_get_opts(struct csv_parser *p) 97 | { 98 | /* Return the currently set options of parser */ 99 | if (p == NULL) 100 | return -1; 101 | 102 | return p->options; 103 | } 104 | 105 | int csv_set_opts(struct csv_parser *p, unsigned char options) 106 | { 107 | /* Set the options */ 108 | if (p == NULL) 109 | return -1; 110 | 111 | p->options = options; 112 | return 0; 113 | } 114 | 115 | int csv_init(struct csv_parser *p, unsigned char options) 116 | { 117 | /* Initialize a csv_parser object returns 0 on success, -1 on error */ 118 | if (p == NULL) 119 | return -1; 120 | 121 | p->entry_buf = NULL; 122 | p->pstate = ROW_NOT_BEGUN; 123 | p->quoted = 0; 124 | p->spaces = 0; 125 | p->entry_pos = 0; 126 | p->entry_size = 0; 127 | p->status = 0; 128 | p->options = options; 129 | p->quote_char = CSV_QUOTE; 130 | p->delim_char = CSV_COMMA; 131 | p->is_space = NULL; 132 | p->is_term = NULL; 133 | p->blk_size = MEM_BLK_SIZE; 134 | p->malloc_func = NULL; 135 | p->realloc_func = realloc; 136 | p->free_func = free; 137 | 138 | return 0; 139 | } 140 | 141 | void csv_free(struct csv_parser *p) 142 | { 143 | /* Free the entry_buffer of csv_parser object */ 144 | if (p == NULL) 145 | return; 146 | 147 | if (p->entry_buf) 148 | p->free_func(p->entry_buf); 149 | 150 | p->entry_buf = NULL; 151 | p->entry_size = 0; 152 | 153 | return; 154 | } 155 | 156 | int csv_fini(struct csv_parser *p, void (*cb1)(void *, size_t, void *), void (*cb2)(int c, void *), void *data) 157 | { 158 | /* Finalize parsing. Needed, for example, when file does not end in a newline */ 159 | int quoted = p->quoted; 160 | int pstate = p->pstate; 161 | size_t spaces = p->spaces; 162 | size_t entry_pos = p->entry_pos; 163 | 164 | /* Liu2guang add MDK5 warring */ 165 | pstate = pstate; 166 | 167 | if (p == NULL) 168 | return -1; 169 | 170 | if (p->pstate == FIELD_BEGUN && p->quoted && p->options & CSV_STRICT && p->options & CSV_STRICT_FINI) 171 | { 172 | /* Current field is quoted, no end-quote was seen, and CSV_STRICT_FINI is set */ 173 | p->status = CSV_EPARSE; 174 | return -1; 175 | } 176 | 177 | switch (p->pstate) 178 | { 179 | case FIELD_MIGHT_HAVE_ENDED: 180 | p->entry_pos -= p->spaces + 1; /* get rid of spaces and original quote */ 181 | /* Fall-through */ 182 | case FIELD_NOT_BEGUN: 183 | case FIELD_BEGUN: 184 | quoted = p->quoted, pstate = p->pstate; 185 | spaces = p->spaces, entry_pos = p->entry_pos; 186 | SUBMIT_FIELD(p); 187 | SUBMIT_ROW(p, -1); 188 | case ROW_NOT_BEGUN: /* Already ended properly */ 189 | ; 190 | } 191 | 192 | /* Reset parser */ 193 | p->spaces = p->quoted = p->entry_pos = p->status = 0; 194 | p->pstate = ROW_NOT_BEGUN; 195 | 196 | return 0; 197 | } 198 | 199 | void csv_set_delim(struct csv_parser *p, unsigned char c) 200 | { 201 | /* Set the delimiter */ 202 | if (p) 203 | p->delim_char = c; 204 | } 205 | 206 | void csv_set_quote(struct csv_parser *p, unsigned char c) 207 | { 208 | /* Set the quote character */ 209 | if (p) 210 | p->quote_char = c; 211 | } 212 | 213 | unsigned char csv_get_delim(struct csv_parser *p) 214 | { 215 | /* Get the delimiter */ 216 | return p->delim_char; 217 | } 218 | 219 | unsigned char csv_get_quote(struct csv_parser *p) 220 | { 221 | /* Get the quote character */ 222 | return p->quote_char; 223 | } 224 | 225 | void csv_set_space_func(struct csv_parser *p, int (*f)(unsigned char)) 226 | { 227 | /* Set the space function */ 228 | if (p) 229 | p->is_space = f; 230 | } 231 | 232 | void csv_set_term_func(struct csv_parser *p, int (*f)(unsigned char)) 233 | { 234 | /* Set the term function */ 235 | if (p) 236 | p->is_term = f; 237 | } 238 | 239 | void csv_set_realloc_func(struct csv_parser *p, void *(*f)(void *, size_t)) 240 | { 241 | /* Set the realloc function used to increase buffer size */ 242 | if (p && f) 243 | p->realloc_func = f; 244 | } 245 | 246 | void csv_set_free_func(struct csv_parser *p, void (*f)(void *)) 247 | { 248 | /* Set the free function used to free the buffer */ 249 | if (p && f) 250 | p->free_func = f; 251 | } 252 | 253 | void csv_set_blk_size(struct csv_parser *p, size_t size) 254 | { 255 | /* Set the block size used to increment buffer size */ 256 | if (p) 257 | p->blk_size = size; 258 | } 259 | 260 | size_t csv_get_buffer_size(struct csv_parser *p) 261 | { 262 | /* Get the size of the entry buffer */ 263 | if (p) 264 | return p->entry_size; 265 | return 0; 266 | } 267 | 268 | static int csv_increase_buffer(struct csv_parser *p) 269 | { 270 | /* Increase the size of the entry buffer. Attempt to increase size by 271 | * p->blk_size, if this is larger than SIZE_MAX try to increase current 272 | * buffer size to SIZE_MAX. If allocation fails, try to allocate halve 273 | * the size and try again until successful or increment size is zero. 274 | */ 275 | 276 | size_t to_add = p->blk_size; 277 | void *vp; 278 | 279 | if (p->entry_size >= SIZE_MAX - to_add) 280 | to_add = SIZE_MAX - p->entry_size; 281 | 282 | if (!to_add) 283 | { 284 | p->status = CSV_ETOOBIG; 285 | return -1; 286 | } 287 | 288 | while ((vp = p->realloc_func(p->entry_buf, p->entry_size + to_add)) == NULL) 289 | { 290 | to_add /= 2; 291 | if (!to_add) 292 | { 293 | p->status = CSV_ENOMEM; 294 | return -1; 295 | } 296 | } 297 | 298 | /* Update entry buffer pointer and entry_size if successful */ 299 | p->entry_buf = vp; 300 | p->entry_size += to_add; 301 | return 0; 302 | } 303 | 304 | size_t csv_parse(struct csv_parser *p, const void *s, size_t len, void (*cb1)(void *, size_t, void *), void (*cb2)(int c, void *), void *data) 305 | { 306 | unsigned const char *us = s; /* Access input data as array of unsigned char */ 307 | unsigned char c; /* The character we are currently processing */ 308 | size_t pos = 0; /* The number of characters we have processed in this call */ 309 | 310 | /* Store key fields into local variables for performance */ 311 | unsigned char delim = p->delim_char; 312 | unsigned char quote = p->quote_char; 313 | int (*is_space)(unsigned char) = p->is_space; 314 | int (*is_term)(unsigned char) = p->is_term; 315 | int quoted = p->quoted; 316 | int pstate = p->pstate; 317 | size_t spaces = p->spaces; 318 | size_t entry_pos = p->entry_pos; 319 | 320 | if (!p->entry_buf && pos < len) 321 | { 322 | /* Buffer hasn't been allocated yet and len > 0 */ 323 | if (csv_increase_buffer(p) != 0) 324 | { 325 | p->quoted = quoted, p->pstate = pstate, p->spaces = spaces, p->entry_pos = entry_pos; 326 | return pos; 327 | } 328 | } 329 | 330 | while (pos < len) 331 | { 332 | /* Check memory usage, increase buffer if neccessary */ 333 | if (entry_pos == ((p->options & CSV_APPEND_NULL) ? p->entry_size - 1 : p->entry_size)) 334 | { 335 | if (csv_increase_buffer(p) != 0) 336 | { 337 | p->quoted = quoted, p->pstate = pstate, p->spaces = spaces, p->entry_pos = entry_pos; 338 | return pos; 339 | } 340 | } 341 | 342 | c = us[pos++]; 343 | 344 | switch (pstate) 345 | { 346 | case ROW_NOT_BEGUN: 347 | case FIELD_NOT_BEGUN: 348 | if ((is_space ? is_space(c) : c == CSV_SPACE || c == CSV_TAB) && c != delim) /* Space or Tab */ 349 | { 350 | continue; 351 | } 352 | else if (is_term ? is_term(c) : c == CSV_CR || c == CSV_LF) /* Carriage Return or Line Feed */ 353 | { 354 | if (pstate == FIELD_NOT_BEGUN) 355 | { 356 | SUBMIT_FIELD(p); 357 | SUBMIT_ROW(p, (unsigned char)c); 358 | } 359 | else /* ROW_NOT_BEGUN */ 360 | { 361 | /* Don't submit empty rows by default */ 362 | if (p->options & CSV_REPALL_NL) 363 | { 364 | SUBMIT_ROW(p, (unsigned char)c); 365 | } 366 | } 367 | continue; 368 | } 369 | else if (c == delim) /* Comma */ 370 | { 371 | SUBMIT_FIELD(p); 372 | break; 373 | } 374 | else if (c == quote) /* Quote */ 375 | { 376 | pstate = FIELD_BEGUN; 377 | quoted = 1; 378 | } 379 | else /* Anything else */ 380 | { 381 | pstate = FIELD_BEGUN; 382 | quoted = 0; 383 | SUBMIT_CHAR(p, c); 384 | } 385 | break; 386 | case FIELD_BEGUN: 387 | if (c == quote) /* Quote */ 388 | { 389 | if (quoted) 390 | { 391 | SUBMIT_CHAR(p, c); 392 | pstate = FIELD_MIGHT_HAVE_ENDED; 393 | } 394 | else 395 | { 396 | /* STRICT ERROR - double quote inside non-quoted field */ 397 | if (p->options & CSV_STRICT) 398 | { 399 | p->status = CSV_EPARSE; 400 | p->quoted = quoted, p->pstate = pstate, p->spaces = spaces, p->entry_pos = entry_pos; 401 | return pos - 1; 402 | } 403 | SUBMIT_CHAR(p, c); 404 | spaces = 0; 405 | } 406 | } 407 | else if (c == delim) /* Comma */ 408 | { 409 | if (quoted) 410 | { 411 | SUBMIT_CHAR(p, c); 412 | } 413 | else 414 | { 415 | SUBMIT_FIELD(p); 416 | } 417 | } 418 | else if (is_term ? is_term(c) : c == CSV_CR || c == CSV_LF) /* Carriage Return or Line Feed */ 419 | { 420 | if (!quoted) 421 | { 422 | SUBMIT_FIELD(p); 423 | SUBMIT_ROW(p, (unsigned char)c); 424 | } 425 | else 426 | { 427 | SUBMIT_CHAR(p, c); 428 | } 429 | } 430 | else if (!quoted && (is_space ? is_space(c) : c == CSV_SPACE || c == CSV_TAB)) /* Tab or space for non-quoted field */ 431 | { 432 | SUBMIT_CHAR(p, c); 433 | spaces++; 434 | } 435 | else /* Anything else */ 436 | { 437 | SUBMIT_CHAR(p, c); 438 | spaces = 0; 439 | } 440 | break; 441 | case FIELD_MIGHT_HAVE_ENDED: 442 | /* This only happens when a quote character is encountered in a quoted field */ 443 | if (c == delim) /* Comma */ 444 | { 445 | entry_pos -= spaces + 1; /* get rid of spaces and original quote */ 446 | SUBMIT_FIELD(p); 447 | } 448 | else if (is_term ? is_term(c) : c == CSV_CR || c == CSV_LF) /* Carriage Return or Line Feed */ 449 | { 450 | entry_pos -= spaces + 1; /* get rid of spaces and original quote */ 451 | SUBMIT_FIELD(p); 452 | SUBMIT_ROW(p, (unsigned char)c); 453 | } 454 | else if (is_space ? is_space(c) : c == CSV_SPACE || c == CSV_TAB) /* Space or Tab */ 455 | { 456 | SUBMIT_CHAR(p, c); 457 | spaces++; 458 | } 459 | else if (c == quote) /* Quote */ 460 | { 461 | if (spaces) 462 | { 463 | /* STRICT ERROR - unescaped double quote */ 464 | if (p->options & CSV_STRICT) 465 | { 466 | p->status = CSV_EPARSE; 467 | p->quoted = quoted, p->pstate = pstate, p->spaces = spaces, p->entry_pos = entry_pos; 468 | return pos - 1; 469 | } 470 | spaces = 0; 471 | SUBMIT_CHAR(p, c); 472 | } 473 | else 474 | { 475 | /* Two quotes in a row */ 476 | pstate = FIELD_BEGUN; 477 | } 478 | } 479 | else /* Anything else */ 480 | { 481 | /* STRICT ERROR - unescaped double quote */ 482 | if (p->options & CSV_STRICT) 483 | { 484 | p->status = CSV_EPARSE; 485 | p->quoted = quoted, p->pstate = pstate, p->spaces = spaces, p->entry_pos = entry_pos; 486 | return pos - 1; 487 | } 488 | pstate = FIELD_BEGUN; 489 | spaces = 0; 490 | SUBMIT_CHAR(p, c); 491 | } 492 | break; 493 | default: 494 | break; 495 | } 496 | } 497 | p->quoted = quoted, p->pstate = pstate, p->spaces = spaces, p->entry_pos = entry_pos; 498 | return pos; 499 | } 500 | 501 | size_t csv_write(void *dest, size_t dest_size, const void *src, size_t src_size) 502 | { 503 | unsigned char *cdest = dest; 504 | const unsigned char *csrc = src; 505 | size_t chars = 0; 506 | 507 | if (src == NULL) 508 | return 0; 509 | 510 | if (cdest == NULL) 511 | dest_size = 0; 512 | 513 | if (dest_size > 0) 514 | *cdest++ = '"'; 515 | chars++; 516 | 517 | while (src_size) 518 | { 519 | if (*csrc == '"') 520 | { 521 | if (dest_size > chars) 522 | *cdest++ = '"'; 523 | if (chars < SIZE_MAX) 524 | chars++; 525 | } 526 | if (dest_size > chars) 527 | *cdest++ = *csrc; 528 | if (chars < SIZE_MAX) 529 | chars++; 530 | src_size--; 531 | csrc++; 532 | } 533 | 534 | if (dest_size > chars) 535 | *cdest = '"'; 536 | if (chars < SIZE_MAX) 537 | chars++; 538 | 539 | return chars; 540 | } 541 | 542 | int csv_fwrite(FILE *fp, const void *src, size_t src_size) 543 | { 544 | const unsigned char *csrc = src; 545 | 546 | if (fp == NULL || src == NULL) 547 | return 0; 548 | 549 | if (fputc('"', fp) == EOF) 550 | return EOF; 551 | 552 | while (src_size) 553 | { 554 | if (*csrc == '"') 555 | { 556 | if (fputc('"', fp) == EOF) 557 | return EOF; 558 | } 559 | if (fputc(*csrc, fp) == EOF) 560 | return EOF; 561 | src_size--; 562 | csrc++; 563 | } 564 | 565 | if (fputc('"', fp) == EOF) 566 | { 567 | return EOF; 568 | } 569 | 570 | return 0; 571 | } 572 | 573 | size_t csv_write2(void *dest, size_t dest_size, const void *src, size_t src_size, unsigned char quote) 574 | { 575 | unsigned char *cdest = dest; 576 | const unsigned char *csrc = src; 577 | size_t chars = 0; 578 | 579 | if (src == NULL) 580 | return 0; 581 | 582 | if (dest == NULL) 583 | dest_size = 0; 584 | 585 | if (dest_size > 0) 586 | *cdest++ = quote; 587 | chars++; 588 | 589 | while (src_size) 590 | { 591 | if (*csrc == quote) 592 | { 593 | if (dest_size > chars) 594 | *cdest++ = quote; 595 | if (chars < SIZE_MAX) 596 | chars++; 597 | } 598 | if (dest_size > chars) 599 | *cdest++ = *csrc; 600 | if (chars < SIZE_MAX) 601 | chars++; 602 | src_size--; 603 | csrc++; 604 | } 605 | 606 | if (dest_size > chars) 607 | *cdest = quote; 608 | if (chars < SIZE_MAX) 609 | chars++; 610 | 611 | return chars; 612 | } 613 | 614 | int csv_fwrite2(FILE *fp, const void *src, size_t src_size, unsigned char quote) 615 | { 616 | const unsigned char *csrc = src; 617 | 618 | if (fp == NULL || src == NULL) 619 | return 0; 620 | 621 | if (fputc(quote, fp) == EOF) 622 | return EOF; 623 | 624 | while (src_size) 625 | { 626 | if (*csrc == quote) 627 | { 628 | if (fputc(quote, fp) == EOF) 629 | return EOF; 630 | } 631 | if (fputc(*csrc, fp) == EOF) 632 | return EOF; 633 | src_size--; 634 | csrc++; 635 | } 636 | 637 | if (fputc(quote, fp) == EOF) 638 | { 639 | return EOF; 640 | } 641 | 642 | return 0; 643 | } 644 | -------------------------------------------------------------------------------- /libcsv.h: -------------------------------------------------------------------------------- 1 | #ifndef LIBCSV_H__ 2 | #define LIBCSV_H__ 3 | 4 | #include 5 | #include 6 | 7 | #ifdef __cplusplus 8 | extern "C" 9 | { 10 | #endif 11 | 12 | #define CSV_MAJOR 3 13 | #define CSV_MINOR 0 14 | #define CSV_RELEASE 3 15 | 16 | /* Error Codes */ 17 | #define CSV_SUCCESS 0 18 | #define CSV_EPARSE 1 /* Parse error in strict mode */ 19 | #define CSV_ENOMEM 2 /* Out of memory while increasing buffer size */ 20 | #define CSV_ETOOBIG 3 /* Buffer larger than SIZE_MAX needed */ 21 | #define CSV_EINVALID 4 /* Invalid code,should never be received from csv_error */ 22 | 23 | /* parser options */ 24 | #define CSV_STRICT 1 /* enable strict mode */ 25 | #define CSV_REPALL_NL 2 /* report all unquoted carriage returns and linefeeds */ 26 | #define CSV_STRICT_FINI 4 /* causes csv_fini to return CSV_EPARSE if last field is quoted and doesn't containg ending quote */ 27 | #define CSV_APPEND_NULL 8 /* Ensure that all fields are null-terminated */ 28 | #define CSV_EMPTY_IS_NULL 16 /* Pass null pointer to cb1 function when empty, unquoted fields are encountered */ 29 | 30 | /* Character values */ 31 | #define CSV_TAB 0x09 32 | #define CSV_SPACE 0x20 33 | #define CSV_CR 0x0d 34 | #define CSV_LF 0x0a 35 | #define CSV_COMMA 0x2c 36 | #define CSV_QUOTE 0x22 37 | 38 | struct csv_parser 39 | { 40 | int pstate; /* Parser state */ 41 | int quoted; /* Is the current field a quoted field? */ 42 | size_t spaces; /* Number of continious spaces after quote or in a non-quoted field */ 43 | unsigned char *entry_buf; /* Entry buffer */ 44 | size_t entry_pos; /* Current position in entry_buf (and current size of entry) */ 45 | size_t entry_size; /* Size of entry buffer */ 46 | int status; /* Operation status */ 47 | unsigned char options; 48 | unsigned char quote_char; 49 | unsigned char delim_char; 50 | int (*is_space)(unsigned char); 51 | int (*is_term)(unsigned char); 52 | size_t blk_size; 53 | void *(*malloc_func)(size_t); 54 | void *(*realloc_func)(void *, size_t); 55 | void (*free_func)(void *); 56 | }; 57 | 58 | /* Function Prototypes */ 59 | int csv_init(struct csv_parser *p, unsigned char options); 60 | int csv_fini(struct csv_parser *p, void (*cb1)(void *, size_t, void *), void (*cb2)(int, void *), void *data); 61 | void csv_free(struct csv_parser *p); 62 | int csv_error(struct csv_parser *p); 63 | char *csv_strerror(int error); 64 | size_t csv_parse(struct csv_parser *p, const void *s, size_t len, void (*cb1)(void *, size_t, void *), void (*cb2)(int, void *), void *data); 65 | size_t csv_write(void *dest, size_t dest_size, const void *src, size_t src_size); 66 | int csv_fwrite(FILE *fp, const void *src, size_t src_size); 67 | size_t csv_write2(void *dest, size_t dest_size, const void *src, size_t src_size, unsigned char quote); 68 | int csv_fwrite2(FILE *fp, const void *src, size_t src_size, unsigned char quote); 69 | int csv_get_opts(struct csv_parser *p); 70 | int csv_set_opts(struct csv_parser *p, unsigned char options); 71 | void csv_set_delim(struct csv_parser *p, unsigned char c); 72 | void csv_set_quote(struct csv_parser *p, unsigned char c); 73 | unsigned char csv_get_delim(struct csv_parser *p); 74 | unsigned char csv_get_quote(struct csv_parser *p); 75 | void csv_set_space_func(struct csv_parser *p, int (*f)(unsigned char)); 76 | void csv_set_term_func(struct csv_parser *p, int (*f)(unsigned char)); 77 | void csv_set_realloc_func(struct csv_parser *p, void *(*)(void *, size_t)); 78 | void csv_set_free_func(struct csv_parser *p, void (*)(void *)); 79 | void csv_set_blk_size(struct csv_parser *p, size_t); 80 | size_t csv_get_buffer_size(struct csv_parser *p); 81 | 82 | #ifdef __cplusplus 83 | } 84 | #endif 85 | 86 | #endif 87 | -------------------------------------------------------------------------------- /libcsv_development_manual.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liu2guang/libcsv/99576e1e56762900895aeea7eb595916e4b84d86/libcsv_development_manual.pdf -------------------------------------------------------------------------------- /script/script_bspcfg: -------------------------------------------------------------------------------- 1 | #ifndef RT_CONFIG_H__ 2 | #define RT_CONFIG_H__ 3 | 4 | /* Automatically generated file; DO NOT EDIT. */ 5 | /* RT-Thread Configuration */ 6 | 7 | /* RT-Thread Kernel */ 8 | 9 | #define RT_NAME_MAX 8 10 | #define RT_ALIGN_SIZE 4 11 | #define RT_THREAD_PRIORITY_32 12 | #define RT_THREAD_PRIORITY_MAX 32 13 | #define RT_TICK_PER_SECOND 100 14 | #define RT_DEBUG 15 | #define RT_USING_OVERFLOW_CHECK 16 | #define RT_DEBUG_INIT 0 17 | #define RT_DEBUG_THREAD 0 18 | #define RT_USING_HOOK 19 | #define RT_IDEL_HOOK_LIST_SIZE 4 20 | #define IDLE_THREAD_STACK_SIZE 256 21 | 22 | /* Inter-Thread communication */ 23 | 24 | #define RT_USING_SEMAPHORE 25 | #define RT_USING_MUTEX 26 | #define RT_USING_EVENT 27 | #define RT_USING_MAILBOX 28 | #define RT_USING_MESSAGEQUEUE 29 | 30 | /* Memory Management */ 31 | 32 | #define RT_USING_MEMHEAP 33 | #define RT_USING_MEMHEAP_AS_HEAP 34 | #define RT_USING_HEAP 35 | 36 | /* Kernel Device Object */ 37 | 38 | #define RT_USING_DEVICE 39 | #define RT_USING_CONSOLE 40 | #define RT_CONSOLEBUF_SIZE 128 41 | #define RT_CONSOLE_DEVICE_NAME "uart1" 42 | #define ARCH_ARM 43 | #define ARCH_ARM_CORTEX_M 44 | #define ARCH_ARM_CORTEX_M7 45 | 46 | /* RT-Thread Components */ 47 | 48 | #define RT_USING_COMPONENTS_INIT 49 | #define RT_USING_USER_MAIN 50 | #define RT_MAIN_THREAD_STACK_SIZE 2048 51 | 52 | /* C++ features */ 53 | 54 | 55 | /* Command shell */ 56 | 57 | #define RT_USING_FINSH 58 | #define FINSH_THREAD_NAME "tshell" 59 | #define FINSH_USING_HISTORY 60 | #define FINSH_HISTORY_LINES 5 61 | #define FINSH_USING_SYMTAB 62 | #define FINSH_USING_DESCRIPTION 63 | #define FINSH_THREAD_PRIORITY 20 64 | #define FINSH_THREAD_STACK_SIZE 4096 65 | #define FINSH_CMD_SIZE 80 66 | #define FINSH_USING_MSH 67 | #define FINSH_USING_MSH_DEFAULT 68 | #define FINSH_ARG_MAX 10 69 | 70 | /* Device virtual file system */ 71 | 72 | #define RT_USING_DFS 73 | #define DFS_USING_WORKDIR 74 | #define DFS_FILESYSTEMS_MAX 2 75 | #define DFS_FILESYSTEM_TYPES_MAX 2 76 | #define DFS_FD_MAX 4 77 | #define RT_USING_DFS_ELMFAT 78 | 79 | /* elm-chan's FatFs, Generic FAT Filesystem Module */ 80 | 81 | #define RT_DFS_ELM_CODE_PAGE 437 82 | #define RT_DFS_ELM_WORD_ACCESS 83 | #define RT_DFS_ELM_USE_LFN_3 84 | #define RT_DFS_ELM_USE_LFN 3 85 | #define RT_DFS_ELM_MAX_LFN 255 86 | #define RT_DFS_ELM_DRIVES 2 87 | #define RT_DFS_ELM_MAX_SECTOR_SIZE 512 88 | #define RT_DFS_ELM_REENTRANT 89 | #define RT_USING_DFS_DEVFS 90 | 91 | /* Device Drivers */ 92 | 93 | #define RT_USING_DEVICE_IPC 94 | #define RT_PIPE_BUFSZ 512 95 | #define RT_USING_SERIAL 96 | #define RT_USING_I2C 97 | #define RT_USING_I2C_BITOPS 98 | #define RT_USING_PIN 99 | #define RT_USING_RTC 100 | #define RT_USING_SDIO 101 | #define RT_SDIO_STACK_SIZE 512 102 | #define RT_SDIO_THREAD_PRIORITY 15 103 | #define RT_MMCSD_STACK_SIZE 1024 104 | #define RT_MMCSD_THREAD_PREORITY 22 105 | #define RT_MMCSD_MAX_PARTITION 16 106 | #define RT_USING_SPI 107 | 108 | /* Using USB */ 109 | 110 | 111 | /* POSIX layer and C standard library */ 112 | 113 | #define RT_USING_LIBC 114 | 115 | /* Network stack */ 116 | 117 | /* light weight TCP/IP stack */ 118 | 119 | #define RT_USING_LWIP 120 | #define RT_USING_LWIP202 121 | #define RT_LWIP_ICMP 122 | #define RT_LWIP_DNS 123 | #define RT_LWIP_DHCP 124 | #define IP_SOF_BROADCAST 1 125 | #define IP_SOF_BROADCAST_RECV 1 126 | 127 | /* Static IPv4 Address */ 128 | 129 | #define RT_LWIP_IPADDR "192.168.1.30" 130 | #define RT_LWIP_GWADDR "192.168.1.1" 131 | #define RT_LWIP_MSKADDR "255.255.255.0" 132 | #define RT_LWIP_UDP 133 | #define RT_LWIP_TCP 134 | #define RT_MEMP_NUM_NETCONN 8 135 | #define RT_LWIP_PBUF_NUM 16 136 | #define RT_LWIP_RAW_PCB_NUM 4 137 | #define RT_LWIP_UDP_PCB_NUM 4 138 | #define RT_LWIP_TCP_PCB_NUM 4 139 | #define RT_LWIP_TCP_SEG_NUM 40 140 | #define RT_LWIP_TCP_SND_BUF 8196 141 | #define RT_LWIP_TCP_WND 8196 142 | #define RT_LWIP_TCPTHREAD_PRIORITY 10 143 | #define RT_LWIP_TCPTHREAD_MBOX_SIZE 8 144 | #define RT_LWIP_TCPTHREAD_STACKSIZE 1024 145 | #define RT_LWIP_ETHTHREAD_PRIORITY 12 146 | #define RT_LWIP_ETHTHREAD_STACKSIZE 1024 147 | #define RT_LWIP_ETHTHREAD_MBOX_SIZE 8 148 | #define LWIP_NETIF_STATUS_CALLBACK 1 149 | #define SO_REUSE 1 150 | #define LWIP_SO_RCVTIMEO 1 151 | #define LWIP_SO_SNDTIMEO 1 152 | #define LWIP_SO_RCVBUF 1 153 | #define LWIP_NETIF_LOOPBACK 0 154 | 155 | /* Modbus master and slave stack */ 156 | 157 | 158 | /* VBUS(Virtual Software BUS) */ 159 | 160 | 161 | /* Utilities */ 162 | 163 | 164 | /* ARM CMSIS */ 165 | 166 | 167 | /* RT-Thread online packages */ 168 | 169 | /* IoT - internet of things */ 170 | 171 | 172 | /* Wi-Fi */ 173 | 174 | /* Marvell WiFi */ 175 | 176 | 177 | /* Wiced WiFi */ 178 | 179 | 180 | /* security packages */ 181 | 182 | 183 | /* language packages */ 184 | 185 | 186 | /* multimedia packages */ 187 | 188 | 189 | /* tools packages */ 190 | 191 | 192 | /* system packages */ 193 | 194 | #define PKG_USING_LITTLEVGL2RTT 195 | #define PKG_USING_LITTLEVGL2RTT_V001 196 | 197 | /* LittlevGL2RTT Options */ 198 | 199 | #define LV_MEM_DYNAMIC 200 | #define LV_MEM_CUSTOM 1 201 | #define LV_COLOR_DEPTH_16 202 | #define LV_COLOR_DEPTH 16 203 | #define LV_HOR_RES 480 204 | #define LV_VER_RES 272 205 | #define LV_DPI 50 206 | #define LITTLEVGL2RTT_USING_DEMO 207 | 208 | /* peripheral libraries and drivers */ 209 | 210 | 211 | /* miscellaneous packages */ 212 | 213 | 214 | /* sample package */ 215 | 216 | 217 | /* example package: hello */ 218 | 219 | #define SOC_IMXRT1052 220 | #define BOARD_USING_QSPIFLASH 221 | #define BOARD_RT1050_FIRE 222 | 223 | /* RT1050 Bsp Config */ 224 | 225 | /* Select uart drivers */ 226 | 227 | #define RT_USING_UART1 228 | 229 | /* Select spi bus and dev drivers */ 230 | 231 | #define LPSPI_CLK_SOURCE_FROM_PLL3PFD1 232 | #define LPSPI_CLK_SOURCE 0 233 | #define LPSPI_CLK_SOURCE_DIVIDER 8 234 | 235 | /* Select iic bus drivers */ 236 | 237 | 238 | /* Select lcd driver */ 239 | 240 | /* Notice: Fire Board para: 800*480 4 4 8 2 40 10 58 45 */ 241 | 242 | #define RT_USING_LCD 243 | #define LCD_WIDTH 480 244 | #define LCD_HEIGHT 272 245 | #define LCD_HFP 4 246 | #define LCD_VFP 4 247 | #define LCD_HBP 8 248 | #define LCD_VBP 2 249 | #define LCD_HSW 40 250 | #define LCD_VSW 10 251 | #define LCD_BL_PIN 106 252 | #define LCD_RST_PIN 45 253 | #define RT_USING_SDRAM 254 | 255 | #endif 256 | -------------------------------------------------------------------------------- /script/script_scons: -------------------------------------------------------------------------------- 1 | import os 2 | from building import * 3 | 4 | objs = [] 5 | cwd = GetCurrentDir() 6 | list = os.listdir(cwd) 7 | 8 | for item in list: 9 | if os.path.isfile(os.path.join(cwd, item, 'SConscript')): 10 | objs = objs + SConscript(os.path.join(item, 'SConscript')) 11 | 12 | Return('objs') 13 | --------------------------------------------------------------------------------