From: Banana Date: Sun, 6 Aug 2023 20:06:39 +0000 (+0200) Subject: adding utf8filenamecheck to the c tools X-Git-Url: http://91.132.146.200/gitweb/?a=commitdiff_plain;h=532aec52d4157c25768c774727a581eab1eeecbb;p=klimbim.git adding utf8filenamecheck to the c tools --- diff --git a/c/README b/c/README new file mode 100644 index 0000000..3e80216 --- /dev/null +++ b/c/README @@ -0,0 +1,17 @@ +# Klimbim Software collection, A bag full of things +# Copyright (C) 2011-2023 Johannes 'Banana' Keßler +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +A selection of C tools and examples \ No newline at end of file diff --git a/c/utf8filenamecheck/linux/COPYING b/c/utf8filenamecheck/linux/COPYING new file mode 100644 index 0000000..80e659d --- /dev/null +++ b/c/utf8filenamecheck/linux/COPYING @@ -0,0 +1,674 @@ +GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. \ No newline at end of file diff --git a/c/utf8filenamecheck/linux/Makefile b/c/utf8filenamecheck/linux/Makefile new file mode 100644 index 0000000..a1f91c7 --- /dev/null +++ b/c/utf8filenamecheck/linux/Makefile @@ -0,0 +1,9 @@ +CC=gcc +CFLAGS=-O2 --machine-arch=native -pipe -Wall +LIBS= + +# https://github.com/adamretter/utf8-validator-c +CFLAGS+=-Isimdutf8check/ + +all: utf8filenamecheck.c + $(CC) $(CFLAGS) -o bin/utf8filenamecheck utf8filenamecheck.c $(LIBS) \ No newline at end of file diff --git a/c/utf8filenamecheck/linux/README b/c/utf8filenamecheck/linux/README new file mode 100644 index 0000000..5cacc7d --- /dev/null +++ b/c/utf8filenamecheck/linux/README @@ -0,0 +1,18 @@ +# Klimbim Software collection, A bag full of things +# Copyright (C) 2011-2023 Johannes 'Banana' Keßler +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# utf8filenamecheck +Small windows C tool to check if paths in a folder are utf-8 formatted \ No newline at end of file diff --git a/c/utf8filenamecheck/linux/USES b/c/utf8filenamecheck/linux/USES new file mode 100644 index 0000000..6aebec2 --- /dev/null +++ b/c/utf8filenamecheck/linux/USES @@ -0,0 +1 @@ +https://github.com/adamretter/utf8-validator-c \ No newline at end of file diff --git a/c/utf8filenamecheck/linux/bin/utf8filenamecheck b/c/utf8filenamecheck/linux/bin/utf8filenamecheck new file mode 100755 index 0000000..8abae88 Binary files /dev/null and b/c/utf8filenamecheck/linux/bin/utf8filenamecheck differ diff --git a/c/utf8filenamecheck/linux/simdutf8check/simdutf8check.h b/c/utf8filenamecheck/linux/simdutf8check/simdutf8check.h new file mode 100644 index 0000000..7644bfa --- /dev/null +++ b/c/utf8filenamecheck/linux/simdutf8check/simdutf8check.h @@ -0,0 +1,457 @@ +#ifndef SIMDUTF8CHECK_H +#define SIMDUTF8CHECK_H +#include +#include +#include +#include +#include +/* + * legal utf-8 byte sequence + * http://www.unicode.org/versions/Unicode6.0.0/ch03.pdf - page 94 + * + * Code Points 1st 2s 3s 4s + * U+0000..U+007F 00..7F + * U+0080..U+07FF C2..DF 80..BF + * U+0800..U+0FFF E0 A0..BF 80..BF + * U+1000..U+CFFF E1..EC 80..BF 80..BF + * U+D000..U+D7FF ED 80..9F 80..BF + * U+E000..U+FFFF EE..EF 80..BF 80..BF + * U+10000..U+3FFFF F0 90..BF 80..BF 80..BF + * U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF + * U+100000..U+10FFFF F4 80..8F 80..BF 80..BF + * + */ + +// all byte values must be no larger than 0xF4 +static inline void checkSmallerThan0xF4(__m128i current_bytes, + __m128i *has_error) { + // unsigned, saturates to 0 below max + *has_error = _mm_or_si128(*has_error, + _mm_subs_epu8(current_bytes, _mm_set1_epi8(0xF4))); +} + +static inline __m128i continuationLengths(__m128i high_nibbles) { + return _mm_shuffle_epi8( + _mm_setr_epi8(1, 1, 1, 1, 1, 1, 1, 1, // 0xxx (ASCII) + 0, 0, 0, 0, // 10xx (continuation) + 2, 2, // 110x + 3, // 1110 + 4), // 1111, next should be 0 (not checked here) + high_nibbles); +} + +static inline __m128i carryContinuations(__m128i initial_lengths, + __m128i previous_carries) { + + __m128i right1 = + _mm_subs_epu8(_mm_alignr_epi8(initial_lengths, previous_carries, 16 - 1), + _mm_set1_epi8(1)); + __m128i sum = _mm_add_epi8(initial_lengths, right1); + + __m128i right2 = _mm_subs_epu8(_mm_alignr_epi8(sum, previous_carries, 16 - 2), + _mm_set1_epi8(2)); + return _mm_add_epi8(sum, right2); +} + +static inline void checkContinuations(__m128i initial_lengths, __m128i carries, + __m128i *has_error) { + + // overlap || underlap + // carry > length && length > 0 || !(carry > length) && !(length > 0) + // (carries > length) == (lengths > 0) + __m128i overunder = + _mm_cmpeq_epi8(_mm_cmpgt_epi8(carries, initial_lengths), + _mm_cmpgt_epi8(initial_lengths, _mm_setzero_si128())); + + *has_error = _mm_or_si128(*has_error, overunder); +} + +// when 0xED is found, next byte must be no larger than 0x9F +// when 0xF4 is found, next byte must be no larger than 0x8F +// next byte must be continuation, ie sign bit is set, so signed < is ok +static inline void checkFirstContinuationMax(__m128i current_bytes, + __m128i off1_current_bytes, + __m128i *has_error) { + __m128i maskED = _mm_cmpeq_epi8(off1_current_bytes, _mm_set1_epi8(0xED)); + __m128i maskF4 = _mm_cmpeq_epi8(off1_current_bytes, _mm_set1_epi8(0xF4)); + + __m128i badfollowED = + _mm_and_si128(_mm_cmpgt_epi8(current_bytes, _mm_set1_epi8(0x9F)), maskED); + __m128i badfollowF4 = + _mm_and_si128(_mm_cmpgt_epi8(current_bytes, _mm_set1_epi8(0x8F)), maskF4); + + *has_error = _mm_or_si128(*has_error, _mm_or_si128(badfollowED, badfollowF4)); +} + +// map off1_hibits => error condition +// hibits off1 cur +// C => < C2 && true +// E => < E1 && < A0 +// F => < F1 && < 90 +// else false && false +static inline void checkOverlong(__m128i current_bytes, + __m128i off1_current_bytes, __m128i hibits, + __m128i previous_hibits, __m128i *has_error) { + __m128i off1_hibits = _mm_alignr_epi8(hibits, previous_hibits, 16 - 1); + __m128i initial_mins = _mm_shuffle_epi8( + _mm_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128, -128, + -128, -128, // 10xx => false + 0xC2, -128, // 110x + 0xE1, // 1110 + 0xF1), + off1_hibits); + + __m128i initial_under = _mm_cmpgt_epi8(initial_mins, off1_current_bytes); + + __m128i second_mins = _mm_shuffle_epi8( + _mm_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128, -128, + -128, -128, // 10xx => false + 127, 127, // 110x => true + 0xA0, // 1110 + 0x90), + off1_hibits); + __m128i second_under = _mm_cmpgt_epi8(second_mins, current_bytes); + *has_error = + _mm_or_si128(*has_error, _mm_and_si128(initial_under, second_under)); +} + +struct processed_utf_bytes { + __m128i rawbytes; + __m128i high_nibbles; + __m128i carried_continuations; +}; + +static inline void count_nibbles(__m128i bytes, + struct processed_utf_bytes *answer) { + answer->rawbytes = bytes; + answer->high_nibbles = + _mm_and_si128(_mm_srli_epi16(bytes, 4), _mm_set1_epi8(0x0F)); +} + +// check whether the current bytes are valid UTF-8 +// at the end of the function, previous gets updated +static struct processed_utf_bytes +checkUTF8Bytes(__m128i current_bytes, struct processed_utf_bytes *previous, + __m128i *has_error) { + struct processed_utf_bytes pb; + count_nibbles(current_bytes, &pb); + + checkSmallerThan0xF4(current_bytes, has_error); + + __m128i initial_lengths = continuationLengths(pb.high_nibbles); + + pb.carried_continuations = + carryContinuations(initial_lengths, previous->carried_continuations); + + checkContinuations(initial_lengths, pb.carried_continuations, has_error); + + __m128i off1_current_bytes = + _mm_alignr_epi8(pb.rawbytes, previous->rawbytes, 16 - 1); + checkFirstContinuationMax(current_bytes, off1_current_bytes, has_error); + + checkOverlong(current_bytes, off1_current_bytes, pb.high_nibbles, + previous->high_nibbles, has_error); + return pb; +} + +static bool validate_utf8_fast(const char *src, size_t len) { + size_t i = 0; + __m128i has_error = _mm_setzero_si128(); + struct processed_utf_bytes previous = {.rawbytes = _mm_setzero_si128(), + .high_nibbles = _mm_setzero_si128(), + .carried_continuations = + _mm_setzero_si128()}; + if (len >= 16) { + for (; i <= len - 16; i += 16) { + __m128i current_bytes = _mm_loadu_si128((const __m128i *)(src + i)); + previous = checkUTF8Bytes(current_bytes, &previous, &has_error); + } + } + + // last part + if (i < len) { + char buffer[16]; + memset(buffer, 0, 16); + memcpy(buffer, src + i, len - i); + __m128i current_bytes = _mm_loadu_si128((const __m128i *)(buffer)); + previous = checkUTF8Bytes(current_bytes, &previous, &has_error); + } else { + has_error = + _mm_or_si128(_mm_cmpgt_epi8(previous.carried_continuations, + _mm_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 1)), + has_error); + } + + return _mm_testz_si128(has_error, has_error); +} + +#ifdef __AVX2__ + +/*****************************/ +static inline __m256i push_last_byte_of_a_to_b(__m256i a, __m256i b) { + return _mm256_alignr_epi8(b, _mm256_permute2x128_si256(a, b, 0x21), 15); +} + +static inline __m256i push_last_2bytes_of_a_to_b(__m256i a, __m256i b) { + return _mm256_alignr_epi8(b, _mm256_permute2x128_si256(a, b, 0x21), 14); +} + +// all byte values must be no larger than 0xF4 +static inline void avxcheckSmallerThan0xF4(__m256i current_bytes, + __m256i *has_error) { + // unsigned, saturates to 0 below max + *has_error = _mm256_or_si256( + *has_error, _mm256_subs_epu8(current_bytes, _mm256_set1_epi8(0xF4))); +} + +static inline __m256i avxcontinuationLengths(__m256i high_nibbles) { + return _mm256_shuffle_epi8( + _mm256_setr_epi8(1, 1, 1, 1, 1, 1, 1, 1, // 0xxx (ASCII) + 0, 0, 0, 0, // 10xx (continuation) + 2, 2, // 110x + 3, // 1110 + 4, // 1111, next should be 0 (not checked here) + 1, 1, 1, 1, 1, 1, 1, 1, // 0xxx (ASCII) + 0, 0, 0, 0, // 10xx (continuation) + 2, 2, // 110x + 3, // 1110 + 4 // 1111, next should be 0 (not checked here) + ), + high_nibbles); +} + +static inline __m256i avxcarryContinuations(__m256i initial_lengths, + __m256i previous_carries) { + + __m256i right1 = _mm256_subs_epu8( + push_last_byte_of_a_to_b(previous_carries, initial_lengths), + _mm256_set1_epi8(1)); + __m256i sum = _mm256_add_epi8(initial_lengths, right1); + + __m256i right2 = _mm256_subs_epu8( + push_last_2bytes_of_a_to_b(previous_carries, sum), _mm256_set1_epi8(2)); + return _mm256_add_epi8(sum, right2); +} + +static inline void avxcheckContinuations(__m256i initial_lengths, + __m256i carries, __m256i *has_error) { + + // overlap || underlap + // carry > length && length > 0 || !(carry > length) && !(length > 0) + // (carries > length) == (lengths > 0) + __m256i overunder = _mm256_cmpeq_epi8( + _mm256_cmpgt_epi8(carries, initial_lengths), + _mm256_cmpgt_epi8(initial_lengths, _mm256_setzero_si256())); + + *has_error = _mm256_or_si256(*has_error, overunder); +} + +// when 0xED is found, next byte must be no larger than 0x9F +// when 0xF4 is found, next byte must be no larger than 0x8F +// next byte must be continuation, ie sign bit is set, so signed < is ok +static inline void avxcheckFirstContinuationMax(__m256i current_bytes, + __m256i off1_current_bytes, + __m256i *has_error) { + __m256i maskED = + _mm256_cmpeq_epi8(off1_current_bytes, _mm256_set1_epi8(0xED)); + __m256i maskF4 = + _mm256_cmpeq_epi8(off1_current_bytes, _mm256_set1_epi8(0xF4)); + + __m256i badfollowED = _mm256_and_si256( + _mm256_cmpgt_epi8(current_bytes, _mm256_set1_epi8(0x9F)), maskED); + __m256i badfollowF4 = _mm256_and_si256( + _mm256_cmpgt_epi8(current_bytes, _mm256_set1_epi8(0x8F)), maskF4); + + *has_error = + _mm256_or_si256(*has_error, _mm256_or_si256(badfollowED, badfollowF4)); +} + +// map off1_hibits => error condition +// hibits off1 cur +// C => < C2 && true +// E => < E1 && < A0 +// F => < F1 && < 90 +// else false && false +static inline void avxcheckOverlong(__m256i current_bytes, + __m256i off1_current_bytes, __m256i hibits, + __m256i previous_hibits, + __m256i *has_error) { + __m256i off1_hibits = push_last_byte_of_a_to_b(previous_hibits, hibits); + __m256i initial_mins = _mm256_shuffle_epi8( + _mm256_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128, + -128, -128, -128, // 10xx => false + 0xC2, -128, // 110x + 0xE1, // 1110 + 0xF1, -128, -128, -128, -128, -128, -128, -128, -128, + -128, -128, -128, -128, // 10xx => false + 0xC2, -128, // 110x + 0xE1, // 1110 + 0xF1), + off1_hibits); + + __m256i initial_under = _mm256_cmpgt_epi8(initial_mins, off1_current_bytes); + + __m256i second_mins = _mm256_shuffle_epi8( + _mm256_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128, + -128, -128, -128, // 10xx => false + 127, 127, // 110x => true + 0xA0, // 1110 + 0x90, -128, -128, -128, -128, -128, -128, -128, -128, + -128, -128, -128, -128, // 10xx => false + 127, 127, // 110x => true + 0xA0, // 1110 + 0x90), + off1_hibits); + __m256i second_under = _mm256_cmpgt_epi8(second_mins, current_bytes); + *has_error = _mm256_or_si256(*has_error, + _mm256_and_si256(initial_under, second_under)); +} + +struct avx_processed_utf_bytes { + __m256i rawbytes; + __m256i high_nibbles; + __m256i carried_continuations; +}; + +static inline void avx_count_nibbles(__m256i bytes, + struct avx_processed_utf_bytes *answer) { + answer->rawbytes = bytes; + answer->high_nibbles = + _mm256_and_si256(_mm256_srli_epi16(bytes, 4), _mm256_set1_epi8(0x0F)); +} + +// check whether the current bytes are valid UTF-8 +// at the end of the function, previous gets updated +static struct avx_processed_utf_bytes +avxcheckUTF8Bytes(__m256i current_bytes, + struct avx_processed_utf_bytes *previous, + __m256i *has_error) { + struct avx_processed_utf_bytes pb; + avx_count_nibbles(current_bytes, &pb); + + avxcheckSmallerThan0xF4(current_bytes, has_error); + + __m256i initial_lengths = avxcontinuationLengths(pb.high_nibbles); + + pb.carried_continuations = + avxcarryContinuations(initial_lengths, previous->carried_continuations); + + avxcheckContinuations(initial_lengths, pb.carried_continuations, has_error); + + __m256i off1_current_bytes = + push_last_byte_of_a_to_b(previous->rawbytes, pb.rawbytes); + avxcheckFirstContinuationMax(current_bytes, off1_current_bytes, has_error); + + avxcheckOverlong(current_bytes, off1_current_bytes, pb.high_nibbles, + previous->high_nibbles, has_error); + return pb; +} + +// check whether the current bytes are valid UTF-8 +// at the end of the function, previous gets updated +static struct avx_processed_utf_bytes +avxcheckUTF8Bytes_asciipath(__m256i current_bytes, + struct avx_processed_utf_bytes *previous, + __m256i *has_error) { + if (_mm256_testz_si256(current_bytes, + _mm256_set1_epi8(0x80))) { // fast ascii path + *has_error = _mm256_or_si256( + _mm256_cmpgt_epi8(previous->carried_continuations, + _mm256_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 1)), + *has_error); + return *previous; + } + + struct avx_processed_utf_bytes pb; + avx_count_nibbles(current_bytes, &pb); + + avxcheckSmallerThan0xF4(current_bytes, has_error); + + __m256i initial_lengths = avxcontinuationLengths(pb.high_nibbles); + + pb.carried_continuations = + avxcarryContinuations(initial_lengths, previous->carried_continuations); + + avxcheckContinuations(initial_lengths, pb.carried_continuations, has_error); + + __m256i off1_current_bytes = + push_last_byte_of_a_to_b(previous->rawbytes, pb.rawbytes); + avxcheckFirstContinuationMax(current_bytes, off1_current_bytes, has_error); + + avxcheckOverlong(current_bytes, off1_current_bytes, pb.high_nibbles, + previous->high_nibbles, has_error); + return pb; +} + +static bool validate_utf8_fast_avx_asciipath(const char *src, size_t len) { + size_t i = 0; + __m256i has_error = _mm256_setzero_si256(); + struct avx_processed_utf_bytes previous = { + .rawbytes = _mm256_setzero_si256(), + .high_nibbles = _mm256_setzero_si256(), + .carried_continuations = _mm256_setzero_si256()}; + if (len >= 32) { + for (; i <= len - 32; i += 32) { + __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(src + i)); + previous = + avxcheckUTF8Bytes_asciipath(current_bytes, &previous, &has_error); + } + } + + // last part + if (i < len) { + char buffer[32]; + memset(buffer, 0, 32); + memcpy(buffer, src + i, len - i); + __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(buffer)); + previous = avxcheckUTF8Bytes(current_bytes, &previous, &has_error); + } else { + has_error = _mm256_or_si256( + _mm256_cmpgt_epi8(previous.carried_continuations, + _mm256_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 1)), + has_error); + } + + return _mm256_testz_si256(has_error, has_error); +} + +static bool validate_utf8_fast_avx(const char *src, size_t len) { + size_t i = 0; + __m256i has_error = _mm256_setzero_si256(); + struct avx_processed_utf_bytes previous = { + .rawbytes = _mm256_setzero_si256(), + .high_nibbles = _mm256_setzero_si256(), + .carried_continuations = _mm256_setzero_si256()}; + if (len >= 32) { + for (; i <= len - 32; i += 32) { + __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(src + i)); + previous = avxcheckUTF8Bytes(current_bytes, &previous, &has_error); + } + } + + // last part + if (i < len) { + char buffer[32]; + memset(buffer, 0, 32); + memcpy(buffer, src + i, len - i); + __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(buffer)); + previous = avxcheckUTF8Bytes(current_bytes, &previous, &has_error); + } else { + has_error = _mm256_or_si256( + _mm256_cmpgt_epi8(previous.carried_continuations, + _mm256_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 1)), + has_error); + } + + return _mm256_testz_si256(has_error, has_error); +} + +#endif // __AVX2__ +#endif diff --git a/c/utf8filenamecheck/linux/utf8filenamecheck.c b/c/utf8filenamecheck/linux/utf8filenamecheck.c new file mode 100644 index 0000000..1a98014 --- /dev/null +++ b/c/utf8filenamecheck/linux/utf8filenamecheck.c @@ -0,0 +1,148 @@ +/** + * utf8filenamecheck Check if all the filenames in a fiven folder are UTF-8 + * Copyright (C) 2023 Johannes 'Banana' Keßler + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * 2023 Small linux C tool to check if paths in a folder are utf-8 formatted + * Linux version + */ +// https://linux.die.net/man/3/nftw +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include +#include + +// https://github.com/simdutf/simdutf +#include + +/** + * Commandline arguments + * see: https://www.gnu.org/software/libc/manual/html_node/Argp-Example-3.html#Argp-Example-3 + */ +const char *argp_program_version = "1.0"; +const char *argp_program_bug_address = "https://www.bananas-playground.net/"; +static char doc[] = "utf8filenamecheck. Small linux C tool to check if paths in a folder are utf-8 formatted."; +static char args_doc[] = "folder"; + +/* The options we understand. */ +static struct argp_option options[] = { + {"verbose",'v', 0, 0, "Produce verbose output" }, + {"quiet",'q', 0, 0, "Produce verbose output" }, + { 0 } +}; + +struct cmdArguments { + char *args[1]; + int verbose, quiet; +}; + +/* Parse a single option. */ +static error_t +parse_opt (int key, char *arg, struct argp_state *state) { + struct cmdArguments *arguments = state->input; + + switch (key) { + case 'v': + arguments->verbose = 1; + break; + case 'q': + arguments->quiet = 1; + break; + + case ARGP_KEY_ARG: + if (state->arg_num >= 1) + // Too many arguments. + argp_usage (state); + + arguments->args[state->arg_num] = arg; + break; + + case ARGP_KEY_END: + if (state->arg_num < 1) + /* Not enough arguments. */ + argp_usage (state); + break; + + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; +} + +static struct argp argp = { options, parse_opt, args_doc, doc }; +struct cmdArguments arguments; + +/** + * the callback function for nftw + * https://linux.die.net/man/3/nftw + */ +static int nftw_callback(const char *fpath, + const struct stat *sb, + int tflag, + struct FTW *ftwbuf) { + if (strcmp(fpath, ".") == 0 || strcmp(fpath, "..") == 0) { + return 0; + } + + if(tflag == FTW_DNR) { + if(!arguments.quiet) printf("Can not read %s", fpath); + } + + bool result = validate_utf8_fast(fpath, strlen(fpath)); + if(result) { + if(!arguments.quiet) printf("%s OK \n", fpath); + } else { + printf("%s FAILED \n", fpath); + } + + // continue + return 0; +} + +/** + * main routine + */ +int main(int argc, char *argv[]) { + + /** + * command line argument parsing and default values + */ + arguments.verbose = 0; + arguments.quiet = 0; + + argp_parse (&argp, argc, argv, 0, 0, &arguments); + + if(arguments.verbose) { + printf ("Folder = %s\n" + "Verbose = %s\n" + "Quiet = %s\n", + arguments.args[0], + arguments.verbose ? "yes" : "no", + arguments.quiet ? "yes" : "no" + ); + } + + if (nftw(arguments.args[0], nftw_callback, 15, FTW_PHYS)== -1) { + perror("Reading dir failed"); + exit(EXIT_FAILURE); + } + + exit(EXIT_SUCCESS); +} \ No newline at end of file diff --git a/c/utf8filenamecheck/windows/COPYING b/c/utf8filenamecheck/windows/COPYING new file mode 100644 index 0000000..80e659d --- /dev/null +++ b/c/utf8filenamecheck/windows/COPYING @@ -0,0 +1,674 @@ +GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. \ No newline at end of file diff --git a/c/utf8filenamecheck/windows/README b/c/utf8filenamecheck/windows/README new file mode 100644 index 0000000..39abb8d --- /dev/null +++ b/c/utf8filenamecheck/windows/README @@ -0,0 +1,35 @@ +# Klimbim Software collection, A bag full of things +# Copyright (C) 2011-2023 Johannes 'Banana' Keßler +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# utf8filenamecheck +Small windows C tool to check if paths in a folder are utf-8 formatted + +# Uses +- https://www.argtable.org : Argument parsing (argtable-3.1.5-amalgamation) +- https://github.com/adamretter/utf8-validator-c : Check if a string is utf-8 + +# WARNING +This is a very simple, with limited experience written, windows C program. +Use at own risk and feel free to improve. + +# How to build +- gcc from https://jmeubank.github.io/tdm-gcc/ (build with tdm64-gcc-9.2.0) +Run the makefile.bat in a cmd terminal. +Resulting utf8filenamecheck.exe is in bin/ directory. + +# How to use +Run in a terminal `utf8filenamecheck.exe c:\some\dir` +Run utf8filenamecheck.exe --help to see its options. \ No newline at end of file diff --git a/c/utf8filenamecheck/windows/USES b/c/utf8filenamecheck/windows/USES new file mode 100644 index 0000000..84ddd54 --- /dev/null +++ b/c/utf8filenamecheck/windows/USES @@ -0,0 +1,2 @@ +https://www.argtable.org +https://github.com/adamretter/utf8-validator-c \ No newline at end of file diff --git a/c/utf8filenamecheck/windows/argtable/LICENSE b/c/utf8filenamecheck/windows/argtable/LICENSE new file mode 100644 index 0000000..b7853f1 --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/LICENSE @@ -0,0 +1,175 @@ +Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of STEWART HEITMANN nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +NetBSD getopt library +===================== + +Copyright (c) 2000 The NetBSD Foundation, Inc. +All rights reserved. + +This code is derived from software contributed to The NetBSD Foundation +by Dieter Baron and Thomas Klausner. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: + This product includes software developed by the NetBSD + Foundation, Inc. and its contributors. +4. Neither the name of The NetBSD Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS +``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + + +Tcl library +=========== + +This software is copyrighted by the Regents of the University of +California, Sun Microsystems, Inc., Scriptics Corporation, ActiveState +Corporation and other parties. The following terms apply to all files +associated with the software unless explicitly disclaimed in +individual files. + +The authors hereby grant permission to use, copy, modify, distribute, +and license this software and its documentation for any purpose, provided +that existing copyright notices are retained in all copies and that this +notice is included verbatim in any distributions. No written agreement, +license, or royalty fee is required for any of the authorized uses. +Modifications to this software may be copyrighted by their authors +and need not follow the licensing terms described here, provided that +the new terms are clearly indicated on the first page of each file where +they apply. + +IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY +FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES +ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY +DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE +IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE +NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR +MODIFICATIONS. + +GOVERNMENT USE: If you are acquiring this software on behalf of the +U.S. government, the Government shall have only "Restricted Rights" +in the software and related documentation as defined in the Federal +Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you +are acquiring the software on behalf of the Department of Defense, the +software shall be classified as "Commercial Computer Software" and the +Government shall have only "Restricted Rights" as defined in Clause +252.227-7014 (b) (3) of DFARs. Notwithstanding the foregoing, the +authors grant the U.S. Government and others acting in its behalf +permission to use and distribute the software in accordance with the +terms specified in this license. + + +C Hash Table library +==================== + +Copyright (c) 2002, Christopher Clark +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +* Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +* Neither the name of the original author; nor the names of any contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER +OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + +The Better String library +========================= + +Copyright (c) 2014, Paul Hsieh +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of bstrlib nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + diff --git a/c/utf8filenamecheck/windows/argtable/README.md b/c/utf8filenamecheck/windows/argtable/README.md new file mode 100644 index 0000000..8a71ba8 --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/README.md @@ -0,0 +1,203 @@ +[![Build Status](https://travis-ci.org/argtable/argtable3.svg?branch=master)](https://travis-ci.org/argtable/argtable3) + + +Introduction of Argtable3 +========================= + +**Argtable3** is an open source ANSI C library that parses GNU-style +command-line options. It simplifies command-line parsing by defining a +declarative-style API that you can use to specify what your command-line syntax +looks like. Argtable3 will automatically generate consistent error handling +logic and textual descriptions of the command line syntax, which are essential +but tedious to implement for a robust CLI program. + + +Quick Start +----------- + +> We no longer provide the amalgamation source code (`argtable3.c` and +> `argtable3.h`) in the source code repository. You can get the amalgamation +> distribution either from the release page (`argtable-3.x.x-amalgamation.zip`), +> or generate the distribution yourself by using the generator under the `tools` +> directory: +> +> 1. Navigate to the `tools` directory. +> 2. Run `./build dist`, which will generate the distribution under the `/dist` +> directory. + + +Argtable3 is a single-file ANSI-C library. All you have to do is adding +`argtable3.c` to your projects, and including `argtable3.h` in your source code. + +To build the library, examples, and unit tests, use CMake to generate +out-of-source build: + +* If you use GCC (Linux, MacOSX, MinGW, Cygwin), run: + + ``` + $ mkdir build + $ cd build + $ cmake -DCMAKE_BUILD_TYPE=Debug .. + $ make + $ make test + ``` + + Makefile-based generators in CMake only support one configuration at a time, + so you need to specify `CMAKE_BUILD_TYPE` to `Debug`, `Release`, `MinSizeRel`, + or `RelWithDebInfo`. To build multiple configurations, you need to create a + build directory for each configuraiton. + + To cleanup, run `make clean` or remove the build directory: + + ``` + $ rm -rf build + ``` + +* If you use Microsoft Visual C++ compiler, run: + + ``` + $ mkdir build + $ cd build + $ cmake -G "Visual Studio 15 2017 Win64" .. + $ cmake --build . --config Debug + $ ctest -C Debug + ``` + + You can also use Visual Studio 2017 IDE to open the generated solution. To + cleanup, just remove the `build` directory. + + +To build a tagged version, go to the project root directory, and use the +`Makefile` in the project root folder to check out the specified version: + + ``` + $ make taglist + Available TAGs: + v3.1.1.432a160 + $ make co TAG=v3.1.1.432a160 + $ cd .tags/v3.1.1.432a160 + $ mkdir build + $ cd build + $ cmake .. + $ make + $ make test + ``` + +You will find the shared library (or Windows DLL), static library, and the +amalgamation distribution under the build directory. + + +Documentation and Examples +-------------------------- + +To learn how to use the Argtable3 API, you can see the documentation on the web +site, or examples in the `examples` folder. + + +Unit Tests +---------- + +Argtable3 is a BSD-licensed open source library, so you can modify the library +anyway you want. However, before committing your code to your own repository or +the Argtable3 official repository, please make sure your changes won't cause any +compiler warning and can pass the unit tests included in the distribution. + +To build and test each configuration (`Debug`, `Release`, `MinSizeRel`, +`RelWithDebInfo`), you can run CMake and CTest on all supported platforms: + +``` +$ mkdir build_debug && cd build_debug +$ cmake -DCMAKE_BUILD_TYPE=Debug .. +$ cmake --build . --config Debug +$ ctest -C Debug + +$ cd .. && mkdir build_release && cd build_release +$ cmake -DCMAKE_BUILD_TYPE=Release .. +$ cmake --build . --config Release +$ ctest -C Release + +$ cd .. && mkdir build_minsizerel && cd build_minsizerel +$ cmake -DCMAKE_BUILD_TYPE=MinSizeRel .. +$ cmake --build . --config MinSizeRel +$ ctest -C MinSizeRel + +$ cd .. && mkdir build_relwithdebinfo && cd build_relwithdebinfo +$ cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo .. +$ cmake --build . --config RelWithDebInfo +$ ctest -C RelWithDebInfo +``` + + +Authors +------- + +Argtable is Copyright (C) 1998-2001,2003-2011 Stewart Heitmann. +Parts are Copyright (C) 1989-1994, 1996-1999, 2001, 2003 + Free Software Foundation, Inc. + +Argtable was written by Stewart Heitmann + +Argtable is now maintained by Tom G. Huang +The project homepage of argtable 3.x is http://www.argtable.org +The project homepage of argtable 2.x is http://argtable.sourceforge.net/ + +Here is a list of contributors who have helped to improve argtable: + +- **Nina Clemson**: Editing the original argtable-1.0 documentation. +- **Livio Bertacco**: For bug fixes and the argtable-2.x Visual C++ Makefiles. +- **Justin Dearing**: For bug fixes and Windows DLL support, plus code support for the Open Watcom compiler and help with the Mac OS X configuration. +- **Asa Packer**: Contributing bug fixes and upgrades to the Visual C++ Makefiles. +- **Danilo Cicerone**: For the Italian translation of "Introduction to Argtable-2x" on http://www.digitazero.org. +- **Uli Fouquet**: For configuration patches and documentation related to cross-compiling argtable from Unix to Windows, as well as providing the arg_print_glossary_gnu function. +- **Shachar Shemesh**: For Debian package integration and kick-starting the migration to automake/autoconf. +- **Jasper Lievisse Adriaanse**: Maintaining the argtable package in OpenBSD ports. +- **Ulrich Mohr**: For bug fixes relating to Texas Instrument DSP platforms. +- **John Vickers**: For bug fixes relating to Solaris/Motorola platforms. +- **Steve O'Neil**: For bug fixes relating to Solaris/Motorola platforms. +- **Lori A. Pritchett-Sheats**: Fixing a makefile bug relating to "make dist". +- **Paolo Bormida**: For instructions on building argtable with date and regex support on Windows. +- **Michel Valin**: For bug fixes relating to the configure scripts on IBM AIX platforms and instructions on compiling the example code under AIX. +- **Steve Christensen**: Providing prebuilt packages for SPARC/Solaris and x86/Solaris platforms on www.sunfreeware.com. +- **Jess Portnoy**: Reworking the rpm package and integrating argtable into Fedora Linux. +- **Michael Brown**: Incorporating support for pkg-config into the autoconf scripts. +- **Alexander Lindert**: For extensions to the parser to support hex, octal and binary integer formats as well as KB/MB/GB suffixes. +- **Rob Zaborowski**: Providing build configuration files for CMake. +- **Moczik Gabor**: For bug fixes relating to the parsing of filepaths and filename extensions. + +Argtable 2.x uses `getopt` from GNU, which is LGPL licensed. In order to switch to BSD, we replaced GNU `getopt` with NetBSD `getopt`. We really appreciate this high quality library that lays the foundation of Argtable 3.x and here is its copyright notice: + +``` +Copyright (c) 2000 The NetBSD Foundation, Inc. +All rights reserved. + +This code is derived from software contributed to The NetBSD Foundation +by Dieter Baron and Thomas Klausner. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: + This product includes software developed by the NetBSD + Foundation, Inc. and its contributors. +4. Neither the name of The NetBSD Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS +``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +``` diff --git a/c/utf8filenamecheck/windows/argtable/argtable3.c b/c/utf8filenamecheck/windows/argtable/argtable3.c new file mode 100644 index 0000000..e0ab1c5 --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/argtable3.c @@ -0,0 +1,5860 @@ +/******************************************************************************* + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +#define ARG_AMALGAMATION + +/******************************************************************************* + * argtable3_private: Declares private types, constants, and interfaces + * + * This file is part of the argtable3 library. + * + * Copyright (C) 2013-2019 Tom G. Huang + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#ifndef ARG_UTILS_H +#define ARG_UTILS_H + +#include + +#define ARG_ENABLE_TRACE 0 +#define ARG_ENABLE_LOG 1 + +#ifdef __cplusplus +extern "C" { +#endif + +enum { ARG_ERR_MINCOUNT = 1, ARG_ERR_MAXCOUNT, ARG_ERR_BADINT, ARG_ERR_OVERFLOW, ARG_ERR_BADDOUBLE, ARG_ERR_BADDATE, ARG_ERR_REGNOMATCH }; + +typedef void(arg_panicfn)(const char* fmt, ...); + +#if defined(_MSC_VER) +#define ARG_TRACE(x) \ + __pragma(warning(push)) __pragma(warning(disable : 4127)) do { \ + if (ARG_ENABLE_TRACE) \ + dbg_printf x; \ + } \ + while (0) \ + __pragma(warning(pop)) + +#define ARG_LOG(x) \ + __pragma(warning(push)) __pragma(warning(disable : 4127)) do { \ + if (ARG_ENABLE_LOG) \ + dbg_printf x; \ + } \ + while (0) \ + __pragma(warning(pop)) +#else +#define ARG_TRACE(x) \ + do { \ + if (ARG_ENABLE_TRACE) \ + dbg_printf x; \ + } while (0) + +#define ARG_LOG(x) \ + do { \ + if (ARG_ENABLE_LOG) \ + dbg_printf x; \ + } while (0) +#endif + +extern void dbg_printf(const char* fmt, ...); +extern void arg_set_panic(arg_panicfn* proc); +extern void* xmalloc(size_t size); +extern void* xcalloc(size_t count, size_t size); +extern void* xrealloc(void* ptr, size_t size); +extern void xfree(void* ptr); + +struct arg_hashtable_entry { + void *k, *v; + unsigned int h; + struct arg_hashtable_entry* next; +}; + +typedef struct arg_hashtable { + unsigned int tablelength; + struct arg_hashtable_entry** table; + unsigned int entrycount; + unsigned int loadlimit; + unsigned int primeindex; + unsigned int (*hashfn)(const void* k); + int (*eqfn)(const void* k1, const void* k2); +} arg_hashtable_t; + +/** + * @brief Create a hash table. + * + * @param minsize minimum initial size of hash table + * @param hashfn function for hashing keys + * @param eqfn function for determining key equality + * @return newly created hash table or NULL on failure + */ +arg_hashtable_t* arg_hashtable_create(unsigned int minsize, unsigned int (*hashfn)(const void*), int (*eqfn)(const void*, const void*)); + +/** + * @brief This function will cause the table to expand if the insertion would take + * the ratio of entries to table size over the maximum load factor. + * + * This function does not check for repeated insertions with a duplicate key. + * The value returned when using a duplicate key is undefined -- when + * the hash table changes size, the order of retrieval of duplicate key + * entries is reversed. + * If in doubt, remove before insert. + * + * @param h the hash table to insert into + * @param k the key - hash table claims ownership and will free on removal + * @param v the value - does not claim ownership + * @return non-zero for successful insertion + */ +void arg_hashtable_insert(arg_hashtable_t* h, void* k, void* v); + +#define ARG_DEFINE_HASHTABLE_INSERT(fnname, keytype, valuetype) \ + int fnname(arg_hashtable_t* h, keytype* k, valuetype* v) { return arg_hashtable_insert(h, k, v); } + +/** + * @brief Search the specified key in the hash table. + * + * @param h the hash table to search + * @param k the key to search for - does not claim ownership + * @return the value associated with the key, or NULL if none found + */ +void* arg_hashtable_search(arg_hashtable_t* h, const void* k); + +#define ARG_DEFINE_HASHTABLE_SEARCH(fnname, keytype, valuetype) \ + valuetype* fnname(arg_hashtable_t* h, keytype* k) { return (valuetype*)(arg_hashtable_search(h, k)); } + +/** + * @brief Remove the specified key from the hash table. + * + * @param h the hash table to remove the item from + * @param k the key to search for - does not claim ownership + */ +void arg_hashtable_remove(arg_hashtable_t* h, const void* k); + +#define ARG_DEFINE_HASHTABLE_REMOVE(fnname, keytype, valuetype) \ + valuetype* fnname(arg_hashtable_t* h, keytype* k) { return (valuetype*)(arg_hashtable_remove(h, k)); } + +/** + * @brief Return the number of keys in the hash table. + * + * @param h the hash table + * @return the number of items stored in the hash table + */ +unsigned int arg_hashtable_count(arg_hashtable_t* h); + +/** + * @brief Change the value associated with the key. + * + * function to change the value associated with a key, where there already + * exists a value bound to the key in the hash table. + * Source due to Holger Schemel. + * + * @name hashtable_change + * @param h the hash table + * @param key + * @param value + */ +int arg_hashtable_change(arg_hashtable_t* h, void* k, void* v); + +/** + * @brief Free the hash table and the memory allocated for each key-value pair. + * + * @param h the hash table + * @param free_values whether to call 'free' on the remaining values + */ +void arg_hashtable_destroy(arg_hashtable_t* h, int free_values); + +typedef struct arg_hashtable_itr { + arg_hashtable_t* h; + struct arg_hashtable_entry* e; + struct arg_hashtable_entry* parent; + unsigned int index; +} arg_hashtable_itr_t; + +arg_hashtable_itr_t* arg_hashtable_itr_create(arg_hashtable_t* h); + +void arg_hashtable_itr_destroy(arg_hashtable_itr_t* itr); + +/** + * @brief Return the value of the (key,value) pair at the current position. + */ +extern void* arg_hashtable_itr_key(arg_hashtable_itr_t* i); + +/** + * @brief Return the value of the (key,value) pair at the current position. + */ +extern void* arg_hashtable_itr_value(arg_hashtable_itr_t* i); + +/** + * @brief Advance the iterator to the next element. Returns zero if advanced to end of table. + */ +int arg_hashtable_itr_advance(arg_hashtable_itr_t* itr); + +/** + * @brief Remove current element and advance the iterator to the next element. + */ +int arg_hashtable_itr_remove(arg_hashtable_itr_t* itr); + +/** + * @brief Search and overwrite the supplied iterator, to point to the entry matching the supplied key. + * + * @return Zero if not found. + */ +int arg_hashtable_itr_search(arg_hashtable_itr_t* itr, arg_hashtable_t* h, void* k); + +#define ARG_DEFINE_HASHTABLE_ITERATOR_SEARCH(fnname, keytype) \ + int fnname(arg_hashtable_itr_t* i, arg_hashtable_t* h, keytype* k) { return (arg_hashtable_iterator_search(i, h, k)); } + +#ifdef __cplusplus +} +#endif + +#endif +/******************************************************************************* + * arg_utils: Implements memory, panic, and other utility functions + * + * This file is part of the argtable3 library. + * + * Copyright (C) 2013-2019 Tom G. Huang + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +#ifndef ARG_AMALGAMATION +#include "argtable3_private.h" +#endif + +#include +#include +#include +#include + +static void panic(const char* fmt, ...); +static arg_panicfn* s_panic = panic; + +void dbg_printf(const char* fmt, ...) { + va_list args; + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); +} + +static void panic(const char* fmt, ...) { + va_list args; + char* s; + + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4996) +#endif + s = getenv("EF_DUMPCORE"); +#if defined(_MSC_VER) +#pragma warning(pop) +#endif + + if (s != NULL && *s != '\0') { + abort(); + } else { + exit(EXIT_FAILURE); + } +} + +void arg_set_panic(arg_panicfn* proc) { + s_panic = proc; +} + +void* xmalloc(size_t size) { + void* ret = malloc(size); + if (!ret) { + s_panic("Out of memory!\n"); + } + return ret; +} + +void* xcalloc(size_t count, size_t size) { + size_t allocated_count = count && size ? count : 1; + size_t allocated_size = count && size ? size : 1; + void* ret = calloc(allocated_count, allocated_size); + if (!ret) { + s_panic("Out of memory!\n"); + } + return ret; +} + +void* xrealloc(void* ptr, size_t size) { + size_t allocated_size = size ? size : 1; + void* ret = realloc(ptr, allocated_size); + if (!ret) { + s_panic("Out of memory!\n"); + } + return ret; +} + +void xfree(void* ptr) { + free(ptr); +} + +static void merge(void* data, int esize, int i, int j, int k, arg_comparefn* comparefn) { + char* a = (char*)data; + char* m; + int ipos, jpos, mpos; + + /* Initialize the counters used in merging. */ + ipos = i; + jpos = j + 1; + mpos = 0; + + /* Allocate storage for the merged elements. */ + m = (char*)xmalloc(esize * ((k - i) + 1)); + + /* Continue while either division has elements to merge. */ + while (ipos <= j || jpos <= k) { + if (ipos > j) { + /* The left division has no more elements to merge. */ + while (jpos <= k) { + memcpy(&m[mpos * esize], &a[jpos * esize], esize); + jpos++; + mpos++; + } + + continue; + } else if (jpos > k) { + /* The right division has no more elements to merge. */ + while (ipos <= j) { + memcpy(&m[mpos * esize], &a[ipos * esize], esize); + ipos++; + mpos++; + } + + continue; + } + + /* Append the next ordered element to the merged elements. */ + if (comparefn(&a[ipos * esize], &a[jpos * esize]) < 0) { + memcpy(&m[mpos * esize], &a[ipos * esize], esize); + ipos++; + mpos++; + } else { + memcpy(&m[mpos * esize], &a[jpos * esize], esize); + jpos++; + mpos++; + } + } + + /* Prepare to pass back the merged data. */ + memcpy(&a[i * esize], m, esize * ((k - i) + 1)); + xfree(m); +} + +void arg_mgsort(void* data, int size, int esize, int i, int k, arg_comparefn* comparefn) { + int j; + + /* Stop the recursion when no more divisions can be made. */ + if (i < k) { + /* Determine where to divide the elements. */ + j = (int)(((i + k - 1)) / 2); + + /* Recursively sort the two divisions. */ + arg_mgsort(data, size, esize, i, j, comparefn); + arg_mgsort(data, size, esize, j + 1, k, comparefn); + merge(data, esize, i, j, k, comparefn); + } +} +/******************************************************************************* + * arg_hashtable: Implements the hash table utilities + * + * This file is part of the argtable3 library. + * + * Copyright (C) 2013-2019 Tom G. Huang + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#ifndef ARG_AMALGAMATION +#include "argtable3_private.h" +#endif + +#include +#include +#include +#include + +/* + * This hash table module is adapted from the C hash table implementation by + * Christopher Clark. Here is the copyright notice from the library: + * + * Copyright (c) 2002, Christopher Clark + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of the original author; nor the names of any contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Credit for primes table: Aaron Krowne + * http://br.endernet.org/~akrowne/ + * http://planetmath.org/encyclopedia/GoodHashTablePrimes.html + */ +static const unsigned int primes[] = {53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, + 24593, 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469, + 12582917, 25165843, 50331653, 100663319, 201326611, 402653189, 805306457, 1610612741}; +const unsigned int prime_table_length = sizeof(primes) / sizeof(primes[0]); +const float max_load_factor = (float)0.65; + +static unsigned int enhanced_hash(arg_hashtable_t* h, const void* k) { + /* + * Aim to protect against poor hash functions by adding logic here. + * The logic is taken from Java 1.4 hash table source. + */ + unsigned int i = h->hashfn(k); + i += ~(i << 9); + i ^= ((i >> 14) | (i << 18)); /* >>> */ + i += (i << 4); + i ^= ((i >> 10) | (i << 22)); /* >>> */ + return i; +} + +static unsigned int index_for(unsigned int tablelength, unsigned int hashvalue) { + return (hashvalue % tablelength); +} + +arg_hashtable_t* arg_hashtable_create(unsigned int minsize, unsigned int (*hashfn)(const void*), int (*eqfn)(const void*, const void*)) { + arg_hashtable_t* h; + unsigned int pindex; + unsigned int size = primes[0]; + + /* Check requested hash table isn't too large */ + if (minsize > (1u << 30)) + return NULL; + + /* + * Enforce size as prime. The reason is to avoid clustering of values + * into a small number of buckets (yes, distribution). A more even + * distributed hash table will perform more consistently. + */ + for (pindex = 0; pindex < prime_table_length; pindex++) { + if (primes[pindex] > minsize) { + size = primes[pindex]; + break; + } + } + + h = (arg_hashtable_t*)xmalloc(sizeof(arg_hashtable_t)); + h->table = (struct arg_hashtable_entry**)xmalloc(sizeof(struct arg_hashtable_entry*) * size); + memset(h->table, 0, size * sizeof(struct arg_hashtable_entry*)); + h->tablelength = size; + h->primeindex = pindex; + h->entrycount = 0; + h->hashfn = hashfn; + h->eqfn = eqfn; + h->loadlimit = (unsigned int)ceil(size * max_load_factor); + return h; +} + +static int arg_hashtable_expand(arg_hashtable_t* h) { + /* Double the size of the table to accommodate more entries */ + struct arg_hashtable_entry** newtable; + struct arg_hashtable_entry* e; + unsigned int newsize; + unsigned int i; + unsigned int index; + + /* Check we're not hitting max capacity */ + if (h->primeindex == (prime_table_length - 1)) + return 0; + newsize = primes[++(h->primeindex)]; + + newtable = (struct arg_hashtable_entry**)xmalloc(sizeof(struct arg_hashtable_entry*) * newsize); + memset(newtable, 0, newsize * sizeof(struct arg_hashtable_entry*)); + /* + * This algorithm is not 'stable': it reverses the list + * when it transfers entries between the tables + */ + for (i = 0; i < h->tablelength; i++) { + while (NULL != (e = h->table[i])) { + h->table[i] = e->next; + index = index_for(newsize, e->h); + e->next = newtable[index]; + newtable[index] = e; + } + } + + xfree(h->table); + h->table = newtable; + h->tablelength = newsize; + h->loadlimit = (unsigned int)ceil(newsize * max_load_factor); + return -1; +} + +unsigned int arg_hashtable_count(arg_hashtable_t* h) { + return h->entrycount; +} + +void arg_hashtable_insert(arg_hashtable_t* h, void* k, void* v) { + /* This method allows duplicate keys - but they shouldn't be used */ + unsigned int index; + struct arg_hashtable_entry* e; + if ((h->entrycount + 1) > h->loadlimit) { + /* + * Ignore the return value. If expand fails, we should + * still try cramming just this value into the existing table + * -- we may not have memory for a larger table, but one more + * element may be ok. Next time we insert, we'll try expanding again. + */ + arg_hashtable_expand(h); + } + e = (struct arg_hashtable_entry*)xmalloc(sizeof(struct arg_hashtable_entry)); + e->h = enhanced_hash(h, k); + index = index_for(h->tablelength, e->h); + e->k = k; + e->v = v; + e->next = h->table[index]; + h->table[index] = e; + h->entrycount++; +} + +void* arg_hashtable_search(arg_hashtable_t* h, const void* k) { + struct arg_hashtable_entry* e; + unsigned int hashvalue; + unsigned int index; + + hashvalue = enhanced_hash(h, k); + index = index_for(h->tablelength, hashvalue); + e = h->table[index]; + while (e != NULL) { + /* Check hash value to short circuit heavier comparison */ + if ((hashvalue == e->h) && (h->eqfn(k, e->k))) + return e->v; + e = e->next; + } + return NULL; +} + +void arg_hashtable_remove(arg_hashtable_t* h, const void* k) { + /* + * TODO: consider compacting the table when the load factor drops enough, + * or provide a 'compact' method. + */ + + struct arg_hashtable_entry* e; + struct arg_hashtable_entry** pE; + unsigned int hashvalue; + unsigned int index; + + hashvalue = enhanced_hash(h, k); + index = index_for(h->tablelength, hashvalue); + pE = &(h->table[index]); + e = *pE; + while (NULL != e) { + /* Check hash value to short circuit heavier comparison */ + if ((hashvalue == e->h) && (h->eqfn(k, e->k))) { + *pE = e->next; + h->entrycount--; + xfree(e->k); + xfree(e->v); + xfree(e); + return; + } + pE = &(e->next); + e = e->next; + } +} + +void arg_hashtable_destroy(arg_hashtable_t* h, int free_values) { + unsigned int i; + struct arg_hashtable_entry *e, *f; + struct arg_hashtable_entry** table = h->table; + if (free_values) { + for (i = 0; i < h->tablelength; i++) { + e = table[i]; + while (NULL != e) { + f = e; + e = e->next; + xfree(f->k); + xfree(f->v); + xfree(f); + } + } + } else { + for (i = 0; i < h->tablelength; i++) { + e = table[i]; + while (NULL != e) { + f = e; + e = e->next; + xfree(f->k); + xfree(f); + } + } + } + xfree(h->table); + xfree(h); +} + +arg_hashtable_itr_t* arg_hashtable_itr_create(arg_hashtable_t* h) { + unsigned int i; + unsigned int tablelength; + + arg_hashtable_itr_t* itr = (arg_hashtable_itr_t*)xmalloc(sizeof(arg_hashtable_itr_t)); + itr->h = h; + itr->e = NULL; + itr->parent = NULL; + tablelength = h->tablelength; + itr->index = tablelength; + if (0 == h->entrycount) + return itr; + + for (i = 0; i < tablelength; i++) { + if (h->table[i] != NULL) { + itr->e = h->table[i]; + itr->index = i; + break; + } + } + return itr; +} + +void arg_hashtable_itr_destroy(arg_hashtable_itr_t* itr) { + xfree(itr); +} + +void* arg_hashtable_itr_key(arg_hashtable_itr_t* i) { + return i->e->k; +} + +void* arg_hashtable_itr_value(arg_hashtable_itr_t* i) { + return i->e->v; +} + +int arg_hashtable_itr_advance(arg_hashtable_itr_t* itr) { + unsigned int j; + unsigned int tablelength; + struct arg_hashtable_entry** table; + struct arg_hashtable_entry* next; + + if (itr->e == NULL) + return 0; /* stupidity check */ + + next = itr->e->next; + if (NULL != next) { + itr->parent = itr->e; + itr->e = next; + return -1; + } + + tablelength = itr->h->tablelength; + itr->parent = NULL; + if (tablelength <= (j = ++(itr->index))) { + itr->e = NULL; + return 0; + } + + table = itr->h->table; + while (NULL == (next = table[j])) { + if (++j >= tablelength) { + itr->index = tablelength; + itr->e = NULL; + return 0; + } + } + + itr->index = j; + itr->e = next; + return -1; +} + +int arg_hashtable_itr_remove(arg_hashtable_itr_t* itr) { + struct arg_hashtable_entry* remember_e; + struct arg_hashtable_entry* remember_parent; + int ret; + + /* Do the removal */ + if ((itr->parent) == NULL) { + /* element is head of a chain */ + itr->h->table[itr->index] = itr->e->next; + } else { + /* element is mid-chain */ + itr->parent->next = itr->e->next; + } + /* itr->e is now outside the hashtable */ + remember_e = itr->e; + itr->h->entrycount--; + xfree(remember_e->k); + xfree(remember_e->v); + + /* Advance the iterator, correcting the parent */ + remember_parent = itr->parent; + ret = arg_hashtable_itr_advance(itr); + if (itr->parent == remember_e) { + itr->parent = remember_parent; + } + xfree(remember_e); + return ret; +} + +int arg_hashtable_itr_search(arg_hashtable_itr_t* itr, arg_hashtable_t* h, void* k) { + struct arg_hashtable_entry* e; + struct arg_hashtable_entry* parent; + unsigned int hashvalue; + unsigned int index; + + hashvalue = enhanced_hash(h, k); + index = index_for(h->tablelength, hashvalue); + + e = h->table[index]; + parent = NULL; + while (e != NULL) { + /* Check hash value to short circuit heavier comparison */ + if ((hashvalue == e->h) && (h->eqfn(k, e->k))) { + itr->index = index; + itr->e = e; + itr->parent = parent; + itr->h = h; + return -1; + } + parent = e; + e = e->next; + } + return 0; +} + +int arg_hashtable_change(arg_hashtable_t* h, void* k, void* v) { + struct arg_hashtable_entry* e; + unsigned int hashvalue; + unsigned int index; + + hashvalue = enhanced_hash(h, k); + index = index_for(h->tablelength, hashvalue); + e = h->table[index]; + while (e != NULL) { + /* Check hash value to short circuit heavier comparison */ + if ((hashvalue == e->h) && (h->eqfn(k, e->k))) { + xfree(e->v); + e->v = v; + return -1; + } + e = e->next; + } + return 0; +} +/******************************************************************************* + * arg_dstr: Implements the dynamic string utilities + * + * This file is part of the argtable3 library. + * + * Copyright (C) 2013-2019 Tom G. Huang + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +#ifndef ARG_AMALGAMATION +#include "argtable3_private.h" +#endif + +#include +#include +#include + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4996) +#endif + +#define START_VSNBUFF 16 + +/* + * This dynamic string module is adapted from TclResult.c in the Tcl library. + * Here is the copyright notice from the library: + * + * This software is copyrighted by the Regents of the University of + * California, Sun Microsystems, Inc., Scriptics Corporation, ActiveState + * Corporation and other parties. The following terms apply to all files + * associated with the software unless explicitly disclaimed in + * individual files. + * + * The authors hereby grant permission to use, copy, modify, distribute, + * and license this software and its documentation for any purpose, provided + * that existing copyright notices are retained in all copies and that this + * notice is included verbatim in any distributions. No written agreement, + * license, or royalty fee is required for any of the authorized uses. + * Modifications to this software may be copyrighted by their authors + * and need not follow the licensing terms described here, provided that + * the new terms are clearly indicated on the first page of each file where + * they apply. + * + * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY + * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY + * DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE + * IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE + * NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR + * MODIFICATIONS. + * + * GOVERNMENT USE: If you are acquiring this software on behalf of the + * U.S. government, the Government shall have only "Restricted Rights" + * in the software and related documentation as defined in the Federal + * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you + * are acquiring the software on behalf of the Department of Defense, the + * software shall be classified as "Commercial Computer Software" and the + * Government shall have only "Restricted Rights" as defined in Clause + * 252.227-7014 (b) (3) of DFARs. Notwithstanding the foregoing, the + * authors grant the U.S. Government and others acting in its behalf + * permission to use and distribute the software in accordance with the + * terms specified in this license. + */ + +typedef struct _internal_arg_dstr { + char* data; + arg_dstr_freefn* free_proc; + char sbuf[ARG_DSTR_SIZE + 1]; + char* append_data; + int append_data_size; + int append_used; +} _internal_arg_dstr_t; + +static void setup_append_buf(arg_dstr_t res, int newSpace); + +arg_dstr_t arg_dstr_create(void) { + _internal_arg_dstr_t* h = (_internal_arg_dstr_t*)xmalloc(sizeof(_internal_arg_dstr_t)); + memset(h, 0, sizeof(_internal_arg_dstr_t)); + h->sbuf[0] = 0; + h->data = h->sbuf; + h->free_proc = ARG_DSTR_STATIC; + return h; +} + +void arg_dstr_destroy(arg_dstr_t ds) { + if (ds == NULL) + return; + + arg_dstr_reset(ds); + xfree(ds); + return; +} + +void arg_dstr_set(arg_dstr_t ds, char* str, arg_dstr_freefn* free_proc) { + int length; + register arg_dstr_freefn* old_free_proc = ds->free_proc; + char* old_result = ds->data; + + if (str == NULL) { + ds->sbuf[0] = 0; + ds->data = ds->sbuf; + ds->free_proc = ARG_DSTR_STATIC; + } else if (free_proc == ARG_DSTR_VOLATILE) { + length = (int)strlen(str); + if (length > ARG_DSTR_SIZE) { + ds->data = (char*)xmalloc((unsigned)length + 1); + ds->free_proc = ARG_DSTR_DYNAMIC; + } else { + ds->data = ds->sbuf; + ds->free_proc = ARG_DSTR_STATIC; + } + strcpy(ds->data, str); + } else { + ds->data = str; + ds->free_proc = free_proc; + } + + /* + * If the old result was dynamically-allocated, free it up. Do it here, + * rather than at the beginning, in case the new result value was part of + * the old result value. + */ + + if ((old_free_proc != 0) && (old_result != ds->data)) { + if (old_free_proc == ARG_DSTR_DYNAMIC) { + xfree(old_result); + } else { + (*old_free_proc)(old_result); + } + } + + if ((ds->append_data != NULL) && (ds->append_data_size > 0)) { + xfree(ds->append_data); + ds->append_data = NULL; + ds->append_data_size = 0; + } +} + +char* arg_dstr_cstr(arg_dstr_t ds) /* Interpreter whose result to return. */ +{ + return ds->data; +} + +void arg_dstr_cat(arg_dstr_t ds, const char* str) { + setup_append_buf(ds, (int)strlen(str) + 1); + memcpy(ds->data + strlen(ds->data), str, strlen(str)); +} + +void arg_dstr_catc(arg_dstr_t ds, char c) { + setup_append_buf(ds, 2); + memcpy(ds->data + strlen(ds->data), &c, 1); +} + +/* + * The logic of the `arg_dstr_catf` function is adapted from the `bformat` + * function in The Better String Library by Paul Hsieh. Here is the copyright + * notice from the library: + * + * Copyright (c) 2014, Paul Hsieh + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * * Neither the name of bstrlib nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +void arg_dstr_catf(arg_dstr_t ds, const char* fmt, ...) { + va_list arglist; + char* buff; + int n, r; + size_t slen; + + if (fmt == NULL) + return; + + /* Since the length is not determinable beforehand, a search is + performed using the truncating "vsnprintf" call (to avoid buffer + overflows) on increasing potential sizes for the output result. */ + + if ((n = (int)(2 * strlen(fmt))) < START_VSNBUFF) + n = START_VSNBUFF; + + buff = (char*)xmalloc(n + 2); + memset(buff, 0, n + 2); + + for (;;) { + va_start(arglist, fmt); + r = vsnprintf(buff, n + 1, fmt, arglist); + va_end(arglist); + + slen = strlen(buff); + if (slen < (size_t)n) + break; + + if (r > n) + n = r; + else + n += n; + + xfree(buff); + buff = (char*)xmalloc(n + 2); + memset(buff, 0, n + 2); + } + + arg_dstr_cat(ds, buff); + xfree(buff); +} + +static void setup_append_buf(arg_dstr_t ds, int new_space) { + int total_space; + + /* + * Make the append buffer larger, if that's necessary, then copy the + * data into the append buffer and make the append buffer the official + * data. + */ + if (ds->data != ds->append_data) { + /* + * If the buffer is too big, then free it up so we go back to a + * smaller buffer. This avoids tying up memory forever after a large + * operation. + */ + if (ds->append_data_size > 500) { + xfree(ds->append_data); + ds->append_data = NULL; + ds->append_data_size = 0; + } + ds->append_used = (int)strlen(ds->data); + } else if (ds->data[ds->append_used] != 0) { + /* + * Most likely someone has modified a result created by + * arg_dstr_cat et al. so that it has a different size. Just + * recompute the size. + */ + ds->append_used = (int)strlen(ds->data); + } + + total_space = new_space + ds->append_used; + if (total_space >= ds->append_data_size) { + char* newbuf; + + if (total_space < 100) { + total_space = 200; + } else { + total_space *= 2; + } + newbuf = (char*)xmalloc((unsigned)total_space); + memset(newbuf, 0, total_space); + strcpy(newbuf, ds->data); + if (ds->append_data != NULL) { + xfree(ds->append_data); + } + ds->append_data = newbuf; + ds->append_data_size = total_space; + } else if (ds->data != ds->append_data) { + strcpy(ds->append_data, ds->data); + } + + arg_dstr_free(ds); + ds->data = ds->append_data; +} + +void arg_dstr_free(arg_dstr_t ds) { + if (ds->free_proc != NULL) { + if (ds->free_proc == ARG_DSTR_DYNAMIC) { + xfree(ds->data); + } else { + (*ds->free_proc)(ds->data); + } + ds->free_proc = NULL; + } +} + +void arg_dstr_reset(arg_dstr_t ds) { + arg_dstr_free(ds); + if ((ds->append_data != NULL) && (ds->append_data_size > 0)) { + xfree(ds->append_data); + ds->append_data = NULL; + ds->append_data_size = 0; + } + + ds->data = ds->sbuf; + ds->sbuf[0] = 0; +} + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif +/* $Id: getopt.h,v 1.1 2009/10/16 19:50:28 rodney Exp rodney $ */ +/* $OpenBSD: getopt.h,v 1.1 2002/12/03 20:24:29 millert Exp $ */ +/* $NetBSD: getopt.h,v 1.4 2000/07/07 10:43:54 ad Exp $ */ + +/* + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dieter Baron and Thomas Klausner. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgment: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#if ARG_REPLACE_GETOPT == 1 + +#ifndef _GETOPT_H_ +#define _GETOPT_H_ + +/* + * GNU-like getopt_long() and 4.4BSD getsubopt()/optreset extensions + */ +#define no_argument 0 +#define required_argument 1 +#define optional_argument 2 + +struct option { + /* name of long option */ + const char* name; + /* + * one of no_argument, required_argument, and optional_argument: + * whether option takes an argument + */ + int has_arg; + /* if not NULL, set *flag to val when option found */ + int* flag; + /* if flag not NULL, value to set *flag to; else return value */ + int val; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +int getopt_long(int, char* const*, const char*, const struct option*, int*); +int getopt_long_only(int, char* const*, const char*, const struct option*, int*); +#ifndef _GETOPT_DEFINED +#define _GETOPT_DEFINED +int getopt(int, char* const*, const char*); +int getsubopt(char**, char* const*, char**); + +extern char* optarg; /* getopt(3) external variables */ +extern int opterr; +extern int optind; +extern int optopt; +extern int optreset; +extern char* suboptarg; /* getsubopt(3) external variable */ +#endif /* _GETOPT_DEFINED */ + +#ifdef __cplusplus +} +#endif +#endif /* !_GETOPT_H_ */ + +#else +#include +#endif /* ARG_REPLACE_GETOPT */ +/* $Id: getopt_long.c,v 1.1 2009/10/16 19:50:28 rodney Exp rodney $ */ +/* $OpenBSD: getopt_long.c,v 1.23 2007/10/31 12:34:57 chl Exp $ */ +/* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */ + +/* + * Copyright (c) 2002 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * Sponsored in part by the Defense Advanced Research Projects + * Agency (DARPA) and Air Force Research Laboratory, Air Force + * Materiel Command, USAF, under agreement number F39502-99-1-0512. + */ + +/* + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dieter Baron and Thomas Klausner. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "argtable3.h" + +#ifndef ARG_AMALGAMATION +#include "getopt.h" +#endif + +#include +#include +#include + +#if ARG_REPLACE_GETOPT == 1 +int opterr = 1; /* if error message should be printed */ +int optind = 1; /* index into parent argv vector */ +int optopt = '?'; /* character checked for validity */ +int optreset; /* reset getopt */ +char* optarg; /* argument associated with option */ +#endif /* ARG_REPLACE_GETOPT */ + +#define PRINT_ERROR ((opterr) && (*options != ':')) + +#define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */ +#define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */ +#define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */ + +/* return values */ +#define BADCH (int)'?' +#define BADARG ((*options == ':') ? (int)':' : (int)'?') +#define INORDER (int)1 + +#define EMSG "" + +#if ARG_REPLACE_GETOPT == 1 +static int getopt_internal(int, char* const*, const char*, const struct option*, int*, int); +#endif /* ARG_REPLACE_GETOPT */ +static int parse_long_options(char* const*, const char*, const struct option*, int*, int); +static int gcd(int, int); +static void permute_args(int, int, int, char* const*); + +static char* place = EMSG; /* option letter processing */ + +/* XXX: set optreset to 1 rather than these two */ +static int nonopt_start = -1; /* first non option argument (for permute) */ +static int nonopt_end = -1; /* first option after non options (for permute) */ + +/* Error messages */ +static const char recargchar[] = "option requires an argument -- %c"; +static const char recargstring[] = "option requires an argument -- %s"; +static const char ambig[] = "ambiguous option -- %.*s"; +static const char noarg[] = "option doesn't take an argument -- %.*s"; +static const char illoptchar[] = "unknown option -- %c"; +static const char illoptstring[] = "unknown option -- %s"; + +#ifdef _WIN32 + +/* + * Windows needs warnx(). We change the definition though: + * 1. (another) global is defined, opterrmsg, which holds the error message + * 2. errors are always printed out on stderr w/o the program name + * Note that opterrmsg always gets set no matter what opterr is set to. The + * error message will not be printed if opterr is 0 as usual. + */ + +#include +#include + +#define MAX_OPTERRMSG_SIZE 128 + +extern char opterrmsg[MAX_OPTERRMSG_SIZE]; +char opterrmsg[MAX_OPTERRMSG_SIZE]; /* buffer for the last error message */ + +static void warnx(const char* fmt, ...) { + va_list ap; + va_start(ap, fmt); + + /* + * Make sure opterrmsg is always zero-terminated despite the _vsnprintf() + * implementation specifics and manually suppress the warning. + */ + memset(opterrmsg, 0, sizeof(opterrmsg)); + if (fmt != NULL) +#if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) + _vsnprintf_s(opterrmsg, sizeof(opterrmsg), sizeof(opterrmsg) - 1, fmt, ap); +#else + _vsnprintf(opterrmsg, sizeof(opterrmsg) - 1, fmt, ap); +#endif + + va_end(ap); + +#ifdef _MSC_VER +#pragma warning(suppress : 6053) +#endif + fprintf(stderr, "%s\n", opterrmsg); +} + +#else +#include +#endif /*_WIN32*/ + +/* + * Compute the greatest common divisor of a and b. + */ +static int gcd(int a, int b) { + int c; + + c = a % b; + while (c != 0) { + a = b; + b = c; + c = a % b; + } + + return (b); +} + +/* + * Exchange the block from nonopt_start to nonopt_end with the block + * from nonopt_end to opt_end (keeping the same order of arguments + * in each block). + */ +static void permute_args(int panonopt_start, int panonopt_end, int opt_end, char* const* nargv) { + int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; + char* swap; + + /* + * compute lengths of blocks and number and size of cycles + */ + nnonopts = panonopt_end - panonopt_start; + nopts = opt_end - panonopt_end; + ncycle = gcd(nnonopts, nopts); + cyclelen = (opt_end - panonopt_start) / ncycle; + + for (i = 0; i < ncycle; i++) { + cstart = panonopt_end + i; + pos = cstart; + for (j = 0; j < cyclelen; j++) { + if (pos >= panonopt_end) + pos -= nnonopts; + else + pos += nopts; + swap = nargv[pos]; + /* LINTED const cast */ + ((char**)nargv)[pos] = nargv[cstart]; + /* LINTED const cast */ + ((char**)nargv)[cstart] = swap; + } + } +} + +/* + * parse_long_options -- + * Parse long options in argc/argv argument vector. + * Returns -1 if short_too is set and the option does not match long_options. + */ +static int parse_long_options(char* const* nargv, const char* options, const struct option* long_options, int* idx, int short_too) { + char *current_argv, *has_equal; + size_t current_argv_len; + int i, match; + + current_argv = place; + match = -1; + + optind++; + + if ((has_equal = strchr(current_argv, '=')) != NULL) { + /* argument found (--option=arg) */ + current_argv_len = has_equal - current_argv; + has_equal++; + } else + current_argv_len = strlen(current_argv); + + for (i = 0; long_options[i].name; i++) { + /* find matching long option */ + if (strncmp(current_argv, long_options[i].name, current_argv_len)) + continue; + + if (strlen(long_options[i].name) == current_argv_len) { + /* exact match */ + match = i; + break; + } + /* + * If this is a known short option, don't allow + * a partial match of a single character. + */ + if (short_too && current_argv_len == 1) + continue; + + if (match == -1) /* partial match */ + match = i; + else { + /* ambiguous abbreviation */ + if (PRINT_ERROR) + warnx(ambig, (int)current_argv_len, current_argv); + optopt = 0; + return (BADCH); + } + } + if (match != -1) { /* option found */ + if (long_options[match].has_arg == no_argument && has_equal) { + if (PRINT_ERROR) + warnx(noarg, (int)current_argv_len, current_argv); + /* + * XXX: GNU sets optopt to val regardless of flag + */ + if (long_options[match].flag == NULL) + optopt = long_options[match].val; + else + optopt = 0; + return (BADARG); + } + if (long_options[match].has_arg == required_argument || long_options[match].has_arg == optional_argument) { + if (has_equal) + optarg = has_equal; + else if (long_options[match].has_arg == required_argument) { + /* + * optional argument doesn't use next nargv + */ + optarg = nargv[optind++]; + } + } + if ((long_options[match].has_arg == required_argument) && (optarg == NULL)) { + /* + * Missing argument; leading ':' indicates no error + * should be generated. + */ + if (PRINT_ERROR) + warnx(recargstring, current_argv); + /* + * XXX: GNU sets optopt to val regardless of flag + */ + if (long_options[match].flag == NULL) + optopt = long_options[match].val; + else + optopt = 0; + --optind; + return (BADARG); + } + } else { /* unknown option */ + if (short_too) { + --optind; + return (-1); + } + if (PRINT_ERROR) + warnx(illoptstring, current_argv); + optopt = 0; + return (BADCH); + } + if (idx) + *idx = match; + if (long_options[match].flag) { + *long_options[match].flag = long_options[match].val; + return (0); + } else + return (long_options[match].val); +} + +#if ARG_REPLACE_GETOPT == 1 +/* + * getopt_internal -- + * Parse argc/argv argument vector. Called by user level routines. + */ +static int getopt_internal(int nargc, char* const* nargv, const char* options, const struct option* long_options, int* idx, int flags) { + char* oli; /* option letter list index */ + int optchar, short_too; + static int posixly_correct = -1; + + if (options == NULL) + return (-1); + + /* + * Disable GNU extensions if POSIXLY_CORRECT is set or options + * string begins with a '+'. + */ + if (posixly_correct == -1) +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4996) +#endif + posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); +#if defined(_MSC_VER) +#pragma warning(pop) +#endif + if (posixly_correct || *options == '+') + flags &= ~FLAG_PERMUTE; + else if (*options == '-') + flags |= FLAG_ALLARGS; + if (*options == '+' || *options == '-') + options++; + + /* + * XXX Some GNU programs (like cvs) set optind to 0 instead of + * XXX using optreset. Work around this braindamage. + */ + if (optind == 0) + optind = optreset = 1; + + optarg = NULL; + if (optreset) + nonopt_start = nonopt_end = -1; +start: + if (optreset || !*place) { /* update scanning pointer */ + optreset = 0; + if (optind >= nargc) { /* end of argument vector */ + place = EMSG; + if (nonopt_end != -1) { + /* do permutation, if we have to */ + permute_args(nonopt_start, nonopt_end, optind, nargv); + optind -= nonopt_end - nonopt_start; + } else if (nonopt_start != -1) { + /* + * If we skipped non-options, set optind + * to the first of them. + */ + optind = nonopt_start; + } + nonopt_start = nonopt_end = -1; + return (-1); + } + if (*(place = nargv[optind]) != '-' || (place[1] == '\0' && strchr(options, '-') == NULL)) { + place = EMSG; /* found non-option */ + if (flags & FLAG_ALLARGS) { + /* + * GNU extension: + * return non-option as argument to option 1 + */ + optarg = nargv[optind++]; + return (INORDER); + } + if (!(flags & FLAG_PERMUTE)) { + /* + * If no permutation wanted, stop parsing + * at first non-option. + */ + return (-1); + } + /* do permutation */ + if (nonopt_start == -1) + nonopt_start = optind; + else if (nonopt_end != -1) { + permute_args(nonopt_start, nonopt_end, optind, nargv); + nonopt_start = optind - (nonopt_end - nonopt_start); + nonopt_end = -1; + } + optind++; + /* process next argument */ + goto start; + } + if (nonopt_start != -1 && nonopt_end == -1) + nonopt_end = optind; + + /* + * If we have "-" do nothing, if "--" we are done. + */ + if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { + optind++; + place = EMSG; + /* + * We found an option (--), so if we skipped + * non-options, we have to permute. + */ + if (nonopt_end != -1) { + permute_args(nonopt_start, nonopt_end, optind, nargv); + optind -= nonopt_end - nonopt_start; + } + nonopt_start = nonopt_end = -1; + return (-1); + } + } + + /* + * Check long options if: + * 1) we were passed some + * 2) the arg is not just "-" + * 3) either the arg starts with -- we are getopt_long_only() + */ + if (long_options != NULL && place != nargv[optind] && (*place == '-' || (flags & FLAG_LONGONLY))) { + short_too = 0; + if (*place == '-') + place++; /* --foo long option */ + else if (*place != ':' && strchr(options, *place) != NULL) + short_too = 1; /* could be short option too */ + + optchar = parse_long_options(nargv, options, long_options, idx, short_too); + if (optchar != -1) { + place = EMSG; + return (optchar); + } + } + + if ((optchar = (int)*place++) == (int)':' || (optchar == (int)'-' && *place != '\0') || (oli = strchr(options, optchar)) == NULL) { + /* + * If the user specified "-" and '-' isn't listed in + * options, return -1 (non-option) as per POSIX. + * Otherwise, it is an unknown option character (or ':'). + */ + if (optchar == (int)'-' && *place == '\0') + return (-1); + if (!*place) + ++optind; + if (PRINT_ERROR) + warnx(illoptchar, optchar); + optopt = optchar; + return (BADCH); + } + if (long_options != NULL && optchar == 'W' && oli[1] == ';') { + /* -W long-option */ + if (*place) /* no space */ + /* NOTHING */; + else if (++optind >= nargc) { /* no arg */ + place = EMSG; + if (PRINT_ERROR) + warnx(recargchar, optchar); + optopt = optchar; + return (BADARG); + } else /* white space */ + place = nargv[optind]; + optchar = parse_long_options(nargv, options, long_options, idx, 0); + place = EMSG; + return (optchar); + } + if (*++oli != ':') { /* doesn't take argument */ + if (!*place) + ++optind; + } else { /* takes (optional) argument */ + optarg = NULL; + if (*place) /* no white space */ + optarg = place; + else if (oli[1] != ':') { /* arg not optional */ + if (++optind >= nargc) { /* no arg */ + place = EMSG; + if (PRINT_ERROR) + warnx(recargchar, optchar); + optopt = optchar; + return (BADARG); + } else + optarg = nargv[optind]; + } + place = EMSG; + ++optind; + } + /* dump back option letter */ + return (optchar); +} + +/* + * getopt -- + * Parse argc/argv argument vector. + * + * [eventually this will replace the BSD getopt] + */ +int getopt(int nargc, char* const* nargv, const char* options) { + /* + * We don't pass FLAG_PERMUTE to getopt_internal() since + * the BSD getopt(3) (unlike GNU) has never done this. + * + * Furthermore, since many privileged programs call getopt() + * before dropping privileges it makes sense to keep things + * as simple (and bug-free) as possible. + */ + return (getopt_internal(nargc, nargv, options, NULL, NULL, 0)); +} +#endif /* ARG_REPLACE_GETOPT */ + +/* + * getopt_long -- + * Parse argc/argv argument vector. + */ +int getopt_long(int nargc, char* const* nargv, const char* options, const struct option* long_options, int* idx) { + return (getopt_internal(nargc, nargv, options, long_options, idx, FLAG_PERMUTE)); +} + +/* + * getopt_long_only -- + * Parse argc/argv argument vector. + */ +int getopt_long_only(int nargc, char* const* nargv, const char* options, const struct option* long_options, int* idx) { + return (getopt_internal(nargc, nargv, options, long_options, idx, FLAG_PERMUTE | FLAG_LONGONLY)); +} +/******************************************************************************* + * arg_date: Implements the date command-line option + * + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +#ifndef ARG_AMALGAMATION +#include "argtable3_private.h" +#endif + +#include +#include + +char* arg_strptime(const char* buf, const char* fmt, struct tm* tm); + +static void arg_date_resetfn(struct arg_date* parent) { + ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent)); + parent->count = 0; +} + +static int arg_date_scanfn(struct arg_date* parent, const char* argval) { + int errorcode = 0; + + if (parent->count == parent->hdr.maxcount) { + errorcode = ARG_ERR_MAXCOUNT; + } else if (!argval) { + /* no argument value was given, leave parent->tmval[] unaltered but still count it */ + parent->count++; + } else { + const char* pend; + struct tm tm = parent->tmval[parent->count]; + + /* parse the given argument value, store result in parent->tmval[] */ + pend = arg_strptime(argval, parent->format, &tm); + if (pend && pend[0] == '\0') + parent->tmval[parent->count++] = tm; + else + errorcode = ARG_ERR_BADDATE; + } + + ARG_TRACE(("%s:scanfn(%p) returns %d\n", __FILE__, parent, errorcode)); + return errorcode; +} + +static int arg_date_checkfn(struct arg_date* parent) { + int errorcode = (parent->count < parent->hdr.mincount) ? ARG_ERR_MINCOUNT : 0; + + ARG_TRACE(("%s:checkfn(%p) returns %d\n", __FILE__, parent, errorcode)); + return errorcode; +} + +static void arg_date_errorfn(struct arg_date* parent, arg_dstr_t ds, int errorcode, const char* argval, const char* progname) { + const char* shortopts = parent->hdr.shortopts; + const char* longopts = parent->hdr.longopts; + const char* datatype = parent->hdr.datatype; + + /* make argval NULL safe */ + argval = argval ? argval : ""; + + arg_dstr_catf(ds, "%s: ", progname); + switch (errorcode) { + case ARG_ERR_MINCOUNT: + arg_dstr_cat(ds, "missing option "); + arg_print_option_ds(ds, shortopts, longopts, datatype, "\n"); + break; + + case ARG_ERR_MAXCOUNT: + arg_dstr_cat(ds, "excess option "); + arg_print_option_ds(ds, shortopts, longopts, argval, "\n"); + break; + + case ARG_ERR_BADDATE: { + struct tm tm; + char buff[200]; + + arg_dstr_catf(ds, "illegal timestamp format \"%s\"\n", argval); + memset(&tm, 0, sizeof(tm)); + arg_strptime("1999-12-31 23:59:59", "%F %H:%M:%S", &tm); + strftime(buff, sizeof(buff), parent->format, &tm); + arg_dstr_catf(ds, "correct format is \"%s\"\n", buff); + break; + } + } +} + +struct arg_date* arg_date0(const char* shortopts, const char* longopts, const char* format, const char* datatype, const char* glossary) { + return arg_daten(shortopts, longopts, format, datatype, 0, 1, glossary); +} + +struct arg_date* arg_date1(const char* shortopts, const char* longopts, const char* format, const char* datatype, const char* glossary) { + return arg_daten(shortopts, longopts, format, datatype, 1, 1, glossary); +} + +struct arg_date* +arg_daten(const char* shortopts, const char* longopts, const char* format, const char* datatype, int mincount, int maxcount, const char* glossary) { + size_t nbytes; + struct arg_date* result; + + /* foolproof things by ensuring maxcount is not less than mincount */ + maxcount = (maxcount < mincount) ? mincount : maxcount; + + /* default time format is the national date format for the locale */ + if (!format) + format = "%x"; + + nbytes = sizeof(struct arg_date) /* storage for struct arg_date */ + + maxcount * sizeof(struct tm); /* storage for tmval[maxcount] array */ + + /* allocate storage for the arg_date struct + tmval[] array. */ + /* we use calloc because we want the tmval[] array zero filled. */ + result = (struct arg_date*)xcalloc(1, nbytes); + + /* init the arg_hdr struct */ + result->hdr.flag = ARG_HASVALUE; + result->hdr.shortopts = shortopts; + result->hdr.longopts = longopts; + result->hdr.datatype = datatype ? datatype : format; + result->hdr.glossary = glossary; + result->hdr.mincount = mincount; + result->hdr.maxcount = maxcount; + result->hdr.parent = result; + result->hdr.resetfn = (arg_resetfn*)arg_date_resetfn; + result->hdr.scanfn = (arg_scanfn*)arg_date_scanfn; + result->hdr.checkfn = (arg_checkfn*)arg_date_checkfn; + result->hdr.errorfn = (arg_errorfn*)arg_date_errorfn; + + /* store the tmval[maxcount] array immediately after the arg_date struct */ + result->tmval = (struct tm*)(result + 1); + + /* init the remaining arg_date member variables */ + result->count = 0; + result->format = format; + + ARG_TRACE(("arg_daten() returns %p\n", result)); + return result; +} + +/*- + * Copyright (c) 1997, 1998, 2005, 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code was contributed to The NetBSD Foundation by Klaus Klein. + * Heavily optimised by David Laight + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +/* + * We do not implement alternate representations. However, we always + * check whether a given modifier is allowed for a certain conversion. + */ +#define ALT_E 0x01 +#define ALT_O 0x02 +#define LEGAL_ALT(x) \ + { \ + if (alt_format & ~(x)) \ + return (0); \ + } +#define TM_YEAR_BASE (1900) + +static int conv_num(const char**, int*, int, int); + +static const char* day[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; + +static const char* abday[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; + +static const char* mon[12] = {"January", "February", "March", "April", "May", "June", + "July", "August", "September", "October", "November", "December"}; + +static const char* abmon[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; + +static const char* am_pm[2] = {"AM", "PM"}; + +static int arg_strcasecmp(const char* s1, const char* s2) { + const unsigned char* us1 = (const unsigned char*)s1; + const unsigned char* us2 = (const unsigned char*)s2; + while (tolower(*us1) == tolower(*us2++)) + if (*us1++ == '\0') + return 0; + + return tolower(*us1) - tolower(*--us2); +} + +static int arg_strncasecmp(const char* s1, const char* s2, size_t n) { + if (n != 0) { + const unsigned char* us1 = (const unsigned char*)s1; + const unsigned char* us2 = (const unsigned char*)s2; + do { + if (tolower(*us1) != tolower(*us2++)) + return tolower(*us1) - tolower(*--us2); + + if (*us1++ == '\0') + break; + } while (--n != 0); + } + + return 0; +} + +char* arg_strptime(const char* buf, const char* fmt, struct tm* tm) { + char c; + const char* bp; + size_t len = 0; + int alt_format, i, split_year = 0; + + bp = buf; + + while ((c = *fmt) != '\0') { + /* Clear `alternate' modifier prior to new conversion. */ + alt_format = 0; + + /* Eat up white-space. */ + if (isspace(c)) { + while (isspace(*bp)) + bp++; + + fmt++; + continue; + } + + if ((c = *fmt++) != '%') + goto literal; + + again: + switch (c = *fmt++) { + case '%': /* "%%" is converted to "%". */ + literal: + if (c != *bp++) + return (0); + break; + + /* + * "Alternative" modifiers. Just set the appropriate flag + * and start over again. + */ + case 'E': /* "%E?" alternative conversion modifier. */ + LEGAL_ALT(0); + alt_format |= ALT_E; + goto again; + + case 'O': /* "%O?" alternative conversion modifier. */ + LEGAL_ALT(0); + alt_format |= ALT_O; + goto again; + + /* + * "Complex" conversion rules, implemented through recursion. + */ + case 'c': /* Date and time, using the locale's format. */ + LEGAL_ALT(ALT_E); + bp = arg_strptime(bp, "%x %X", tm); + if (!bp) + return (0); + break; + + case 'D': /* The date as "%m/%d/%y". */ + LEGAL_ALT(0); + bp = arg_strptime(bp, "%m/%d/%y", tm); + if (!bp) + return (0); + break; + + case 'R': /* The time as "%H:%M". */ + LEGAL_ALT(0); + bp = arg_strptime(bp, "%H:%M", tm); + if (!bp) + return (0); + break; + + case 'r': /* The time in 12-hour clock representation. */ + LEGAL_ALT(0); + bp = arg_strptime(bp, "%I:%M:%S %p", tm); + if (!bp) + return (0); + break; + + case 'T': /* The time as "%H:%M:%S". */ + LEGAL_ALT(0); + bp = arg_strptime(bp, "%H:%M:%S", tm); + if (!bp) + return (0); + break; + + case 'X': /* The time, using the locale's format. */ + LEGAL_ALT(ALT_E); + bp = arg_strptime(bp, "%H:%M:%S", tm); + if (!bp) + return (0); + break; + + case 'x': /* The date, using the locale's format. */ + LEGAL_ALT(ALT_E); + bp = arg_strptime(bp, "%m/%d/%y", tm); + if (!bp) + return (0); + break; + + /* + * "Elementary" conversion rules. + */ + case 'A': /* The day of week, using the locale's form. */ + case 'a': + LEGAL_ALT(0); + for (i = 0; i < 7; i++) { + /* Full name. */ + len = strlen(day[i]); + if (arg_strncasecmp(day[i], bp, len) == 0) + break; + + /* Abbreviated name. */ + len = strlen(abday[i]); + if (arg_strncasecmp(abday[i], bp, len) == 0) + break; + } + + /* Nothing matched. */ + if (i == 7) + return (0); + + tm->tm_wday = i; + bp += len; + break; + + case 'B': /* The month, using the locale's form. */ + case 'b': + case 'h': + LEGAL_ALT(0); + for (i = 0; i < 12; i++) { + /* Full name. */ + len = strlen(mon[i]); + if (arg_strncasecmp(mon[i], bp, len) == 0) + break; + + /* Abbreviated name. */ + len = strlen(abmon[i]); + if (arg_strncasecmp(abmon[i], bp, len) == 0) + break; + } + + /* Nothing matched. */ + if (i == 12) + return (0); + + tm->tm_mon = i; + bp += len; + break; + + case 'C': /* The century number. */ + LEGAL_ALT(ALT_E); + if (!(conv_num(&bp, &i, 0, 99))) + return (0); + + if (split_year) { + tm->tm_year = (tm->tm_year % 100) + (i * 100); + } else { + tm->tm_year = i * 100; + split_year = 1; + } + break; + + case 'd': /* The day of month. */ + case 'e': + LEGAL_ALT(ALT_O); + if (!(conv_num(&bp, &tm->tm_mday, 1, 31))) + return (0); + break; + + case 'k': /* The hour (24-hour clock representation). */ + LEGAL_ALT(0); + /* FALLTHROUGH */ + case 'H': + LEGAL_ALT(ALT_O); + if (!(conv_num(&bp, &tm->tm_hour, 0, 23))) + return (0); + break; + + case 'l': /* The hour (12-hour clock representation). */ + LEGAL_ALT(0); + /* FALLTHROUGH */ + case 'I': + LEGAL_ALT(ALT_O); + if (!(conv_num(&bp, &tm->tm_hour, 1, 12))) + return (0); + if (tm->tm_hour == 12) + tm->tm_hour = 0; + break; + + case 'j': /* The day of year. */ + LEGAL_ALT(0); + if (!(conv_num(&bp, &i, 1, 366))) + return (0); + tm->tm_yday = i - 1; + break; + + case 'M': /* The minute. */ + LEGAL_ALT(ALT_O); + if (!(conv_num(&bp, &tm->tm_min, 0, 59))) + return (0); + break; + + case 'm': /* The month. */ + LEGAL_ALT(ALT_O); + if (!(conv_num(&bp, &i, 1, 12))) + return (0); + tm->tm_mon = i - 1; + break; + + case 'p': /* The locale's equivalent of AM/PM. */ + LEGAL_ALT(0); + /* AM? */ + if (arg_strcasecmp(am_pm[0], bp) == 0) { + if (tm->tm_hour > 11) + return (0); + + bp += strlen(am_pm[0]); + break; + } + /* PM? */ + else if (arg_strcasecmp(am_pm[1], bp) == 0) { + if (tm->tm_hour > 11) + return (0); + + tm->tm_hour += 12; + bp += strlen(am_pm[1]); + break; + } + + /* Nothing matched. */ + return (0); + + case 'S': /* The seconds. */ + LEGAL_ALT(ALT_O); + if (!(conv_num(&bp, &tm->tm_sec, 0, 61))) + return (0); + break; + + case 'U': /* The week of year, beginning on sunday. */ + case 'W': /* The week of year, beginning on monday. */ + LEGAL_ALT(ALT_O); + /* + * XXX This is bogus, as we can not assume any valid + * information present in the tm structure at this + * point to calculate a real value, so just check the + * range for now. + */ + if (!(conv_num(&bp, &i, 0, 53))) + return (0); + break; + + case 'w': /* The day of week, beginning on sunday. */ + LEGAL_ALT(ALT_O); + if (!(conv_num(&bp, &tm->tm_wday, 0, 6))) + return (0); + break; + + case 'Y': /* The year. */ + LEGAL_ALT(ALT_E); + if (!(conv_num(&bp, &i, 0, 9999))) + return (0); + + tm->tm_year = i - TM_YEAR_BASE; + break; + + case 'y': /* The year within 100 years of the epoch. */ + LEGAL_ALT(ALT_E | ALT_O); + if (!(conv_num(&bp, &i, 0, 99))) + return (0); + + if (split_year) { + tm->tm_year = ((tm->tm_year / 100) * 100) + i; + break; + } + split_year = 1; + if (i <= 68) + tm->tm_year = i + 2000 - TM_YEAR_BASE; + else + tm->tm_year = i + 1900 - TM_YEAR_BASE; + break; + + /* + * Miscellaneous conversions. + */ + case 'n': /* Any kind of white-space. */ + case 't': + LEGAL_ALT(0); + while (isspace(*bp)) + bp++; + break; + + default: /* Unknown/unsupported conversion. */ + return (0); + } + } + + /* LINTED functional specification */ + return ((char*)bp); +} + +static int conv_num(const char** buf, int* dest, int llim, int ulim) { + int result = 0; + + /* The limit also determines the number of valid digits. */ + int rulim = ulim; + + if (**buf < '0' || **buf > '9') + return (0); + + do { + result *= 10; + result += *(*buf)++ - '0'; + rulim /= 10; + } while ((result * 10 <= ulim) && rulim && **buf >= '0' && **buf <= '9'); + + if (result < llim || result > ulim) + return (0); + + *dest = result; + return (1); +} +/******************************************************************************* + * arg_dbl: Implements the double command-line option + * + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +#ifndef ARG_AMALGAMATION +#include "argtable3_private.h" +#endif + +#include + +static void arg_dbl_resetfn(struct arg_dbl* parent) { + ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent)); + parent->count = 0; +} + +static int arg_dbl_scanfn(struct arg_dbl* parent, const char* argval) { + int errorcode = 0; + + if (parent->count == parent->hdr.maxcount) { + /* maximum number of arguments exceeded */ + errorcode = ARG_ERR_MAXCOUNT; + } else if (!argval) { + /* a valid argument with no argument value was given. */ + /* This happens when an optional argument value was invoked. */ + /* leave parent argument value unaltered but still count the argument. */ + parent->count++; + } else { + double val; + char* end; + + /* extract double from argval into val */ + val = strtod(argval, &end); + + /* if success then store result in parent->dval[] array otherwise return error*/ + if (*end == 0) + parent->dval[parent->count++] = val; + else + errorcode = ARG_ERR_BADDOUBLE; + } + + ARG_TRACE(("%s:scanfn(%p) returns %d\n", __FILE__, parent, errorcode)); + return errorcode; +} + +static int arg_dbl_checkfn(struct arg_dbl* parent) { + int errorcode = (parent->count < parent->hdr.mincount) ? ARG_ERR_MINCOUNT : 0; + + ARG_TRACE(("%s:checkfn(%p) returns %d\n", __FILE__, parent, errorcode)); + return errorcode; +} + +static void arg_dbl_errorfn(struct arg_dbl* parent, arg_dstr_t ds, int errorcode, const char* argval, const char* progname) { + const char* shortopts = parent->hdr.shortopts; + const char* longopts = parent->hdr.longopts; + const char* datatype = parent->hdr.datatype; + + /* make argval NULL safe */ + argval = argval ? argval : ""; + + arg_dstr_catf(ds, "%s: ", progname); + switch (errorcode) { + case ARG_ERR_MINCOUNT: + arg_dstr_cat(ds, "missing option "); + arg_print_option_ds(ds, shortopts, longopts, datatype, "\n"); + break; + + case ARG_ERR_MAXCOUNT: + arg_dstr_cat(ds, "excess option "); + arg_print_option_ds(ds, shortopts, longopts, argval, "\n"); + break; + + case ARG_ERR_BADDOUBLE: + arg_dstr_catf(ds, "invalid argument \"%s\" to option ", argval); + arg_print_option_ds(ds, shortopts, longopts, datatype, "\n"); + break; + } +} + +struct arg_dbl* arg_dbl0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary) { + return arg_dbln(shortopts, longopts, datatype, 0, 1, glossary); +} + +struct arg_dbl* arg_dbl1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary) { + return arg_dbln(shortopts, longopts, datatype, 1, 1, glossary); +} + +struct arg_dbl* arg_dbln(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary) { + size_t nbytes; + struct arg_dbl* result; + size_t addr; + size_t rem; + + /* foolproof things by ensuring maxcount is not less than mincount */ + maxcount = (maxcount < mincount) ? mincount : maxcount; + + nbytes = sizeof(struct arg_dbl) /* storage for struct arg_dbl */ + + (maxcount + 1) * sizeof(double); /* storage for dval[maxcount] array plus one extra for padding to memory boundary */ + + result = (struct arg_dbl*)xmalloc(nbytes); + + /* init the arg_hdr struct */ + result->hdr.flag = ARG_HASVALUE; + result->hdr.shortopts = shortopts; + result->hdr.longopts = longopts; + result->hdr.datatype = datatype ? datatype : ""; + result->hdr.glossary = glossary; + result->hdr.mincount = mincount; + result->hdr.maxcount = maxcount; + result->hdr.parent = result; + result->hdr.resetfn = (arg_resetfn*)arg_dbl_resetfn; + result->hdr.scanfn = (arg_scanfn*)arg_dbl_scanfn; + result->hdr.checkfn = (arg_checkfn*)arg_dbl_checkfn; + result->hdr.errorfn = (arg_errorfn*)arg_dbl_errorfn; + + /* Store the dval[maxcount] array on the first double boundary that + * immediately follows the arg_dbl struct. We do the memory alignment + * purely for SPARC and Motorola systems. They require floats and + * doubles to be aligned on natural boundaries. + */ + addr = (size_t)(result + 1); + rem = addr % sizeof(double); + result->dval = (double*)(addr + sizeof(double) - rem); + ARG_TRACE(("addr=%p, dval=%p, sizeof(double)=%d rem=%d\n", addr, result->dval, (int)sizeof(double), (int)rem)); + + result->count = 0; + + ARG_TRACE(("arg_dbln() returns %p\n", result)); + return result; +} +/******************************************************************************* + * arg_end: Implements the error handling utilities + * + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +#ifndef ARG_AMALGAMATION +#include "argtable3_private.h" +#endif + +#include + +static void arg_end_resetfn(struct arg_end* parent) { + ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent)); + parent->count = 0; +} + +static void arg_end_errorfn(void* parent, arg_dstr_t ds, int error, const char* argval, const char* progname) { + /* suppress unreferenced formal parameter warning */ + (void)parent; + + progname = progname ? progname : ""; + argval = argval ? argval : ""; + + arg_dstr_catf(ds, "%s: ", progname); + switch (error) { + case ARG_ELIMIT: + arg_dstr_cat(ds, "too many errors to display"); + break; + case ARG_EMALLOC: + arg_dstr_cat(ds, "insufficient memory"); + break; + case ARG_ENOMATCH: + arg_dstr_catf(ds, "unexpected argument \"%s\"", argval); + break; + case ARG_EMISSARG: + arg_dstr_catf(ds, "option \"%s\" requires an argument", argval); + break; + case ARG_ELONGOPT: + arg_dstr_catf(ds, "invalid option \"%s\"", argval); + break; + default: + arg_dstr_catf(ds, "invalid option \"-%c\"", error); + break; + } + + arg_dstr_cat(ds, "\n"); +} + +struct arg_end* arg_end(int maxcount) { + size_t nbytes; + struct arg_end* result; + + nbytes = sizeof(struct arg_end) + maxcount * sizeof(int) /* storage for int error[maxcount] array*/ + + maxcount * sizeof(void*) /* storage for void* parent[maxcount] array */ + + maxcount * sizeof(char*); /* storage for char* argval[maxcount] array */ + + result = (struct arg_end*)xmalloc(nbytes); + + /* init the arg_hdr struct */ + result->hdr.flag = ARG_TERMINATOR; + result->hdr.shortopts = NULL; + result->hdr.longopts = NULL; + result->hdr.datatype = NULL; + result->hdr.glossary = NULL; + result->hdr.mincount = 1; + result->hdr.maxcount = maxcount; + result->hdr.parent = result; + result->hdr.resetfn = (arg_resetfn*)arg_end_resetfn; + result->hdr.scanfn = NULL; + result->hdr.checkfn = NULL; + result->hdr.errorfn = (arg_errorfn*)arg_end_errorfn; + + /* store error[maxcount] array immediately after struct arg_end */ + result->error = (int*)(result + 1); + + /* store parent[maxcount] array immediately after error[] array */ + result->parent = (void**)(result->error + maxcount); + + /* store argval[maxcount] array immediately after parent[] array */ + result->argval = (const char**)(result->parent + maxcount); + + ARG_TRACE(("arg_end(%d) returns %p\n", maxcount, result)); + return result; +} + +void arg_print_errors_ds(arg_dstr_t ds, struct arg_end* end, const char* progname) { + int i; + ARG_TRACE(("arg_errors()\n")); + for (i = 0; i < end->count; i++) { + struct arg_hdr* errorparent = (struct arg_hdr*)(end->parent[i]); + if (errorparent->errorfn) + errorparent->errorfn(end->parent[i], ds, end->error[i], end->argval[i], progname); + } +} + +void arg_print_errors(FILE* fp, struct arg_end* end, const char* progname) { + arg_dstr_t ds = arg_dstr_create(); + arg_print_errors_ds(ds, end, progname); + fputs(arg_dstr_cstr(ds), fp); + arg_dstr_destroy(ds); +} +/******************************************************************************* + * arg_file: Implements the file command-line option + * + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +#ifndef ARG_AMALGAMATION +#include "argtable3_private.h" +#endif + +#include +#include + +#ifdef WIN32 +#define FILESEPARATOR1 '\\' +#define FILESEPARATOR2 '/' +#else +#define FILESEPARATOR1 '/' +#define FILESEPARATOR2 '/' +#endif + +static void arg_file_resetfn(struct arg_file* parent) { + ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent)); + parent->count = 0; +} + +/* Returns ptr to the base filename within *filename */ +static const char* arg_basename(const char* filename) { + const char *result = NULL, *result1, *result2; + + /* Find the last occurrence of eother file separator character. */ + /* Two alternative file separator chars are supported as legal */ + /* file separators but not both together in the same filename. */ + result1 = (filename ? strrchr(filename, FILESEPARATOR1) : NULL); + result2 = (filename ? strrchr(filename, FILESEPARATOR2) : NULL); + + if (result2) + result = result2 + 1; /* using FILESEPARATOR2 (the alternative file separator) */ + + if (result1) + result = result1 + 1; /* using FILESEPARATOR1 (the preferred file separator) */ + + if (!result) + result = filename; /* neither file separator was found so basename is the whole filename */ + + /* special cases of "." and ".." are not considered basenames */ + if (result && (strcmp(".", result) == 0 || strcmp("..", result) == 0)) + result = filename + strlen(filename); + + return result; +} + +/* Returns ptr to the file extension within *basename */ +static const char* arg_extension(const char* basename) { + /* find the last occurrence of '.' in basename */ + const char* result = (basename ? strrchr(basename, '.') : NULL); + + /* if no '.' was found then return pointer to end of basename */ + if (basename && !result) + result = basename + strlen(basename); + + /* special case: basenames with a single leading dot (eg ".foo") are not considered as true extensions */ + if (basename && result == basename) + result = basename + strlen(basename); + + /* special case: empty extensions (eg "foo.","foo..") are not considered as true extensions */ + if (basename && result && result[1] == '\0') + result = basename + strlen(basename); + + return result; +} + +static int arg_file_scanfn(struct arg_file* parent, const char* argval) { + int errorcode = 0; + + if (parent->count == parent->hdr.maxcount) { + /* maximum number of arguments exceeded */ + errorcode = ARG_ERR_MAXCOUNT; + } else if (!argval) { + /* a valid argument with no argument value was given. */ + /* This happens when an optional argument value was invoked. */ + /* leave parent arguiment value unaltered but still count the argument. */ + parent->count++; + } else { + parent->filename[parent->count] = argval; + parent->basename[parent->count] = arg_basename(argval); + parent->extension[parent->count] = + arg_extension(parent->basename[parent->count]); /* only seek extensions within the basename (not the file path)*/ + parent->count++; + } + + ARG_TRACE(("%s4:scanfn(%p) returns %d\n", __FILE__, parent, errorcode)); + return errorcode; +} + +static int arg_file_checkfn(struct arg_file* parent) { + int errorcode = (parent->count < parent->hdr.mincount) ? ARG_ERR_MINCOUNT : 0; + + ARG_TRACE(("%s:checkfn(%p) returns %d\n", __FILE__, parent, errorcode)); + return errorcode; +} + +static void arg_file_errorfn(struct arg_file* parent, arg_dstr_t ds, int errorcode, const char* argval, const char* progname) { + const char* shortopts = parent->hdr.shortopts; + const char* longopts = parent->hdr.longopts; + const char* datatype = parent->hdr.datatype; + + /* make argval NULL safe */ + argval = argval ? argval : ""; + + arg_dstr_catf(ds, "%s: ", progname); + switch (errorcode) { + case ARG_ERR_MINCOUNT: + arg_dstr_cat(ds, "missing option "); + arg_print_option_ds(ds, shortopts, longopts, datatype, "\n"); + break; + + case ARG_ERR_MAXCOUNT: + arg_dstr_cat(ds, "excess option "); + arg_print_option_ds(ds, shortopts, longopts, argval, "\n"); + break; + + default: + arg_dstr_catf(ds, "unknown error at \"%s\"\n", argval); + } +} + +struct arg_file* arg_file0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary) { + return arg_filen(shortopts, longopts, datatype, 0, 1, glossary); +} + +struct arg_file* arg_file1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary) { + return arg_filen(shortopts, longopts, datatype, 1, 1, glossary); +} + +struct arg_file* arg_filen(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary) { + size_t nbytes; + struct arg_file* result; + int i; + + /* foolproof things by ensuring maxcount is not less than mincount */ + maxcount = (maxcount < mincount) ? mincount : maxcount; + + nbytes = sizeof(struct arg_file) /* storage for struct arg_file */ + + sizeof(char*) * maxcount /* storage for filename[maxcount] array */ + + sizeof(char*) * maxcount /* storage for basename[maxcount] array */ + + sizeof(char*) * maxcount; /* storage for extension[maxcount] array */ + + result = (struct arg_file*)xmalloc(nbytes); + + /* init the arg_hdr struct */ + result->hdr.flag = ARG_HASVALUE; + result->hdr.shortopts = shortopts; + result->hdr.longopts = longopts; + result->hdr.glossary = glossary; + result->hdr.datatype = datatype ? datatype : ""; + result->hdr.mincount = mincount; + result->hdr.maxcount = maxcount; + result->hdr.parent = result; + result->hdr.resetfn = (arg_resetfn*)arg_file_resetfn; + result->hdr.scanfn = (arg_scanfn*)arg_file_scanfn; + result->hdr.checkfn = (arg_checkfn*)arg_file_checkfn; + result->hdr.errorfn = (arg_errorfn*)arg_file_errorfn; + + /* store the filename,basename,extension arrays immediately after the arg_file struct */ + result->filename = (const char**)(result + 1); + result->basename = result->filename + maxcount; + result->extension = result->basename + maxcount; + result->count = 0; + + /* foolproof the string pointers by initialising them with empty strings */ + for (i = 0; i < maxcount; i++) { + result->filename[i] = ""; + result->basename[i] = ""; + result->extension[i] = ""; + } + + ARG_TRACE(("arg_filen() returns %p\n", result)); + return result; +} +/******************************************************************************* + * arg_int: Implements the int command-line option + * + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +#ifndef ARG_AMALGAMATION +#include "argtable3_private.h" +#endif + +#include +#include +#include + +static void arg_int_resetfn(struct arg_int* parent) { + ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent)); + parent->count = 0; +} + +/* strtol0x() is like strtol() except that the numeric string is */ +/* expected to be prefixed by "0X" where X is a user supplied char. */ +/* The string may optionally be prefixed by white space and + or - */ +/* as in +0X123 or -0X123. */ +/* Once the prefix has been scanned, the remainder of the numeric */ +/* string is converted using strtol() with the given base. */ +/* eg: to parse hex str="-0X12324", specify X='X' and base=16. */ +/* eg: to parse oct str="+0o12324", specify X='O' and base=8. */ +/* eg: to parse bin str="-0B01010", specify X='B' and base=2. */ +/* Failure of conversion is indicated by result where *endptr==str. */ +static long int strtol0X(const char* str, const char** endptr, char X, int base) { + long int val; /* stores result */ + int s = 1; /* sign is +1 or -1 */ + const char* ptr = str; /* ptr to current position in str */ + + /* skip leading whitespace */ + while (isspace(*ptr)) + ptr++; + /* printf("1) %s\n",ptr); */ + + /* scan optional sign character */ + switch (*ptr) { + case '+': + ptr++; + s = 1; + break; + case '-': + ptr++; + s = -1; + break; + default: + s = 1; + break; + } + /* printf("2) %s\n",ptr); */ + + /* '0X' prefix */ + if ((*ptr++) != '0') { + /* printf("failed to detect '0'\n"); */ + *endptr = str; + return 0; + } + /* printf("3) %s\n",ptr); */ + if (toupper(*ptr++) != toupper(X)) { + /* printf("failed to detect '%c'\n",X); */ + *endptr = str; + return 0; + } + /* printf("4) %s\n",ptr); */ + + /* attempt conversion on remainder of string using strtol() */ + val = strtol(ptr, (char**)endptr, base); + if (*endptr == ptr) { + /* conversion failed */ + *endptr = str; + return 0; + } + + /* success */ + return s * val; +} + +/* Returns 1 if str matches suffix (case insensitive). */ +/* Str may contain trailing whitespace, but nothing else. */ +static int detectsuffix(const char* str, const char* suffix) { + /* scan pairwise through strings until mismatch detected */ + while (toupper(*str) == toupper(*suffix)) { + /* printf("'%c' '%c'\n", *str, *suffix); */ + + /* return 1 (success) if match persists until the string terminator */ + if (*str == '\0') + return 1; + + /* next chars */ + str++; + suffix++; + } + /* printf("'%c' '%c' mismatch\n", *str, *suffix); */ + + /* return 0 (fail) if the matching did not consume the entire suffix */ + if (*suffix != 0) + return 0; /* failed to consume entire suffix */ + + /* skip any remaining whitespace in str */ + while (isspace(*str)) + str++; + + /* return 1 (success) if we have reached end of str else return 0 (fail) */ + return (*str == '\0') ? 1 : 0; +} + +static int arg_int_scanfn(struct arg_int* parent, const char* argval) { + int errorcode = 0; + + if (parent->count == parent->hdr.maxcount) { + /* maximum number of arguments exceeded */ + errorcode = ARG_ERR_MAXCOUNT; + } else if (!argval) { + /* a valid argument with no argument value was given. */ + /* This happens when an optional argument value was invoked. */ + /* leave parent arguiment value unaltered but still count the argument. */ + parent->count++; + } else { + long int val; + const char* end; + + /* attempt to extract hex integer (eg: +0x123) from argval into val conversion */ + val = strtol0X(argval, &end, 'X', 16); + if (end == argval) { + /* hex failed, attempt octal conversion (eg +0o123) */ + val = strtol0X(argval, &end, 'O', 8); + if (end == argval) { + /* octal failed, attempt binary conversion (eg +0B101) */ + val = strtol0X(argval, &end, 'B', 2); + if (end == argval) { + /* binary failed, attempt decimal conversion with no prefix (eg 1234) */ + val = strtol(argval, (char**)&end, 10); + if (end == argval) { + /* all supported number formats failed */ + return ARG_ERR_BADINT; + } + } + } + } + + /* Safety check for integer overflow. WARNING: this check */ + /* achieves nothing on machines where size(int)==size(long). */ + if (val > INT_MAX || val < INT_MIN) + errorcode = ARG_ERR_OVERFLOW; + + /* Detect any suffixes (KB,MB,GB) and multiply argument value appropriately. */ + /* We need to be mindful of integer overflows when using such big numbers. */ + if (detectsuffix(end, "KB")) /* kilobytes */ + { + if (val > (INT_MAX / 1024) || val < (INT_MIN / 1024)) + errorcode = ARG_ERR_OVERFLOW; /* Overflow would occur if we proceed */ + else + val *= 1024; /* 1KB = 1024 */ + } else if (detectsuffix(end, "MB")) /* megabytes */ + { + if (val > (INT_MAX / 1048576) || val < (INT_MIN / 1048576)) + errorcode = ARG_ERR_OVERFLOW; /* Overflow would occur if we proceed */ + else + val *= 1048576; /* 1MB = 1024*1024 */ + } else if (detectsuffix(end, "GB")) /* gigabytes */ + { + if (val > (INT_MAX / 1073741824) || val < (INT_MIN / 1073741824)) + errorcode = ARG_ERR_OVERFLOW; /* Overflow would occur if we proceed */ + else + val *= 1073741824; /* 1GB = 1024*1024*1024 */ + } else if (!detectsuffix(end, "")) + errorcode = ARG_ERR_BADINT; /* invalid suffix detected */ + + /* if success then store result in parent->ival[] array */ + if (errorcode == 0) + parent->ival[parent->count++] = (int)val; + } + + /* printf("%s:scanfn(%p,%p) returns %d\n",__FILE__,parent,argval,errorcode); */ + return errorcode; +} + +static int arg_int_checkfn(struct arg_int* parent) { + int errorcode = (parent->count < parent->hdr.mincount) ? ARG_ERR_MINCOUNT : 0; + /*printf("%s:checkfn(%p) returns %d\n",__FILE__,parent,errorcode);*/ + return errorcode; +} + +static void arg_int_errorfn(struct arg_int* parent, arg_dstr_t ds, int errorcode, const char* argval, const char* progname) { + const char* shortopts = parent->hdr.shortopts; + const char* longopts = parent->hdr.longopts; + const char* datatype = parent->hdr.datatype; + + /* make argval NULL safe */ + argval = argval ? argval : ""; + + arg_dstr_catf(ds, "%s: ", progname); + switch (errorcode) { + case ARG_ERR_MINCOUNT: + arg_dstr_cat(ds, "missing option "); + arg_print_option_ds(ds, shortopts, longopts, datatype, "\n"); + break; + + case ARG_ERR_MAXCOUNT: + arg_dstr_cat(ds, "excess option "); + arg_print_option_ds(ds, shortopts, longopts, argval, "\n"); + break; + + case ARG_ERR_BADINT: + arg_dstr_catf(ds, "invalid argument \"%s\" to option ", argval); + arg_print_option_ds(ds, shortopts, longopts, datatype, "\n"); + break; + + case ARG_ERR_OVERFLOW: + arg_dstr_cat(ds, "integer overflow at option "); + arg_print_option_ds(ds, shortopts, longopts, datatype, " "); + arg_dstr_catf(ds, "(%s is too large)\n", argval); + break; + } +} + +struct arg_int* arg_int0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary) { + return arg_intn(shortopts, longopts, datatype, 0, 1, glossary); +} + +struct arg_int* arg_int1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary) { + return arg_intn(shortopts, longopts, datatype, 1, 1, glossary); +} + +struct arg_int* arg_intn(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary) { + size_t nbytes; + struct arg_int* result; + + /* foolproof things by ensuring maxcount is not less than mincount */ + maxcount = (maxcount < mincount) ? mincount : maxcount; + + nbytes = sizeof(struct arg_int) /* storage for struct arg_int */ + + maxcount * sizeof(int); /* storage for ival[maxcount] array */ + + result = (struct arg_int*)xmalloc(nbytes); + + /* init the arg_hdr struct */ + result->hdr.flag = ARG_HASVALUE; + result->hdr.shortopts = shortopts; + result->hdr.longopts = longopts; + result->hdr.datatype = datatype ? datatype : ""; + result->hdr.glossary = glossary; + result->hdr.mincount = mincount; + result->hdr.maxcount = maxcount; + result->hdr.parent = result; + result->hdr.resetfn = (arg_resetfn*)arg_int_resetfn; + result->hdr.scanfn = (arg_scanfn*)arg_int_scanfn; + result->hdr.checkfn = (arg_checkfn*)arg_int_checkfn; + result->hdr.errorfn = (arg_errorfn*)arg_int_errorfn; + + /* store the ival[maxcount] array immediately after the arg_int struct */ + result->ival = (int*)(result + 1); + result->count = 0; + + ARG_TRACE(("arg_intn() returns %p\n", result)); + return result; +} +/******************************************************************************* + * arg_lit: Implements the literature command-line option + * + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +#ifndef ARG_AMALGAMATION +#include "argtable3_private.h" +#endif + +#include + +static void arg_lit_resetfn(struct arg_lit* parent) { + ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent)); + parent->count = 0; +} + +static int arg_lit_scanfn(struct arg_lit* parent, const char* argval) { + int errorcode = 0; + if (parent->count < parent->hdr.maxcount) + parent->count++; + else + errorcode = ARG_ERR_MAXCOUNT; + + ARG_TRACE(("%s:scanfn(%p,%s) returns %d\n", __FILE__, parent, argval, errorcode)); + return errorcode; +} + +static int arg_lit_checkfn(struct arg_lit* parent) { + int errorcode = (parent->count < parent->hdr.mincount) ? ARG_ERR_MINCOUNT : 0; + ARG_TRACE(("%s:checkfn(%p) returns %d\n", __FILE__, parent, errorcode)); + return errorcode; +} + +static void arg_lit_errorfn(struct arg_lit* parent, arg_dstr_t ds, int errorcode, const char* argval, const char* progname) { + const char* shortopts = parent->hdr.shortopts; + const char* longopts = parent->hdr.longopts; + const char* datatype = parent->hdr.datatype; + + switch (errorcode) { + case ARG_ERR_MINCOUNT: + arg_dstr_catf(ds, "%s: missing option ", progname); + arg_print_option_ds(ds, shortopts, longopts, datatype, "\n"); + arg_dstr_cat(ds, "\n"); + break; + + case ARG_ERR_MAXCOUNT: + arg_dstr_catf(ds, "%s: extraneous option ", progname); + arg_print_option_ds(ds, shortopts, longopts, datatype, "\n"); + break; + } + + ARG_TRACE(("%s:errorfn(%p, %p, %d, %s, %s)\n", __FILE__, parent, ds, errorcode, argval, progname)); +} + +struct arg_lit* arg_lit0(const char* shortopts, const char* longopts, const char* glossary) { + return arg_litn(shortopts, longopts, 0, 1, glossary); +} + +struct arg_lit* arg_lit1(const char* shortopts, const char* longopts, const char* glossary) { + return arg_litn(shortopts, longopts, 1, 1, glossary); +} + +struct arg_lit* arg_litn(const char* shortopts, const char* longopts, int mincount, int maxcount, const char* glossary) { + struct arg_lit* result; + + /* foolproof things by ensuring maxcount is not less than mincount */ + maxcount = (maxcount < mincount) ? mincount : maxcount; + + result = (struct arg_lit*)xmalloc(sizeof(struct arg_lit)); + + /* init the arg_hdr struct */ + result->hdr.flag = 0; + result->hdr.shortopts = shortopts; + result->hdr.longopts = longopts; + result->hdr.datatype = NULL; + result->hdr.glossary = glossary; + result->hdr.mincount = mincount; + result->hdr.maxcount = maxcount; + result->hdr.parent = result; + result->hdr.resetfn = (arg_resetfn*)arg_lit_resetfn; + result->hdr.scanfn = (arg_scanfn*)arg_lit_scanfn; + result->hdr.checkfn = (arg_checkfn*)arg_lit_checkfn; + result->hdr.errorfn = (arg_errorfn*)arg_lit_errorfn; + + /* init local variables */ + result->count = 0; + + ARG_TRACE(("arg_litn() returns %p\n", result)); + return result; +} +/******************************************************************************* + * arg_rem: Implements the rem command-line option + * + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +#ifndef ARG_AMALGAMATION +#include "argtable3_private.h" +#endif + +#include + +struct arg_rem* arg_rem(const char* datatype, const char* glossary) { + struct arg_rem* result = (struct arg_rem*)xmalloc(sizeof(struct arg_rem)); + + result->hdr.flag = 0; + result->hdr.shortopts = NULL; + result->hdr.longopts = NULL; + result->hdr.datatype = datatype; + result->hdr.glossary = glossary; + result->hdr.mincount = 1; + result->hdr.maxcount = 1; + result->hdr.parent = result; + result->hdr.resetfn = NULL; + result->hdr.scanfn = NULL; + result->hdr.checkfn = NULL; + result->hdr.errorfn = NULL; + + ARG_TRACE(("arg_rem() returns %p\n", result)); + return result; +} +/******************************************************************************* + * arg_rex: Implements the regex command-line option + * + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +#ifndef ARG_AMALGAMATION +#include "argtable3_private.h" +#endif + +#include +#include + +#ifndef _TREX_H_ +#define _TREX_H_ + +/* + * This module uses the T-Rex regular expression library to implement the regex + * logic. Here is the copyright notice of the library: + * + * Copyright (C) 2003-2006 Alberto Demichelis + * + * This software is provided 'as-is', without any express + * or implied warranty. In no event will the authors be held + * liable for any damages arising from the use of this software. + * + * Permission is granted to anyone to use this software for + * any purpose, including commercial applications, and to alter + * it and redistribute it freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; + * you must not claim that you wrote the original software. + * If you use this software in a product, an acknowledgment + * in the product documentation would be appreciated but + * is not required. + * + * 2. Altered source versions must be plainly marked as such, + * and must not be misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any + * source distribution. + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#define TRexChar char +#define MAX_CHAR 0xFF +#define _TREXC(c) (c) +#define trex_strlen strlen +#define trex_printf printf + +#ifndef TREX_API +#define TREX_API extern +#endif + +#define TRex_True 1 +#define TRex_False 0 + +#define TREX_ICASE ARG_REX_ICASE + +typedef unsigned int TRexBool; +typedef struct TRex TRex; + +typedef struct { + const TRexChar* begin; + int len; +} TRexMatch; + +TREX_API TRex* trex_compile(const TRexChar* pattern, const TRexChar** error, int flags); +TREX_API void trex_free(TRex* exp); +TREX_API TRexBool trex_match(TRex* exp, const TRexChar* text); +TREX_API TRexBool trex_search(TRex* exp, const TRexChar* text, const TRexChar** out_begin, const TRexChar** out_end); +TREX_API TRexBool +trex_searchrange(TRex* exp, const TRexChar* text_begin, const TRexChar* text_end, const TRexChar** out_begin, const TRexChar** out_end); +TREX_API int trex_getsubexpcount(TRex* exp); +TREX_API TRexBool trex_getsubexp(TRex* exp, int n, TRexMatch* subexp); + +#ifdef __cplusplus +} +#endif + +#endif + +struct privhdr { + const char* pattern; + int flags; +}; + +static void arg_rex_resetfn(struct arg_rex* parent) { + ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent)); + parent->count = 0; +} + +static int arg_rex_scanfn(struct arg_rex* parent, const char* argval) { + int errorcode = 0; + const TRexChar* error = NULL; + TRex* rex = NULL; + TRexBool is_match = TRex_False; + + if (parent->count == parent->hdr.maxcount) { + /* maximum number of arguments exceeded */ + errorcode = ARG_ERR_MAXCOUNT; + } else if (!argval) { + /* a valid argument with no argument value was given. */ + /* This happens when an optional argument value was invoked. */ + /* leave parent argument value unaltered but still count the argument. */ + parent->count++; + } else { + struct privhdr* priv = (struct privhdr*)parent->hdr.priv; + + /* test the current argument value for a match with the regular expression */ + /* if a match is detected, record the argument value in the arg_rex struct */ + + rex = trex_compile(priv->pattern, &error, priv->flags); + is_match = trex_match(rex, argval); + if (!is_match) + errorcode = ARG_ERR_REGNOMATCH; + else + parent->sval[parent->count++] = argval; + + trex_free(rex); + } + + ARG_TRACE(("%s:scanfn(%p) returns %d\n", __FILE__, parent, errorcode)); + return errorcode; +} + +static int arg_rex_checkfn(struct arg_rex* parent) { + int errorcode = (parent->count < parent->hdr.mincount) ? ARG_ERR_MINCOUNT : 0; +#if 0 + struct privhdr *priv = (struct privhdr*)parent->hdr.priv; + + /* free the regex "program" we constructed in resetfn */ + regfree(&(priv->regex)); + + /*printf("%s:checkfn(%p) returns %d\n",__FILE__,parent,errorcode);*/ +#endif + return errorcode; +} + +static void arg_rex_errorfn(struct arg_rex* parent, arg_dstr_t ds, int errorcode, const char* argval, const char* progname) { + const char* shortopts = parent->hdr.shortopts; + const char* longopts = parent->hdr.longopts; + const char* datatype = parent->hdr.datatype; + + /* make argval NULL safe */ + argval = argval ? argval : ""; + + arg_dstr_catf(ds, "%s: ", progname); + switch (errorcode) { + case ARG_ERR_MINCOUNT: + arg_dstr_cat(ds, "missing option "); + arg_print_option_ds(ds, shortopts, longopts, datatype, "\n"); + break; + + case ARG_ERR_MAXCOUNT: + arg_dstr_cat(ds, "excess option "); + arg_print_option_ds(ds, shortopts, longopts, argval, "\n"); + break; + + case ARG_ERR_REGNOMATCH: + arg_dstr_cat(ds, "illegal value "); + arg_print_option_ds(ds, shortopts, longopts, argval, "\n"); + break; + + default: { + #if 0 + char errbuff[256]; + regerror(errorcode, NULL, errbuff, sizeof(errbuff)); + printf("%s\n", errbuff); + #endif + } break; + } +} + +struct arg_rex* arg_rex0(const char* shortopts, const char* longopts, const char* pattern, const char* datatype, int flags, const char* glossary) { + return arg_rexn(shortopts, longopts, pattern, datatype, 0, 1, flags, glossary); +} + +struct arg_rex* arg_rex1(const char* shortopts, const char* longopts, const char* pattern, const char* datatype, int flags, const char* glossary) { + return arg_rexn(shortopts, longopts, pattern, datatype, 1, 1, flags, glossary); +} + +struct arg_rex* arg_rexn(const char* shortopts, + const char* longopts, + const char* pattern, + const char* datatype, + int mincount, + int maxcount, + int flags, + const char* glossary) { + size_t nbytes; + struct arg_rex* result; + struct privhdr* priv; + int i; + const TRexChar* error = NULL; + TRex* rex = NULL; + + if (!pattern) { + printf("argtable: ERROR - illegal regular expression pattern \"(NULL)\"\n"); + printf("argtable: Bad argument table.\n"); + return NULL; + } + + /* foolproof things by ensuring maxcount is not less than mincount */ + maxcount = (maxcount < mincount) ? mincount : maxcount; + + nbytes = sizeof(struct arg_rex) /* storage for struct arg_rex */ + + sizeof(struct privhdr) /* storage for private arg_rex data */ + + maxcount * sizeof(char*); /* storage for sval[maxcount] array */ + + /* init the arg_hdr struct */ + result = (struct arg_rex*)xmalloc(nbytes); + result->hdr.flag = ARG_HASVALUE; + result->hdr.shortopts = shortopts; + result->hdr.longopts = longopts; + result->hdr.datatype = datatype ? datatype : pattern; + result->hdr.glossary = glossary; + result->hdr.mincount = mincount; + result->hdr.maxcount = maxcount; + result->hdr.parent = result; + result->hdr.resetfn = (arg_resetfn*)arg_rex_resetfn; + result->hdr.scanfn = (arg_scanfn*)arg_rex_scanfn; + result->hdr.checkfn = (arg_checkfn*)arg_rex_checkfn; + result->hdr.errorfn = (arg_errorfn*)arg_rex_errorfn; + + /* store the arg_rex_priv struct immediately after the arg_rex struct */ + result->hdr.priv = result + 1; + priv = (struct privhdr*)(result->hdr.priv); + priv->pattern = pattern; + priv->flags = flags; + + /* store the sval[maxcount] array immediately after the arg_rex_priv struct */ + result->sval = (const char**)(priv + 1); + result->count = 0; + + /* foolproof the string pointers by initializing them to reference empty strings */ + for (i = 0; i < maxcount; i++) + result->sval[i] = ""; + + /* here we construct and destroy a regex representation of the regular + * expression for no other reason than to force any regex errors to be + * trapped now rather than later. If we don't, then errors may go undetected + * until an argument is actually parsed. + */ + + rex = trex_compile(priv->pattern, &error, priv->flags); + if (rex == NULL) { + ARG_LOG(("argtable: %s \"%s\"\n", error ? error : _TREXC("undefined"), priv->pattern)); + ARG_LOG(("argtable: Bad argument table.\n")); + } + + trex_free(rex); + + ARG_TRACE(("arg_rexn() returns %p\n", result)); + return result; +} + +/* see copyright notice in trex.h */ +#include +#include +#include +#include + +#ifdef _UINCODE +#define scisprint iswprint +#define scstrlen wcslen +#define scprintf wprintf +#define _SC(x) L(x) +#else +#define scisprint isprint +#define scstrlen strlen +#define scprintf printf +#define _SC(x) (x) +#endif + +#ifdef _DEBUG +#include + +static const TRexChar* g_nnames[] = {_SC("NONE"), _SC("OP_GREEDY"), _SC("OP_OR"), _SC("OP_EXPR"), _SC("OP_NOCAPEXPR"), + _SC("OP_DOT"), _SC("OP_CLASS"), _SC("OP_CCLASS"), _SC("OP_NCLASS"), _SC("OP_RANGE"), + _SC("OP_CHAR"), _SC("OP_EOL"), _SC("OP_BOL"), _SC("OP_WB")}; + +#endif +#define OP_GREEDY (MAX_CHAR + 1) /* * + ? {n} */ +#define OP_OR (MAX_CHAR + 2) +#define OP_EXPR (MAX_CHAR + 3) /* parentesis () */ +#define OP_NOCAPEXPR (MAX_CHAR + 4) /* parentesis (?:) */ +#define OP_DOT (MAX_CHAR + 5) +#define OP_CLASS (MAX_CHAR + 6) +#define OP_CCLASS (MAX_CHAR + 7) +#define OP_NCLASS (MAX_CHAR + 8) /* negates class the [^ */ +#define OP_RANGE (MAX_CHAR + 9) +#define OP_CHAR (MAX_CHAR + 10) +#define OP_EOL (MAX_CHAR + 11) +#define OP_BOL (MAX_CHAR + 12) +#define OP_WB (MAX_CHAR + 13) + +#define TREX_SYMBOL_ANY_CHAR ('.') +#define TREX_SYMBOL_GREEDY_ONE_OR_MORE ('+') +#define TREX_SYMBOL_GREEDY_ZERO_OR_MORE ('*') +#define TREX_SYMBOL_GREEDY_ZERO_OR_ONE ('?') +#define TREX_SYMBOL_BRANCH ('|') +#define TREX_SYMBOL_END_OF_STRING ('$') +#define TREX_SYMBOL_BEGINNING_OF_STRING ('^') +#define TREX_SYMBOL_ESCAPE_CHAR ('\\') + +typedef int TRexNodeType; + +typedef struct tagTRexNode { + TRexNodeType type; + int left; + int right; + int next; +} TRexNode; + +struct TRex { + const TRexChar* _eol; + const TRexChar* _bol; + const TRexChar* _p; + int _first; + int _op; + TRexNode* _nodes; + int _nallocated; + int _nsize; + int _nsubexpr; + TRexMatch* _matches; + int _currsubexp; + void* _jmpbuf; + const TRexChar** _error; + int _flags; +}; + +static int trex_list(TRex* exp); + +static int trex_newnode(TRex* exp, TRexNodeType type) { + TRexNode n; + int newid; + n.type = type; + n.next = n.right = n.left = -1; + if (type == OP_EXPR) + n.right = exp->_nsubexpr++; + if (exp->_nallocated < (exp->_nsize + 1)) { + exp->_nallocated *= 2; + exp->_nodes = (TRexNode*)xrealloc(exp->_nodes, exp->_nallocated * sizeof(TRexNode)); + } + exp->_nodes[exp->_nsize++] = n; + newid = exp->_nsize - 1; + return (int)newid; +} + +static void trex_error(TRex* exp, const TRexChar* error) { + if (exp->_error) + *exp->_error = error; + longjmp(*((jmp_buf*)exp->_jmpbuf), -1); +} + +static void trex_expect(TRex* exp, int n) { + if ((*exp->_p) != n) + trex_error(exp, _SC("expected paren")); + exp->_p++; +} + +static TRexChar trex_escapechar(TRex* exp) { + if (*exp->_p == TREX_SYMBOL_ESCAPE_CHAR) { + exp->_p++; + switch (*exp->_p) { + case 'v': + exp->_p++; + return '\v'; + case 'n': + exp->_p++; + return '\n'; + case 't': + exp->_p++; + return '\t'; + case 'r': + exp->_p++; + return '\r'; + case 'f': + exp->_p++; + return '\f'; + default: + return (*exp->_p++); + } + } else if (!scisprint(*exp->_p)) + trex_error(exp, _SC("letter expected")); + return (*exp->_p++); +} + +static int trex_charclass(TRex* exp, int classid) { + int n = trex_newnode(exp, OP_CCLASS); + exp->_nodes[n].left = classid; + return n; +} + +static int trex_charnode(TRex* exp, TRexBool isclass) { + TRexChar t; + if (*exp->_p == TREX_SYMBOL_ESCAPE_CHAR) { + exp->_p++; + switch (*exp->_p) { + case 'n': + exp->_p++; + return trex_newnode(exp, '\n'); + case 't': + exp->_p++; + return trex_newnode(exp, '\t'); + case 'r': + exp->_p++; + return trex_newnode(exp, '\r'); + case 'f': + exp->_p++; + return trex_newnode(exp, '\f'); + case 'v': + exp->_p++; + return trex_newnode(exp, '\v'); + case 'a': + case 'A': + case 'w': + case 'W': + case 's': + case 'S': + case 'd': + case 'D': + case 'x': + case 'X': + case 'c': + case 'C': + case 'p': + case 'P': + case 'l': + case 'u': { + t = *exp->_p; + exp->_p++; + return trex_charclass(exp, t); + } + case 'b': + case 'B': + if (!isclass) { + int node = trex_newnode(exp, OP_WB); + exp->_nodes[node].left = *exp->_p; + exp->_p++; + return node; + } + /* fall through */ + default: + t = *exp->_p; + exp->_p++; + return trex_newnode(exp, t); + } + } else if (!scisprint(*exp->_p)) { + trex_error(exp, _SC("letter expected")); + } + t = *exp->_p; + exp->_p++; + return trex_newnode(exp, t); +} +static int trex_class(TRex* exp) { + int ret = -1; + int first = -1, chain; + if (*exp->_p == TREX_SYMBOL_BEGINNING_OF_STRING) { + ret = trex_newnode(exp, OP_NCLASS); + exp->_p++; + } else + ret = trex_newnode(exp, OP_CLASS); + + if (*exp->_p == ']') + trex_error(exp, _SC("empty class")); + chain = ret; + while (*exp->_p != ']' && exp->_p != exp->_eol) { + if (*exp->_p == '-' && first != -1) { + int r, t; + if (*exp->_p++ == ']') + trex_error(exp, _SC("unfinished range")); + r = trex_newnode(exp, OP_RANGE); + if (first > *exp->_p) + trex_error(exp, _SC("invalid range")); + if (exp->_nodes[first].type == OP_CCLASS) + trex_error(exp, _SC("cannot use character classes in ranges")); + exp->_nodes[r].left = exp->_nodes[first].type; + t = trex_escapechar(exp); + exp->_nodes[r].right = t; + exp->_nodes[chain].next = r; + chain = r; + first = -1; + } else { + if (first != -1) { + int c = first; + exp->_nodes[chain].next = c; + chain = c; + first = trex_charnode(exp, TRex_True); + } else { + first = trex_charnode(exp, TRex_True); + } + } + } + if (first != -1) { + int c = first; + exp->_nodes[chain].next = c; + chain = c; + first = -1; + } + /* hack? */ + exp->_nodes[ret].left = exp->_nodes[ret].next; + exp->_nodes[ret].next = -1; + return ret; +} + +static int trex_parsenumber(TRex* exp) { + int ret = *exp->_p - '0'; + int positions = 10; + exp->_p++; + while (isdigit(*exp->_p)) { + ret = ret * 10 + (*exp->_p++ - '0'); + if (positions == 1000000000) + trex_error(exp, _SC("overflow in numeric constant")); + positions *= 10; + }; + return ret; +} + +static int trex_element(TRex* exp) { + int ret = -1; + switch (*exp->_p) { + case '(': { + int expr, newn; + exp->_p++; + + if (*exp->_p == '?') { + exp->_p++; + trex_expect(exp, ':'); + expr = trex_newnode(exp, OP_NOCAPEXPR); + } else + expr = trex_newnode(exp, OP_EXPR); + newn = trex_list(exp); + exp->_nodes[expr].left = newn; + ret = expr; + trex_expect(exp, ')'); + } break; + case '[': + exp->_p++; + ret = trex_class(exp); + trex_expect(exp, ']'); + break; + case TREX_SYMBOL_END_OF_STRING: + exp->_p++; + ret = trex_newnode(exp, OP_EOL); + break; + case TREX_SYMBOL_ANY_CHAR: + exp->_p++; + ret = trex_newnode(exp, OP_DOT); + break; + default: + ret = trex_charnode(exp, TRex_False); + break; + } + + { + TRexBool isgreedy = TRex_False; + unsigned short p0 = 0, p1 = 0; + switch (*exp->_p) { + case TREX_SYMBOL_GREEDY_ZERO_OR_MORE: + p0 = 0; + p1 = 0xFFFF; + exp->_p++; + isgreedy = TRex_True; + break; + case TREX_SYMBOL_GREEDY_ONE_OR_MORE: + p0 = 1; + p1 = 0xFFFF; + exp->_p++; + isgreedy = TRex_True; + break; + case TREX_SYMBOL_GREEDY_ZERO_OR_ONE: + p0 = 0; + p1 = 1; + exp->_p++; + isgreedy = TRex_True; + break; + case '{': + exp->_p++; + if (!isdigit(*exp->_p)) + trex_error(exp, _SC("number expected")); + p0 = (unsigned short)trex_parsenumber(exp); + /*******************************/ + switch (*exp->_p) { + case '}': + p1 = p0; + exp->_p++; + break; + case ',': + exp->_p++; + p1 = 0xFFFF; + if (isdigit(*exp->_p)) { + p1 = (unsigned short)trex_parsenumber(exp); + } + trex_expect(exp, '}'); + break; + default: + trex_error(exp, _SC(", or } expected")); + } + /*******************************/ + isgreedy = TRex_True; + break; + } + if (isgreedy) { + int nnode = trex_newnode(exp, OP_GREEDY); + exp->_nodes[nnode].left = ret; + exp->_nodes[nnode].right = ((p0) << 16) | p1; + ret = nnode; + } + } + if ((*exp->_p != TREX_SYMBOL_BRANCH) && (*exp->_p != ')') && (*exp->_p != TREX_SYMBOL_GREEDY_ZERO_OR_MORE) && + (*exp->_p != TREX_SYMBOL_GREEDY_ONE_OR_MORE) && (*exp->_p != '\0')) { + int nnode = trex_element(exp); + exp->_nodes[ret].next = nnode; + } + + return ret; +} + +static int trex_list(TRex* exp) { + int ret = -1, e; + if (*exp->_p == TREX_SYMBOL_BEGINNING_OF_STRING) { + exp->_p++; + ret = trex_newnode(exp, OP_BOL); + } + e = trex_element(exp); + if (ret != -1) { + exp->_nodes[ret].next = e; + } else + ret = e; + + if (*exp->_p == TREX_SYMBOL_BRANCH) { + int temp, tright; + exp->_p++; + temp = trex_newnode(exp, OP_OR); + exp->_nodes[temp].left = ret; + tright = trex_list(exp); + exp->_nodes[temp].right = tright; + ret = temp; + } + return ret; +} + +static TRexBool trex_matchcclass(int cclass, TRexChar c) { + switch (cclass) { + case 'a': + return isalpha(c) ? TRex_True : TRex_False; + case 'A': + return !isalpha(c) ? TRex_True : TRex_False; + case 'w': + return (isalnum(c) || c == '_') ? TRex_True : TRex_False; + case 'W': + return (!isalnum(c) && c != '_') ? TRex_True : TRex_False; + case 's': + return isspace(c) ? TRex_True : TRex_False; + case 'S': + return !isspace(c) ? TRex_True : TRex_False; + case 'd': + return isdigit(c) ? TRex_True : TRex_False; + case 'D': + return !isdigit(c) ? TRex_True : TRex_False; + case 'x': + return isxdigit(c) ? TRex_True : TRex_False; + case 'X': + return !isxdigit(c) ? TRex_True : TRex_False; + case 'c': + return iscntrl(c) ? TRex_True : TRex_False; + case 'C': + return !iscntrl(c) ? TRex_True : TRex_False; + case 'p': + return ispunct(c) ? TRex_True : TRex_False; + case 'P': + return !ispunct(c) ? TRex_True : TRex_False; + case 'l': + return islower(c) ? TRex_True : TRex_False; + case 'u': + return isupper(c) ? TRex_True : TRex_False; + } + return TRex_False; /*cannot happen*/ +} + +static TRexBool trex_matchclass(TRex* exp, TRexNode* node, TRexChar c) { + do { + switch (node->type) { + case OP_RANGE: + if (exp->_flags & TREX_ICASE) { + if (c >= toupper(node->left) && c <= toupper(node->right)) + return TRex_True; + if (c >= tolower(node->left) && c <= tolower(node->right)) + return TRex_True; + } else { + if (c >= node->left && c <= node->right) + return TRex_True; + } + break; + case OP_CCLASS: + if (trex_matchcclass(node->left, c)) + return TRex_True; + break; + default: + if (exp->_flags & TREX_ICASE) { + if (c == tolower(node->type) || c == toupper(node->type)) + return TRex_True; + } else { + if (c == node->type) + return TRex_True; + } + } + } while ((node->next != -1) && ((node = &exp->_nodes[node->next]) != NULL)); + return TRex_False; +} + +static const TRexChar* trex_matchnode(TRex* exp, TRexNode* node, const TRexChar* str, TRexNode* next) { + TRexNodeType type = node->type; + switch (type) { + case OP_GREEDY: { + /* TRexNode *greedystop = (node->next != -1) ? &exp->_nodes[node->next] : NULL; */ + TRexNode* greedystop = NULL; + int p0 = (node->right >> 16) & 0x0000FFFF, p1 = node->right & 0x0000FFFF, nmaches = 0; + const TRexChar *s = str, *good = str; + + if (node->next != -1) { + greedystop = &exp->_nodes[node->next]; + } else { + greedystop = next; + } + + while ((nmaches == 0xFFFF || nmaches < p1)) { + const TRexChar* stop; + if ((s = trex_matchnode(exp, &exp->_nodes[node->left], s, greedystop)) == NULL) + break; + nmaches++; + good = s; + if (greedystop) { + /* checks that 0 matches satisfy the expression(if so skips) */ + /* if not would always stop(for instance if is a '?') */ + if (greedystop->type != OP_GREEDY || (greedystop->type == OP_GREEDY && ((greedystop->right >> 16) & 0x0000FFFF) != 0)) { + TRexNode* gnext = NULL; + if (greedystop->next != -1) { + gnext = &exp->_nodes[greedystop->next]; + } else if (next && next->next != -1) { + gnext = &exp->_nodes[next->next]; + } + stop = trex_matchnode(exp, greedystop, s, gnext); + if (stop) { + /* if satisfied stop it */ + if (p0 == p1 && p0 == nmaches) + break; + else if (nmaches >= p0 && p1 == 0xFFFF) + break; + else if (nmaches >= p0 && nmaches <= p1) + break; + } + } + } + + if (s >= exp->_eol) + break; + } + if (p0 == p1 && p0 == nmaches) + return good; + else if (nmaches >= p0 && p1 == 0xFFFF) + return good; + else if (nmaches >= p0 && nmaches <= p1) + return good; + return NULL; + } + case OP_OR: { + const TRexChar* asd = str; + TRexNode* temp = &exp->_nodes[node->left]; + while ((asd = trex_matchnode(exp, temp, asd, NULL)) != NULL) { + if (temp->next != -1) + temp = &exp->_nodes[temp->next]; + else + return asd; + } + asd = str; + temp = &exp->_nodes[node->right]; + while ((asd = trex_matchnode(exp, temp, asd, NULL)) != NULL) { + if (temp->next != -1) + temp = &exp->_nodes[temp->next]; + else + return asd; + } + return NULL; + break; + } + case OP_EXPR: + case OP_NOCAPEXPR: { + TRexNode* n = &exp->_nodes[node->left]; + const TRexChar* cur = str; + int capture = -1; + if (node->type != OP_NOCAPEXPR && node->right == exp->_currsubexp) { + capture = exp->_currsubexp; + exp->_matches[capture].begin = cur; + exp->_currsubexp++; + } + + do { + TRexNode* subnext = NULL; + if (n->next != -1) { + subnext = &exp->_nodes[n->next]; + } else { + subnext = next; + } + if ((cur = trex_matchnode(exp, n, cur, subnext)) == NULL) { + if (capture != -1) { + exp->_matches[capture].begin = 0; + exp->_matches[capture].len = 0; + } + return NULL; + } + } while ((n->next != -1) && ((n = &exp->_nodes[n->next]) != NULL)); + + if (capture != -1) + exp->_matches[capture].len = (int)(cur - exp->_matches[capture].begin); + return cur; + } + case OP_WB: + if ((str == exp->_bol && !isspace(*str)) || (str == exp->_eol && !isspace(*(str - 1))) || (!isspace(*str) && isspace(*(str + 1))) || + (isspace(*str) && !isspace(*(str + 1)))) { + return (node->left == 'b') ? str : NULL; + } + return (node->left == 'b') ? NULL : str; + case OP_BOL: + if (str == exp->_bol) + return str; + return NULL; + case OP_EOL: + if (str == exp->_eol) + return str; + return NULL; + case OP_DOT: { + str++; + } + return str; + case OP_NCLASS: + case OP_CLASS: + if (trex_matchclass(exp, &exp->_nodes[node->left], *str) ? (type == OP_CLASS ? TRex_True : TRex_False) + : (type == OP_NCLASS ? TRex_True : TRex_False)) { + str++; + return str; + } + return NULL; + case OP_CCLASS: + if (trex_matchcclass(node->left, *str)) { + str++; + return str; + } + return NULL; + default: /* char */ + if (exp->_flags & TREX_ICASE) { + if (*str != tolower(node->type) && *str != toupper(node->type)) + return NULL; + } else { + if (*str != node->type) + return NULL; + } + str++; + return str; + } +} + +/* public api */ +TRex* trex_compile(const TRexChar* pattern, const TRexChar** error, int flags) { + TRex* exp = (TRex*)xmalloc(sizeof(TRex)); + exp->_eol = exp->_bol = NULL; + exp->_p = pattern; + exp->_nallocated = (int)scstrlen(pattern) * sizeof(TRexChar); + exp->_nodes = (TRexNode*)xmalloc(exp->_nallocated * sizeof(TRexNode)); + exp->_nsize = 0; + exp->_matches = 0; + exp->_nsubexpr = 0; + exp->_first = trex_newnode(exp, OP_EXPR); + exp->_error = error; + exp->_jmpbuf = xmalloc(sizeof(jmp_buf)); + exp->_flags = flags; + if (setjmp(*((jmp_buf*)exp->_jmpbuf)) == 0) { + int res = trex_list(exp); + exp->_nodes[exp->_first].left = res; + if (*exp->_p != '\0') + trex_error(exp, _SC("unexpected character")); +#ifdef _DEBUG + { + int nsize, i; + TRexNode* t; + nsize = exp->_nsize; + t = &exp->_nodes[0]; + scprintf(_SC("\n")); + for (i = 0; i < nsize; i++) { + if (exp->_nodes[i].type > MAX_CHAR) + scprintf(_SC("[%02d] %10s "), i, g_nnames[exp->_nodes[i].type - MAX_CHAR]); + else + scprintf(_SC("[%02d] %10c "), i, exp->_nodes[i].type); + scprintf(_SC("left %02d right %02d next %02d\n"), exp->_nodes[i].left, exp->_nodes[i].right, exp->_nodes[i].next); + } + scprintf(_SC("\n")); + } +#endif + exp->_matches = (TRexMatch*)xmalloc(exp->_nsubexpr * sizeof(TRexMatch)); + memset(exp->_matches, 0, exp->_nsubexpr * sizeof(TRexMatch)); + } else { + trex_free(exp); + return NULL; + } + return exp; +} + +void trex_free(TRex* exp) { + if (exp) { + xfree(exp->_nodes); + xfree(exp->_jmpbuf); + xfree(exp->_matches); + xfree(exp); + } +} + +TRexBool trex_match(TRex* exp, const TRexChar* text) { + const TRexChar* res = NULL; + exp->_bol = text; + exp->_eol = text + scstrlen(text); + exp->_currsubexp = 0; + res = trex_matchnode(exp, exp->_nodes, text, NULL); + if (res == NULL || res != exp->_eol) + return TRex_False; + return TRex_True; +} + +TRexBool trex_searchrange(TRex* exp, const TRexChar* text_begin, const TRexChar* text_end, const TRexChar** out_begin, const TRexChar** out_end) { + const TRexChar* cur = NULL; + int node = exp->_first; + if (text_begin >= text_end) + return TRex_False; + exp->_bol = text_begin; + exp->_eol = text_end; + do { + cur = text_begin; + while (node != -1) { + exp->_currsubexp = 0; + cur = trex_matchnode(exp, &exp->_nodes[node], cur, NULL); + if (!cur) + break; + node = exp->_nodes[node].next; + } + text_begin++; + } while (cur == NULL && text_begin != text_end); + + if (cur == NULL) + return TRex_False; + + --text_begin; + + if (out_begin) + *out_begin = text_begin; + if (out_end) + *out_end = cur; + return TRex_True; +} + +TRexBool trex_search(TRex* exp, const TRexChar* text, const TRexChar** out_begin, const TRexChar** out_end) { + return trex_searchrange(exp, text, text + scstrlen(text), out_begin, out_end); +} + +int trex_getsubexpcount(TRex* exp) { + return exp->_nsubexpr; +} + +TRexBool trex_getsubexp(TRex* exp, int n, TRexMatch* subexp) { + if (n < 0 || n >= exp->_nsubexpr) + return TRex_False; + *subexp = exp->_matches[n]; + return TRex_True; +} +/******************************************************************************* + * arg_str: Implements the str command-line option + * + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +#ifndef ARG_AMALGAMATION +#include "argtable3_private.h" +#endif + +#include + +static void arg_str_resetfn(struct arg_str* parent) { + int i; + + ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent)); + for (i = 0; i < parent->count; i++) { + parent->sval[i] = ""; + } + parent->count = 0; +} + +static int arg_str_scanfn(struct arg_str* parent, const char* argval) { + int errorcode = 0; + + if (parent->count == parent->hdr.maxcount) { + /* maximum number of arguments exceeded */ + errorcode = ARG_ERR_MAXCOUNT; + } else if (!argval) { + /* a valid argument with no argument value was given. */ + /* This happens when an optional argument value was invoked. */ + /* leave parent argument value unaltered but still count the argument. */ + parent->count++; + } else { + parent->sval[parent->count++] = argval; + } + + ARG_TRACE(("%s:scanfn(%p) returns %d\n", __FILE__, parent, errorcode)); + return errorcode; +} + +static int arg_str_checkfn(struct arg_str* parent) { + int errorcode = (parent->count < parent->hdr.mincount) ? ARG_ERR_MINCOUNT : 0; + + ARG_TRACE(("%s:checkfn(%p) returns %d\n", __FILE__, parent, errorcode)); + return errorcode; +} + +static void arg_str_errorfn(struct arg_str* parent, arg_dstr_t ds, int errorcode, const char* argval, const char* progname) { + const char* shortopts = parent->hdr.shortopts; + const char* longopts = parent->hdr.longopts; + const char* datatype = parent->hdr.datatype; + + /* make argval NULL safe */ + argval = argval ? argval : ""; + + arg_dstr_catf(ds, "%s: ", progname); + switch (errorcode) { + case ARG_ERR_MINCOUNT: + arg_dstr_cat(ds, "missing option "); + arg_print_option_ds(ds, shortopts, longopts, datatype, "\n"); + break; + + case ARG_ERR_MAXCOUNT: + arg_dstr_cat(ds, "excess option "); + arg_print_option_ds(ds, shortopts, longopts, argval, "\n"); + break; + } +} + +struct arg_str* arg_str0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary) { + return arg_strn(shortopts, longopts, datatype, 0, 1, glossary); +} + +struct arg_str* arg_str1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary) { + return arg_strn(shortopts, longopts, datatype, 1, 1, glossary); +} + +struct arg_str* arg_strn(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary) { + size_t nbytes; + struct arg_str* result; + int i; + + /* should not allow this stupid error */ + /* we should return an error code warning this logic error */ + /* foolproof things by ensuring maxcount is not less than mincount */ + maxcount = (maxcount < mincount) ? mincount : maxcount; + + nbytes = sizeof(struct arg_str) /* storage for struct arg_str */ + + maxcount * sizeof(char*); /* storage for sval[maxcount] array */ + + result = (struct arg_str*)xmalloc(nbytes); + + /* init the arg_hdr struct */ + result->hdr.flag = ARG_HASVALUE; + result->hdr.shortopts = shortopts; + result->hdr.longopts = longopts; + result->hdr.datatype = datatype ? datatype : ""; + result->hdr.glossary = glossary; + result->hdr.mincount = mincount; + result->hdr.maxcount = maxcount; + result->hdr.parent = result; + result->hdr.resetfn = (arg_resetfn*)arg_str_resetfn; + result->hdr.scanfn = (arg_scanfn*)arg_str_scanfn; + result->hdr.checkfn = (arg_checkfn*)arg_str_checkfn; + result->hdr.errorfn = (arg_errorfn*)arg_str_errorfn; + + /* store the sval[maxcount] array immediately after the arg_str struct */ + result->sval = (const char**)(result + 1); + result->count = 0; + + /* foolproof the string pointers by initializing them to reference empty strings */ + for (i = 0; i < maxcount; i++) + result->sval[i] = ""; + + ARG_TRACE(("arg_strn() returns %p\n", result)); + return result; +} +/******************************************************************************* + * arg_cmd: Provides the sub-command mechanism + * + * This file is part of the argtable3 library. + * + * Copyright (C) 2013-2019 Tom G. Huang + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +#ifndef ARG_AMALGAMATION +#include "argtable3_private.h" +#endif + +#include +#include +#include + +#define MAX_MODULE_VERSION_SIZE 128 + +static arg_hashtable_t* s_hashtable = NULL; +static char* s_module_name = NULL; +static int s_mod_ver_major = 0; +static int s_mod_ver_minor = 0; +static int s_mod_ver_patch = 0; +static char* s_mod_ver_tag = NULL; +static char* s_mod_ver = NULL; + +void arg_set_module_name(const char* name) { + size_t slen; + + xfree(s_module_name); + slen = strlen(name); + s_module_name = (char*)xmalloc(slen + 1); + memset(s_module_name, 0, slen + 1); + +#if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) + strncpy_s(s_module_name, slen + 1, name, slen); +#else + memcpy(s_module_name, name, slen); +#endif +} + +void arg_set_module_version(int major, int minor, int patch, const char* tag) { + size_t slen_tag, slen_ds; + arg_dstr_t ds; + + s_mod_ver_major = major; + s_mod_ver_minor = minor; + s_mod_ver_patch = patch; + + xfree(s_mod_ver_tag); + slen_tag = strlen(tag); + s_mod_ver_tag = (char*)xmalloc(slen_tag + 1); + memset(s_mod_ver_tag, 0, slen_tag + 1); + +#if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) + strncpy_s(s_mod_ver_tag, slen_tag + 1, tag, slen_tag); +#else + memcpy(s_mod_ver_tag, tag, slen_tag); +#endif + + ds = arg_dstr_create(); + arg_dstr_catf(ds, "%d.", s_mod_ver_major); + arg_dstr_catf(ds, "%d.", s_mod_ver_minor); + arg_dstr_catf(ds, "%d.", s_mod_ver_patch); + arg_dstr_cat(ds, s_mod_ver_tag); + + xfree(s_mod_ver); + slen_ds = strlen(arg_dstr_cstr(ds)); + s_mod_ver = (char*)xmalloc(slen_ds + 1); + memset(s_mod_ver, 0, slen_ds + 1); + +#if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) + strncpy_s(s_mod_ver, slen_ds + 1, arg_dstr_cstr(ds), slen_ds); +#else + memcpy(s_mod_ver, arg_dstr_cstr(ds), slen_ds); +#endif + + arg_dstr_destroy(ds); +} + +static unsigned int hash_key(const void* key) { + const char* str = (const char*)key; + int c; + unsigned int hash = 5381; + + while ((c = *str++) != 0) + hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + + return hash; +} + +static int equal_keys(const void* key1, const void* key2) { + char* k1 = (char*)key1; + char* k2 = (char*)key2; + return (0 == strcmp(k1, k2)); +} + +void arg_cmd_init(void) { + s_hashtable = arg_hashtable_create(32, hash_key, equal_keys); +} + +void arg_cmd_uninit(void) { + arg_hashtable_destroy(s_hashtable, 1); +} + +void arg_cmd_register(const char* name, arg_cmdfn* proc, const char* description) { + arg_cmd_info_t* cmd_info; + size_t slen_name; + void* k; + + assert(strlen(name) < ARG_CMD_NAME_LEN); + assert(strlen(description) < ARG_CMD_DESCRIPTION_LEN); + + /* Check if the command already exists. */ + /* If the command exists, replace the existing command. */ + /* If the command doesn't exist, insert the command. */ + cmd_info = (arg_cmd_info_t*)arg_hashtable_search(s_hashtable, name); + if (cmd_info) { + arg_hashtable_remove(s_hashtable, name); + cmd_info = NULL; + } + + cmd_info = (arg_cmd_info_t*)xmalloc(sizeof(arg_cmd_info_t)); + memset(cmd_info, 0, sizeof(arg_cmd_info_t)); + +#if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) + strncpy_s(cmd_info->name, ARG_CMD_NAME_LEN, name, strlen(name)); + strncpy_s(cmd_info->description, ARG_CMD_DESCRIPTION_LEN, description, strlen(description)); +#else + memcpy(cmd_info->name, name, strlen(name)); + memcpy(cmd_info->description, description, strlen(description)); +#endif + + cmd_info->proc = proc; + + slen_name = strlen(name); + k = xmalloc(slen_name + 1); + memset(k, 0, slen_name + 1); + +#if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) + strncpy_s((char*)k, slen_name + 1, name, slen_name); +#else + memcpy((char*)k, name, slen_name); +#endif + + arg_hashtable_insert(s_hashtable, k, cmd_info); +} + +void arg_cmd_unregister(const char* name) { + arg_hashtable_remove(s_hashtable, name); +} + +int arg_cmd_dispatch(const char* name, int argc, char* argv[], arg_dstr_t res) { + arg_cmd_info_t* cmd_info = arg_cmd_info(name); + + assert(cmd_info != NULL); + assert(cmd_info->proc != NULL); + + return cmd_info->proc(argc, argv, res); +} + +arg_cmd_info_t* arg_cmd_info(const char* name) { + return (arg_cmd_info_t*)arg_hashtable_search(s_hashtable, name); +} + +unsigned int arg_cmd_count(void) { + return arg_hashtable_count(s_hashtable); +} + +arg_cmd_itr_t arg_cmd_itr_create(void) { + return (arg_cmd_itr_t)arg_hashtable_itr_create(s_hashtable); +} + +int arg_cmd_itr_advance(arg_cmd_itr_t itr) { + return arg_hashtable_itr_advance((arg_hashtable_itr_t*)itr); +} + +char* arg_cmd_itr_key(arg_cmd_itr_t itr) { + return (char*)arg_hashtable_itr_key((arg_hashtable_itr_t*)itr); +} + +arg_cmd_info_t* arg_cmd_itr_value(arg_cmd_itr_t itr) { + return (arg_cmd_info_t*)arg_hashtable_itr_value((arg_hashtable_itr_t*)itr); +} + +void arg_cmd_itr_destroy(arg_cmd_itr_t itr) { + arg_hashtable_itr_destroy((arg_hashtable_itr_t*)itr); +} + +int arg_cmd_itr_search(arg_cmd_itr_t itr, void* k) { + return arg_hashtable_itr_search((arg_hashtable_itr_t*)itr, s_hashtable, k); +} + +static const char* module_name(void) { + if (s_module_name == NULL || strlen(s_module_name) == 0) + return ""; + + return s_module_name; +} + +static const char* module_version(void) { + if (s_mod_ver == NULL || strlen(s_mod_ver) == 0) + return "0.0.0.0"; + + return s_mod_ver; +} + +void arg_make_get_help_msg(arg_dstr_t res) { + arg_dstr_catf(res, "%s v%s\n", module_name(), module_version()); + arg_dstr_catf(res, "Please type '%s help' to get more information.\n", module_name()); +} + +void arg_make_help_msg(arg_dstr_t ds, char* cmd_name, void** argtable) { + arg_cmd_info_t* cmd_info = (arg_cmd_info_t*)arg_hashtable_search(s_hashtable, cmd_name); + if (cmd_info) { + arg_dstr_catf(ds, "%s: %s\n", cmd_name, cmd_info->description); + } + + arg_dstr_cat(ds, "Usage:\n"); + arg_dstr_catf(ds, " %s", module_name()); + + arg_print_syntaxv_ds(ds, argtable, "\n \nAvailable options:\n"); + arg_print_glossary_ds(ds, argtable, " %-23s %s\n"); + + arg_dstr_cat(ds, "\n"); +} + +void arg_make_syntax_err_msg(arg_dstr_t ds, void** argtable, struct arg_end* end) { + arg_print_errors_ds(ds, end, module_name()); + arg_dstr_cat(ds, "Usage: \n"); + arg_dstr_catf(ds, " %s", module_name()); + arg_print_syntaxv_ds(ds, argtable, "\n"); + arg_dstr_cat(ds, "\n"); +} + +int arg_make_syntax_err_help_msg(arg_dstr_t ds, char* name, int help, int nerrors, void** argtable, struct arg_end* end, int* exitcode) { + /* help handling + * note: '-h|--help' takes precedence over error reporting + */ + if (help > 0) { + arg_make_help_msg(ds, name, argtable); + *exitcode = EXIT_SUCCESS; + return 1; + } + + /* syntax error handling */ + if (nerrors > 0) { + arg_make_syntax_err_msg(ds, argtable, end); + *exitcode = EXIT_FAILURE; + return 1; + } + + return 0; +} +/******************************************************************************* + * argtable3: Implements the main interfaces of the library + * + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +#ifndef ARG_AMALGAMATION +#include "argtable3_private.h" +#include "getopt.h" +#endif + +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#undef WIN32_LEAN_AND_MEAN +#endif + +#include +#include +#include +#include +#include + +static void arg_register_error(struct arg_end* end, void* parent, int error, const char* argval) { + /* printf("arg_register_error(%p,%p,%d,%s)\n",end,parent,error,argval); */ + if (end->count < end->hdr.maxcount) { + end->error[end->count] = error; + end->parent[end->count] = parent; + end->argval[end->count] = argval; + end->count++; + } else { + end->error[end->hdr.maxcount - 1] = ARG_ELIMIT; + end->parent[end->hdr.maxcount - 1] = end; + end->argval[end->hdr.maxcount - 1] = NULL; + } +} + +/* + * Return index of first table entry with a matching short option + * or -1 if no match was found. + */ +static int find_shortoption(struct arg_hdr** table, char shortopt) { + int tabindex; + for (tabindex = 0; !(table[tabindex]->flag & ARG_TERMINATOR); tabindex++) { + if (table[tabindex]->shortopts && strchr(table[tabindex]->shortopts, shortopt)) + return tabindex; + } + return -1; +} + +struct longoptions { + int getoptval; + int noptions; + struct option* options; +}; + +#if 0 +static +void dump_longoptions(struct longoptions * longoptions) +{ + int i; + printf("getoptval = %d\n", longoptions->getoptval); + printf("noptions = %d\n", longoptions->noptions); + for (i = 0; i < longoptions->noptions; i++) + { + printf("options[%d].name = \"%s\"\n", + i, + longoptions->options[i].name); + printf("options[%d].has_arg = %d\n", i, longoptions->options[i].has_arg); + printf("options[%d].flag = %p\n", i, longoptions->options[i].flag); + printf("options[%d].val = %d\n", i, longoptions->options[i].val); + } +} +#endif + +static struct longoptions* alloc_longoptions(struct arg_hdr** table) { + struct longoptions* result; + size_t nbytes; + int noptions = 1; + size_t longoptlen = 0; + int tabindex; + int option_index = 0; + char* store; + + /* + * Determine the total number of option structs required + * by counting the number of comma separated long options + * in all table entries and return the count in noptions. + * note: noptions starts at 1 not 0 because we getoptlong + * requires a NULL option entry to terminate the option array. + * While we are at it, count the number of chars required + * to store private copies of all the longoption strings + * and return that count in logoptlen. + */ + tabindex = 0; + do { + const char* longopts = table[tabindex]->longopts; + longoptlen += (longopts ? strlen(longopts) : 0) + 1; + while (longopts) { + noptions++; + longopts = strchr(longopts + 1, ','); + } + } while (!(table[tabindex++]->flag & ARG_TERMINATOR)); + /*printf("%d long options consuming %d chars in total\n",noptions,longoptlen);*/ + + /* allocate storage for return data structure as: */ + /* (struct longoptions) + (struct options)[noptions] + char[longoptlen] */ + nbytes = sizeof(struct longoptions) + sizeof(struct option) * noptions + longoptlen; + result = (struct longoptions*)xmalloc(nbytes); + + result->getoptval = 0; + result->noptions = noptions; + result->options = (struct option*)(result + 1); + store = (char*)(result->options + noptions); + + for (tabindex = 0; !(table[tabindex]->flag & ARG_TERMINATOR); tabindex++) { + const char* longopts = table[tabindex]->longopts; + + while (longopts && *longopts) { + char* storestart = store; + + /* copy progressive longopt strings into the store */ + while (*longopts != 0 && *longopts != ',') + *store++ = *longopts++; + *store++ = 0; + if (*longopts == ',') + longopts++; + /*fprintf(stderr,"storestart=\"%s\"\n",storestart);*/ + + result->options[option_index].name = storestart; + result->options[option_index].flag = &(result->getoptval); + result->options[option_index].val = tabindex; + if (table[tabindex]->flag & ARG_HASOPTVALUE) + result->options[option_index].has_arg = 2; + else if (table[tabindex]->flag & ARG_HASVALUE) + result->options[option_index].has_arg = 1; + else + result->options[option_index].has_arg = 0; + + option_index++; + } + } + /* terminate the options array with a zero-filled entry */ + result->options[option_index].name = 0; + result->options[option_index].has_arg = 0; + result->options[option_index].flag = 0; + result->options[option_index].val = 0; + + /*dump_longoptions(result);*/ + return result; +} + +static char* alloc_shortoptions(struct arg_hdr** table) { + char* result; + size_t len = 2; + int tabindex; + char* res; + + /* determine the total number of option chars required */ + for (tabindex = 0; !(table[tabindex]->flag & ARG_TERMINATOR); tabindex++) { + struct arg_hdr* hdr = table[tabindex]; + len += 3 * (hdr->shortopts ? strlen(hdr->shortopts) : 0); + } + + result = xmalloc(len); + + res = result; + + /* add a leading ':' so getopt return codes distinguish */ + /* unrecognised option and options missing argument values */ + *res++ = ':'; + + for (tabindex = 0; !(table[tabindex]->flag & ARG_TERMINATOR); tabindex++) { + struct arg_hdr* hdr = table[tabindex]; + const char* shortopts = hdr->shortopts; + while (shortopts && *shortopts) { + *res++ = *shortopts++; + if (hdr->flag & ARG_HASVALUE) + *res++ = ':'; + if (hdr->flag & ARG_HASOPTVALUE) + *res++ = ':'; + } + } + /* null terminate the string */ + *res = 0; + + /*printf("alloc_shortoptions() returns \"%s\"\n",(result?result:"NULL"));*/ + return result; +} + +/* return index of the table terminator entry */ +static int arg_endindex(struct arg_hdr** table) { + int tabindex = 0; + while (!(table[tabindex]->flag & ARG_TERMINATOR)) + tabindex++; + return tabindex; +} + +static void arg_parse_tagged(int argc, char** argv, struct arg_hdr** table, struct arg_end* endtable) { + struct longoptions* longoptions; + char* shortoptions; + int copt; + + /*printf("arg_parse_tagged(%d,%p,%p,%p)\n",argc,argv,table,endtable);*/ + + /* allocate short and long option arrays for the given opttable[]. */ + /* if the allocs fail then put an error msg in the last table entry. */ + longoptions = alloc_longoptions(table); + shortoptions = alloc_shortoptions(table); + + /*dump_longoptions(longoptions);*/ + + /* reset getopts internal option-index to zero, and disable error reporting */ + optind = 0; + opterr = 0; + + /* fetch and process args using getopt_long */ + while ((copt = getopt_long(argc, argv, shortoptions, longoptions->options, NULL)) != -1) { + /* + printf("optarg='%s'\n",optarg); + printf("optind=%d\n",optind); + printf("copt=%c\n",(char)copt); + printf("optopt=%c (%d)\n",optopt, (int)(optopt)); + */ + switch (copt) { + case 0: { + int tabindex = longoptions->getoptval; + void* parent = table[tabindex]->parent; + /*printf("long option detected from argtable[%d]\n", tabindex);*/ + if (optarg && optarg[0] == 0 && (table[tabindex]->flag & ARG_HASVALUE)) { + /* printf(": long option %s requires an argument\n",argv[optind-1]); */ + arg_register_error(endtable, endtable, ARG_EMISSARG, argv[optind - 1]); + /* continue to scan the (empty) argument value to enforce argument count checking */ + } + if (table[tabindex]->scanfn) { + int errorcode = table[tabindex]->scanfn(parent, optarg); + if (errorcode != 0) + arg_register_error(endtable, parent, errorcode, optarg); + } + } break; + + case '?': + /* + * getopt_long() found an unrecognised short option. + * if it was a short option its value is in optopt + * if it was a long option then optopt=0 + */ + switch (optopt) { + case 0: + /*printf("?0 unrecognised long option %s\n",argv[optind-1]);*/ + arg_register_error(endtable, endtable, ARG_ELONGOPT, argv[optind - 1]); + break; + default: + /*printf("?* unrecognised short option '%c'\n",optopt);*/ + arg_register_error(endtable, endtable, optopt, NULL); + break; + } + break; + + case ':': + /* + * getopt_long() found an option with its argument missing. + */ + /*printf(": option %s requires an argument\n",argv[optind-1]); */ + arg_register_error(endtable, endtable, ARG_EMISSARG, argv[optind - 1]); + break; + + default: { + /* getopt_long() found a valid short option */ + int tabindex = find_shortoption(table, (char)copt); + /*printf("short option detected from argtable[%d]\n", tabindex);*/ + if (tabindex == -1) { + /* should never get here - but handle it just in case */ + /*printf("unrecognised short option %d\n",copt);*/ + arg_register_error(endtable, endtable, copt, NULL); + } else { + if (table[tabindex]->scanfn) { + void* parent = table[tabindex]->parent; + int errorcode = table[tabindex]->scanfn(parent, optarg); + if (errorcode != 0) + arg_register_error(endtable, parent, errorcode, optarg); + } + } + break; + } + } + } + + xfree(shortoptions); + xfree(longoptions); +} + +static void arg_parse_untagged(int argc, char** argv, struct arg_hdr** table, struct arg_end* endtable) { + int tabindex = 0; + int errorlast = 0; + const char* optarglast = NULL; + void* parentlast = NULL; + + /*printf("arg_parse_untagged(%d,%p,%p,%p)\n",argc,argv,table,endtable);*/ + while (!(table[tabindex]->flag & ARG_TERMINATOR)) { + void* parent; + int errorcode; + + /* if we have exhausted our argv[optind] entries then we have finished */ + if (optind >= argc) { + /*printf("arg_parse_untagged(): argv[] exhausted\n");*/ + return; + } + + /* skip table entries with non-null long or short options (they are not untagged entries) */ + if (table[tabindex]->longopts || table[tabindex]->shortopts) { + /*printf("arg_parse_untagged(): skipping argtable[%d] (tagged argument)\n",tabindex);*/ + tabindex++; + continue; + } + + /* skip table entries with NULL scanfn */ + if (!(table[tabindex]->scanfn)) { + /*printf("arg_parse_untagged(): skipping argtable[%d] (NULL scanfn)\n",tabindex);*/ + tabindex++; + continue; + } + + /* attempt to scan the current argv[optind] with the current */ + /* table[tabindex] entry. If it succeeds then keep it, otherwise */ + /* try again with the next table[] entry. */ + parent = table[tabindex]->parent; + errorcode = table[tabindex]->scanfn(parent, argv[optind]); + if (errorcode == 0) { + /* success, move onto next argv[optind] but stay with same table[tabindex] */ + /*printf("arg_parse_untagged(): argtable[%d] successfully matched\n",tabindex);*/ + optind++; + + /* clear the last tentative error */ + errorlast = 0; + } else { + /* failure, try same argv[optind] with next table[tabindex] entry */ + /*printf("arg_parse_untagged(): argtable[%d] failed match\n",tabindex);*/ + tabindex++; + + /* remember this as a tentative error we may wish to reinstate later */ + errorlast = errorcode; + optarglast = argv[optind]; + parentlast = parent; + } + } + + /* if a tenative error still remains at this point then register it as a proper error */ + if (errorlast) { + arg_register_error(endtable, parentlast, errorlast, optarglast); + optind++; + } + + /* only get here when not all argv[] entries were consumed */ + /* register an error for each unused argv[] entry */ + while (optind < argc) { + /*printf("arg_parse_untagged(): argv[%d]=\"%s\" not consumed\n",optind,argv[optind]);*/ + arg_register_error(endtable, endtable, ARG_ENOMATCH, argv[optind++]); + } + + return; +} + +static void arg_parse_check(struct arg_hdr** table, struct arg_end* endtable) { + int tabindex = 0; + /* printf("arg_parse_check()\n"); */ + do { + if (table[tabindex]->checkfn) { + void* parent = table[tabindex]->parent; + int errorcode = table[tabindex]->checkfn(parent); + if (errorcode != 0) + arg_register_error(endtable, parent, errorcode, NULL); + } + } while (!(table[tabindex++]->flag & ARG_TERMINATOR)); +} + +static void arg_reset(void** argtable) { + struct arg_hdr** table = (struct arg_hdr**)argtable; + int tabindex = 0; + /*printf("arg_reset(%p)\n",argtable);*/ + do { + if (table[tabindex]->resetfn) + table[tabindex]->resetfn(table[tabindex]->parent); + } while (!(table[tabindex++]->flag & ARG_TERMINATOR)); +} + +int arg_parse(int argc, char** argv, void** argtable) { + struct arg_hdr** table = (struct arg_hdr**)argtable; + struct arg_end* endtable; + int endindex; + char** argvcopy = NULL; + int i; + + /*printf("arg_parse(%d,%p,%p)\n",argc,argv,argtable);*/ + + /* reset any argtable data from previous invocations */ + arg_reset(argtable); + + /* locate the first end-of-table marker within the array */ + endindex = arg_endindex(table); + endtable = (struct arg_end*)table[endindex]; + + /* Special case of argc==0. This can occur on Texas Instruments DSP. */ + /* Failure to trap this case results in an unwanted NULL result from */ + /* the malloc for argvcopy (next code block). */ + if (argc == 0) { + /* We must still perform post-parse checks despite the absence of command line arguments */ + arg_parse_check(table, endtable); + + /* Now we are finished */ + return endtable->count; + } + + argvcopy = (char**)xmalloc(sizeof(char*) * (argc + 1)); + + /* + Fill in the local copy of argv[]. We need a local copy + because getopt rearranges argv[] which adversely affects + susbsequent parsing attempts. + */ + for (i = 0; i < argc; i++) + argvcopy[i] = argv[i]; + + argvcopy[argc] = NULL; + + /* parse the command line (local copy) for tagged options */ + arg_parse_tagged(argc, argvcopy, table, endtable); + + /* parse the command line (local copy) for untagged options */ + arg_parse_untagged(argc, argvcopy, table, endtable); + + /* if no errors so far then perform post-parse checks otherwise dont bother */ + if (endtable->count == 0) + arg_parse_check(table, endtable); + + /* release the local copt of argv[] */ + xfree(argvcopy); + + return endtable->count; +} + +/* + * Concatenate contents of src[] string onto *pdest[] string. + * The *pdest pointer is altered to point to the end of the + * target string and *pndest is decremented by the same number + * of chars. + * Does not append more than *pndest chars into *pdest[] + * so as to prevent buffer overruns. + * Its something like strncat() but more efficient for repeated + * calls on the same destination string. + * Example of use: + * char dest[30] = "good" + * size_t ndest = sizeof(dest); + * char *pdest = dest; + * arg_char(&pdest,"bye ",&ndest); + * arg_char(&pdest,"cruel ",&ndest); + * arg_char(&pdest,"world!",&ndest); + * Results in: + * dest[] == "goodbye cruel world!" + * ndest == 10 + */ +static void arg_cat(char** pdest, const char* src, size_t* pndest) { + char* dest = *pdest; + char* end = dest + *pndest; + + /*locate null terminator of dest string */ + while (dest < end && *dest != 0) + dest++; + + /* concat src string to dest string */ + while (dest < end && *src != 0) + *dest++ = *src++; + + /* null terminate dest string */ + *dest = 0; + + /* update *pdest and *pndest */ + *pndest = end - dest; + *pdest = dest; +} + +static void arg_cat_option(char* dest, size_t ndest, const char* shortopts, const char* longopts, const char* datatype, int optvalue) { + if (shortopts) { + char option[3]; + + /* note: option array[] is initialiazed dynamically here to satisfy */ + /* a deficiency in the watcom compiler wrt static array initializers. */ + option[0] = '-'; + option[1] = shortopts[0]; + option[2] = 0; + + arg_cat(&dest, option, &ndest); + if (datatype) { + arg_cat(&dest, " ", &ndest); + if (optvalue) { + arg_cat(&dest, "[", &ndest); + arg_cat(&dest, datatype, &ndest); + arg_cat(&dest, "]", &ndest); + } else + arg_cat(&dest, datatype, &ndest); + } + } else if (longopts) { + size_t ncspn; + + /* add "--" tag prefix */ + arg_cat(&dest, "--", &ndest); + + /* add comma separated option tag */ + ncspn = strcspn(longopts, ","); +#if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) + strncat_s(dest, ndest, longopts, (ncspn < ndest) ? ncspn : ndest); +#else + strncat(dest, longopts, (ncspn < ndest) ? ncspn : ndest); +#endif + + if (datatype) { + arg_cat(&dest, "=", &ndest); + if (optvalue) { + arg_cat(&dest, "[", &ndest); + arg_cat(&dest, datatype, &ndest); + arg_cat(&dest, "]", &ndest); + } else + arg_cat(&dest, datatype, &ndest); + } + } else if (datatype) { + if (optvalue) { + arg_cat(&dest, "[", &ndest); + arg_cat(&dest, datatype, &ndest); + arg_cat(&dest, "]", &ndest); + } else + arg_cat(&dest, datatype, &ndest); + } +} + +static void +arg_cat_optionv(char* dest, size_t ndest, const char* shortopts, const char* longopts, const char* datatype, int optvalue, const char* separator) { + separator = separator ? separator : ""; + + if (shortopts) { + const char* c = shortopts; + while (*c) { + /* "-a|-b|-c" */ + char shortopt[3]; + + /* note: shortopt array[] is initialiazed dynamically here to satisfy */ + /* a deficiency in the watcom compiler wrt static array initializers. */ + shortopt[0] = '-'; + shortopt[1] = *c; + shortopt[2] = 0; + + arg_cat(&dest, shortopt, &ndest); + if (*++c) + arg_cat(&dest, separator, &ndest); + } + } + + /* put separator between long opts and short opts */ + if (shortopts && longopts) + arg_cat(&dest, separator, &ndest); + + if (longopts) { + const char* c = longopts; + while (*c) { + size_t ncspn; + + /* add "--" tag prefix */ + arg_cat(&dest, "--", &ndest); + + /* add comma separated option tag */ + ncspn = strcspn(c, ","); +#if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) + strncat_s(dest, ndest, c, (ncspn < ndest) ? ncspn : ndest); +#else + strncat(dest, c, (ncspn < ndest) ? ncspn : ndest); +#endif + c += ncspn; + + /* add given separator in place of comma */ + if (*c == ',') { + arg_cat(&dest, separator, &ndest); + c++; + } + } + } + + if (datatype) { + if (longopts) + arg_cat(&dest, "=", &ndest); + else if (shortopts) + arg_cat(&dest, " ", &ndest); + + if (optvalue) { + arg_cat(&dest, "[", &ndest); + arg_cat(&dest, datatype, &ndest); + arg_cat(&dest, "]", &ndest); + } else + arg_cat(&dest, datatype, &ndest); + } +} + +void arg_print_option_ds(arg_dstr_t ds, const char* shortopts, const char* longopts, const char* datatype, const char* suffix) { + char syntax[200] = ""; + suffix = suffix ? suffix : ""; + + /* there is no way of passing the proper optvalue for optional argument values here, so we must ignore it */ + arg_cat_optionv(syntax, sizeof(syntax), shortopts, longopts, datatype, 0, "|"); + + arg_dstr_cat(ds, syntax); + arg_dstr_cat(ds, (char*)suffix); +} + +/* this function should be deprecated because it doesn't consider optional argument values (ARG_HASOPTVALUE) */ +void arg_print_option(FILE* fp, const char* shortopts, const char* longopts, const char* datatype, const char* suffix) { + arg_dstr_t ds = arg_dstr_create(); + arg_print_option_ds(ds, shortopts, longopts, datatype, suffix); + fputs(arg_dstr_cstr(ds), fp); + arg_dstr_destroy(ds); +} + +/* + * Print a GNU style [OPTION] string in which all short options that + * do not take argument values are presented in abbreviated form, as + * in: -xvfsd, or -xvf[sd], or [-xvsfd] + */ +static void arg_print_gnuswitch_ds(arg_dstr_t ds, struct arg_hdr** table) { + int tabindex; + char* format1 = " -%c"; + char* format2 = " [-%c"; + char* suffix = ""; + + /* print all mandatory switches that are without argument values */ + for (tabindex = 0; table[tabindex] && !(table[tabindex]->flag & ARG_TERMINATOR); tabindex++) { + /* skip optional options */ + if (table[tabindex]->mincount < 1) + continue; + + /* skip non-short options */ + if (table[tabindex]->shortopts == NULL) + continue; + + /* skip options that take argument values */ + if (table[tabindex]->flag & ARG_HASVALUE) + continue; + + /* print the short option (only the first short option char, ignore multiple choices)*/ + arg_dstr_catf(ds, format1, table[tabindex]->shortopts[0]); + format1 = "%c"; + format2 = "[%c"; + } + + /* print all optional switches that are without argument values */ + for (tabindex = 0; table[tabindex] && !(table[tabindex]->flag & ARG_TERMINATOR); tabindex++) { + /* skip mandatory args */ + if (table[tabindex]->mincount > 0) + continue; + + /* skip args without short options */ + if (table[tabindex]->shortopts == NULL) + continue; + + /* skip args with values */ + if (table[tabindex]->flag & ARG_HASVALUE) + continue; + + /* print first short option */ + arg_dstr_catf(ds, format2, table[tabindex]->shortopts[0]); + format2 = "%c"; + suffix = "]"; + } + + arg_dstr_catf(ds, "%s", suffix); +} + +void arg_print_syntax_ds(arg_dstr_t ds, void** argtable, const char* suffix) { + struct arg_hdr** table = (struct arg_hdr**)argtable; + int i, tabindex; + + /* print GNU style [OPTION] string */ + arg_print_gnuswitch_ds(ds, table); + + /* print remaining options in abbreviated style */ + for (tabindex = 0; table[tabindex] && !(table[tabindex]->flag & ARG_TERMINATOR); tabindex++) { + char syntax[200] = ""; + const char *shortopts, *longopts, *datatype; + + /* skip short options without arg values (they were printed by arg_print_gnu_switch) */ + if (table[tabindex]->shortopts && !(table[tabindex]->flag & ARG_HASVALUE)) + continue; + + shortopts = table[tabindex]->shortopts; + longopts = table[tabindex]->longopts; + datatype = table[tabindex]->datatype; + arg_cat_option(syntax, sizeof(syntax), shortopts, longopts, datatype, table[tabindex]->flag & ARG_HASOPTVALUE); + + if (strlen(syntax) > 0) { + /* print mandatory instances of this option */ + for (i = 0; i < table[tabindex]->mincount; i++) { + arg_dstr_cat(ds, " "); + arg_dstr_cat(ds, syntax); + } + + /* print optional instances enclosed in "[..]" */ + switch (table[tabindex]->maxcount - table[tabindex]->mincount) { + case 0: + break; + case 1: + arg_dstr_cat(ds, " ["); + arg_dstr_cat(ds, syntax); + arg_dstr_cat(ds, "]"); + break; + case 2: + arg_dstr_cat(ds, " ["); + arg_dstr_cat(ds, syntax); + arg_dstr_cat(ds, "]"); + arg_dstr_cat(ds, " ["); + arg_dstr_cat(ds, syntax); + arg_dstr_cat(ds, "]"); + break; + default: + arg_dstr_cat(ds, " ["); + arg_dstr_cat(ds, syntax); + arg_dstr_cat(ds, "]..."); + break; + } + } + } + + if (suffix) { + arg_dstr_cat(ds, (char*)suffix); + } +} + +void arg_print_syntax(FILE* fp, void** argtable, const char* suffix) { + arg_dstr_t ds = arg_dstr_create(); + arg_print_syntax_ds(ds, argtable, suffix); + fputs(arg_dstr_cstr(ds), fp); + arg_dstr_destroy(ds); +} + +void arg_print_syntaxv_ds(arg_dstr_t ds, void** argtable, const char* suffix) { + struct arg_hdr** table = (struct arg_hdr**)argtable; + int i, tabindex; + + /* print remaining options in abbreviated style */ + for (tabindex = 0; table[tabindex] && !(table[tabindex]->flag & ARG_TERMINATOR); tabindex++) { + char syntax[200] = ""; + const char *shortopts, *longopts, *datatype; + + shortopts = table[tabindex]->shortopts; + longopts = table[tabindex]->longopts; + datatype = table[tabindex]->datatype; + arg_cat_optionv(syntax, sizeof(syntax), shortopts, longopts, datatype, table[tabindex]->flag & ARG_HASOPTVALUE, "|"); + + /* print mandatory options */ + for (i = 0; i < table[tabindex]->mincount; i++) { + arg_dstr_cat(ds, " "); + arg_dstr_cat(ds, syntax); + } + + /* print optional args enclosed in "[..]" */ + switch (table[tabindex]->maxcount - table[tabindex]->mincount) { + case 0: + break; + case 1: + arg_dstr_cat(ds, " ["); + arg_dstr_cat(ds, syntax); + arg_dstr_cat(ds, "]"); + break; + case 2: + arg_dstr_cat(ds, " ["); + arg_dstr_cat(ds, syntax); + arg_dstr_cat(ds, "]"); + arg_dstr_cat(ds, " ["); + arg_dstr_cat(ds, syntax); + arg_dstr_cat(ds, "]"); + break; + default: + arg_dstr_cat(ds, " ["); + arg_dstr_cat(ds, syntax); + arg_dstr_cat(ds, "]..."); + break; + } + } + + if (suffix) { + arg_dstr_cat(ds, (char*)suffix); + } +} + +void arg_print_syntaxv(FILE* fp, void** argtable, const char* suffix) { + arg_dstr_t ds = arg_dstr_create(); + arg_print_syntaxv_ds(ds, argtable, suffix); + fputs(arg_dstr_cstr(ds), fp); + arg_dstr_destroy(ds); +} + +void arg_print_glossary_ds(arg_dstr_t ds, void** argtable, const char* format) { + struct arg_hdr** table = (struct arg_hdr**)argtable; + int tabindex; + + format = format ? format : " %-20s %s\n"; + for (tabindex = 0; !(table[tabindex]->flag & ARG_TERMINATOR); tabindex++) { + if (table[tabindex]->glossary) { + char syntax[200] = ""; + const char* shortopts = table[tabindex]->shortopts; + const char* longopts = table[tabindex]->longopts; + const char* datatype = table[tabindex]->datatype; + const char* glossary = table[tabindex]->glossary; + arg_cat_optionv(syntax, sizeof(syntax), shortopts, longopts, datatype, table[tabindex]->flag & ARG_HASOPTVALUE, ", "); + arg_dstr_catf(ds, format, syntax, glossary); + } + } +} + +void arg_print_glossary(FILE* fp, void** argtable, const char* format) { + arg_dstr_t ds = arg_dstr_create(); + arg_print_glossary_ds(ds, argtable, format); + fputs(arg_dstr_cstr(ds), fp); + arg_dstr_destroy(ds); +} + +/** + * Print a piece of text formatted, which means in a column with a + * left and a right margin. The lines are wrapped at whitspaces next + * to right margin. The function does not indent the first line, but + * only the following ones. + * + * Example: + * arg_print_formatted( fp, 0, 5, "Some text that doesn't fit." ) + * will result in the following output: + * + * Some + * text + * that + * doesn' + * t fit. + * + * Too long lines will be wrapped in the middle of a word. + * + * arg_print_formatted( fp, 2, 7, "Some text that doesn't fit." ) + * will result in the following output: + * + * Some + * text + * that + * doesn' + * t fit. + * + * As you see, the first line is not indented. This enables output of + * lines, which start in a line where output already happened. + * + * Author: Uli Fouquet + */ +static void arg_print_formatted_ds(arg_dstr_t ds, const unsigned lmargin, const unsigned rmargin, const char* text) { + const unsigned int textlen = (unsigned int)strlen(text); + unsigned int line_start = 0; + unsigned int line_end = textlen; + const unsigned int colwidth = (rmargin - lmargin) + 1; + + assert(strlen(text) < UINT_MAX); + + /* Someone doesn't like us... */ + if (line_end < line_start) { + arg_dstr_catf(ds, "%s\n", text); + } + + while (line_end > line_start) { + /* Eat leading white spaces. This is essential because while + wrapping lines, there will often be a whitespace at beginning + of line */ + while (isspace(*(text + line_start))) { + line_start++; + } + + /* Find last whitespace, that fits into line */ + if (line_end - line_start > colwidth) { + line_end = line_start + colwidth; + + while ((line_end > line_start) && !isspace(*(text + line_end))) { + line_end--; + } + + /* Consume trailing spaces */ + while ((line_end > line_start) && isspace(*(text + line_end))) { + line_end--; + } + + /* Restore the last non-space character */ + line_end++; + } + + /* Output line of text */ + while (line_start < line_end) { + char c = *(text + line_start); + arg_dstr_catc(ds, c); + line_start++; + } + arg_dstr_cat(ds, "\n"); + + /* Initialize another line */ + if (line_end < textlen) { + unsigned i; + + for (i = 0; i < lmargin; i++) { + arg_dstr_cat(ds, " "); + } + + line_end = textlen; + } + } /* lines of text */ +} + +/** + * Prints the glossary in strict GNU format. + * Differences to arg_print_glossary() are: + * - wraps lines after 80 chars + * - indents lines without shortops + * - does not accept formatstrings + * + * Contributed by Uli Fouquet + */ +void arg_print_glossary_gnu_ds(arg_dstr_t ds, void** argtable) { + struct arg_hdr** table = (struct arg_hdr**)argtable; + int tabindex; + + for (tabindex = 0; !(table[tabindex]->flag & ARG_TERMINATOR); tabindex++) { + if (table[tabindex]->glossary) { + char syntax[200] = ""; + const char* shortopts = table[tabindex]->shortopts; + const char* longopts = table[tabindex]->longopts; + const char* datatype = table[tabindex]->datatype; + const char* glossary = table[tabindex]->glossary; + + if (!shortopts && longopts) { + /* Indent trailing line by 4 spaces... */ + memset(syntax, ' ', 4); + *(syntax + 4) = '\0'; + } + + arg_cat_optionv(syntax, sizeof(syntax), shortopts, longopts, datatype, table[tabindex]->flag & ARG_HASOPTVALUE, ", "); + + /* If syntax fits not into column, print glossary in new line... */ + if (strlen(syntax) > 25) { + arg_dstr_catf(ds, " %-25s %s\n", syntax, ""); + *syntax = '\0'; + } + + arg_dstr_catf(ds, " %-25s ", syntax); + arg_print_formatted_ds(ds, 28, 79, glossary); + } + } /* for each table entry */ + + arg_dstr_cat(ds, "\n"); +} + +void arg_print_glossary_gnu(FILE* fp, void** argtable) { + arg_dstr_t ds = arg_dstr_create(); + arg_print_glossary_gnu_ds(ds, argtable); + fputs(arg_dstr_cstr(ds), fp); + arg_dstr_destroy(ds); +} + +/** + * Checks the argtable[] array for NULL entries and returns 1 + * if any are found, zero otherwise. + */ +int arg_nullcheck(void** argtable) { + struct arg_hdr** table = (struct arg_hdr**)argtable; + int tabindex; + /*printf("arg_nullcheck(%p)\n",argtable);*/ + + if (!table) + return 1; + + tabindex = 0; + do { + /*printf("argtable[%d]=%p\n",tabindex,argtable[tabindex]);*/ + if (!table[tabindex]) + return 1; + } while (!(table[tabindex++]->flag & ARG_TERMINATOR)); + + return 0; +} + +/* + * arg_free() is deprecated in favour of arg_freetable() due to a flaw in its design. + * The flaw results in memory leak in the (very rare) case that an intermediate + * entry in the argtable array failed its memory allocation while others following + * that entry were still allocated ok. Those subsequent allocations will not be + * deallocated by arg_free(). + * Despite the unlikeliness of the problem occurring, and the even unlikelier event + * that it has any deliterious effect, it is fixed regardless by replacing arg_free() + * with the newer arg_freetable() function. + * We still keep arg_free() for backwards compatibility. + */ +void arg_free(void** argtable) { + struct arg_hdr** table = (struct arg_hdr**)argtable; + int tabindex = 0; + int flag; + /*printf("arg_free(%p)\n",argtable);*/ + do { + /* + if we encounter a NULL entry then somewhat incorrectly we presume + we have come to the end of the array. It isnt strictly true because + an intermediate entry could be NULL with other non-NULL entries to follow. + The subsequent argtable entries would then not be freed as they should. + */ + if (table[tabindex] == NULL) + break; + + flag = table[tabindex]->flag; + xfree(table[tabindex]); + table[tabindex++] = NULL; + + } while (!(flag & ARG_TERMINATOR)); +} + +/* frees each non-NULL element of argtable[], where n is the size of the number of entries in the array */ +void arg_freetable(void** argtable, size_t n) { + struct arg_hdr** table = (struct arg_hdr**)argtable; + size_t tabindex = 0; + /*printf("arg_freetable(%p)\n",argtable);*/ + for (tabindex = 0; tabindex < n; tabindex++) { + if (table[tabindex] == NULL) + continue; + + xfree(table[tabindex]); + table[tabindex] = NULL; + }; +} + +#ifdef _WIN32 +BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { + return TRUE; + UNREFERENCED_PARAMETER(hinstDLL); + UNREFERENCED_PARAMETER(fdwReason); + UNREFERENCED_PARAMETER(lpvReserved); +} +#endif diff --git a/c/utf8filenamecheck/windows/argtable/argtable3.h b/c/utf8filenamecheck/windows/argtable/argtable3.h new file mode 100644 index 0000000..2042538 --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/argtable3.h @@ -0,0 +1,277 @@ +/******************************************************************************* + * argtable3: Declares the main interfaces of the library + * + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#ifndef ARGTABLE3 +#define ARGTABLE3 + +#include /* FILE */ +#include /* struct tm */ + +#ifdef __cplusplus +extern "C" { +#endif + +#define ARG_REX_ICASE 1 +#define ARG_DSTR_SIZE 200 +#define ARG_CMD_NAME_LEN 100 +#define ARG_CMD_DESCRIPTION_LEN 256 + +#ifndef ARG_REPLACE_GETOPT +#define ARG_REPLACE_GETOPT 1 /* use the embedded getopt as the system getopt(3) */ +#endif /* ARG_REPLACE_GETOPT */ + +/* bit masks for arg_hdr.flag */ +enum { ARG_TERMINATOR = 0x1, ARG_HASVALUE = 0x2, ARG_HASOPTVALUE = 0x4 }; + +#if defined(_WIN32) + #if defined(argtable3_EXPORTS) + #define ARG_EXTERN __declspec(dllexport) + #elif defined(argtable3_IMPORTS) + #define ARG_EXTERN __declspec(dllimport) + #else + #define ARG_EXTERN + #endif +#else + #define ARG_EXTERN +#endif + +typedef struct _internal_arg_dstr* arg_dstr_t; +typedef void* arg_cmd_itr_t; + +typedef void(arg_resetfn)(void* parent); +typedef int(arg_scanfn)(void* parent, const char* argval); +typedef int(arg_checkfn)(void* parent); +typedef void(arg_errorfn)(void* parent, arg_dstr_t ds, int error, const char* argval, const char* progname); +typedef void(arg_dstr_freefn)(char* buf); +typedef int(arg_cmdfn)(int argc, char* argv[], arg_dstr_t res); +typedef int(arg_comparefn)(const void* k1, const void* k2); + +/* + * The arg_hdr struct defines properties that are common to all arg_xxx structs. + * The argtable library requires each arg_xxx struct to have an arg_hdr + * struct as its first data member. + * The argtable library functions then use this data to identify the + * properties of the command line option, such as its option tags, + * datatype string, and glossary strings, and so on. + * Moreover, the arg_hdr struct contains pointers to custom functions that + * are provided by each arg_xxx struct which perform the tasks of parsing + * that particular arg_xxx arguments, performing post-parse checks, and + * reporting errors. + * These functions are private to the individual arg_xxx source code + * and are the pointer to them are initiliased by that arg_xxx struct's + * constructor function. The user could alter them after construction + * if desired, but the original intention is for them to be set by the + * constructor and left unaltered. + */ +struct arg_hdr { + char flag; /* Modifier flags: ARG_TERMINATOR, ARG_HASVALUE. */ + const char* shortopts; /* String defining the short options */ + const char* longopts; /* String defiing the long options */ + const char* datatype; /* Description of the argument data type */ + const char* glossary; /* Description of the option as shown by arg_print_glossary function */ + int mincount; /* Minimum number of occurences of this option accepted */ + int maxcount; /* Maximum number of occurences if this option accepted */ + void* parent; /* Pointer to parent arg_xxx struct */ + arg_resetfn* resetfn; /* Pointer to parent arg_xxx reset function */ + arg_scanfn* scanfn; /* Pointer to parent arg_xxx scan function */ + arg_checkfn* checkfn; /* Pointer to parent arg_xxx check function */ + arg_errorfn* errorfn; /* Pointer to parent arg_xxx error function */ + void* priv; /* Pointer to private header data for use by arg_xxx functions */ +}; + +struct arg_rem { + struct arg_hdr hdr; /* The mandatory argtable header struct */ +}; + +struct arg_lit { + struct arg_hdr hdr; /* The mandatory argtable header struct */ + int count; /* Number of matching command line args */ +}; + +struct arg_int { + struct arg_hdr hdr; /* The mandatory argtable header struct */ + int count; /* Number of matching command line args */ + int* ival; /* Array of parsed argument values */ +}; + +struct arg_dbl { + struct arg_hdr hdr; /* The mandatory argtable header struct */ + int count; /* Number of matching command line args */ + double* dval; /* Array of parsed argument values */ +}; + +struct arg_str { + struct arg_hdr hdr; /* The mandatory argtable header struct */ + int count; /* Number of matching command line args */ + const char** sval; /* Array of parsed argument values */ +}; + +struct arg_rex { + struct arg_hdr hdr; /* The mandatory argtable header struct */ + int count; /* Number of matching command line args */ + const char** sval; /* Array of parsed argument values */ +}; + +struct arg_file { + struct arg_hdr hdr; /* The mandatory argtable header struct */ + int count; /* Number of matching command line args*/ + const char** filename; /* Array of parsed filenames (eg: /home/foo.bar) */ + const char** basename; /* Array of parsed basenames (eg: foo.bar) */ + const char** extension; /* Array of parsed extensions (eg: .bar) */ +}; + +struct arg_date { + struct arg_hdr hdr; /* The mandatory argtable header struct */ + const char* format; /* strptime format string used to parse the date */ + int count; /* Number of matching command line args */ + struct tm* tmval; /* Array of parsed time values */ +}; + +enum { ARG_ELIMIT = 1, ARG_EMALLOC, ARG_ENOMATCH, ARG_ELONGOPT, ARG_EMISSARG }; +struct arg_end { + struct arg_hdr hdr; /* The mandatory argtable header struct */ + int count; /* Number of errors encountered */ + int* error; /* Array of error codes */ + void** parent; /* Array of pointers to offending arg_xxx struct */ + const char** argval; /* Array of pointers to offending argv[] string */ +}; + +typedef struct arg_cmd_info { + char name[ARG_CMD_NAME_LEN]; + char description[ARG_CMD_DESCRIPTION_LEN]; + arg_cmdfn* proc; +} arg_cmd_info_t; + +/**** arg_xxx constructor functions *********************************/ + +ARG_EXTERN struct arg_rem* arg_rem(const char* datatype, const char* glossary); + +ARG_EXTERN struct arg_lit* arg_lit0(const char* shortopts, const char* longopts, const char* glossary); +ARG_EXTERN struct arg_lit* arg_lit1(const char* shortopts, const char* longopts, const char* glossary); +ARG_EXTERN struct arg_lit* arg_litn(const char* shortopts, const char* longopts, int mincount, int maxcount, const char* glossary); + +ARG_EXTERN struct arg_key* arg_key0(const char* keyword, int flags, const char* glossary); +ARG_EXTERN struct arg_key* arg_key1(const char* keyword, int flags, const char* glossary); +ARG_EXTERN struct arg_key* arg_keyn(const char* keyword, int flags, int mincount, int maxcount, const char* glossary); + +ARG_EXTERN struct arg_int* arg_int0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary); +ARG_EXTERN struct arg_int* arg_int1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary); +ARG_EXTERN struct arg_int* arg_intn(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary); + +ARG_EXTERN struct arg_dbl* arg_dbl0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary); +ARG_EXTERN struct arg_dbl* arg_dbl1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary); +ARG_EXTERN struct arg_dbl* arg_dbln(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary); + +ARG_EXTERN struct arg_str* arg_str0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary); +ARG_EXTERN struct arg_str* arg_str1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary); +ARG_EXTERN struct arg_str* arg_strn(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary); + +ARG_EXTERN struct arg_rex* arg_rex0(const char* shortopts, const char* longopts, const char* pattern, const char* datatype, int flags, const char* glossary); +ARG_EXTERN struct arg_rex* arg_rex1(const char* shortopts, const char* longopts, const char* pattern, const char* datatype, int flags, const char* glossary); +ARG_EXTERN struct arg_rex* arg_rexn(const char* shortopts, + const char* longopts, + const char* pattern, + const char* datatype, + int mincount, + int maxcount, + int flags, + const char* glossary); + +ARG_EXTERN struct arg_file* arg_file0(const char* shortopts, const char* longopts, const char* datatype, const char* glossary); +ARG_EXTERN struct arg_file* arg_file1(const char* shortopts, const char* longopts, const char* datatype, const char* glossary); +ARG_EXTERN struct arg_file* arg_filen(const char* shortopts, const char* longopts, const char* datatype, int mincount, int maxcount, const char* glossary); + +ARG_EXTERN struct arg_date* arg_date0(const char* shortopts, const char* longopts, const char* format, const char* datatype, const char* glossary); +ARG_EXTERN struct arg_date* arg_date1(const char* shortopts, const char* longopts, const char* format, const char* datatype, const char* glossary); +ARG_EXTERN struct arg_date* arg_daten(const char* shortopts, const char* longopts, const char* format, const char* datatype, int mincount, int maxcount, const char* glossary); + +ARG_EXTERN struct arg_end* arg_end(int maxerrors); + +#define ARG_DSTR_STATIC ((arg_dstr_freefn*)0) +#define ARG_DSTR_VOLATILE ((arg_dstr_freefn*)1) +#define ARG_DSTR_DYNAMIC ((arg_dstr_freefn*)3) + +/**** other functions *******************************************/ +ARG_EXTERN int arg_nullcheck(void** argtable); +ARG_EXTERN int arg_parse(int argc, char** argv, void** argtable); +ARG_EXTERN void arg_print_option(FILE* fp, const char* shortopts, const char* longopts, const char* datatype, const char* suffix); +ARG_EXTERN void arg_print_syntax(FILE* fp, void** argtable, const char* suffix); +ARG_EXTERN void arg_print_syntaxv(FILE* fp, void** argtable, const char* suffix); +ARG_EXTERN void arg_print_glossary(FILE* fp, void** argtable, const char* format); +ARG_EXTERN void arg_print_glossary_gnu(FILE* fp, void** argtable); +ARG_EXTERN void arg_print_errors(FILE* fp, struct arg_end* end, const char* progname); +ARG_EXTERN void arg_print_option_ds(arg_dstr_t ds, const char* shortopts, const char* longopts, const char* datatype, const char* suffix); +ARG_EXTERN void arg_print_syntax_ds(arg_dstr_t ds, void** argtable, const char* suffix); +ARG_EXTERN void arg_print_syntaxv_ds(arg_dstr_t ds, void** argtable, const char* suffix); +ARG_EXTERN void arg_print_glossary_ds(arg_dstr_t ds, void** argtable, const char* format); +ARG_EXTERN void arg_print_glossary_gnu_ds(arg_dstr_t ds, void** argtable); +ARG_EXTERN void arg_print_errors_ds(arg_dstr_t ds, struct arg_end* end, const char* progname); +ARG_EXTERN void arg_freetable(void** argtable, size_t n); + +ARG_EXTERN arg_dstr_t arg_dstr_create(void); +ARG_EXTERN void arg_dstr_destroy(arg_dstr_t ds); +ARG_EXTERN void arg_dstr_reset(arg_dstr_t ds); +ARG_EXTERN void arg_dstr_free(arg_dstr_t ds); +ARG_EXTERN void arg_dstr_set(arg_dstr_t ds, char* str, arg_dstr_freefn* free_proc); +ARG_EXTERN void arg_dstr_cat(arg_dstr_t ds, const char* str); +ARG_EXTERN void arg_dstr_catc(arg_dstr_t ds, char c); +ARG_EXTERN void arg_dstr_catf(arg_dstr_t ds, const char* fmt, ...); +ARG_EXTERN char* arg_dstr_cstr(arg_dstr_t ds); + +ARG_EXTERN void arg_cmd_init(void); +ARG_EXTERN void arg_cmd_uninit(void); +ARG_EXTERN void arg_cmd_register(const char* name, arg_cmdfn* proc, const char* description); +ARG_EXTERN void arg_cmd_unregister(const char* name); +ARG_EXTERN int arg_cmd_dispatch(const char* name, int argc, char* argv[], arg_dstr_t res); +ARG_EXTERN unsigned int arg_cmd_count(void); +ARG_EXTERN arg_cmd_info_t* arg_cmd_info(const char* name); +ARG_EXTERN arg_cmd_itr_t arg_cmd_itr_create(void); +ARG_EXTERN void arg_cmd_itr_destroy(arg_cmd_itr_t itr); +ARG_EXTERN int arg_cmd_itr_advance(arg_cmd_itr_t itr); +ARG_EXTERN char* arg_cmd_itr_key(arg_cmd_itr_t itr); +ARG_EXTERN arg_cmd_info_t* arg_cmd_itr_value(arg_cmd_itr_t itr); +ARG_EXTERN int arg_cmd_itr_search(arg_cmd_itr_t itr, void* k); +ARG_EXTERN void arg_mgsort(void* data, int size, int esize, int i, int k, arg_comparefn* comparefn); +ARG_EXTERN void arg_make_get_help_msg(arg_dstr_t res); +ARG_EXTERN void arg_make_help_msg(arg_dstr_t ds, char* cmd_name, void** argtable); +ARG_EXTERN void arg_make_syntax_err_msg(arg_dstr_t ds, void** argtable, struct arg_end* end); +ARG_EXTERN int arg_make_syntax_err_help_msg(arg_dstr_t ds, char* name, int help, int nerrors, void** argtable, struct arg_end* end, int* exitcode); +ARG_EXTERN void arg_set_module_name(const char* name); +ARG_EXTERN void arg_set_module_version(int major, int minor, int patch, const char* tag); + +/**** deprecated functions, for back-compatibility only ********/ +ARG_EXTERN void arg_free(void** argtable); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/c/utf8filenamecheck/windows/argtable/examples/CMakeLists.txt b/c/utf8filenamecheck/windows/argtable/examples/CMakeLists.txt new file mode 100644 index 0000000..9e0be34 --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/examples/CMakeLists.txt @@ -0,0 +1,46 @@ +################################################################################ +# This file is part of the argtable3 library. +# +# Copyright (C) 2016-2019 Tom G. Huang +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of STEWART HEITMANN nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, +# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +################################################################################ + +file(GLOB EXAMPLES_SOURCES RELATIVE ${PROJECT_SOURCE_DIR}/examples *.c) + +if(UNIX) + set(ARGTABLE3_EXTRA_LIBS m) +endif() + +foreach(examples_src ${EXAMPLES_SOURCES}) + string(REPLACE ".c" "" examplename ${examples_src}) + add_executable(${examplename} ${PROJECT_SOURCE_DIR}/examples/${examples_src}) + target_include_directories(${examplename} PRIVATE ${PROJECT_SOURCE_DIR}/src) + if(ARGTABLE3_BUILD_STATIC_EXAMPLES) + target_link_libraries(${examplename} argtable3_static ${ARGTABLE3_EXTRA_LIBS}) + else() + target_link_libraries(${examplename} argtable3 ${ARGTABLE3_EXTRA_LIBS}) + endif() +endforeach() diff --git a/c/utf8filenamecheck/windows/argtable/examples/echo.c b/c/utf8filenamecheck/windows/argtable/examples/echo.c new file mode 100644 index 0000000..584db02 --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/examples/echo.c @@ -0,0 +1,128 @@ +/******************************************************************************* + * Example source code for using the argtable3 library to implement: + * + * echo [-neE] [--help] [--version] [STRING]... + * + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +/* Here we only approximate the echo functionality */ +void mymain(int n, int e, int E, const char** strings, int nstrings) + { + int j; + + printf("option -n = %s\n", ((n)?"YES":"NO")); + printf("option -e = %s\n", ((e)?"YES":"NO")); + printf("option -E = %s\n", ((E)?"YES":"NO")); + for (j=0; js"); + struct arg_lit *help = arg_lit0(NULL,"help", "print this help and exit"); + struct arg_lit *vers = arg_lit0(NULL,"version", "print version information and exit"); + struct arg_str *strs = arg_strn(NULL,NULL,"STRING",0,argc+2,NULL); + struct arg_end *end = arg_end(20); + void* argtable[] = {n,e,E,help,vers,strs,end}; + const char* progname = "echo"; + int exitcode=0; + int nerrors; + + /* verify the argtable[] entries were allocated sucessfully */ + if (arg_nullcheck(argtable) != 0) + { + /* NULL entries were detected, some allocations must have failed */ + printf("%s: insufficient memory\n",progname); + exitcode=1; + goto exit; + } + + /* Parse the command line as defined by argtable[] */ + nerrors = arg_parse(argc,argv,argtable); + + /* special case: '--help' takes precedence over error reporting */ + if (help->count > 0) + { + printf("Usage: %s", progname); + arg_print_syntax(stdout,argtable,"\n"); + printf("Echo the STRINGs to standard output.\n\n"); + arg_print_glossary(stdout,argtable," %-10s %s\n"); + printf("\nWithout -E, the following sequences are recognized and interpolated:\n\n" + " \\NNN the character whose ASCII code is NNN (octal)\n" + " \\\\ backslash\n" + " \\a alert (BEL)\n" + " \\b backspace\n" + " \\c suppress trailing newline\n" + " \\f form feed\n" + " \\n new line\n" + " \\r carriage return\n" + " \\t horizontal tab\n" + " \\v vertical tab\n\n" + "Report bugs to .\n"); + exitcode=0; + goto exit; + } + + /* special case: '--version' takes precedence error reporting */ + if (vers->count > 0) + { + printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname); + printf("September 2003, Stewart Heitmann\n"); + exitcode=0; + goto exit; + } + + /* If the parser returned any errors then display them and exit */ + if (nerrors > 0) + { + /* Display the error details contained in the arg_end struct.*/ + arg_print_errors(stdout,end,progname); + printf("Try '%s --help' for more information.\n",progname); + exitcode=1; + goto exit; + } + + /* Command line parsing is complete, do the main processing */ + mymain(n->count, e->count, E->count, strs->sval, strs->count); + + exit: + /* deallocate each non-null entry in argtable[] */ + arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); + + return exitcode; + } diff --git a/c/utf8filenamecheck/windows/argtable/examples/ls.c b/c/utf8filenamecheck/windows/argtable/examples/ls.c new file mode 100644 index 0000000..345eb38 --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/examples/ls.c @@ -0,0 +1,326 @@ +/******************************************************************************* + * Example source code for using the argtable3 library to implement: + * + * ls [-aAbBcCdDfFgGhHiklLmnNopqQrRsStuUvxX1] [--author] + * [--block-size=SIZE] [--color=[WHEN]] [--format=WORD] [--full-time] + * [--si] [--dereference-command-line-symlink-to-dir] [--indicator-style=WORD] + * [-I PATTERN] [--show-control-chars] [--quoting-style=WORD] [--sort=WORD] + * [--time=WORD] [--time-style=STYLE] [-T COLS] [-w COLS] [--help] + * [--version] [FILE]... + * + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +/* These variables hold the values parsed from the comand line by arg_parse() */ +struct arg_lit *a, *A, *author, *b, *B, *c, *C, *d, *D, *f, *F, *fulltime; +struct arg_lit *g, *G, *h, *H, *si, *deref, *i, *k, *l, *L, *m, *n, *N, *o, *p; +struct arg_lit *q, *shcont, *Q, *r, *R, *s, *S, *t, *u, *U, *v, *x, *X, *one; +struct arg_lit *help, *version; +struct arg_int *blocksize, *T, *w; +struct arg_str *color, *format, *indic, *I, *Qstyle, *sort, *Time, *timesty; +struct arg_file *files; +struct arg_end *end; + +/* Here we simply echo the command line option values as a demonstration. */ +/* In a real program, this is where we would perform the main processing. */ +int mymain(void) + { + int j; + + if (a->count > 0) + printf("a=YES\n"); + if (A->count > 0) + printf("A=YES\n"); + if (author->count > 0) + printf("author=YES\n"); + if (b->count > 0) + printf("b=YES\n"); + if (blocksize->count > 0) + printf("blocksize=%d\n",blocksize->count); + if (B->count > 0) + printf("B=YES\n"); + if (c->count > 0) + printf("c=YES\n"); + if (C->count > 0) + printf("C=YES\n"); + if (color->count > 0) + printf("color=%s\n",color->sval[0]); + if (d->count > 0) + printf("d=YES\n"); + if (D->count > 0) + printf("D=YES\n"); + if (f->count > 0) + printf("f=YES\n"); + if (F->count > 0) + printf("F=YES\n"); + if (format->count > 0) + printf("format=%s\n",format->sval[0]); + if (fulltime->count > 0) + printf("fulltime=YES\n"); + if (g->count > 0) + printf("g=YES\n"); + if (G->count > 0) + printf("G=YES\n"); + if (h->count > 0) + printf("h=YES\n"); + if (si->count > 0) + printf("si=YES\n"); + if (H->count > 0) + printf("H=YES\n"); + if (deref->count > 0) + printf("deref=YES\n"); + if (indic->count > 0) + printf("indic=%s\n",indic->sval[0]); + if (i->count > 0) + printf("i=YES\n"); + if (I->count > 0) + printf("I=%s\n",I->sval[0]); + if (k->count > 0) + printf("k=YES\n"); + if (l->count > 0) + printf("l=YES\n"); + if (L->count > 0) + printf("L=YES\n"); + if (m->count > 0) + printf("m=YES\n"); + if (n->count > 0) + printf("n=YES\n"); + if (N->count > 0) + printf("N=YES\n"); + if (o->count > 0) + printf("o=YES\n"); + if (p->count > 0) + printf("p=YES\n"); + if (q->count > 0) + printf("q=YES\n"); + if (shcont->count > 0) + printf("shcont=YES\n"); + if (Q->count > 0) + printf("Q=YES\n"); + if (Qstyle->count > 0) + printf("Qstyle=%s\n",Qstyle->sval[0]); + if (r->count > 0) + printf("r=YES\n"); + if (R->count > 0) + printf("R=YES\n"); + if (s->count > 0) + printf("s=YES\n"); + if (S->count > 0) + printf("S=YES\n"); + if (sort->count > 0) + printf("sort=%s\n",sort->sval[0]); + if (Time->count > 0) + printf("time=%s\n",Time->sval[0]); + if (timesty->count > 0) + printf("timesty=%s\n",timesty->sval[0]); + if (t->count > 0) + printf("t=YES\n"); + if (T->count > 0) + printf("T=%d\n",T->ival[0]); + if (u->count > 0) + printf("u=YES\n"); + if (U->count > 0) + printf("U=YES\n"); + if (v->count > 0) + printf("v=YES\n"); + if (w->count > 0) + printf("w=%d\n",w->ival[0]); + if (x->count > 0) + printf("x=YES\n"); + if (X->count > 0) + printf("X=YES\n"); + if (one->count > 0) + printf("1=YES\n"); + + /* print the filenames */ + for (j=0; jcount; j++) + printf("filename[%d] = \"%s\"\n", j, files->filename[j]); + + return 0; + } + + +int main(int argc, char **argv) + { + /* The argtable[] entries define the command line options */ + void *argtable[] = { + a = arg_lit0("a", "all", "do not hide entries starting with ."), + A = arg_lit0("A", "almost-all", "do not list implied . and .."), + author = arg_lit0(NULL,"author", "print the author of each file"), + b = arg_lit0("b", "escape", "print octal escapes for nongraphic characters"), + blocksize = arg_int0(NULL,"block-size","SIZE", "use SIZE-byte blocks"), + B = arg_lit0("B", "ignore-backups", "do not list implied entries ending with ~"), + c = arg_lit0("c", NULL, "with -lt: sort by, and show, ctime (time of last"), + arg_rem(NULL, " modification of file status information)"), + arg_rem(NULL, " with -l: show ctime and sort by name"), + arg_rem(NULL, " otherwise: sort by ctime"), + C = arg_lit0("C", NULL, "list entries by columns"), + color = arg_str0(NULL,"color","WHEN", "control whether color is used to distinguish file"), + arg_rem(NULL, " types. WHEN may be `never', `always', or `auto'"), + d = arg_lit0("d", "directory", "list directory entries instead of contents,"), + arg_rem(NULL, " and do not dereference symbolic links"), + D = arg_lit0("D", "dired", "generate output designed for Emacs' dired mode"), + f = arg_lit0("f", NULL, "do not sort, enable -aU, disable -lst"), + F = arg_lit0("F", "classify", "append indicator (one of */=@|) to entries"), + format = arg_str0(NULL,"format","WORD", "across -x, commas -m, horizontal -x, long -l,"), + arg_rem (NULL, " single-column -1, verbose -l, vertical -C"), + fulltime = arg_lit0(NULL,"full-time", "like -l --time-style=full-iso"), + g = arg_lit0("g", NULL, "like -l, but do not list owner"), + G = arg_lit0("G", "no-group", "inhibit display of group information"), + h = arg_lit0("h", "human-readable", "print sizes in human readable format (e.g., 1K 234M 2G)"), + si = arg_lit0(NULL,"si", "likewise, but use powers of 1000 not 1024"), + H = arg_lit0("H", "dereference-command-line","follow symbolic links listed on the command line"), + deref = arg_lit0(NULL,"dereference-command-line-symlink-to-dir","follow each command line symbolic link"), + arg_rem(NULL, " that points to a directory"), + indic = arg_str0(NULL,"indicator-style","WORD","append indicator with style WORD to entry names:"), + arg_rem (NULL, " none (default), classify (-F), file-type (-p)"), + i = arg_lit0("i", "inode", "print index number of each file"), + I = arg_str0("I", "ignore","PATTERN", "do not list implied entries matching shell PATTERN"), + k = arg_lit0("k", NULL, "like --block-size=1K"), + l = arg_lit0("l", NULL, "use a long listing format"), + L = arg_lit0("L", "dereference", "when showing file information for a symbolic"), + arg_rem (NULL, " link, show information for the file the link"), + arg_rem (NULL, " references rather than for the link itself"), + m = arg_lit0("m", NULL, "fill width with a comma separated list of entries"), + n = arg_lit0("n", "numeric-uid-gid", "like -l, but list numeric UIDs and GIDs"), + N = arg_lit0("N", "literal", "print raw entry names (don't treat e.g. control"), + arg_rem (NULL, " characters specially)"), + o = arg_lit0("o", NULL, "like -l, but do not list group information"), + p = arg_lit0("p", "file-type", "append indicator (one of /=@|) to entries"), + q = arg_lit0("q", "hide-control-chars", "print ? instead of non graphic characters"), + shcont = arg_lit0(NULL,"show-control-chars", "show non graphic characters as-is (default"), + arg_rem (NULL, "unless program is `ls' and output is a terminal)"), + Q = arg_lit0("Q", "quote-name", "enclose entry names in double quotes"), + Qstyle = arg_str0(NULL,"quoting-style","WORD","use quoting style WORD for entry names:"), + arg_rem (NULL, " literal, locale, shell, shell-always, c, escape"), + r = arg_lit0("r", "reverse", "reverse order while sorting"), + R = arg_lit0("R", "recursive", "list subdirectories recursively"), + s = arg_lit0("s", "size", "print size of each file, in blocks"), + S = arg_lit0("S", NULL, "sort by file size"), + sort = arg_str0(NULL,"sort","WORD", "extension -X, none -U, size -S, time -t, version -v,"), + arg_rem (NULL, "status -c, time -t, atime -u, access -u, use -u"), + Time = arg_str0(NULL,"time","WORD", "show time as WORD instead of modification time:"), + arg_rem (NULL, " atime, access, use, ctime or status; use"), + arg_rem (NULL, " specified time as sort key if --sort=time"), + timesty = arg_str0(NULL, "time-style","STYLE", "show times using style STYLE:"), + arg_rem (NULL, " full-iso, long-iso, iso, locale, +FORMAT"), + arg_rem (NULL, "FORMAT is interpreted like `date'; if FORMAT is"), + arg_rem (NULL, "FORMAT1FORMAT2, FORMAT1 applies to"), + arg_rem (NULL, "non-recent files and FORMAT2 to recent files;"), + arg_rem (NULL, "if STYLE is prefixed with `posix-', STYLE"), + arg_rem (NULL, "takes effect only outside the POSIX locale"), + t = arg_lit0("t", NULL, "sort by modification time"), + T = arg_int0("T", "tabsize", "COLS", "assume tab stops at each COLS instead of 8"), + u = arg_lit0("u", NULL, "with -lt: sort by, and show, access time"), + arg_rem (NULL, " with -l: show access time and sort by name"), + arg_rem (NULL, " otherwise: sort by access time"), + U = arg_lit0("U", NULL, "do not sort; list entries in directory order"), + v = arg_lit0("v", NULL, "sort by version"), + w = arg_int0("w", "width", "COLS", "assume screen width instead of current value"), + x = arg_lit0("x", NULL, "list entries by lines instead of by columns"), + X = arg_lit0("X", NULL, "sort alphabetically by entry extension"), + one = arg_lit0("1", NULL, "list one file per line"), + help = arg_lit0(NULL,"help", "display this help and exit"), + version = arg_lit0(NULL,"version", "display version information and exit"), + files = arg_filen(NULL, NULL, "FILE", 0, argc+2, NULL), + end = arg_end(20), + }; + const char *progname = "ls"; + int exitcode=0; + int nerrors; + + /* verify the argtable[] entries were allocated sucessfully */ + if (arg_nullcheck(argtable) != 0) + { + /* NULL entries were detected, some allocations must have failed */ + printf("%s: insufficient memory\n",progname); + exitcode=1; + goto exit; + } + + /* allow optional argument values for --color */ + /* and set the default value to "always" */ + color->hdr.flag |= ARG_HASOPTVALUE; + color->sval[0] = "always"; + + /* Parse the command line as defined by argtable[] */ + nerrors = arg_parse(argc,argv,argtable); + + /* special case: '--help' takes precedence over error reporting */ + if (help->count > 0) + { + printf("Usage: %s", progname); + arg_print_syntax(stdout,argtable,"\n"); + printf("List information about the FILE(s) (the current directory by default).\n"); + printf("Sort entries alphabetically if none of -cftuSUX nor --sort.\n\n"); + arg_print_glossary(stdout,argtable," %-25s %s\n"); + printf("\nSIZE may be (or may be an integer optionally followed by) one of following:\n" + "kB 1000, K 1024, MB 1,000,000, M 1,048,576, and so on for G, T, P, E, Z, Y.\n\n" + "By default, color is not used to distinguish types of files. That is\n" + "equivalent to using --color=none. Using the --color option without the\n" + "optional WHEN argument is equivalent to using --color=always. With\n" + "--color=auto, color codes are output only if standard output is connected\n" + "to a terminal (tty).\n\n" + "Report bugs to .\n"); + exitcode=0; + goto exit; + } + + /* special case: '--version' takes precedence error reporting */ + if (version->count > 0) + { + printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname); + printf("September 2003, Stewart Heitmann\n"); + exitcode=0; + goto exit; + } + + /* If the parser returned any errors then display them and exit */ + if (nerrors > 0) + { + /* Display the error details contained in the arg_end struct.*/ + arg_print_errors(stdout,end,progname); + printf("Try '%s --help' for more information.\n",progname); + exitcode=1; + goto exit; + } + + /* Command line parsing is complete, do the main processing */ + exitcode = mymain(); + +exit: + /* deallocate each non-null entry in argtable[] */ + arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); + + return exitcode; + } + + diff --git a/c/utf8filenamecheck/windows/argtable/examples/multisyntax.c b/c/utf8filenamecheck/windows/argtable/examples/multisyntax.c new file mode 100644 index 0000000..b5b7853 --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/examples/multisyntax.c @@ -0,0 +1,239 @@ +/******************************************************************************* + * Example source code for using the argtable3 library to implement + * a multi-syntax command line argument program + * + * usage 1: multisyntax [-nvR] insert []... [-o ] + * usage 2: multisyntax [-nv] remove + * usage 3: multisyntax [-v] search [-o ] + * usage 4: multisyntax [--help] [--version] + * + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +#define REG_EXTENDED 1 +#define REG_ICASE (REG_EXTENDED << 1) + +/* mymain1 implements the actions for syntax 1 */ +int mymain1(int n, int v, int R, const char *outfile, + const char **infiles, int ninfiles) + { + int i; + printf("syntax 1 matched OK:\n"); + printf("n=%d\n", n); + printf("v=%d\n", v); + printf("R=%d\n", R); + printf("outfile=\"%s\"\n", outfile); + for (i=0; i [file]... -o */ + struct arg_rex *cmd1 = arg_rex1(NULL, NULL, "insert", NULL, REG_ICASE, NULL); + struct arg_lit *noact1 = arg_lit0("n", NULL, "take no action"); + struct arg_lit *verbose1 = arg_lit0("v", "verbose", "verbose messages"); + struct arg_lit *recurse1 = arg_lit0("R", NULL, "recurse through subdirectories"); + struct arg_file *infiles1 = arg_filen(NULL, NULL, NULL, 1,argc+2, "input file(s)"); + struct arg_file *outfile1 = arg_file0("o", NULL, "", "output file (default is \"-\")"); + struct arg_end *end1 = arg_end(20); + void* argtable1[] = {cmd1,noact1,verbose1,recurse1,infiles1,outfile1,end1}; + int nerrors1; + + /* SYNTAX 2: remove [-nv] */ + struct arg_rex *cmd2 = arg_rex1(NULL, NULL, "remove", NULL, REG_ICASE, NULL); + struct arg_lit *noact2 = arg_lit0("n", NULL, NULL); + struct arg_lit *verbose2 = arg_lit0("v", "verbose", NULL); + struct arg_file *infiles2 = arg_file1(NULL, NULL, NULL, NULL); + struct arg_end *end2 = arg_end(20); + void* argtable2[] = {cmd2,noact2,verbose2,infiles2,end2}; + int nerrors2; + + /* SYNTAX 3: search [-v] [-o ] [--help] [--version] */ + struct arg_rex *cmd3 = arg_rex1(NULL, NULL, "search", NULL, REG_ICASE, NULL); + struct arg_lit *verbose3 = arg_lit0("v", "verbose", NULL); + struct arg_str *pattern3 = arg_str1(NULL, NULL, "", "search string"); + struct arg_file *outfile3 = arg_file0("o", NULL, "", NULL); + struct arg_end *end3 = arg_end(20); + void* argtable3[] = {cmd3,verbose3,pattern3,outfile3,end3}; + int nerrors3; + + /* SYNTAX 4: [-help] [-version] */ + struct arg_lit *help4 = arg_lit0(NULL,"help", "print this help and exit"); + struct arg_lit *version4 = arg_lit0(NULL,"version", "print version information and exit"); + struct arg_end *end4 = arg_end(20); + void* argtable4[] = {help4,version4,end4}; + int nerrors4; + + const char* progname = "multisyntax"; + int exitcode=0; + + /* verify all argtable[] entries were allocated sucessfully */ + if (arg_nullcheck(argtable1)!=0 || + arg_nullcheck(argtable2)!=0 || + arg_nullcheck(argtable3)!=0 || + arg_nullcheck(argtable4)!=0 ) + { + /* NULL entries were detected, some allocations must have failed */ + printf("%s: insufficient memory\n",progname); + exitcode=1; + goto exit; + } + + /* set any command line default values prior to parsing */ + outfile1->filename[0]="-"; + outfile3->filename[0]="-"; + + /* Above we defined a separate argtable for each possible command line syntax */ + /* and here we parse each one in turn to see if any of them are successful */ + nerrors1 = arg_parse(argc,argv,argtable1); + nerrors2 = arg_parse(argc,argv,argtable2); + nerrors3 = arg_parse(argc,argv,argtable3); + nerrors4 = arg_parse(argc,argv,argtable4); + + /* Execute the appropriate main routine for the matching command line syntax */ + /* In this example program our alternate command line syntaxes are mutually */ + /* exclusive, so we know in advance that only one of them can be successful. */ + if (nerrors1==0) + exitcode = mymain1(noact1->count, verbose1->count, recurse1->count, + outfile1->filename[0], infiles1->filename, infiles1->count); + else if (nerrors2==0) + exitcode = mymain2(noact2->count, verbose2->count, infiles2->filename[0]); + else if (nerrors3==0) + exitcode = mymain3(verbose3->count, pattern3->sval[0], outfile3->filename[0]); + else if (nerrors4==0) + exitcode = mymain4(help4->count, version4->count, progname, + argtable1, argtable2, argtable3, argtable4); + else + { + /* We get here if the command line matched none of the possible syntaxes */ + if (cmd1->count > 0) + { + /* here the cmd1 argument was correct, so presume syntax 1 was intended target */ + arg_print_errors(stdout,end1,progname); + printf("usage: %s ", progname); + arg_print_syntax(stdout,argtable1,"\n"); + } + else if (cmd2->count > 0) + { + /* here the cmd2 argument was correct, so presume syntax 2 was intended target */ + arg_print_errors(stdout,end2,progname); + printf("usage: %s ", progname); + arg_print_syntax(stdout,argtable2,"\n"); + } + else if (cmd3->count > 0) + { + /* here the cmd3 argument was correct, so presume syntax 3 was intended target */ + arg_print_errors(stdout,end3,progname); + printf("usage: %s ", progname); + arg_print_syntax(stdout,argtable3,"\n"); + } + else + { + /* no correct cmd literals were given, so we cant presume which syntax was intended */ + printf("%s: missing command.\n",progname); + printf("usage 1: %s ", progname); arg_print_syntax(stdout,argtable1,"\n"); + printf("usage 2: %s ", progname); arg_print_syntax(stdout,argtable2,"\n"); + printf("usage 3: %s ", progname); arg_print_syntax(stdout,argtable3,"\n"); + printf("usage 4: %s", progname); arg_print_syntax(stdout,argtable4,"\n"); + } + } + +exit: + /* deallocate each non-null entry in each argtable */ + arg_freetable(argtable1,sizeof(argtable1)/sizeof(argtable1[0])); + arg_freetable(argtable2,sizeof(argtable2)/sizeof(argtable2[0])); + arg_freetable(argtable3,sizeof(argtable3)/sizeof(argtable3[0])); + arg_freetable(argtable4,sizeof(argtable4)/sizeof(argtable4[0])); + + return exitcode; + } diff --git a/c/utf8filenamecheck/windows/argtable/examples/mv.c b/c/utf8filenamecheck/windows/argtable/examples/mv.c new file mode 100644 index 0000000..1381adf --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/examples/mv.c @@ -0,0 +1,179 @@ +/******************************************************************************* + * Example source code for using the argtable3 library to implement: + * + * mv [-bfiuv] [--backup=[CONTROL]] [--reply={yes,no,query}] + * [--strip-trailing-slashes] [-S SUFFIX] [--target-directory=DIRECTORY] + * [--help] [--version] SOURCE [SOURCE]... DEST|DIRECTORY + * + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +int mymain(const char *backup_control, + int backup, + int force, + int interactive, + const char *reply, + int strip_trailing_slashes, + const char *suffix, + const char *targetdir, + int update, + int verbose, + const char **files, + int nfiles) + { + int j; + + /* if verbose option was given then display all option settings */ + if (verbose) + { + printf("backup = %s\n", ((backup)?"YES":"NO")); + printf("backup CONTROL = %s\n", backup_control); + printf("force = %s\n", ((force)?"YES":"NO")); + printf("interactive mode = %s\n", ((interactive)?"YES":"NO")); + printf("reply = %s\n", reply); + printf("strip-trailing-slashes = %s\n", ((strip_trailing_slashes)?"YES":"NO")); + printf("suffix = %s\n", suffix); + printf("target-directory = %s\n", targetdir); + printf("update = %s\n", ((update)?"YES":"NO")); + printf("verbose = %s\n", ((verbose)?"YES":"NO")); + } + + /* print the source filenames */ + for (j=0; jsval[0] = "existing"; /* --backup={none,off,numbered,t,existing,nil,simple,never} */ + suffix->sval[0] = "~"; /* --suffix=~ */ + reply->sval[0] = "query"; /* --reply={yes,no,query} */ + targetd->sval[0] = NULL; + + /* Parse the command line as defined by argtable[] */ + nerrors = arg_parse(argc,argv,argtable); + + /* special case: '--help' takes precedence over error reporting */ + if (help->count > 0) + { + printf("Usage: %s", progname); + arg_print_syntax(stdout,argtable,"\n"); + printf("Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n\n"); + arg_print_glossary(stdout,argtable," %-30s %s\n"); + printf("\nThe backup suffix is \"~\", unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n" + "The version control method may be selected via the --backup option or through\n" + "the VERSION_CONTROL environment variable. Here are the values:\n\n" + " none, off never make backups (even if --backup is given)\n" + " numbered, t make numbered backups\n" + " existing, nil numbered if numbered backups exist, simple otherwise\n" + " simple, never always make simple backups\n\n" + "Report bugs to .\n"); + exitcode=0; + goto exit; + } + + /* special case: '--version' takes precedence error reporting */ + if (version->count > 0) + { + printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname); + printf("September 2003, Stewart Heitmann\n"); + exitcode=0; + goto exit; + } + + /* If the parser returned any errors then display them and exit */ + if (nerrors > 0) + { + /* Display the error details contained in the arg_end struct.*/ + arg_print_errors(stdout,end,progname); + printf("Try '%s --help' for more information.\n",progname); + exitcode=1; + goto exit; + } + + /* Command line parsing is complete, do the main processing */ + exitcode = mymain(backupc->sval[0], + backup->count, + force->count, + interact->count, + reply->sval[0], + strpslsh->count, + suffix->sval[0], + targetd->sval[0], + update->count, + verbose->count, + files->filename, + files->count); + +exit: + /* deallocate each non-null entry in argtable[] */ + arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); + + return exitcode; + } diff --git a/c/utf8filenamecheck/windows/argtable/examples/myprog.c b/c/utf8filenamecheck/windows/argtable/examples/myprog.c new file mode 100644 index 0000000..5a152ba --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/examples/myprog.c @@ -0,0 +1,147 @@ +/******************************************************************************* + * Example source code for using the argtable3 library to implement: + * + * myprog [-lRv] [-k ] [-D MACRO]... [-o ] [--help] + * [--version] []... + * + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include + +int mymain(int l, int R, int k, + const char **defines, int ndefines, + const char *outfile, + int v, + const char **infiles, int ninfiles) + { + int i; + + if (l>0) printf("list files (-l)\n"); + if (R>0) printf("recurse through directories (-R)\n"); + if (v>0) printf("verbose is enabled (-v)\n"); + printf("scalar k=%d\n",k); + printf("output is \"%s\"\n", outfile); + + for (i=0; i", "output file (default is \"-\")"); + struct arg_lit *verbose = arg_lit0("v","verbose,debug", "verbose messages"); + struct arg_lit *help = arg_lit0(NULL,"help", "print this help and exit"); + struct arg_lit *version = arg_lit0(NULL,"version", "print version information and exit"); + struct arg_file *infiles = arg_filen(NULL,NULL,NULL,1,argc+2, "input file(s)"); + struct arg_end *end = arg_end(20); + void* argtable[] = {list,recurse,repeat,defines,outfile,verbose,help,version,infiles,end}; + const char* progname = "myprog"; + int nerrors; + int exitcode=0; + + /* verify the argtable[] entries were allocated sucessfully */ + if (arg_nullcheck(argtable) != 0) + { + /* NULL entries were detected, some allocations must have failed */ + printf("%s: insufficient memory\n",progname); + exitcode=1; + goto exit; + } + + /* set any command line default values prior to parsing */ + repeat->ival[0]=3; + outfile->filename[0]="-"; + + /* Parse the command line as defined by argtable[] */ + nerrors = arg_parse(argc,argv,argtable); + + /* special case: '--help' takes precedence over error reporting */ + if (help->count > 0) + { + printf("Usage: %s", progname); + arg_print_syntax(stdout,argtable,"\n"); + printf("This program demonstrates the use of the argtable2 library\n"); + printf("for parsing command line arguments. Argtable accepts integers\n"); + printf("in decimal (123), hexadecimal (0xff), octal (0o123) and binary\n"); + printf("(0b101101) formats. Suffixes KB, MB and GB are also accepted.\n"); + arg_print_glossary(stdout,argtable," %-25s %s\n"); + exitcode=0; + goto exit; + } + + /* special case: '--version' takes precedence error reporting */ + if (version->count > 0) + { + printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname); + printf("September 2003, Stewart Heitmann\n"); + exitcode=0; + goto exit; + } + + /* If the parser returned any errors then display them and exit */ + if (nerrors > 0) + { + /* Display the error details contained in the arg_end struct.*/ + arg_print_errors(stdout,end,progname); + printf("Try '%s --help' for more information.\n",progname); + exitcode=1; + goto exit; + } + + /* special case: uname with no command line options induces brief help */ + if (argc==1) + { + printf("Try '%s --help' for more information.\n",progname); + exitcode=0; + goto exit; + } + + /* normal case: take the command line options at face value */ + exitcode = mymain(list->count, recurse->count, repeat->ival[0], + defines->sval, defines->count, + outfile->filename[0], verbose->count, + infiles->filename, infiles->count); + + exit: + /* deallocate each non-null entry in argtable[] */ + arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); + + return exitcode; + } diff --git a/c/utf8filenamecheck/windows/argtable/examples/myprog_C89.c b/c/utf8filenamecheck/windows/argtable/examples/myprog_C89.c new file mode 100644 index 0000000..b88d278 --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/examples/myprog_C89.c @@ -0,0 +1,165 @@ +/******************************************************************************* + * This example source code is an alternate version of myprog.c + * that adheres to ansi C89 standards rather than ansi C99. + * The only difference being that C89 does not permit the argtable array + * to be statically initialized with the contents of variables set at + * runtime whereas C99 does. + * Hence we cannot declare and initialize the argtable array in one declaration + * as + * void* argtable[] = {list, recurse, repeat, defines, outfile, verbose, + * help, version, infiles, end}; + * Instead, we must declare + * void* argtable[10]; + * and initialize the contents of the array separately. + * + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +int mymain(int l, int R, int k, + const char **defines, int ndefines, + const char *outfile, + int v, + const char **infiles, int ninfiles) + { + int i; + + if (l>0) printf("list files (-l)\n"); + if (R>0) printf("recurse through directories (-R)\n"); + if (v>0) printf("verbose is enabled (-v)\n"); + printf("scalar k=%d\n",k); + printf("output is \"%s\"\n", outfile); + + for (i=0; i", "output file (default is \"-\")"); + struct arg_lit *verbose = arg_lit0("v","verbose,debug", "verbose messages"); + struct arg_lit *help = arg_lit0(NULL,"help", "print this help and exit"); + struct arg_lit *version = arg_lit0(NULL,"version", "print version information and exit"); + struct arg_file *infiles = arg_filen(NULL,NULL,NULL,1,argc+2, "input file(s)"); + struct arg_end *end = arg_end(20); + void* argtable[10]; + const char* progname = "myprog_C89"; + int nerrors; + int exitcode=0; + + /* initialize the argtable array with ptrs to the arg_xxx structures constructed above */ + argtable[0] = list; + argtable[1] = recurse; + argtable[2] = repeat; + argtable[3] = defines; + argtable[4] = outfile; + argtable[5] = verbose; + argtable[6] = help; + argtable[7] = version; + argtable[8] = infiles; + argtable[9] = end; + + /* verify the argtable[] entries were allocated sucessfully */ + if (arg_nullcheck(argtable) != 0) + { + /* NULL entries were detected, some allocations must have failed */ + printf("%s: insufficient memory\n",progname); + exitcode=1; + goto exit; + } + + /* set any command line default values prior to parsing */ + repeat->ival[0]=3; + outfile->filename[0]="-"; + + /* Parse the command line as defined by argtable[] */ + nerrors = arg_parse(argc,argv,argtable); + + /* special case: '--help' takes precedence over error reporting */ + if (help->count > 0) + { + printf("Usage: %s", progname); + arg_print_syntax(stdout,argtable,"\n"); + printf("This program demonstrates the use of the argtable2 library\n"); + printf("for parsing command line arguments.\n"); + arg_print_glossary(stdout,argtable," %-25s %s\n"); + exitcode=0; + goto exit; + } + + /* special case: '--version' takes precedence error reporting */ + if (version->count > 0) + { + printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname); + printf("September 2003, Stewart Heitmann\n"); + exitcode=0; + goto exit; + } + + /* If the parser returned any errors then display them and exit */ + if (nerrors > 0) + { + /* Display the error details contained in the arg_end struct.*/ + arg_print_errors(stdout,end,progname); + printf("Try '%s --help' for more information.\n",progname); + exitcode=1; + goto exit; + } + + /* special case: uname with no command line options induces brief help */ + if (argc==1) + { + printf("Try '%s --help' for more information.\n",progname); + exitcode=0; + goto exit; + } + + /* normal case: take the command line options at face value */ + exitcode = mymain(list->count, recurse->count, repeat->ival[0], + defines->sval, defines->count, + outfile->filename[0], verbose->count, + infiles->filename, infiles->count); + + exit: + /* deallocate each non-null entry in argtable[] */ + arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); + + return exitcode; + } diff --git a/c/utf8filenamecheck/windows/argtable/examples/testargtable3.c b/c/utf8filenamecheck/windows/argtable/examples/testargtable3.c new file mode 100644 index 0000000..be19f3c --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/examples/testargtable3.c @@ -0,0 +1,58 @@ +#include "argtable3.h" + +/* global arg_xxx structs */ +struct arg_lit *a, *b, *c, *verb, *help, *version; +struct arg_int *scal; +struct arg_file *o, *file; +struct arg_end *end; + +int main(int argc, char *argv[]) +{ + /* the global arg_xxx structs are initialised within the argtable */ + void *argtable[] = { + help = arg_lit0(NULL, "help", "display this help and exit"), + version = arg_lit0(NULL, "version", "display version info and exit"), + a = arg_lit0("a", NULL,"the -a option"), + b = arg_lit0("b", NULL, "the -b option"), + c = arg_lit0("c", NULL, "the -c option"), + scal = arg_int0(NULL, "scalar", "", "foo value"), + verb = arg_lit0("v", "verbose", "verbose output"), + o = arg_file0("o", NULL, "myfile", "output file"), + file = arg_filen(NULL, NULL, "", 1, 100, "input files"), + end = arg_end(20), + }; + + int exitcode = 0; + char progname[] = "testargtable2.exe"; + + int nerrors; + nerrors = arg_parse(argc,argv,argtable); + + /* special case: '--help' takes precedence over error reporting */ + if (help->count > 0) + { + printf("Usage: %s", progname); + arg_print_syntax(stdout, argtable, "\n"); + printf("List information about the FILE(s) " + "(the current directory by default).\n\n"); + arg_print_glossary(stdout, argtable, " %-25s %s\n"); + exitcode = 0; + goto exit; + } + + /* If the parser returned any errors then display them and exit */ + if (nerrors > 0) + { + /* Display the error details contained in the arg_end struct.*/ + arg_print_errors(stdout, end, progname); + printf("Try '%s --help' for more information.\n", progname); + exitcode = 1; + goto exit; + } + +exit: + /* deallocate each non-null entry in argtable[] */ + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); + return exitcode; +} + diff --git a/c/utf8filenamecheck/windows/argtable/examples/uname.c b/c/utf8filenamecheck/windows/argtable/examples/uname.c new file mode 100644 index 0000000..ac3e40c --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/examples/uname.c @@ -0,0 +1,137 @@ +/******************************************************************************* + * Example source code for using the argtable3 library to implement: + * + * uname [-asnrvmpio] [--help] [--version] + * + * This file is part of the argtable3 library. + * + * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include "argtable3.h" + +/* Here we simulate the uname functionality */ +int mymain(int kname, int nname, int krel, int kver, int mach, int proc, int hard, int opsys) + { + if (kname) printf("Linux "); + if (nname) printf("localhost.localdomain "); + if (krel) printf("2.4.19-16 "); + if (kver) printf("#1 Fri Sep 20 18:15:05 CEST 2002 "); + if (mach) printf("i686 "); + if (proc) printf("Intel "); + if (hard) printf("unknown "); + if (opsys) printf("GNU/Linux "); + printf("\n"); + return 0; + } + + +int main(int argc, char **argv) + { + const char* progname = "uname"; + struct arg_lit *all = arg_lit0("a", "all", "print all information, in the following order:"); + struct arg_lit *kname = arg_lit0("s", "kernel-name", "print the kernel name"); + struct arg_lit *nname = arg_lit0("n", "nodename", "print the node name"); + struct arg_lit *krel = arg_lit0("r", "kernel-release", "print the kernel release"); + struct arg_lit *kver = arg_lit0("v", "kernel-version", "print the kernel version"); + struct arg_lit *mach = arg_lit0("m", "machine", "print the machine hardware name"); + struct arg_lit *proc = arg_lit0("p", "processor", "print the processor type"); + struct arg_lit *hard = arg_lit0("i", "hardware-platform","print the hardware platform"); + struct arg_lit *opsys = arg_lit0("o", "operating-system", "print the operating system"); + struct arg_lit *help = arg_lit0(NULL,"help", "print this help and exit"); + struct arg_lit *vers = arg_lit0(NULL,"version", "print version information and exit"); + struct arg_end *end = arg_end(20); + void* argtable[] = {all,kname,nname,krel,kver,mach,proc,hard,opsys,help,vers,end}; + int nerrors; + int exitcode=0; + + /* verify the argtable[] entries were allocated sucessfully */ + if (arg_nullcheck(argtable) != 0) + { + /* NULL entries were detected, some allocations must have failed */ + printf("%s: insufficient memory\n",progname); + exitcode=1; + goto exit; + } + + /* Parse the command line as defined by argtable[] */ + nerrors = arg_parse(argc,argv,argtable); + + /* special case: '--help' takes precedence over error reporting */ + if (help->count > 0) + { + printf("Usage: %s", progname); + arg_print_syntax(stdout,argtable,"\n"); + printf("Print certain system information. With no options, same as -s.\n\n"); + arg_print_glossary(stdout,argtable," %-25s %s\n"); + printf("\nReport bugs to .\n"); + exitcode=0; + goto exit; + } + + /* special case: '--version' takes precedence error reporting */ + if (vers->count > 0) + { + printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname); + printf("September 2003, Stewart Heitmann\n"); + exitcode=0; + goto exit; + } + + /* If the parser returned any errors then display them and exit */ + if (nerrors > 0) + { + /* Display the error details contained in the arg_end struct.*/ + arg_print_errors(stdout,end,progname); + printf("Try '%s --help' for more information.\n",progname); + exitcode=1; + goto exit; + } + + /* special case: uname with no command line options is equivalent to "uname -s" */ + if (argc==1) + { + exitcode = mymain(0,1,0,0,0,0,0,0); + goto exit; + } + + /* special case: "uname -a" is equivalent to "uname -snrvmpi" */ + if (all->count>0) + { + exitcode = mymain(1,1,1,1,1,1,1,1); + goto exit; + } + + /* normal case: take the command line options at face value */ + exitcode = mymain(kname->count, nname->count, krel->count, kver->count, mach->count, proc->count, hard->count, opsys->count); + + exit: + /* deallocate each non-null entry in argtable[] */ + arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0])); + + return exitcode; + } diff --git a/c/utf8filenamecheck/windows/argtable/tests/CMakeLists.txt b/c/utf8filenamecheck/windows/argtable/tests/CMakeLists.txt new file mode 100644 index 0000000..e4a553c --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/tests/CMakeLists.txt @@ -0,0 +1,89 @@ +################################################################################ +# This file is part of the argtable3 library. +# +# Copyright (C) 2016-2019 Tom G. Huang +# +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of STEWART HEITMANN nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, +# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +################################################################################ + +set(TEST_PUBLIC_SRC_FILES + testall.c + testarglit.c + testargstr.c + testargint.c + testargdate.c + testargdbl.c + testargfile.c + testargrex.c + testargdstr.c + testargcmd.c + CuTest.c +) + +set(TEST_SRC_FILES + ${TEST_PUBLIC_SRC_FILES} + testarghashtable.c +) + +if(UNIX) + set(ARGTABLE3_EXTRA_LIBS m) +endif() + +add_executable(test_shared ${TEST_PUBLIC_SRC_FILES}) +target_compile_definitions(test_shared PRIVATE -DARGTABLE3_TEST_PUBLIC_ONLY) +target_include_directories(test_shared PRIVATE ${PROJECT_SOURCE_DIR}/src) +target_link_libraries(test_shared argtable3 ${ARGTABLE3_EXTRA_LIBS}) +add_custom_command(TARGET test_shared POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "$" + "$" +) + +add_executable(test_static ${TEST_SRC_FILES}) +target_compile_definitions(test_static PRIVATE -DARGTABLE3_TEST_PUBLIC_ONLY) +target_include_directories(test_static PRIVATE ${PROJECT_SOURCE_DIR}/src) +target_link_libraries(test_static argtable3_static ${ARGTABLE3_EXTRA_LIBS}) + +add_executable(test_src ${TEST_SRC_FILES} ${ARGTABLE3_SRC_FILES}) +target_include_directories(test_src PRIVATE ${PROJECT_SOURCE_DIR}/src) +target_link_libraries(test_src ${ARGTABLE3_EXTRA_LIBS}) + +add_custom_command(OUTPUT ${ARGTABLE3_AMALGAMATION_SRC_FILE} + COMMAND "${PROJECT_SOURCE_DIR}/tools/build" dist + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/tools" +) + +add_executable(test_amalgamation ${TEST_SRC_FILES} ${ARGTABLE3_AMALGAMATION_SRC_FILE}) +target_include_directories(test_amalgamation PRIVATE ${PROJECT_SOURCE_DIR}/src) +target_link_libraries(test_amalgamation ${ARGTABLE3_EXTRA_LIBS}) +add_custom_command(TARGET test_amalgamation PRE_BUILD + COMMAND "${PROJECT_SOURCE_DIR}/tools/build" dist + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/tools" +) + +add_test(NAME test_shared COMMAND "$") +add_test(NAME test_static COMMAND "$") +add_test(NAME test_src COMMAND "$") +add_test(NAME test_amalgamation COMMAND "$") diff --git a/c/utf8filenamecheck/windows/argtable/tests/CuTest.c b/c/utf8filenamecheck/windows/argtable/tests/CuTest.c new file mode 100644 index 0000000..9d885a7 --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/tests/CuTest.c @@ -0,0 +1,325 @@ +#include +#include +#include +#include +#include +#include + +#include "CuTest.h" + +/*-------------------------------------------------------------------------* + * CuStr + *-------------------------------------------------------------------------*/ + +char* CuStrAlloc(size_t size) { + char* newStr = (char*)malloc(sizeof(char) * (size)); + return newStr; +} + +char* CuStrCopy(const char* old) { + size_t len = strlen(old); + char* newStr = CuStrAlloc(len + 1); +#if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) + strcpy_s(newStr, len + 1, old); +#else + strcpy(newStr, old); +#endif + + return newStr; +} + +/*-------------------------------------------------------------------------* + * CuString + *-------------------------------------------------------------------------*/ + +void CuStringInit(CuString* str) { + str->length = 0; + str->size = STRING_MAX; + str->buffer = (char*)malloc(sizeof(char) * str->size); + str->buffer[0] = '\0'; +} + +CuString* CuStringNew(void) { + CuString* str = (CuString*)malloc(sizeof(CuString)); + str->length = 0; + str->size = STRING_MAX; + str->buffer = (char*)malloc(sizeof(char) * str->size); + str->buffer[0] = '\0'; + return str; +} + +void CuStringDelete(CuString* str) { + if (!str) + return; + free(str->buffer); + free(str); +} + +void CuStringResize(CuString* str, size_t newSize) { + str->buffer = (char*)realloc(str->buffer, sizeof(char) * newSize); + str->size = newSize; +} + +void CuStringAppend(CuString* str, const char* text) { + size_t length; + + if (text == NULL) { + text = "NULL"; + } + + length = strlen(text); + if (str->length + length + 1 >= str->size) + CuStringResize(str, str->length + length + 1 + STRING_INC); + str->length += length; +#if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) + strcat_s(str->buffer, str->size, text); +#else + strcat(str->buffer, text); +#endif +} + +void CuStringAppendChar(CuString* str, char ch) { + char text[2]; + text[0] = ch; + text[1] = '\0'; + CuStringAppend(str, text); +} + +void CuStringAppendFormat(CuString* str, const char* format, ...) { + va_list argp; + char buf[HUGE_STRING_LEN]; + va_start(argp, format); +#if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) + vsprintf_s(buf, sizeof(buf), format, argp); +#else + vsprintf(buf, format, argp); +#endif + + va_end(argp); + CuStringAppend(str, buf); +} + +void CuStringInsert(CuString* str, const char* text, int pos) { + size_t length = strlen(text); + if ((size_t)pos > str->length) + pos = (int)str->length; + if (str->length + length + 1 >= str->size) + CuStringResize(str, str->length + length + 1 + STRING_INC); + memmove(str->buffer + pos + length, str->buffer + pos, (str->length - pos) + 1); + str->length += length; + memcpy(str->buffer + pos, text, length); +} + +/*-------------------------------------------------------------------------* + * CuTest + *-------------------------------------------------------------------------*/ + +void CuTestInit(CuTest* t, const char* name, TestFunction function) { + t->name = CuStrCopy(name); + t->failed = 0; + t->ran = 0; + t->message = NULL; + t->function = function; + t->jumpBuf = NULL; +} + +CuTest* CuTestNew(const char* name, TestFunction function) { + CuTest* tc = CU_ALLOC(CuTest); + CuTestInit(tc, name, function); + return tc; +} + +void CuTestDelete(CuTest* t) { + if (!t) + return; + free(t->name); + free(t); +} + +void CuTestRun(CuTest* tc) { + jmp_buf buf; + tc->jumpBuf = &buf; + if (setjmp(buf) == 0) { + tc->ran = 1; + (tc->function)(tc); + } + tc->jumpBuf = 0; +} + +static void CuFailInternal(CuTest* tc, const char* file, int line, CuString* string) { + char buf[HUGE_STRING_LEN]; + +#if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) + sprintf_s(buf, sizeof(buf), "%s:%d: ", file, line); +#else + sprintf(buf, "%s:%d: ", file, line); +#endif + CuStringInsert(string, buf, 0); + + tc->failed = 1; + tc->message = string->buffer; + if (tc->jumpBuf != 0) + longjmp(*(tc->jumpBuf), 0); +} + +void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message) { + CuString string; + + CuStringInit(&string); + if (message2 != NULL) { + CuStringAppend(&string, message2); + CuStringAppend(&string, ": "); + } + CuStringAppend(&string, message); + CuFailInternal(tc, file, line, &string); +} + +void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition) { + if (condition) + return; + CuFail_Line(tc, file, line, NULL, message); +} + +void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, const char* expected, const char* actual) { + CuString string; + if ((expected == NULL && actual == NULL) || (expected != NULL && actual != NULL && strcmp(expected, actual) == 0)) { + return; + } + + CuStringInit(&string); + if (message != NULL) { + CuStringAppend(&string, message); + CuStringAppend(&string, ": "); + } + CuStringAppend(&string, "expected <"); + CuStringAppend(&string, expected); + CuStringAppend(&string, "> but was <"); + CuStringAppend(&string, actual); + CuStringAppend(&string, ">"); + CuFailInternal(tc, file, line, &string); +} + +void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, int expected, int actual) { + char buf[STRING_MAX]; + if (expected == actual) + return; +#if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) + sprintf_s(buf, sizeof(buf), "expected <%d> but was <%d>", expected, actual); +#else + sprintf(buf, "expected <%d> but was <%d>", expected, actual); +#endif + CuFail_Line(tc, file, line, message, buf); +} + +void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, double expected, double actual, double delta) { + char buf[STRING_MAX]; + if (fabs(expected - actual) <= delta) + return; +#if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) + sprintf_s(buf, sizeof(buf), "expected <%f> but was <%f>", expected, actual); +#else + sprintf(buf, "expected <%f> but was <%f>", expected, actual); +#endif + CuFail_Line(tc, file, line, message, buf); +} + +void CuAssertPtrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, void* expected, void* actual) { + char buf[STRING_MAX]; + if (expected == actual) + return; +#if (defined(__STDC_LIB_EXT1__) && defined(__STDC_WANT_LIB_EXT1__)) || (defined(__STDC_SECURE_LIB__) && defined(__STDC_WANT_SECURE_LIB__)) + sprintf_s(buf, sizeof(buf), "expected pointer <0x%p> but was <0x%p>", expected, actual); +#else + sprintf(buf, "expected pointer <0x%p> but was <0x%p>", expected, actual); +#endif + CuFail_Line(tc, file, line, message, buf); +} + +/*-------------------------------------------------------------------------* + * CuSuite + *-------------------------------------------------------------------------*/ + +void CuSuiteInit(CuSuite* testSuite) { + testSuite->count = 0; + testSuite->failCount = 0; + memset(testSuite->list, 0, sizeof(testSuite->list)); +} + +CuSuite* CuSuiteNew(void) { + CuSuite* testSuite = CU_ALLOC(CuSuite); + CuSuiteInit(testSuite); + return testSuite; +} + +void CuSuiteDelete(CuSuite* testSuite) { + unsigned int n; + for (n = 0; n < MAX_TEST_CASES; n++) { + if (testSuite->list[n]) { + CuTestDelete(testSuite->list[n]); + } + } + free(testSuite); +} + +void CuSuiteAdd(CuSuite* testSuite, CuTest* testCase) { + assert(testSuite->count < MAX_TEST_CASES); + testSuite->list[testSuite->count] = testCase; + testSuite->count++; +} + +void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2) { + int i; + for (i = 0; i < testSuite2->count; ++i) { + CuTest* testCase = testSuite2->list[i]; + CuSuiteAdd(testSuite, testCase); + } +} + +void CuSuiteRun(CuSuite* testSuite) { + int i; + for (i = 0; i < testSuite->count; ++i) { + CuTest* testCase = testSuite->list[i]; + CuTestRun(testCase); + if (testCase->failed) { + testSuite->failCount += 1; + } + } +} + +void CuSuiteSummary(CuSuite* testSuite, CuString* summary) { + int i; + for (i = 0; i < testSuite->count; ++i) { + CuTest* testCase = testSuite->list[i]; + CuStringAppend(summary, testCase->failed ? "F" : "."); + } + CuStringAppend(summary, "\n\n"); +} + +void CuSuiteDetails(CuSuite* testSuite, CuString* details) { + int i; + int failCount = 0; + + if (testSuite->failCount == 0) { + int passCount = testSuite->count - testSuite->failCount; + const char* testWord = passCount == 1 ? "test" : "tests"; + CuStringAppendFormat(details, "OK (%d %s)\n", passCount, testWord); + } else { + if (testSuite->failCount == 1) + CuStringAppend(details, "There was 1 failure:\n"); + else + CuStringAppendFormat(details, "There were %d failures:\n", testSuite->failCount); + + for (i = 0; i < testSuite->count; ++i) { + CuTest* testCase = testSuite->list[i]; + if (testCase->failed) { + failCount++; + CuStringAppendFormat(details, "%d) %s: %s\n", failCount, testCase->name, testCase->message); + } + } + CuStringAppend(details, "\n!!!FAILURES!!!\n"); + + CuStringAppendFormat(details, "Runs: %d ", testSuite->count); + CuStringAppendFormat(details, "Passes: %d ", testSuite->count - testSuite->failCount); + CuStringAppendFormat(details, "Fails: %d\n", testSuite->failCount); + } +} diff --git a/c/utf8filenamecheck/windows/argtable/tests/CuTest.h b/c/utf8filenamecheck/windows/argtable/tests/CuTest.h new file mode 100644 index 0000000..671cbcf --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/tests/CuTest.h @@ -0,0 +1,104 @@ +#ifndef CU_TEST_H +#define CU_TEST_H + +#include +#include + +#define CUTEST_VERSION "CuTest 1.5" + +/* CuString */ + +char* CuStrAlloc(size_t size); +char* CuStrCopy(const char* old); + +#define CU_ALLOC(TYPE) ((TYPE*)malloc(sizeof(TYPE))) + +#define HUGE_STRING_LEN 8192 +#define STRING_MAX 256 +#define STRING_INC 256 + +typedef struct { + size_t length; + size_t size; + char* buffer; +} CuString; + +void CuStringInit(CuString* str); +CuString* CuStringNew(void); +void CuStringRead(CuString* str, const char* path); +void CuStringAppend(CuString* str, const char* text); +void CuStringAppendChar(CuString* str, char ch); +void CuStringAppendFormat(CuString* str, const char* format, ...); +void CuStringInsert(CuString* str, const char* text, int pos); +void CuStringResize(CuString* str, size_t newSize); +void CuStringDelete(CuString* str); + +/* CuTest */ + +typedef struct CuTest CuTest; + +typedef void (*TestFunction)(CuTest*); + +struct CuTest { + char* name; + TestFunction function; + int failed; + int ran; + const char* message; + jmp_buf* jumpBuf; +}; + +void CuTestInit(CuTest* t, const char* name, TestFunction function); +CuTest* CuTestNew(const char* name, TestFunction function); +void CuTestRun(CuTest* tc); +void CuTestDelete(CuTest* t); + +/* Internal versions of assert functions -- use the public versions */ +void CuFail_Line(CuTest* tc, const char* file, int line, const char* message2, const char* message); +void CuAssert_Line(CuTest* tc, const char* file, int line, const char* message, int condition); +void CuAssertStrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, const char* expected, const char* actual); +void CuAssertIntEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, int expected, int actual); +void CuAssertDblEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, double expected, double actual, double delta); +void CuAssertPtrEquals_LineMsg(CuTest* tc, const char* file, int line, const char* message, void* expected, void* actual); + +/* public assert functions */ + +#define CuFail(tc, ms) CuFail_Line((tc), __FILE__, __LINE__, NULL, (ms)) +#define CuAssert(tc, ms, cond) CuAssert_Line((tc), __FILE__, __LINE__, (ms), (cond)) +#define CuAssertTrue(tc, cond) CuAssert_Line((tc), __FILE__, __LINE__, "assert failed", (cond)) + +#define CuAssertStrEquals(tc, ex, ac) CuAssertStrEquals_LineMsg((tc), __FILE__, __LINE__, NULL, (ex), (ac)) +#define CuAssertStrEquals_Msg(tc, ms, ex, ac) CuAssertStrEquals_LineMsg((tc), __FILE__, __LINE__, (ms), (ex), (ac)) +#define CuAssertIntEquals(tc, ex, ac) CuAssertIntEquals_LineMsg((tc), __FILE__, __LINE__, NULL, (ex), (ac)) +#define CuAssertIntEquals_Msg(tc, ms, ex, ac) CuAssertIntEquals_LineMsg((tc), __FILE__, __LINE__, (ms), (ex), (ac)) +#define CuAssertDblEquals(tc, ex, ac, dl) CuAssertDblEquals_LineMsg((tc), __FILE__, __LINE__, NULL, (ex), (ac), (dl)) +#define CuAssertDblEquals_Msg(tc, ms, ex, ac, dl) CuAssertDblEquals_LineMsg((tc), __FILE__, __LINE__, (ms), (ex), (ac), (dl)) +#define CuAssertPtrEquals(tc, ex, ac) CuAssertPtrEquals_LineMsg((tc), __FILE__, __LINE__, NULL, (ex), (ac)) +#define CuAssertPtrEquals_Msg(tc, ms, ex, ac) CuAssertPtrEquals_LineMsg((tc), __FILE__, __LINE__, (ms), (ex), (ac)) + +#define CuAssertPtrNotNull(tc, p) CuAssert_Line((tc), __FILE__, __LINE__, "null pointer unexpected", (p != NULL)) +#define CuAssertPtrNotNullMsg(tc, msg, p) CuAssert_Line((tc), __FILE__, __LINE__, (msg), (p != NULL)) + +/* CuSuite */ + +#define MAX_TEST_CASES 1024 + +#define SUITE_ADD_TEST(SUITE, TEST) CuSuiteAdd(SUITE, CuTestNew(#TEST, TEST)) + +typedef struct { + int count; + CuTest* list[MAX_TEST_CASES]; + int failCount; + +} CuSuite; + +void CuSuiteInit(CuSuite* testSuite); +CuSuite* CuSuiteNew(void); +void CuSuiteDelete(CuSuite* testSuite); +void CuSuiteAdd(CuSuite* testSuite, CuTest* testCase); +void CuSuiteAddSuite(CuSuite* testSuite, CuSuite* testSuite2); +void CuSuiteRun(CuSuite* testSuite); +void CuSuiteSummary(CuSuite* testSuite, CuString* summary); +void CuSuiteDetails(CuSuite* testSuite, CuString* details); + +#endif /* CU_TEST_H */ diff --git a/c/utf8filenamecheck/windows/argtable/tests/argtable3_private.h b/c/utf8filenamecheck/windows/argtable/tests/argtable3_private.h new file mode 100644 index 0000000..ad61cfa --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/tests/argtable3_private.h @@ -0,0 +1,230 @@ +/******************************************************************************* + * argtable3_private: Declares private types, constants, and interfaces + * + * This file is part of the argtable3 library. + * + * Copyright (C) 2013-2019 Tom G. Huang + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#ifndef ARG_UTILS_H +#define ARG_UTILS_H + +#include + +#define ARG_ENABLE_TRACE 0 +#define ARG_ENABLE_LOG 1 + +#ifdef __cplusplus +extern "C" { +#endif + +enum { ARG_ERR_MINCOUNT = 1, ARG_ERR_MAXCOUNT, ARG_ERR_BADINT, ARG_ERR_OVERFLOW, ARG_ERR_BADDOUBLE, ARG_ERR_BADDATE, ARG_ERR_REGNOMATCH }; + +typedef void(arg_panicfn)(const char* fmt, ...); + +#if defined(_MSC_VER) +#define ARG_TRACE(x) \ + __pragma(warning(push)) __pragma(warning(disable : 4127)) do { \ + if (ARG_ENABLE_TRACE) \ + dbg_printf x; \ + } \ + while (0) \ + __pragma(warning(pop)) + +#define ARG_LOG(x) \ + __pragma(warning(push)) __pragma(warning(disable : 4127)) do { \ + if (ARG_ENABLE_LOG) \ + dbg_printf x; \ + } \ + while (0) \ + __pragma(warning(pop)) +#else +#define ARG_TRACE(x) \ + do { \ + if (ARG_ENABLE_TRACE) \ + dbg_printf x; \ + } while (0) + +#define ARG_LOG(x) \ + do { \ + if (ARG_ENABLE_LOG) \ + dbg_printf x; \ + } while (0) +#endif + +extern void dbg_printf(const char* fmt, ...); +extern void arg_set_panic(arg_panicfn* proc); +extern void* xmalloc(size_t size); +extern void* xcalloc(size_t count, size_t size); +extern void* xrealloc(void* ptr, size_t size); +extern void xfree(void* ptr); + +struct arg_hashtable_entry { + void *k, *v; + unsigned int h; + struct arg_hashtable_entry* next; +}; + +typedef struct arg_hashtable { + unsigned int tablelength; + struct arg_hashtable_entry** table; + unsigned int entrycount; + unsigned int loadlimit; + unsigned int primeindex; + unsigned int (*hashfn)(const void* k); + int (*eqfn)(const void* k1, const void* k2); +} arg_hashtable_t; + +/** + * @brief Create a hash table. + * + * @param minsize minimum initial size of hash table + * @param hashfn function for hashing keys + * @param eqfn function for determining key equality + * @return newly created hash table or NULL on failure + */ +arg_hashtable_t* arg_hashtable_create(unsigned int minsize, unsigned int (*hashfn)(const void*), int (*eqfn)(const void*, const void*)); + +/** + * @brief This function will cause the table to expand if the insertion would take + * the ratio of entries to table size over the maximum load factor. + * + * This function does not check for repeated insertions with a duplicate key. + * The value returned when using a duplicate key is undefined -- when + * the hash table changes size, the order of retrieval of duplicate key + * entries is reversed. + * If in doubt, remove before insert. + * + * @param h the hash table to insert into + * @param k the key - hash table claims ownership and will free on removal + * @param v the value - does not claim ownership + * @return non-zero for successful insertion + */ +void arg_hashtable_insert(arg_hashtable_t* h, void* k, void* v); + +#define ARG_DEFINE_HASHTABLE_INSERT(fnname, keytype, valuetype) \ + int fnname(arg_hashtable_t* h, keytype* k, valuetype* v) { return arg_hashtable_insert(h, k, v); } + +/** + * @brief Search the specified key in the hash table. + * + * @param h the hash table to search + * @param k the key to search for - does not claim ownership + * @return the value associated with the key, or NULL if none found + */ +void* arg_hashtable_search(arg_hashtable_t* h, const void* k); + +#define ARG_DEFINE_HASHTABLE_SEARCH(fnname, keytype, valuetype) \ + valuetype* fnname(arg_hashtable_t* h, keytype* k) { return (valuetype*)(arg_hashtable_search(h, k)); } + +/** + * @brief Remove the specified key from the hash table. + * + * @param h the hash table to remove the item from + * @param k the key to search for - does not claim ownership + */ +void arg_hashtable_remove(arg_hashtable_t* h, const void* k); + +#define ARG_DEFINE_HASHTABLE_REMOVE(fnname, keytype, valuetype) \ + valuetype* fnname(arg_hashtable_t* h, keytype* k) { return (valuetype*)(arg_hashtable_remove(h, k)); } + +/** + * @brief Return the number of keys in the hash table. + * + * @param h the hash table + * @return the number of items stored in the hash table + */ +unsigned int arg_hashtable_count(arg_hashtable_t* h); + +/** + * @brief Change the value associated with the key. + * + * function to change the value associated with a key, where there already + * exists a value bound to the key in the hash table. + * Source due to Holger Schemel. + * + * @name hashtable_change + * @param h the hash table + * @param key + * @param value + */ +int arg_hashtable_change(arg_hashtable_t* h, void* k, void* v); + +/** + * @brief Free the hash table and the memory allocated for each key-value pair. + * + * @param h the hash table + * @param free_values whether to call 'free' on the remaining values + */ +void arg_hashtable_destroy(arg_hashtable_t* h, int free_values); + +typedef struct arg_hashtable_itr { + arg_hashtable_t* h; + struct arg_hashtable_entry* e; + struct arg_hashtable_entry* parent; + unsigned int index; +} arg_hashtable_itr_t; + +arg_hashtable_itr_t* arg_hashtable_itr_create(arg_hashtable_t* h); + +void arg_hashtable_itr_destroy(arg_hashtable_itr_t* itr); + +/** + * @brief Return the value of the (key,value) pair at the current position. + */ +extern void* arg_hashtable_itr_key(arg_hashtable_itr_t* i); + +/** + * @brief Return the value of the (key,value) pair at the current position. + */ +extern void* arg_hashtable_itr_value(arg_hashtable_itr_t* i); + +/** + * @brief Advance the iterator to the next element. Returns zero if advanced to end of table. + */ +int arg_hashtable_itr_advance(arg_hashtable_itr_t* itr); + +/** + * @brief Remove current element and advance the iterator to the next element. + */ +int arg_hashtable_itr_remove(arg_hashtable_itr_t* itr); + +/** + * @brief Search and overwrite the supplied iterator, to point to the entry matching the supplied key. + * + * @return Zero if not found. + */ +int arg_hashtable_itr_search(arg_hashtable_itr_t* itr, arg_hashtable_t* h, void* k); + +#define ARG_DEFINE_HASHTABLE_ITERATOR_SEARCH(fnname, keytype) \ + int fnname(arg_hashtable_itr_t* i, arg_hashtable_t* h, keytype* k) { return (arg_hashtable_iterator_search(i, h, k)); } + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/c/utf8filenamecheck/windows/argtable/tests/testall.c b/c/utf8filenamecheck/windows/argtable/tests/testall.c new file mode 100644 index 0000000..8f4ed6f --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/tests/testall.c @@ -0,0 +1,76 @@ +/******************************************************************************* + * This file is part of the argtable3 library. + * + * Copyright (C) 2013-2019 Tom G. Huang + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include + +#include "CuTest.h" + +CuSuite* get_arglit_testsuite(); +CuSuite* get_argstr_testsuite(); +CuSuite* get_argint_testsuite(); +CuSuite* get_argdate_testsuite(); +CuSuite* get_argdbl_testsuite(); +CuSuite* get_argfile_testsuite(); +CuSuite* get_argrex_testsuite(); +CuSuite* get_argdstr_testsuite(); +CuSuite* get_argcmd_testsuite(); + +#ifndef ARGTABLE3_TEST_PUBLIC_ONLY +CuSuite* get_arghashtable_testsuite(); +#endif + +int RunAllTests(void) { + CuString* output = CuStringNew(); + CuSuite* suite = CuSuiteNew(); + + CuSuiteAddSuite(suite, get_arglit_testsuite()); + CuSuiteAddSuite(suite, get_argstr_testsuite()); + CuSuiteAddSuite(suite, get_argint_testsuite()); + CuSuiteAddSuite(suite, get_argdate_testsuite()); + CuSuiteAddSuite(suite, get_argdbl_testsuite()); + CuSuiteAddSuite(suite, get_argfile_testsuite()); + CuSuiteAddSuite(suite, get_argrex_testsuite()); + CuSuiteAddSuite(suite, get_argdstr_testsuite()); + CuSuiteAddSuite(suite, get_argcmd_testsuite()); +#ifndef ARGTABLE3_TEST_PUBLIC_ONLY + CuSuiteAddSuite(suite, get_arghashtable_testsuite()); +#endif + + CuSuiteRun(suite); + CuSuiteSummary(suite, output); + CuSuiteDetails(suite, output); + printf("%s\n", output->buffer); + + return suite->failCount; +} + +int main(void) { + return RunAllTests(); +} diff --git a/c/utf8filenamecheck/windows/argtable/tests/testargcmd.c b/c/utf8filenamecheck/windows/argtable/tests/testargcmd.c new file mode 100644 index 0000000..5ec0836 --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/tests/testargcmd.c @@ -0,0 +1,92 @@ +/******************************************************************************* + * This file is part of the argtable3 library. + * + * Copyright (C) 2013-2019 Tom G. Huang + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include +#include + +#include + +#include "CuTest.h" +#include "argtable3.h" + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4204) +#endif + +int cmd1_proc(int argc, char* argv[], arg_dstr_t res) { + if (argc == 0) { + arg_dstr_catf(res, "cmd1 fail"); + return 1; + } + + arg_dstr_catf(res, "%d %s", argc, argv[0]); + return 0; +} + +void test_argcmd_basic_001(CuTest* tc) { + arg_cmd_init(); + CuAssertIntEquals(tc, 0, arg_cmd_count()); + + arg_cmd_register("cmd1", cmd1_proc, "description of cmd1"); + CuAssertIntEquals(tc, 1, arg_cmd_count()); + + char* argv[] = { + "cmd1", + "-o", + "file1", + }; + int argc = 3; + CuAssertTrue(tc, strcmp(argv[0], "cmd1") == 0); + CuAssertTrue(tc, strcmp(argv[1], "-o") == 0); + CuAssertTrue(tc, strcmp(argv[2], "file1") == 0); + + arg_dstr_t res = arg_dstr_create(); + int err = arg_cmd_dispatch("cmd1", argc, argv, res); + CuAssertIntEquals(tc, 0, err); + CuAssertTrue(tc, strcmp(arg_dstr_cstr(res), "3 cmd1") == 0); + + arg_dstr_reset(res); + err = arg_cmd_dispatch("cmd1", 0, NULL, res); + CuAssertIntEquals(tc, 1, err); + CuAssertTrue(tc, strcmp(arg_dstr_cstr(res), "cmd1 fail") == 0); + + arg_cmd_uninit(); +} + +CuSuite* get_argcmd_testsuite() { + CuSuite* suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_argcmd_basic_001); + return suite; +} + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif diff --git a/c/utf8filenamecheck/windows/argtable/tests/testargdate.c b/c/utf8filenamecheck/windows/argtable/tests/testargdate.c new file mode 100644 index 0000000..3b4f5a0 --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/tests/testargdate.c @@ -0,0 +1,377 @@ +/******************************************************************************* + * This file is part of the argtable3 library. + * + * Copyright (C) 2013-2019 Tom G. Huang + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include +#include + +#include "CuTest.h" +#include "argtable3.h" + +/* + printf("tm_sec = %d\n", c->tmval->tm_sec); + printf("tm_min = %d\n", c->tmval->tm_min); + printf("tm_hour = %d\n", c->tmval->tm_hour); + printf("tm_mday = %d\n", c->tmval->tm_mday); + printf("tm_mon = %d\n", c->tmval->tm_mon); + printf("tm_year = %d\n", c->tmval->tm_year); + printf("tm_wday = %d\n", c->tmval->tm_wday); + printf("tm_yday = %d\n", c->tmval->tm_yday); + printf("tm_isdst = %d\n", c->tmval->tm_isdst); + +*/ + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4204) +#endif + +void test_argdate_basic_001(CuTest* tc) { + struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); + struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); + struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, end}; + int nerrors; + + char* argv[] = {"program", "23:59", "--date", "12/31/04", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertIntEquals(tc, a->tmval->tm_sec, 0); + CuAssertIntEquals(tc, a->tmval->tm_min, 59); + CuAssertIntEquals(tc, a->tmval->tm_hour, 23); + CuAssertIntEquals(tc, a->tmval->tm_mday, 0); + CuAssertIntEquals(tc, a->tmval->tm_mon, 0); + CuAssertIntEquals(tc, a->tmval->tm_year, 0); + CuAssertIntEquals(tc, a->tmval->tm_wday, 0); + CuAssertIntEquals(tc, a->tmval->tm_yday, 0); + CuAssertIntEquals(tc, a->tmval->tm_isdst, 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertIntEquals(tc, c->tmval->tm_sec, 0); + CuAssertIntEquals(tc, c->tmval->tm_min, 0); + CuAssertIntEquals(tc, c->tmval->tm_hour, 0); + CuAssertIntEquals(tc, c->tmval->tm_mday, 31); + CuAssertIntEquals(tc, c->tmval->tm_mon, 11); + CuAssertIntEquals(tc, c->tmval->tm_year, 104); + CuAssertIntEquals(tc, c->tmval->tm_wday, 0); + CuAssertIntEquals(tc, c->tmval->tm_yday, 0); + CuAssertIntEquals(tc, c->tmval->tm_isdst, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdate_basic_002(CuTest* tc) { + struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); + struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); + struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, end}; + int nerrors; + + char* argv[] = {"program", "--date", "12/31/04", "20:15", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertIntEquals(tc, a->tmval->tm_sec, 0); + CuAssertIntEquals(tc, a->tmval->tm_min, 15); + CuAssertIntEquals(tc, a->tmval->tm_hour, 20); + CuAssertIntEquals(tc, a->tmval->tm_mday, 0); + CuAssertIntEquals(tc, a->tmval->tm_mon, 0); + CuAssertIntEquals(tc, a->tmval->tm_year, 0); + CuAssertIntEquals(tc, a->tmval->tm_wday, 0); + CuAssertIntEquals(tc, a->tmval->tm_yday, 0); + CuAssertIntEquals(tc, a->tmval->tm_isdst, 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertIntEquals(tc, c->tmval->tm_sec, 0); + CuAssertIntEquals(tc, c->tmval->tm_min, 0); + CuAssertIntEquals(tc, c->tmval->tm_hour, 0); + CuAssertIntEquals(tc, c->tmval->tm_mday, 31); + CuAssertIntEquals(tc, c->tmval->tm_mon, 11); + CuAssertIntEquals(tc, c->tmval->tm_year, 104); + CuAssertIntEquals(tc, c->tmval->tm_wday, 0); + CuAssertIntEquals(tc, c->tmval->tm_yday, 0); + CuAssertIntEquals(tc, c->tmval->tm_isdst, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdate_basic_003(CuTest* tc) { + struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); + struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); + struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, end}; + int nerrors; + + char* argv[] = {"program", "--date", "12/31/04", "20:15", "--date", "06/07/84", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertIntEquals(tc, a->tmval->tm_sec, 0); + CuAssertIntEquals(tc, a->tmval->tm_min, 15); + CuAssertIntEquals(tc, a->tmval->tm_hour, 20); + CuAssertIntEquals(tc, a->tmval->tm_mday, 0); + CuAssertIntEquals(tc, a->tmval->tm_mon, 0); + CuAssertIntEquals(tc, a->tmval->tm_year, 0); + CuAssertIntEquals(tc, a->tmval->tm_wday, 0); + CuAssertIntEquals(tc, a->tmval->tm_yday, 0); + CuAssertIntEquals(tc, a->tmval->tm_isdst, 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 2); + CuAssertIntEquals(tc, c->tmval->tm_sec, 0); + CuAssertIntEquals(tc, c->tmval->tm_min, 0); + CuAssertIntEquals(tc, c->tmval->tm_hour, 0); + CuAssertIntEquals(tc, c->tmval->tm_mday, 31); + CuAssertIntEquals(tc, c->tmval->tm_mon, 11); + CuAssertIntEquals(tc, c->tmval->tm_year, 104); + CuAssertIntEquals(tc, c->tmval->tm_wday, 0); + CuAssertIntEquals(tc, c->tmval->tm_yday, 0); + CuAssertIntEquals(tc, c->tmval->tm_isdst, 0); + CuAssertIntEquals(tc, (c->tmval + 1)->tm_sec, 0); + CuAssertIntEquals(tc, (c->tmval + 1)->tm_min, 0); + CuAssertIntEquals(tc, (c->tmval + 1)->tm_hour, 0); + CuAssertIntEquals(tc, (c->tmval + 1)->tm_mday, 7); + CuAssertIntEquals(tc, (c->tmval + 1)->tm_mon, 5); + CuAssertIntEquals(tc, (c->tmval + 1)->tm_year, 84); + CuAssertIntEquals(tc, (c->tmval + 1)->tm_wday, 0); + CuAssertIntEquals(tc, (c->tmval + 1)->tm_yday, 0); + CuAssertIntEquals(tc, (c->tmval + 1)->tm_isdst, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdate_basic_004(CuTest* tc) { + struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); + struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); + struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, end}; + int nerrors; + + char* argv[] = {"program", "--date", "12/31/04", "20:15", "-b", "1982-11-28", "--date", "06/07/84", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertIntEquals(tc, a->tmval->tm_sec, 0); + CuAssertIntEquals(tc, a->tmval->tm_min, 15); + CuAssertIntEquals(tc, a->tmval->tm_hour, 20); + CuAssertIntEquals(tc, a->tmval->tm_mday, 0); + CuAssertIntEquals(tc, a->tmval->tm_mon, 0); + CuAssertIntEquals(tc, a->tmval->tm_year, 0); + CuAssertIntEquals(tc, a->tmval->tm_wday, 0); + CuAssertIntEquals(tc, a->tmval->tm_yday, 0); + CuAssertIntEquals(tc, a->tmval->tm_isdst, 0); + CuAssertTrue(tc, b->count == 1); + CuAssertIntEquals(tc, b->tmval->tm_sec, 0); + CuAssertIntEquals(tc, b->tmval->tm_min, 0); + CuAssertIntEquals(tc, b->tmval->tm_hour, 0); + CuAssertIntEquals(tc, b->tmval->tm_mday, 28); + CuAssertIntEquals(tc, b->tmval->tm_mon, 10); + CuAssertIntEquals(tc, b->tmval->tm_year, 82); + CuAssertIntEquals(tc, b->tmval->tm_wday, 0); + CuAssertIntEquals(tc, b->tmval->tm_yday, 0); + CuAssertIntEquals(tc, b->tmval->tm_isdst, 0); + CuAssertTrue(tc, c->count == 2); + CuAssertIntEquals(tc, c->tmval->tm_sec, 0); + CuAssertIntEquals(tc, c->tmval->tm_min, 0); + CuAssertIntEquals(tc, c->tmval->tm_hour, 0); + CuAssertIntEquals(tc, c->tmval->tm_mday, 31); + CuAssertIntEquals(tc, c->tmval->tm_mon, 11); + CuAssertIntEquals(tc, c->tmval->tm_year, 104); + CuAssertIntEquals(tc, c->tmval->tm_wday, 0); + CuAssertIntEquals(tc, c->tmval->tm_yday, 0); + CuAssertIntEquals(tc, c->tmval->tm_isdst, 0); + CuAssertIntEquals(tc, (c->tmval + 1)->tm_sec, 0); + CuAssertIntEquals(tc, (c->tmval + 1)->tm_min, 0); + CuAssertIntEquals(tc, (c->tmval + 1)->tm_hour, 0); + CuAssertIntEquals(tc, (c->tmval + 1)->tm_mday, 7); + CuAssertIntEquals(tc, (c->tmval + 1)->tm_mon, 5); + CuAssertIntEquals(tc, (c->tmval + 1)->tm_year, 84); + CuAssertIntEquals(tc, (c->tmval + 1)->tm_wday, 0); + CuAssertIntEquals(tc, (c->tmval + 1)->tm_yday, 0); + CuAssertIntEquals(tc, (c->tmval + 1)->tm_isdst, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdate_basic_005(CuTest* tc) { + struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); + struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); + struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, end}; + int nerrors; + + char* argv[] = {"program", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 2); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdate_basic_006(CuTest* tc) { + struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); + struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); + struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, end}; + int nerrors; + + char* argv[] = {"program", "25:59", "--date", "12/31/04", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdate_basic_007(CuTest* tc) { + struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); + struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); + struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, end}; + int nerrors; + + char* argv[] = {"program", "23:59", "--date", "12/32/04", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdate_basic_008(CuTest* tc) { + struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); + struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); + struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, end}; + int nerrors; + + char* argv[] = {"program", "23:59", "--date", "12/31/04", "22:58", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdate_basic_009(CuTest* tc) { + struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); + struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); + struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, end}; + int nerrors; + + char* argv[] = {"program", "--date", "12/31/04", "20:15", "--date", "26/07/84", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdate_basic_010(CuTest* tc) { + struct arg_date* a = arg_date1(NULL, NULL, "%H:%M", NULL, "time 23:59"); + struct arg_date* b = arg_date0("b", NULL, "%Y-%m-%d", NULL, "date YYYY-MM-DD"); + struct arg_date* c = arg_daten(NULL, "date", "%D", NULL, 1, 2, "MM/DD/YY"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, end}; + int nerrors; + + char* argv[] = {"program", "-b", "1982-11-28", "-b", "1976-11-11", "--date", "12/07/84", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +CuSuite* get_argdate_testsuite() { + CuSuite* suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_argdate_basic_001); + SUITE_ADD_TEST(suite, test_argdate_basic_002); + SUITE_ADD_TEST(suite, test_argdate_basic_003); + SUITE_ADD_TEST(suite, test_argdate_basic_004); + SUITE_ADD_TEST(suite, test_argdate_basic_005); + SUITE_ADD_TEST(suite, test_argdate_basic_006); + SUITE_ADD_TEST(suite, test_argdate_basic_007); + SUITE_ADD_TEST(suite, test_argdate_basic_008); + SUITE_ADD_TEST(suite, test_argdate_basic_009); + SUITE_ADD_TEST(suite, test_argdate_basic_010); + return suite; +} + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif diff --git a/c/utf8filenamecheck/windows/argtable/tests/testargdbl.c b/c/utf8filenamecheck/windows/argtable/tests/testargdbl.c new file mode 100644 index 0000000..9271cae --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/tests/testargdbl.c @@ -0,0 +1,451 @@ +/******************************************************************************* + * This file is part of the argtable3 library. + * + * Copyright (C) 2013-2019 Tom G. Huang + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include +#include + +#include "CuTest.h" +#include "argtable3.h" + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4204) +#endif + +void test_argdbl_basic_001(CuTest* tc) { + struct arg_dbl* a = arg_dbl1(NULL, NULL, "a", "a is "); + struct arg_dbl* b = arg_dbl0(NULL, NULL, "b", "b is "); + struct arg_dbl* c = arg_dbl0(NULL, NULL, "c", "c is "); + struct arg_dbl* d = arg_dbln("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_dbl* e = arg_dbl0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, end}; + int nerrors; + + char* argv[] = {"program", "0", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 0); + + CuAssertTrue(tc, a->count == 1); + CuAssertDblEquals(tc, a->dval[0], 0, DBL_EPSILON); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 0); + CuAssertTrue(tc, d->count == 0); + CuAssertTrue(tc, e->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdbl_basic_002(CuTest* tc) { + struct arg_dbl* a = arg_dbl1(NULL, NULL, "a", "a is "); + struct arg_dbl* b = arg_dbl0(NULL, NULL, "b", "b is "); + struct arg_dbl* c = arg_dbl0(NULL, NULL, "c", "c is "); + struct arg_dbl* d = arg_dbln("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_dbl* e = arg_dbl0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, end}; + int nerrors; + + char* argv[] = {"program", "1.234", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 0); + + CuAssertTrue(tc, a->count == 1); + CuAssertDblEquals(tc, a->dval[0], 1.234, DBL_EPSILON); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 0); + CuAssertTrue(tc, d->count == 0); + CuAssertTrue(tc, e->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdbl_basic_003(CuTest* tc) { + struct arg_dbl* a = arg_dbl1(NULL, NULL, "a", "a is "); + struct arg_dbl* b = arg_dbl0(NULL, NULL, "b", "b is "); + struct arg_dbl* c = arg_dbl0(NULL, NULL, "c", "c is "); + struct arg_dbl* d = arg_dbln("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_dbl* e = arg_dbl0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, end}; + int nerrors; + + char* argv[] = {"program", "1.8", "2.3", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 0); + + CuAssertTrue(tc, a->count == 1); + CuAssertDblEquals(tc, a->dval[0], 1.8, DBL_EPSILON); + CuAssertTrue(tc, b->count == 1); + CuAssertDblEquals(tc, b->dval[0], 2.3, DBL_EPSILON); + CuAssertTrue(tc, c->count == 0); + CuAssertTrue(tc, d->count == 0); + CuAssertTrue(tc, e->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdbl_basic_004(CuTest* tc) { + struct arg_dbl* a = arg_dbl1(NULL, NULL, "a", "a is "); + struct arg_dbl* b = arg_dbl0(NULL, NULL, "b", "b is "); + struct arg_dbl* c = arg_dbl0(NULL, NULL, "c", "c is "); + struct arg_dbl* d = arg_dbln("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_dbl* e = arg_dbl0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, end}; + int nerrors; + + char* argv[] = {"program", "5", "7", "9", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 0); + + CuAssertTrue(tc, a->count == 1); + CuAssertDblEquals(tc, a->dval[0], 5, DBL_EPSILON); + CuAssertTrue(tc, b->count == 1); + CuAssertDblEquals(tc, b->dval[0], 7, DBL_EPSILON); + CuAssertTrue(tc, c->count == 1); + CuAssertDblEquals(tc, c->dval[0], 9, DBL_EPSILON); + CuAssertTrue(tc, d->count == 0); + CuAssertTrue(tc, e->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdbl_basic_005(CuTest* tc) { + struct arg_dbl* a = arg_dbl1(NULL, NULL, "a", "a is "); + struct arg_dbl* b = arg_dbl0(NULL, NULL, "b", "b is "); + struct arg_dbl* c = arg_dbl0(NULL, NULL, "c", "c is "); + struct arg_dbl* d = arg_dbln("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_dbl* e = arg_dbl0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, end}; + int nerrors; + + char* argv[] = {"program", "1.9998", "-d", "13e-1", "-D", "17e-1", "--delta", "36e-1", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 0); + + CuAssertTrue(tc, a->count == 1); + CuAssertDblEquals(tc, a->dval[0], 1.9998, DBL_EPSILON); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 0); + CuAssertTrue(tc, d->count == 3); + CuAssertDblEquals(tc, d->dval[0], 13e-1, DBL_EPSILON); + CuAssertDblEquals(tc, d->dval[1], 17e-1, DBL_EPSILON); + CuAssertDblEquals(tc, d->dval[2], 36e-1, DBL_EPSILON); + CuAssertTrue(tc, e->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdbl_basic_006(CuTest* tc) { + struct arg_dbl* a = arg_dbl1(NULL, NULL, "a", "a is "); + struct arg_dbl* b = arg_dbl0(NULL, NULL, "b", "b is "); + struct arg_dbl* c = arg_dbl0(NULL, NULL, "c", "c is "); + struct arg_dbl* d = arg_dbln("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_dbl* e = arg_dbl0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, end}; + int nerrors; + + char* argv[] = {"program", "1.2", "2.3", "4.5", "--eps", "8.3456789", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 0); + + CuAssertTrue(tc, a->count == 1); + CuAssertDblEquals(tc, a->dval[0], 1.2, DBL_EPSILON); + CuAssertTrue(tc, b->count == 1); + CuAssertDblEquals(tc, b->dval[0], 2.3, DBL_EPSILON); + CuAssertTrue(tc, c->count == 1); + CuAssertDblEquals(tc, c->dval[0], 4.5, DBL_EPSILON); + CuAssertTrue(tc, d->count == 0); + CuAssertTrue(tc, e->count == 1); + CuAssertDblEquals(tc, e->dval[0], 8.3456789, DBL_EPSILON); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdbl_basic_007(CuTest* tc) { + struct arg_dbl* a = arg_dbl1(NULL, NULL, "a", "a is "); + struct arg_dbl* b = arg_dbl0(NULL, NULL, "b", "b is "); + struct arg_dbl* c = arg_dbl0(NULL, NULL, "c", "c is "); + struct arg_dbl* d = arg_dbln("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_dbl* e = arg_dbl0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, end}; + int nerrors; + + char* argv[] = {"program", "1.2", "2.3", "4.5", "--eqn", "8.3456789", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 0); + + CuAssertTrue(tc, a->count == 1); + CuAssertDblEquals(tc, a->dval[0], 1.2, DBL_EPSILON); + CuAssertTrue(tc, b->count == 1); + CuAssertDblEquals(tc, b->dval[0], 2.3, DBL_EPSILON); + CuAssertTrue(tc, c->count == 1); + CuAssertDblEquals(tc, c->dval[0], 4.5, DBL_EPSILON); + CuAssertTrue(tc, d->count == 0); + CuAssertTrue(tc, e->count == 1); + CuAssertDblEquals(tc, e->dval[0], 8.3456789, DBL_EPSILON); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdbl_basic_008(CuTest* tc) { + struct arg_dbl* a = arg_dbl1(NULL, NULL, "a", "a is "); + struct arg_dbl* b = arg_dbl0(NULL, NULL, "b", "b is "); + struct arg_dbl* c = arg_dbl0(NULL, NULL, "c", "c is "); + struct arg_dbl* d = arg_dbln("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_dbl* e = arg_dbl0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, end}; + int nerrors; + + char* argv[] = {"program", "1.2", "2.3", "4.5", "--eqn", "8.345", "-D", "0.234", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 0); + + CuAssertTrue(tc, a->count == 1); + CuAssertDblEquals(tc, a->dval[0], 1.2, DBL_EPSILON); + CuAssertTrue(tc, b->count == 1); + CuAssertDblEquals(tc, b->dval[0], 2.3, DBL_EPSILON); + CuAssertTrue(tc, c->count == 1); + CuAssertDblEquals(tc, c->dval[0], 4.5, DBL_EPSILON); + CuAssertTrue(tc, d->count == 1); + CuAssertDblEquals(tc, d->dval[0], 0.234, DBL_EPSILON); + CuAssertTrue(tc, e->count == 1); + CuAssertDblEquals(tc, e->dval[0], 8.345, DBL_EPSILON); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdbl_basic_009(CuTest* tc) { + struct arg_dbl* a = arg_dbl1(NULL, NULL, "a", "a is "); + struct arg_dbl* b = arg_dbl0(NULL, NULL, "b", "b is "); + struct arg_dbl* c = arg_dbl0(NULL, NULL, "c", "c is "); + struct arg_dbl* d = arg_dbln("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_dbl* e = arg_dbl0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, end}; + int nerrors; + + char* argv[] = {"program", "1", "2", "3", "4", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdbl_basic_010(CuTest* tc) { + struct arg_dbl* a = arg_dbl1(NULL, NULL, "a", "a is "); + struct arg_dbl* b = arg_dbl0(NULL, NULL, "b", "b is "); + struct arg_dbl* c = arg_dbl0(NULL, NULL, "c", "c is "); + struct arg_dbl* d = arg_dbln("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_dbl* e = arg_dbl0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, end}; + int nerrors; + + char* argv[] = {"program", "1", "2", "3", "4", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdbl_basic_011(CuTest* tc) { + struct arg_dbl* a = arg_dbl1(NULL, NULL, "a", "a is "); + struct arg_dbl* b = arg_dbl0(NULL, NULL, "b", "b is "); + struct arg_dbl* c = arg_dbl0(NULL, NULL, "c", "c is "); + struct arg_dbl* d = arg_dbln("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_dbl* e = arg_dbl0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, end}; + int nerrors; + + char* argv[] = {"program", "1", "2", "3", "-d1", "-d2", "-d3", "-d4", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdbl_basic_012(CuTest* tc) { + struct arg_dbl* a = arg_dbl1(NULL, NULL, "a", "a is "); + struct arg_dbl* b = arg_dbl0(NULL, NULL, "b", "b is "); + struct arg_dbl* c = arg_dbl0(NULL, NULL, "c", "c is "); + struct arg_dbl* d = arg_dbln("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_dbl* e = arg_dbl0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, end}; + int nerrors; + + char* argv[] = {"program", "1", "2", "3", "--eps", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdbl_basic_013(CuTest* tc) { + struct arg_dbl* a = arg_dbl1(NULL, NULL, "a", "a is "); + struct arg_dbl* b = arg_dbl0(NULL, NULL, "b", "b is "); + struct arg_dbl* c = arg_dbl0(NULL, NULL, "c", "c is "); + struct arg_dbl* d = arg_dbln("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_dbl* e = arg_dbl0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, end}; + int nerrors; + + char* argv[] = {"program", "1", "2", "3", "--eps", "3", "--eqn", "6", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdbl_basic_014(CuTest* tc) { + struct arg_dbl* a = arg_dbl1(NULL, NULL, "a", "a is "); + struct arg_dbl* b = arg_dbl0(NULL, NULL, "b", "b is "); + struct arg_dbl* c = arg_dbl0(NULL, NULL, "c", "c is "); + struct arg_dbl* d = arg_dbln("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_dbl* e = arg_dbl0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, end}; + int nerrors; + + char* argv[] = {"program", "hello", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argdbl_basic_015(CuTest* tc) { + struct arg_dbl* a = arg_dbl1(NULL, NULL, "a", "a is "); + struct arg_dbl* b = arg_dbl0(NULL, NULL, "b", "b is "); + struct arg_dbl* c = arg_dbl0(NULL, NULL, "c", "c is "); + struct arg_dbl* d = arg_dbln("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_dbl* e = arg_dbl0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, end}; + int nerrors; + + char* argv[] = {"program", "4", "hello", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +CuSuite* get_argdbl_testsuite() { + CuSuite* suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_argdbl_basic_001); + SUITE_ADD_TEST(suite, test_argdbl_basic_002); + SUITE_ADD_TEST(suite, test_argdbl_basic_003); + SUITE_ADD_TEST(suite, test_argdbl_basic_004); + SUITE_ADD_TEST(suite, test_argdbl_basic_005); + SUITE_ADD_TEST(suite, test_argdbl_basic_006); + SUITE_ADD_TEST(suite, test_argdbl_basic_007); + SUITE_ADD_TEST(suite, test_argdbl_basic_008); + SUITE_ADD_TEST(suite, test_argdbl_basic_009); + SUITE_ADD_TEST(suite, test_argdbl_basic_010); + SUITE_ADD_TEST(suite, test_argdbl_basic_011); + SUITE_ADD_TEST(suite, test_argdbl_basic_012); + SUITE_ADD_TEST(suite, test_argdbl_basic_013); + SUITE_ADD_TEST(suite, test_argdbl_basic_014); + SUITE_ADD_TEST(suite, test_argdbl_basic_015); + return suite; +} + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif diff --git a/c/utf8filenamecheck/windows/argtable/tests/testargdstr.c b/c/utf8filenamecheck/windows/argtable/tests/testargdstr.c new file mode 100644 index 0000000..4c8aff0 --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/tests/testargdstr.c @@ -0,0 +1,123 @@ +/******************************************************************************* + * This file is part of the argtable3 library. + * + * Copyright (C) 2013-2019 Tom G. Huang + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include +#include + +#include + +#include "CuTest.h" +#include "argtable3.h" + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4204) +#endif + +void test_argdstr_basic_001(CuTest* tc) { + arg_dstr_t ds = arg_dstr_create(); + + arg_dstr_set(ds, "hello ", ARG_DSTR_VOLATILE); + CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "hello ") == 0); + + arg_dstr_cat(ds, "world"); + CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "hello world") == 0); + + arg_dstr_destroy(ds); +} + +void test_argdstr_basic_002(CuTest* tc) { + arg_dstr_t ds = arg_dstr_create(); + + arg_dstr_set(ds, "hello world", ARG_DSTR_VOLATILE); + CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "hello world") == 0); + + arg_dstr_reset(ds); + CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "") == 0); + + arg_dstr_set(ds, "good", ARG_DSTR_VOLATILE); + CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "good") == 0); + + arg_dstr_destroy(ds); +} + +void test_argdstr_basic_003(CuTest* tc) { + arg_dstr_t ds = arg_dstr_create(); + arg_dstr_cat(ds, "hello world"); + CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "hello world") == 0); + + arg_dstr_destroy(ds); +} + +void test_argdstr_basic_004(CuTest* tc) { + arg_dstr_t ds = arg_dstr_create(); + arg_dstr_catf(ds, "%s %d", "hello world", 1); + CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "hello world 1") == 0); + + arg_dstr_destroy(ds); +} + +void test_argdstr_basic_005(CuTest* tc) { + arg_dstr_t ds = arg_dstr_create(); + arg_dstr_catf(ds, "%d.", 1); + arg_dstr_catf(ds, "%d.", 2); + arg_dstr_catf(ds, "%d.", 3); + arg_dstr_cat(ds, "456"); + CuAssertTrue(tc, strcmp(arg_dstr_cstr(ds), "1.2.3.456") == 0); + + arg_dstr_destroy(ds); +} + +void test_argdstr_basic_006(CuTest* tc) { + int i; + + arg_dstr_t ds = arg_dstr_create(); + for (i = 0; i < 100000; i++) { + arg_dstr_catf(ds, "%s", "1234567890"); + } + CuAssertTrue(tc, strlen(arg_dstr_cstr(ds)) == 1000000); + + arg_dstr_destroy(ds); +} + +CuSuite* get_argdstr_testsuite() { + CuSuite* suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_argdstr_basic_001); + SUITE_ADD_TEST(suite, test_argdstr_basic_002); + SUITE_ADD_TEST(suite, test_argdstr_basic_003); + SUITE_ADD_TEST(suite, test_argdstr_basic_004); + SUITE_ADD_TEST(suite, test_argdstr_basic_005); + SUITE_ADD_TEST(suite, test_argdstr_basic_006); + return suite; +} + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif diff --git a/c/utf8filenamecheck/windows/argtable/tests/testargfile.c b/c/utf8filenamecheck/windows/argtable/tests/testargfile.c new file mode 100644 index 0000000..4ae5e9d --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/tests/testargfile.c @@ -0,0 +1,752 @@ +/******************************************************************************* + * This file is part of the argtable3 library. + * + * Copyright (C) 2013-2019 Tom G. Huang + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include + +#include "CuTest.h" +#include "argtable3.h" + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4204) +#endif + +void test_argfile_basic_001(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "foo.bar", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "foo.bar"); + CuAssertStrEquals(tc, a->basename[0], "foo.bar"); + CuAssertStrEquals(tc, a->extension[0], ".bar"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_002(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "/foo.bar", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "/foo.bar"); + CuAssertStrEquals(tc, a->basename[0], "foo.bar"); + CuAssertStrEquals(tc, a->extension[0], ".bar"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_003(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "./foo.bar", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "./foo.bar"); + CuAssertStrEquals(tc, a->basename[0], "foo.bar"); + CuAssertStrEquals(tc, a->extension[0], ".bar"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_004(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "././foo.bar", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "././foo.bar"); + CuAssertStrEquals(tc, a->basename[0], "foo.bar"); + CuAssertStrEquals(tc, a->extension[0], ".bar"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_005(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "./././foo.bar", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "./././foo.bar"); + CuAssertStrEquals(tc, a->basename[0], "foo.bar"); + CuAssertStrEquals(tc, a->extension[0], ".bar"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_006(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "../foo.bar", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "../foo.bar"); + CuAssertStrEquals(tc, a->basename[0], "foo.bar"); + CuAssertStrEquals(tc, a->extension[0], ".bar"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_007(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "../../foo.bar", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "../../foo.bar"); + CuAssertStrEquals(tc, a->basename[0], "foo.bar"); + CuAssertStrEquals(tc, a->extension[0], ".bar"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_008(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "foo", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "foo"); + CuAssertStrEquals(tc, a->basename[0], "foo"); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_009(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "/foo", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "/foo"); + CuAssertStrEquals(tc, a->basename[0], "foo"); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_010(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "./foo", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "./foo"); + CuAssertStrEquals(tc, a->basename[0], "foo"); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_011(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "././foo", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "././foo"); + CuAssertStrEquals(tc, a->basename[0], "foo"); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_012(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "./././foo", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "./././foo"); + CuAssertStrEquals(tc, a->basename[0], "foo"); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_013(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "../foo", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "../foo"); + CuAssertStrEquals(tc, a->basename[0], "foo"); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_014(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "../../foo", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "../../foo"); + CuAssertStrEquals(tc, a->basename[0], "foo"); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_015(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", ".foo", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], ".foo"); + CuAssertStrEquals(tc, a->basename[0], ".foo"); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_016(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "/.foo", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "/.foo"); + CuAssertStrEquals(tc, a->basename[0], ".foo"); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_017(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "./.foo", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "./.foo"); + CuAssertStrEquals(tc, a->basename[0], ".foo"); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_018(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "../.foo", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "../.foo"); + CuAssertStrEquals(tc, a->basename[0], ".foo"); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_019(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "foo.", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "foo."); + CuAssertStrEquals(tc, a->basename[0], "foo."); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_020(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "/foo.", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "/foo."); + CuAssertStrEquals(tc, a->basename[0], "foo."); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_021(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "./foo.", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "./foo."); + CuAssertStrEquals(tc, a->basename[0], "foo."); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_022(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "../foo.", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "../foo."); + CuAssertStrEquals(tc, a->basename[0], "foo."); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_023(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "/.foo.", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "/.foo."); + CuAssertStrEquals(tc, a->basename[0], ".foo."); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_024(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "/.foo.c", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "/.foo.c"); + CuAssertStrEquals(tc, a->basename[0], ".foo.c"); + CuAssertStrEquals(tc, a->extension[0], ".c"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_025(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "/.foo..b.c", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "/.foo..b.c"); + CuAssertStrEquals(tc, a->basename[0], ".foo..b.c"); + CuAssertStrEquals(tc, a->extension[0], ".c"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_026(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "/", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "/"); + CuAssertStrEquals(tc, a->basename[0], ""); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_027(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", ".", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "."); + CuAssertStrEquals(tc, a->basename[0], ""); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_028(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "..", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], ".."); + CuAssertStrEquals(tc, a->basename[0], ""); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_029(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "/.", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "/."); + CuAssertStrEquals(tc, a->basename[0], ""); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_030(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "/..", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "/.."); + CuAssertStrEquals(tc, a->basename[0], ""); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_031(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "./", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "./"); + CuAssertStrEquals(tc, a->basename[0], ""); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argfile_basic_032(CuTest* tc) { + struct arg_file* a = arg_file1(NULL, NULL, "", "filename to test"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, end}; + int nerrors; + + char* argv[] = {"program", "../", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->filename[0], "../"); + CuAssertStrEquals(tc, a->basename[0], ""); + CuAssertStrEquals(tc, a->extension[0], ""); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +CuSuite* get_argfile_testsuite() { + CuSuite* suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_argfile_basic_001); + SUITE_ADD_TEST(suite, test_argfile_basic_002); + SUITE_ADD_TEST(suite, test_argfile_basic_003); + SUITE_ADD_TEST(suite, test_argfile_basic_004); + SUITE_ADD_TEST(suite, test_argfile_basic_005); + SUITE_ADD_TEST(suite, test_argfile_basic_006); + SUITE_ADD_TEST(suite, test_argfile_basic_007); + SUITE_ADD_TEST(suite, test_argfile_basic_008); + SUITE_ADD_TEST(suite, test_argfile_basic_009); + SUITE_ADD_TEST(suite, test_argfile_basic_010); + SUITE_ADD_TEST(suite, test_argfile_basic_011); + SUITE_ADD_TEST(suite, test_argfile_basic_012); + SUITE_ADD_TEST(suite, test_argfile_basic_013); + SUITE_ADD_TEST(suite, test_argfile_basic_014); + SUITE_ADD_TEST(suite, test_argfile_basic_015); + SUITE_ADD_TEST(suite, test_argfile_basic_016); + SUITE_ADD_TEST(suite, test_argfile_basic_017); + SUITE_ADD_TEST(suite, test_argfile_basic_018); + SUITE_ADD_TEST(suite, test_argfile_basic_019); + SUITE_ADD_TEST(suite, test_argfile_basic_020); + SUITE_ADD_TEST(suite, test_argfile_basic_021); + SUITE_ADD_TEST(suite, test_argfile_basic_022); + SUITE_ADD_TEST(suite, test_argfile_basic_023); + SUITE_ADD_TEST(suite, test_argfile_basic_024); + SUITE_ADD_TEST(suite, test_argfile_basic_025); + SUITE_ADD_TEST(suite, test_argfile_basic_026); + SUITE_ADD_TEST(suite, test_argfile_basic_027); + SUITE_ADD_TEST(suite, test_argfile_basic_028); + SUITE_ADD_TEST(suite, test_argfile_basic_029); + SUITE_ADD_TEST(suite, test_argfile_basic_030); + SUITE_ADD_TEST(suite, test_argfile_basic_031); + SUITE_ADD_TEST(suite, test_argfile_basic_032); + return suite; +} + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif diff --git a/c/utf8filenamecheck/windows/argtable/tests/testarghashtable.c b/c/utf8filenamecheck/windows/argtable/tests/testarghashtable.c new file mode 100644 index 0000000..36e4a7d --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/tests/testarghashtable.c @@ -0,0 +1,276 @@ +/******************************************************************************* + * This file is part of the argtable3 library. + * + * Copyright (C) 2013-2019 Tom G. Huang + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include +#include + +#include + +#include "CuTest.h" +#include "argtable3_private.h" + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4204) +#pragma warning(disable : 4996) +#endif + +static unsigned int hash_key(const void* key) { + char* str = (char*)key; + int c; + unsigned int hash = 5381; + + while ((c = *str++) != 0) + hash = ((hash << 5) + hash) + c; /* hash * 33 + c */ + + return hash; +} + +static int equal_keys(const void* key1, const void* key2) { + char* k1 = (char*)key1; + char* k2 = (char*)key2; + return (0 == strcmp(k1, k2)); +} + +void test_arghashtable_basic_001(CuTest* tc) { + arg_hashtable_t* h = arg_hashtable_create(32, hash_key, equal_keys); + CuAssertTrue(tc, h != 0); + CuAssertIntEquals(tc, arg_hashtable_count(h), 0); + + arg_hashtable_destroy(h, 1); +} + +void test_arghashtable_basic_002(CuTest* tc) { + arg_hashtable_t* h = arg_hashtable_create(32, hash_key, equal_keys); + CuAssertTrue(tc, h != 0); + CuAssertIntEquals(tc, arg_hashtable_count(h), 0); + + char* key_1 = "k1"; + char* k_1 = (char*)malloc(strlen(key_1) + 1); + memset(k_1, 0, strlen(key_1) + 1); + strncpy(k_1, key_1, strlen(key_1)); + + char* value_1 = "v1"; + char* v_1 = (char*)malloc(strlen(value_1) + 1); + memset(v_1, 0, strlen(value_1) + 1); + strncpy(v_1, value_1, strlen(value_1)); + + arg_hashtable_insert(h, k_1, v_1); + CuAssertIntEquals(tc, 1, arg_hashtable_count(h)); + + arg_hashtable_itr_t* itr = arg_hashtable_itr_create(h); + CuAssertTrue(tc, itr != 0); + CuAssertPtrEquals(tc, k_1, arg_hashtable_itr_key(itr)); + CuAssertTrue(tc, strcmp((char*)arg_hashtable_itr_key(itr), key_1) == 0); + CuAssertPtrEquals(tc, v_1, arg_hashtable_itr_value(itr)); + CuAssertTrue(tc, strcmp((char*)arg_hashtable_itr_value(itr), value_1) == 0); + + arg_hashtable_itr_destroy(itr); + arg_hashtable_destroy(h, 1); +} + +void test_arghashtable_basic_003(CuTest* tc) { + arg_hashtable_t* h = arg_hashtable_create(32, hash_key, equal_keys); + CuAssertTrue(tc, h != 0); + CuAssertIntEquals(tc, arg_hashtable_count(h), 0); + + char* key_1 = "k1"; + char* k_1 = (char*)malloc(strlen(key_1) + 1); + memset(k_1, 0, strlen(key_1) + 1); + strncpy(k_1, key_1, strlen(key_1)); + + char* value_1 = "v1"; + char* v_1 = (char*)malloc(strlen(value_1) + 1); + memset(v_1, 0, strlen(value_1) + 1); + strncpy(v_1, value_1, strlen(value_1)); + + arg_hashtable_insert(h, k_1, v_1); + CuAssertIntEquals(tc, 1, arg_hashtable_count(h)); + + char* key_2 = "k2"; + char* k_2 = (char*)malloc(strlen(key_2) + 1); + memset(k_2, 0, strlen(key_2) + 1); + strncpy(k_2, key_2, strlen(key_2)); + + char* value_2 = "v2"; + char* v_2 = (char*)malloc(strlen(value_2) + 1); + memset(v_2, 0, strlen(value_2) + 1); + strncpy(v_2, value_2, strlen(value_2)); + + arg_hashtable_insert(h, k_2, v_2); + CuAssertIntEquals(tc, 2, arg_hashtable_count(h)); + + arg_hashtable_itr_t* itr = arg_hashtable_itr_create(h); + CuAssertTrue(tc, itr != 0); + + int ret = arg_hashtable_itr_advance(itr); + CuAssertTrue(tc, ret != 0); + + ret = arg_hashtable_itr_advance(itr); + CuAssertTrue(tc, ret == 0); + + arg_hashtable_itr_destroy(itr); + arg_hashtable_destroy(h, 1); +} + +void test_arghashtable_basic_004(CuTest* tc) { + arg_hashtable_t* h = arg_hashtable_create(32, hash_key, equal_keys); + CuAssertTrue(tc, h != 0); + CuAssertIntEquals(tc, arg_hashtable_count(h), 0); + + char* key_1 = "k1"; + char* k_1 = (char*)malloc(strlen(key_1) + 1); + memset(k_1, 0, strlen(key_1) + 1); + strncpy(k_1, key_1, strlen(key_1)); + + char* value_1 = "v1"; + char* v_1 = (char*)malloc(strlen(value_1) + 1); + memset(v_1, 0, strlen(value_1) + 1); + strncpy(v_1, value_1, strlen(value_1)); + + arg_hashtable_insert(h, k_1, v_1); + CuAssertTrue(tc, h != 0); + CuAssertIntEquals(tc, 1, arg_hashtable_count(h)); + + arg_hashtable_itr_t* itr = arg_hashtable_itr_create(h); + int ret = arg_hashtable_itr_remove(itr); + CuAssertTrue(tc, ret == 0); + CuAssertIntEquals(tc, 0, arg_hashtable_count(h)); + + arg_hashtable_itr_destroy(itr); + arg_hashtable_destroy(h, 1); +} + +void test_arghashtable_basic_005(CuTest* tc) { + arg_hashtable_t* h = arg_hashtable_create(3, hash_key, equal_keys); + CuAssertTrue(tc, h != 0); + CuAssertIntEquals(tc, arg_hashtable_count(h), 0); + + char* key_1 = "k1"; + char* k_1 = (char*)malloc(strlen(key_1) + 1); + memset(k_1, 0, strlen(key_1) + 1); + strncpy(k_1, key_1, strlen(key_1)); + + char* value_1 = "v1"; + char* v_1 = (char*)malloc(strlen(value_1) + 1); + memset(v_1, 0, strlen(value_1) + 1); + strncpy(v_1, value_1, strlen(value_1)); + + arg_hashtable_insert(h, k_1, v_1); + CuAssertTrue(tc, h != 0); + CuAssertIntEquals(tc, 1, arg_hashtable_count(h)); + + arg_hashtable_remove(h, k_1); + CuAssertIntEquals(tc, 0, arg_hashtable_count(h)); + + arg_hashtable_destroy(h, 1); +} + +void test_arghashtable_basic_006(CuTest* tc) { + arg_hashtable_t* h = arg_hashtable_create(32, hash_key, equal_keys); + CuAssertTrue(tc, h != 0); + CuAssertIntEquals(tc, arg_hashtable_count(h), 0); + + char* key_1 = "k1"; + char* k_1 = (char*)malloc(strlen(key_1) + 1); + memset(k_1, 0, strlen(key_1) + 1); + strncpy(k_1, key_1, strlen(key_1)); + + char* value_1 = "v1"; + char* v_1 = (char*)malloc(strlen(value_1) + 1); + memset(v_1, 0, strlen(value_1) + 1); + strncpy(v_1, value_1, strlen(value_1)); + + arg_hashtable_insert(h, k_1, v_1); + CuAssertTrue(tc, arg_hashtable_count(h) == 1); + + char* vv = (char*)arg_hashtable_search(h, k_1); + CuAssertTrue(tc, strcmp(vv, v_1) == 0); + + arg_hashtable_destroy(h, 1); +} + +void test_arghashtable_basic_007(CuTest* tc) { + arg_hashtable_t* h = arg_hashtable_create(32, hash_key, equal_keys); + CuAssertTrue(tc, h != 0); + CuAssertIntEquals(tc, arg_hashtable_count(h), 0); + + char* key_1 = "k1"; + char* k_1 = (char*)malloc(strlen(key_1) + 1); + memset(k_1, 0, strlen(key_1) + 1); + strncpy(k_1, key_1, strlen(key_1)); + + char* value_1 = "v1"; + char* v_1 = (char*)malloc(strlen(value_1) + 1); + memset(v_1, 0, strlen(value_1) + 1); + strncpy(v_1, value_1, strlen(value_1)); + + arg_hashtable_insert(h, k_1, v_1); + CuAssertIntEquals(tc, 1, arg_hashtable_count(h)); + + char* key_2 = "k2"; + char* k_2 = (char*)malloc(strlen(key_2) + 1); + memset(k_2, 0, strlen(key_2) + 1); + strncpy(k_2, key_2, strlen(key_2)); + + char* value_2 = "v2"; + char* v_2 = (char*)malloc(strlen(value_2) + 1); + memset(v_2, 0, strlen(value_2) + 1); + strncpy(v_2, value_2, strlen(value_2)); + + arg_hashtable_insert(h, k_2, v_2); + CuAssertIntEquals(tc, 2, arg_hashtable_count(h)); + + arg_hashtable_itr_t itr; + int ret = arg_hashtable_itr_search(&itr, h, k_1); + CuAssertTrue(tc, ret != 0); + CuAssertPtrEquals(tc, k_1, arg_hashtable_itr_key(&itr)); + CuAssertPtrEquals(tc, v_1, arg_hashtable_itr_value(&itr)); + CuAssertTrue(tc, strcmp((char*)arg_hashtable_itr_key(&itr), k_1) == 0); + CuAssertTrue(tc, strcmp((char*)arg_hashtable_itr_value(&itr), v_1) == 0); + + arg_hashtable_destroy(h, 1); +} + +CuSuite* get_arghashtable_testsuite() { + CuSuite* suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_arghashtable_basic_001); + SUITE_ADD_TEST(suite, test_arghashtable_basic_002); + SUITE_ADD_TEST(suite, test_arghashtable_basic_003); + SUITE_ADD_TEST(suite, test_arghashtable_basic_004); + SUITE_ADD_TEST(suite, test_arghashtable_basic_005); + SUITE_ADD_TEST(suite, test_arghashtable_basic_006); + SUITE_ADD_TEST(suite, test_arghashtable_basic_007); + return suite; +} + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif diff --git a/c/utf8filenamecheck/windows/argtable/tests/testargint.c b/c/utf8filenamecheck/windows/argtable/tests/testargint.c new file mode 100644 index 0000000..a474430 --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/tests/testargint.c @@ -0,0 +1,2018 @@ +/******************************************************************************* + * This file is part of the argtable3 library. + * + * Copyright (C) 2013-2019 Tom G. Huang + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include + +#include "CuTest.h" +#include "argtable3.h" + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4204) +#endif + +void test_argint_basic_001(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + + char* argv[] = {"program", "0", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertIntEquals(tc, a->ival[0], 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 0); + CuAssertTrue(tc, d->count == 0); + CuAssertTrue(tc, e->count == 0); + CuAssertTrue(tc, f->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_002(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + + char* argv[] = {"program", "1", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertIntEquals(tc, a->ival[0], 1); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 0); + CuAssertTrue(tc, d->count == 0); + CuAssertTrue(tc, e->count == 0); + CuAssertTrue(tc, f->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_003(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + + char* argv[] = {"program", "1", "2", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertIntEquals(tc, a->ival[0], 1); + CuAssertTrue(tc, b->count == 1); + CuAssertIntEquals(tc, b->ival[0], 2); + CuAssertTrue(tc, c->count == 0); + CuAssertTrue(tc, d->count == 0); + CuAssertTrue(tc, e->count == 0); + CuAssertTrue(tc, f->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_004(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + + char* argv[] = {"program", "5", "7", "9", "-d", "-21", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertIntEquals(tc, a->ival[0], 5); + CuAssertTrue(tc, b->count == 1); + CuAssertIntEquals(tc, b->ival[0], 7); + CuAssertTrue(tc, c->count == 1); + CuAssertIntEquals(tc, c->ival[0], 9); + CuAssertTrue(tc, d->count == 1); + CuAssertIntEquals(tc, d->ival[0], -21); + CuAssertTrue(tc, e->count == 0); + CuAssertTrue(tc, f->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_005(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + + char* argv[] = {"program", "-d", "1", "-D2", "--delta", "3", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 1); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 0); + CuAssertTrue(tc, d->count == 3); + CuAssertIntEquals(tc, d->ival[0], 1); + CuAssertIntEquals(tc, d->ival[1], 2); + CuAssertIntEquals(tc, d->ival[2], 3); + CuAssertTrue(tc, e->count == 0); + CuAssertTrue(tc, f->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_006(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + + char* argv[] = {"program", "1", "2", "4", "--eps", "-7", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertIntEquals(tc, a->ival[0], 1); + CuAssertTrue(tc, b->count == 1); + CuAssertIntEquals(tc, b->ival[0], 2); + CuAssertTrue(tc, c->count == 1); + CuAssertIntEquals(tc, c->ival[0], 4); + CuAssertTrue(tc, d->count == 0); + CuAssertTrue(tc, e->count == 1); + CuAssertIntEquals(tc, e->ival[0], -7); + CuAssertTrue(tc, f->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_007(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + + char* argv[] = {"program", "1", "2", "4", "--eqn", "-7", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertIntEquals(tc, a->ival[0], 1); + CuAssertTrue(tc, b->count == 1); + CuAssertIntEquals(tc, b->ival[0], 2); + CuAssertTrue(tc, c->count == 1); + CuAssertIntEquals(tc, c->ival[0], 4); + CuAssertTrue(tc, d->count == 0); + CuAssertTrue(tc, e->count == 1); + CuAssertIntEquals(tc, e->ival[0], -7); + CuAssertTrue(tc, f->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_008(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + + char* argv[] = {"program", "1", "2", "3", "-D4", "--eps", "-10", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertIntEquals(tc, a->ival[0], 1); + CuAssertTrue(tc, b->count == 1); + CuAssertIntEquals(tc, b->ival[0], 2); + CuAssertTrue(tc, c->count == 1); + CuAssertIntEquals(tc, c->ival[0], 3); + CuAssertTrue(tc, d->count == 1); + CuAssertIntEquals(tc, d->ival[0], 4); + CuAssertTrue(tc, e->count == 1); + CuAssertIntEquals(tc, e->ival[0], -10); + CuAssertTrue(tc, f->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_009(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "1", "-f", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 0); + CuAssertTrue(tc, d->count == 0); + CuAssertTrue(tc, e->count == 0); + CuAssertTrue(tc, f->count == 1); + CuAssertIntEquals(tc, f->ival[0], -1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_010(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "-f", "1", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 1); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 1); + CuAssertIntEquals(tc, f->ival[0], -1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_011(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "-f", "2", "--filler", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 2); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 2); + CuAssertIntEquals(tc, f->ival[0], -1); + CuAssertIntEquals(tc, f->ival[1], -1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_012(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "-f", "1", "--filler=2", "-f", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 1); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 3); + CuAssertIntEquals(tc, f->ival[0], -1); + CuAssertIntEquals(tc, f->ival[1], 2); + CuAssertIntEquals(tc, f->ival[0], -1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_013(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0x0", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 0x0); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_014(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0x0", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 0x0); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_015(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0x10", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 0x10); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_016(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0x10", "0x32", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 0x10); + CuAssertIntEquals(tc, b->count, 1); + CuAssertIntEquals(tc, b->ival[0], 0x32); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_017(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0x5", "0xA", "0xF", "-d", "-0x1E", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 0x5); + CuAssertIntEquals(tc, b->count, 1); + CuAssertIntEquals(tc, b->ival[0], 0xA); + CuAssertIntEquals(tc, c->count, 1); + CuAssertIntEquals(tc, c->ival[0], 0xF); + CuAssertIntEquals(tc, d->count, 1); + CuAssertIntEquals(tc, d->ival[0], -0x1E); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_018(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "-d", "0xab", "-D0x09", "--delta", "0x02e", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + CuAssertIntEquals(tc, a->count, 0); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 3); + CuAssertIntEquals(tc, d->ival[0], 0xab); + CuAssertIntEquals(tc, d->ival[1], 0x09); + CuAssertIntEquals(tc, d->ival[2], 0x02e); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_019(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0o0", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 0); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_020(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0o10", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 010); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_021(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0o67", "0O23", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 067); + CuAssertIntEquals(tc, b->count, 1); + CuAssertIntEquals(tc, b->ival[0], 023); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_022(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0o5", "0O0", "0x1", "-d", "-0o6", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 05); + CuAssertIntEquals(tc, b->count, 1); + CuAssertIntEquals(tc, b->ival[0], 0); + CuAssertIntEquals(tc, c->count, 1); + CuAssertIntEquals(tc, c->ival[0], 0x1); + CuAssertIntEquals(tc, d->count, 1); + CuAssertIntEquals(tc, d->ival[0], -06); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_023(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "-d", "0o012", "-D0o0777", "--delta", "0o56", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + CuAssertIntEquals(tc, a->count, 0); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 3); + CuAssertIntEquals(tc, d->ival[0], 012); + CuAssertIntEquals(tc, d->ival[1], 0777); + CuAssertIntEquals(tc, d->ival[2], 056); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_024(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0B0", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 0); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_025(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0B0", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 0); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_026(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0b10", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 2); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_027(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0B10110", "0b111001", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 22); + CuAssertIntEquals(tc, b->count, 1); + CuAssertIntEquals(tc, b->ival[0], 57); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_028(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0B10110", "0b111001", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 22); + CuAssertIntEquals(tc, b->count, 1); + CuAssertIntEquals(tc, b->ival[0], 57); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_029(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0b101001", "0b101", "0b00101010101", "-d", "0B110000011", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 41); + CuAssertIntEquals(tc, b->count, 1); + CuAssertIntEquals(tc, b->ival[0], 5); + CuAssertIntEquals(tc, c->count, 1); + CuAssertIntEquals(tc, c->ival[0], 341); + CuAssertIntEquals(tc, d->count, 1); + CuAssertIntEquals(tc, d->ival[0], 387); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_030(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "-d", "0b101", "-D0B11", "--delta", "0b11011", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + CuAssertIntEquals(tc, a->count, 0); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 3); + CuAssertIntEquals(tc, d->ival[0], 5); + CuAssertIntEquals(tc, d->ival[1], 3); + CuAssertIntEquals(tc, d->ival[2], 27); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_031(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "11", "0x11", "0o11", "-D0b11", "--eps", "-0o50", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 11); + CuAssertIntEquals(tc, b->count, 1); + CuAssertIntEquals(tc, b->ival[0], 0x11); + CuAssertIntEquals(tc, c->count, 1); + CuAssertIntEquals(tc, c->ival[0], 011); + CuAssertIntEquals(tc, d->count, 1); + CuAssertIntEquals(tc, d->ival[0], 3); + CuAssertIntEquals(tc, e->count, 1); + CuAssertIntEquals(tc, e->ival[0], -050); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_032(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "1KB", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 1024); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_033(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "1MB", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 1024 * 1024); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_034(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "1GB", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 1024 * 1024 * 1024); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_035(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0x5KB", "0xAMB", "0x1GB", "-d", "-0x40A01400", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 0); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 0x5 * 1024); + CuAssertIntEquals(tc, b->count, 1); + CuAssertIntEquals(tc, b->ival[0], 0xA * 1024 * 1024); + CuAssertIntEquals(tc, c->count, 1); + CuAssertIntEquals(tc, c->ival[0], 0x1 * 1024 * 1024 * 1024); + CuAssertIntEquals(tc, d->count, 1); + CuAssertIntEquals(tc, d->ival[0], -0x40A01400); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_036(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + CuAssertIntEquals(tc, a->count, 0); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_037(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "1", "2", "3", "4", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 1); + CuAssertIntEquals(tc, b->count, 1); + CuAssertIntEquals(tc, b->ival[0], 2); + CuAssertIntEquals(tc, c->count, 1); + CuAssertIntEquals(tc, c->ival[0], 3); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_038(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "1", "2", "3", "-d1", "-d2", "-d3", "-d4", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 1); + CuAssertIntEquals(tc, b->count, 1); + CuAssertIntEquals(tc, b->ival[0], 2); + CuAssertIntEquals(tc, c->count, 1); + CuAssertIntEquals(tc, c->ival[0], 3); + CuAssertIntEquals(tc, d->count, 3); + CuAssertIntEquals(tc, d->ival[0], 1); + CuAssertIntEquals(tc, d->ival[1], 2); + CuAssertIntEquals(tc, d->ival[2], 3); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_039(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "1", "2", "3", "-d1", "-d2", "-d3", "-d", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 1); + CuAssertIntEquals(tc, b->count, 1); + CuAssertIntEquals(tc, b->ival[0], 2); + CuAssertIntEquals(tc, c->count, 1); + CuAssertIntEquals(tc, c->ival[0], 3); + CuAssertIntEquals(tc, d->count, 3); + CuAssertIntEquals(tc, d->ival[0], 1); + CuAssertIntEquals(tc, d->ival[1], 2); + CuAssertIntEquals(tc, d->ival[2], 3); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_040(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "1", "2", "3", "-d1", "-d2", "-d", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 1); + CuAssertIntEquals(tc, b->count, 1); + CuAssertIntEquals(tc, b->ival[0], 2); + CuAssertIntEquals(tc, c->count, 1); + CuAssertIntEquals(tc, c->ival[0], 3); + CuAssertIntEquals(tc, d->count, 2); + CuAssertIntEquals(tc, d->ival[0], 1); + CuAssertIntEquals(tc, d->ival[1], 2); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_041(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "1", "2", "3", "-d1", "-d", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 1); + CuAssertIntEquals(tc, b->count, 1); + CuAssertIntEquals(tc, b->ival[0], 2); + CuAssertIntEquals(tc, c->count, 1); + CuAssertIntEquals(tc, c->ival[0], 3); + CuAssertIntEquals(tc, d->count, 1); + CuAssertIntEquals(tc, d->ival[0], 1); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_042(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "1", "2", "3", "-d", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 1); + CuAssertIntEquals(tc, b->count, 1); + CuAssertIntEquals(tc, b->ival[0], 2); + CuAssertIntEquals(tc, c->count, 1); + CuAssertIntEquals(tc, c->ival[0], 3); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_043(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "1", "2", "3", "--eps", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_044(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "1", "2", "3", "--eps", "3", "--eqn", "6", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 1); + CuAssertIntEquals(tc, b->count, 1); + CuAssertIntEquals(tc, b->ival[0], 2); + CuAssertIntEquals(tc, c->count, 1); + CuAssertIntEquals(tc, c->ival[0], 3); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 1); + CuAssertIntEquals(tc, e->ival[0], 3); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_045(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "hello", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + CuAssertIntEquals(tc, a->count, 0); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_046(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "1.234", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + CuAssertIntEquals(tc, a->count, 0); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_047(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "4", "hello", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 4); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_048(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "5", "1.234", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + CuAssertIntEquals(tc, a->count, 1); + CuAssertIntEquals(tc, a->ival[0], 5); + CuAssertIntEquals(tc, b->count, 0); + CuAssertIntEquals(tc, c->count, 0); + CuAssertIntEquals(tc, d->count, 0); + CuAssertIntEquals(tc, e->count, 0); + CuAssertIntEquals(tc, f->count, 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_049(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "-f", "2", "--filler=", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 2); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_050(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0x0g", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_051(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0o08", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_052(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "0b02", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_053(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "1000GB", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argint_basic_054(CuTest* tc) { + struct arg_int* a = arg_int1(NULL, NULL, "a", "a is "); + struct arg_int* b = arg_int0(NULL, NULL, "b", "b is "); + struct arg_int* c = arg_int0(NULL, NULL, "c", "c is "); + struct arg_int* d = arg_intn("dD", "delta", "", 0, 3, "d can occur 0..3 times"); + struct arg_int* e = arg_int0(NULL, "eps,eqn", "", "eps is optional"); + struct arg_int* f = arg_intn("fF", "filler", "", 0, 3, "f can occur 0..3 times"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, e, f, end}; + int nerrors; + int i; + + char* argv[] = {"program", "1GBH", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + /* allow missing argument values for the f argument, and set defaults to -1 */ + f->hdr.flag |= ARG_HASOPTVALUE; + for (i = 0; i < f->hdr.maxcount; i++) + f->ival[i] = -1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertIntEquals(tc, nerrors, 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +CuSuite* get_argint_testsuite() { + CuSuite* suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_argint_basic_001); + SUITE_ADD_TEST(suite, test_argint_basic_002); + SUITE_ADD_TEST(suite, test_argint_basic_003); + SUITE_ADD_TEST(suite, test_argint_basic_004); + SUITE_ADD_TEST(suite, test_argint_basic_005); + SUITE_ADD_TEST(suite, test_argint_basic_006); + SUITE_ADD_TEST(suite, test_argint_basic_007); + SUITE_ADD_TEST(suite, test_argint_basic_008); + SUITE_ADD_TEST(suite, test_argint_basic_009); + SUITE_ADD_TEST(suite, test_argint_basic_010); + SUITE_ADD_TEST(suite, test_argint_basic_011); + SUITE_ADD_TEST(suite, test_argint_basic_012); + SUITE_ADD_TEST(suite, test_argint_basic_013); + SUITE_ADD_TEST(suite, test_argint_basic_014); + SUITE_ADD_TEST(suite, test_argint_basic_015); + SUITE_ADD_TEST(suite, test_argint_basic_016); + SUITE_ADD_TEST(suite, test_argint_basic_017); + SUITE_ADD_TEST(suite, test_argint_basic_018); + SUITE_ADD_TEST(suite, test_argint_basic_019); + SUITE_ADD_TEST(suite, test_argint_basic_020); + SUITE_ADD_TEST(suite, test_argint_basic_021); + SUITE_ADD_TEST(suite, test_argint_basic_022); + SUITE_ADD_TEST(suite, test_argint_basic_023); + SUITE_ADD_TEST(suite, test_argint_basic_024); + SUITE_ADD_TEST(suite, test_argint_basic_025); + SUITE_ADD_TEST(suite, test_argint_basic_026); + SUITE_ADD_TEST(suite, test_argint_basic_027); + SUITE_ADD_TEST(suite, test_argint_basic_028); + SUITE_ADD_TEST(suite, test_argint_basic_029); + SUITE_ADD_TEST(suite, test_argint_basic_030); + SUITE_ADD_TEST(suite, test_argint_basic_031); + SUITE_ADD_TEST(suite, test_argint_basic_032); + SUITE_ADD_TEST(suite, test_argint_basic_033); + SUITE_ADD_TEST(suite, test_argint_basic_034); + SUITE_ADD_TEST(suite, test_argint_basic_035); + SUITE_ADD_TEST(suite, test_argint_basic_036); + SUITE_ADD_TEST(suite, test_argint_basic_037); + SUITE_ADD_TEST(suite, test_argint_basic_038); + SUITE_ADD_TEST(suite, test_argint_basic_039); + SUITE_ADD_TEST(suite, test_argint_basic_040); + SUITE_ADD_TEST(suite, test_argint_basic_041); + SUITE_ADD_TEST(suite, test_argint_basic_042); + SUITE_ADD_TEST(suite, test_argint_basic_043); + SUITE_ADD_TEST(suite, test_argint_basic_044); + SUITE_ADD_TEST(suite, test_argint_basic_045); + SUITE_ADD_TEST(suite, test_argint_basic_046); + SUITE_ADD_TEST(suite, test_argint_basic_047); + SUITE_ADD_TEST(suite, test_argint_basic_048); + SUITE_ADD_TEST(suite, test_argint_basic_049); + SUITE_ADD_TEST(suite, test_argint_basic_050); + SUITE_ADD_TEST(suite, test_argint_basic_051); + SUITE_ADD_TEST(suite, test_argint_basic_052); + SUITE_ADD_TEST(suite, test_argint_basic_053); + SUITE_ADD_TEST(suite, test_argint_basic_054); + return suite; +} + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif diff --git a/c/utf8filenamecheck/windows/argtable/tests/testarglit.c b/c/utf8filenamecheck/windows/argtable/tests/testarglit.c new file mode 100644 index 0000000..8109f87 --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/tests/testarglit.c @@ -0,0 +1,538 @@ +/******************************************************************************* + * This file is part of the argtable3 library. + * + * Copyright (C) 2013-2019 Tom G. Huang + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include + +#include "CuTest.h" +#include "argtable3.h" + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4204) +#endif + +void test_arglit_basic_001(CuTest* tc) { + struct arg_lit* a = arg_lit0(NULL, "hello,world", "either --hello or --world or none"); + struct arg_lit* b = arg_lit0("bB", NULL, "either -b or -B or none"); + struct arg_lit* c = arg_lit1("cC", NULL, "either -c or -C"); + struct arg_lit* d = arg_litn("dD", "delta", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_lit* help = arg_lit0(NULL, "help", "print this help and exit"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, help, end}; + int nerrors; + + char* argv[] = {"program", "--help", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 2); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 0); + CuAssertTrue(tc, d->count == 0); + CuAssertTrue(tc, help->count == 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_arglit_basic_002(CuTest* tc) { + struct arg_lit* a = arg_lit0(NULL, "hello,world", "either --hello or --world or none"); + struct arg_lit* b = arg_lit0("bB", NULL, "either -b or -B or none"); + struct arg_lit* c = arg_lit1("cC", NULL, "either -c or -C"); + struct arg_lit* d = arg_litn("dD", "delta", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_lit* help = arg_lit0(NULL, "help", "print this help and exit"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, help, end}; + int nerrors; + + char* argv[] = {"program", "-cDd", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + if (nerrors > 0) + arg_print_errors(stdout, end, argv[0]); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertTrue(tc, d->count == 2); + CuAssertTrue(tc, help->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_arglit_basic_003(CuTest* tc) { + struct arg_lit* a = arg_lit0(NULL, "hello,world", "either --hello or --world or none"); + struct arg_lit* b = arg_lit0("bB", NULL, "either -b or -B or none"); + struct arg_lit* c = arg_lit1("cC", NULL, "either -c or -C"); + struct arg_lit* d = arg_litn("dD", "delta", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_lit* help = arg_lit0(NULL, "help", "print this help and exit"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, help, end}; + int nerrors; + + char* argv[] = {"program", "-cdDd", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + if (nerrors > 0) + arg_print_errors(stdout, end, argv[0]); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertTrue(tc, d->count == 3); + CuAssertTrue(tc, help->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_arglit_basic_004(CuTest* tc) { + struct arg_lit* a = arg_lit0(NULL, "hello,world", "either --hello or --world or none"); + struct arg_lit* b = arg_lit0("bB", NULL, "either -b or -B or none"); + struct arg_lit* c = arg_lit1("cC", NULL, "either -c or -C"); + struct arg_lit* d = arg_litn("dD", "delta", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_lit* help = arg_lit0(NULL, "help", "print this help and exit"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, help, end}; + int nerrors; + + char* argv[] = {"program", "-CDd", "--delta", "--delta", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + if (nerrors > 0) + arg_print_errors(stdout, end, argv[0]); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertTrue(tc, d->count == 4); + CuAssertTrue(tc, help->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_arglit_basic_005(CuTest* tc) { + struct arg_lit* a = arg_lit0(NULL, "hello,world", "either --hello or --world or none"); + struct arg_lit* b = arg_lit0("bB", NULL, "either -b or -B or none"); + struct arg_lit* c = arg_lit1("cC", NULL, "either -c or -C"); + struct arg_lit* d = arg_litn("dD", "delta", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_lit* help = arg_lit0(NULL, "help", "print this help and exit"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, help, end}; + int nerrors; + + char* argv[] = {"program", "--delta", "-cD", "-b", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + if (nerrors > 0) + arg_print_errors(stdout, end, argv[0]); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 1); + CuAssertTrue(tc, c->count == 1); + CuAssertTrue(tc, d->count == 2); + CuAssertTrue(tc, help->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_arglit_basic_006(CuTest* tc) { + struct arg_lit* a = arg_lit0(NULL, "hello,world", "either --hello or --world or none"); + struct arg_lit* b = arg_lit0("bB", NULL, "either -b or -B or none"); + struct arg_lit* c = arg_lit1("cC", NULL, "either -c or -C"); + struct arg_lit* d = arg_litn("dD", "delta", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_lit* help = arg_lit0(NULL, "help", "print this help and exit"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, help, end}; + int nerrors; + + char* argv[] = {"program", "-D", "-B", "--delta", "-C", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + if (nerrors > 0) + arg_print_errors(stdout, end, argv[0]); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 1); + CuAssertTrue(tc, c->count == 1); + CuAssertTrue(tc, d->count == 2); + CuAssertTrue(tc, help->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_arglit_basic_007(CuTest* tc) { + struct arg_lit* a = arg_lit0(NULL, "hello,world", "either --hello or --world or none"); + struct arg_lit* b = arg_lit0("bB", NULL, "either -b or -B or none"); + struct arg_lit* c = arg_lit1("cC", NULL, "either -c or -C"); + struct arg_lit* d = arg_litn("dD", "delta", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_lit* help = arg_lit0(NULL, "help", "print this help and exit"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, help, end}; + int nerrors; + + char* argv[] = {"program", "-D", "-B", "--delta", "-C", "--hello", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + if (nerrors > 0) + arg_print_errors(stdout, end, argv[0]); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertTrue(tc, b->count == 1); + CuAssertTrue(tc, c->count == 1); + CuAssertTrue(tc, d->count == 2); + CuAssertTrue(tc, help->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_arglit_basic_008(CuTest* tc) { + struct arg_lit* a = arg_lit0(NULL, "hello,world", "either --hello or --world or none"); + struct arg_lit* b = arg_lit0("bB", NULL, "either -b or -B or none"); + struct arg_lit* c = arg_lit1("cC", NULL, "either -c or -C"); + struct arg_lit* d = arg_litn("dD", "delta", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_lit* help = arg_lit0(NULL, "help", "print this help and exit"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, help, end}; + int nerrors; + + char* argv[] = {"program", "-D", "-B", "--delta", "-C", "--world", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + if (nerrors > 0) + arg_print_errors(stdout, end, argv[0]); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertTrue(tc, b->count == 1); + CuAssertTrue(tc, c->count == 1); + CuAssertTrue(tc, d->count == 2); + CuAssertTrue(tc, help->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_arglit_basic_009(CuTest* tc) { + struct arg_lit* a = arg_lit0(NULL, "hello,world", "either --hello or --world or none"); + struct arg_lit* b = arg_lit0("bB", NULL, "either -b or -B or none"); + struct arg_lit* c = arg_lit1("cC", NULL, "either -c or -C"); + struct arg_lit* d = arg_litn("dD", "delta", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_lit* help = arg_lit0(NULL, "help", "print this help and exit"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, help, end}; + int nerrors; + + char* argv[] = {"program", "-c", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 1); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertTrue(tc, d->count == 0); + CuAssertTrue(tc, help->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_arglit_basic_010(CuTest* tc) { + struct arg_lit* a = arg_lit0(NULL, "hello,world", "either --hello or --world or none"); + struct arg_lit* b = arg_lit0("bB", NULL, "either -b or -B or none"); + struct arg_lit* c = arg_lit1("cC", NULL, "either -c or -C"); + struct arg_lit* d = arg_litn("dD", "delta", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_lit* help = arg_lit0(NULL, "help", "print this help and exit"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, help, end}; + int nerrors; + + char* argv[] = {"program", "-D", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 2); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 0); + CuAssertTrue(tc, d->count == 1); + CuAssertTrue(tc, help->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_arglit_basic_011(CuTest* tc) { + struct arg_lit* a = arg_lit0(NULL, "hello,world", "either --hello or --world or none"); + struct arg_lit* b = arg_lit0("bB", NULL, "either -b or -B or none"); + struct arg_lit* c = arg_lit1("cC", NULL, "either -c or -C"); + struct arg_lit* d = arg_litn("dD", "delta", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_lit* help = arg_lit0(NULL, "help", "print this help and exit"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, help, end}; + int nerrors; + + char* argv[] = {"program", "-CD", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 1); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertTrue(tc, d->count == 1); + CuAssertTrue(tc, help->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_arglit_basic_012(CuTest* tc) { + struct arg_lit* a = arg_lit0(NULL, "hello,world", "either --hello or --world or none"); + struct arg_lit* b = arg_lit0("bB", NULL, "either -b or -B or none"); + struct arg_lit* c = arg_lit1("cC", NULL, "either -c or -C"); + struct arg_lit* d = arg_litn("dD", "delta", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_lit* help = arg_lit0(NULL, "help", "print this help and exit"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, help, end}; + int nerrors; + + char* argv[] = {"program", "-Dd", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 1); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 0); + CuAssertTrue(tc, d->count == 2); + CuAssertTrue(tc, help->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_arglit_basic_013(CuTest* tc) { + struct arg_lit* a = arg_lit0(NULL, "hello,world", "either --hello or --world or none"); + struct arg_lit* b = arg_lit0("bB", NULL, "either -b or -B or none"); + struct arg_lit* c = arg_lit1("cC", NULL, "either -c or -C"); + struct arg_lit* d = arg_litn("dD", "delta", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_lit* help = arg_lit0(NULL, "help", "print this help and exit"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, help, end}; + int nerrors; + + char* argv[] = {"program", "-cddddd", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 1); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertTrue(tc, d->count == 4); + CuAssertTrue(tc, help->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_arglit_basic_014(CuTest* tc) { + struct arg_lit* a = arg_lit0(NULL, "hello,world", "either --hello or --world or none"); + struct arg_lit* b = arg_lit0("bB", NULL, "either -b or -B or none"); + struct arg_lit* c = arg_lit1("cC", NULL, "either -c or -C"); + struct arg_lit* d = arg_litn("dD", "delta", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_lit* help = arg_lit0(NULL, "help", "print this help and exit"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, help, end}; + int nerrors; + + char* argv[] = {"program", "-ccddd", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 1); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertTrue(tc, d->count == 3); + CuAssertTrue(tc, help->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_arglit_basic_015(CuTest* tc) { + struct arg_lit* a = arg_lit0(NULL, "hello,world", "either --hello or --world or none"); + struct arg_lit* b = arg_lit0("bB", NULL, "either -b or -B or none"); + struct arg_lit* c = arg_lit1("cC", NULL, "either -c or -C"); + struct arg_lit* d = arg_litn("dD", "delta", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_lit* help = arg_lit0(NULL, "help", "print this help and exit"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, help, end}; + int nerrors; + + char* argv[] = {"program", "-C", "-d", "-D", "--delta", "-b", "-B", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 1); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 1); + CuAssertTrue(tc, c->count == 1); + CuAssertTrue(tc, d->count == 3); + CuAssertTrue(tc, help->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_arglit_basic_016(CuTest* tc) { + struct arg_lit* a = arg_lit0(NULL, "hello,world", "either --hello or --world or none"); + struct arg_lit* b = arg_lit0("bB", NULL, "either -b or -B or none"); + struct arg_lit* c = arg_lit1("cC", NULL, "either -c or -C"); + struct arg_lit* d = arg_litn("dD", "delta", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_lit* help = arg_lit0(NULL, "help", "print this help and exit"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, help, end}; + int nerrors; + + char* argv[] = {"program", "-C", "-d", "-D", "--delta", "--hello", "--world", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 1); + CuAssertTrue(tc, a->count == 1); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertTrue(tc, d->count == 3); + CuAssertTrue(tc, help->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_arglit_basic_017(CuTest* tc) { + struct arg_lit* a = arg_lit0(NULL, "hello,world", "either --hello or --world or none"); + struct arg_lit* b = arg_lit0("bB", NULL, "either -b or -B or none"); + struct arg_lit* c = arg_lit1("cC", NULL, "either -c or -C"); + struct arg_lit* d = arg_litn("dD", "delta", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_lit* help = arg_lit0(NULL, "help", "print this help and exit"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, help, end}; + int nerrors; + + char* argv[] = {"program", "-C", "-d", "-D", "--delta", "--hello", "X", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 1); + CuAssertTrue(tc, a->count == 1); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertTrue(tc, d->count == 3); + CuAssertTrue(tc, help->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +CuSuite* get_arglit_testsuite() { + CuSuite* suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_arglit_basic_001); + SUITE_ADD_TEST(suite, test_arglit_basic_002); + SUITE_ADD_TEST(suite, test_arglit_basic_003); + SUITE_ADD_TEST(suite, test_arglit_basic_004); + SUITE_ADD_TEST(suite, test_arglit_basic_005); + SUITE_ADD_TEST(suite, test_arglit_basic_006); + SUITE_ADD_TEST(suite, test_arglit_basic_007); + SUITE_ADD_TEST(suite, test_arglit_basic_008); + SUITE_ADD_TEST(suite, test_arglit_basic_009); + SUITE_ADD_TEST(suite, test_arglit_basic_010); + SUITE_ADD_TEST(suite, test_arglit_basic_011); + SUITE_ADD_TEST(suite, test_arglit_basic_012); + SUITE_ADD_TEST(suite, test_arglit_basic_013); + SUITE_ADD_TEST(suite, test_arglit_basic_014); + SUITE_ADD_TEST(suite, test_arglit_basic_015); + SUITE_ADD_TEST(suite, test_arglit_basic_016); + SUITE_ADD_TEST(suite, test_arglit_basic_017); + return suite; +} + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif diff --git a/c/utf8filenamecheck/windows/argtable/tests/testargrex.c b/c/utf8filenamecheck/windows/argtable/tests/testargrex.c new file mode 100644 index 0000000..8d846b7 --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/tests/testargrex.c @@ -0,0 +1,299 @@ +/******************************************************************************* + * This file is part of the argtable3 library. + * + * Copyright (C) 2013-2019 Tom G. Huang + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include + +#include "CuTest.h" +#include "argtable3.h" + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4204) +#endif + +void test_argrex_basic_001(CuTest* tc) { + struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); + struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); + struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); + struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "--beta", "world", "goodbye", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 0); + + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 1); + CuAssertStrEquals(tc, b->sval[0], "world"); + CuAssertTrue(tc, c->count == 1); + CuAssertStrEquals(tc, c->sval[0], "goodbye"); + CuAssertTrue(tc, d->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argrex_basic_002(CuTest* tc) { + struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); + struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); + struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); + struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "GoodBye", "--beta", "World", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 0); + + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 1); + CuAssertStrEquals(tc, b->sval[0], "World"); + CuAssertTrue(tc, c->count == 1); + CuAssertStrEquals(tc, c->sval[0], "GoodBye"); + CuAssertTrue(tc, d->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argrex_basic_003(CuTest* tc) { + struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); + struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); + struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); + struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "--beta", "world", "GOODBYE", "GoodBye", "gOoDbyE", "Anything", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 0); + + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 1); + CuAssertStrEquals(tc, b->sval[0], "world"); + CuAssertTrue(tc, c->count == 3); + CuAssertStrEquals(tc, c->sval[0], "GOODBYE"); + CuAssertStrEquals(tc, c->sval[1], "GoodBye"); + CuAssertStrEquals(tc, c->sval[2], "gOoDbyE"); + CuAssertTrue(tc, d->count == 1); + CuAssertStrEquals(tc, d->sval[0], "Anything"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argrex_basic_004(CuTest* tc) { + struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); + struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); + struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); + struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "--beta", "world", "GOODBYE", "AnyHow", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 0); + + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 1); + CuAssertStrEquals(tc, b->sval[0], "world"); + CuAssertTrue(tc, c->count == 1); + CuAssertStrEquals(tc, c->sval[0], "GOODBYE"); + CuAssertTrue(tc, d->count == 1); + CuAssertStrEquals(tc, d->sval[0], "AnyHow"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argrex_basic_005(CuTest* tc) { + struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); + struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); + struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); + struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "-a", "hello", "--beta", "world", "GOODBYE", "AnyHow", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 0); + + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->sval[0], "hello"); + CuAssertTrue(tc, b->count == 1); + CuAssertStrEquals(tc, b->sval[0], "world"); + CuAssertTrue(tc, c->count == 1); + CuAssertStrEquals(tc, c->sval[0], "GOODBYE"); + CuAssertTrue(tc, d->count == 1); + CuAssertStrEquals(tc, d->sval[0], "AnyHow"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argrex_basic_006(CuTest* tc) { + struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); + struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); + struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); + struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "--beta", "WORLD", "goodbye", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argrex_basic_007(CuTest* tc) { + struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); + struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); + struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); + struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "--beta", "World", "goodby", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argrex_basic_008(CuTest* tc) { + struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); + struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); + struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); + struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "--beta", "world", "GoodBye", "Goodbye", "gOoDbyE", "Anything", "goodbye", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argrex_basic_009(CuTest* tc) { + struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); + struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); + struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); + struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "--beta", "world", "GoodBye", "Goodbye", "gOoDbyE", "Anything", "Anytime", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argrex_basic_010(CuTest* tc) { + struct arg_rex* a = arg_rex0("a", NULL, "hello", NULL, 0, "blah blah"); + struct arg_rex* b = arg_rex1(NULL, "beta", "[Ww]orld", NULL, 0, "blah blah"); + struct arg_rex* c = arg_rexn(NULL, NULL, "goodbye", NULL, 1, 5, ARG_REX_ICASE, "blah blah"); + struct arg_rex* d = arg_rex0(NULL, NULL, "any.*", NULL, ARG_REX_ICASE, "blah blah"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "--beta", "world", "GoodBye", "Goodbye", "Anything", "-a", "Hello", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + CuAssertTrue(tc, nerrors == 1); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +CuSuite* get_argrex_testsuite() { + CuSuite* suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_argrex_basic_001); + SUITE_ADD_TEST(suite, test_argrex_basic_002); + SUITE_ADD_TEST(suite, test_argrex_basic_003); + SUITE_ADD_TEST(suite, test_argrex_basic_004); + SUITE_ADD_TEST(suite, test_argrex_basic_005); + SUITE_ADD_TEST(suite, test_argrex_basic_006); + SUITE_ADD_TEST(suite, test_argrex_basic_007); + SUITE_ADD_TEST(suite, test_argrex_basic_008); + SUITE_ADD_TEST(suite, test_argrex_basic_009); + SUITE_ADD_TEST(suite, test_argrex_basic_010); + return suite; +} + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif diff --git a/c/utf8filenamecheck/windows/argtable/tests/testargstr.c b/c/utf8filenamecheck/windows/argtable/tests/testargstr.c new file mode 100644 index 0000000..ac2e088 --- /dev/null +++ b/c/utf8filenamecheck/windows/argtable/tests/testargstr.c @@ -0,0 +1,531 @@ +/******************************************************************************* + * This file is part of the argtable3 library. + * + * Copyright (C) 2013-2019 Tom G. Huang + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of STEWART HEITMANN nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + ******************************************************************************/ + +#include + +#include "CuTest.h" +#include "argtable3.h" + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4204) +#endif + +void test_argstr_basic_001(CuTest* tc) { + struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none"); + struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none"); + struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C"); + struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "--hello=string1", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 2); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->sval[0], "string1"); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 0); + CuAssertTrue(tc, d->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argstr_basic_002(CuTest* tc) { + struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none"); + struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none"); + struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C"); + struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "-cstring1", "-Dstring2", "-dstring3", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + if (nerrors > 0) + arg_print_errors(stdout, end, argv[0]); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertStrEquals(tc, c->sval[0], "string1"); + CuAssertTrue(tc, d->count == 2); + CuAssertStrEquals(tc, d->sval[0], "string2"); + CuAssertStrEquals(tc, d->sval[1], "string3"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argstr_basic_003(CuTest* tc) { + struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none"); + struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none"); + struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C"); + struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "-Cstring1", "--delta=string2", "--delta=string3", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + if (nerrors > 0) + arg_print_errors(stdout, end, argv[0]); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertStrEquals(tc, c->sval[0], "string1"); + CuAssertTrue(tc, d->count == 2); + CuAssertStrEquals(tc, d->sval[0], "string2"); + CuAssertStrEquals(tc, d->sval[1], "string3"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argstr_basic_004(CuTest* tc) { + struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none"); + struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none"); + struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C"); + struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "--delta=string1", "-cstring2", "-Dstring3", "-bstring4", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + if (nerrors > 0) + arg_print_errors(stdout, end, argv[0]); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 1); + CuAssertStrEquals(tc, b->sval[0], "string4"); + CuAssertTrue(tc, c->count == 1); + CuAssertStrEquals(tc, c->sval[0], "string2"); + CuAssertTrue(tc, d->count == 2); + CuAssertStrEquals(tc, d->sval[0], "string1"); + CuAssertStrEquals(tc, d->sval[1], "string3"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argstr_basic_005(CuTest* tc) { + struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none"); + struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none"); + struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C"); + struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "-Dstring1", "-Bstring2", "--delta=string3", "-Cstring4", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + if (nerrors > 0) + arg_print_errors(stdout, end, argv[0]); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 1); + CuAssertStrEquals(tc, b->sval[0], "string2"); + CuAssertTrue(tc, c->count == 1); + CuAssertStrEquals(tc, c->sval[0], "string4"); + CuAssertTrue(tc, d->count == 2); + CuAssertStrEquals(tc, d->sval[0], "string1"); + CuAssertStrEquals(tc, d->sval[1], "string3"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argstr_basic_006(CuTest* tc) { + struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none"); + struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none"); + struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C"); + struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "-Dstring1", "-Bstring2", "--delta=string3", "-Cstring4", "--hello=string5", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + if (nerrors > 0) + arg_print_errors(stdout, end, argv[0]); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->sval[0], "string5"); + CuAssertTrue(tc, b->count == 1); + CuAssertStrEquals(tc, b->sval[0], "string2"); + CuAssertTrue(tc, c->count == 1); + CuAssertStrEquals(tc, c->sval[0], "string4"); + CuAssertTrue(tc, d->count == 2); + CuAssertStrEquals(tc, d->sval[0], "string1"); + CuAssertStrEquals(tc, d->sval[1], "string3"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argstr_basic_007(CuTest* tc) { + struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none"); + struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none"); + struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C"); + struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "-Dstring1", "-Bstring2", "--delta=string3", "-Cstring4", "--world=string5", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + if (nerrors > 0) + arg_print_errors(stdout, end, argv[0]); + + CuAssertTrue(tc, nerrors == 0); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->sval[0], "string5"); + CuAssertTrue(tc, b->count == 1); + CuAssertStrEquals(tc, b->sval[0], "string2"); + CuAssertTrue(tc, c->count == 1); + CuAssertStrEquals(tc, c->sval[0], "string4"); + CuAssertTrue(tc, d->count == 2); + CuAssertStrEquals(tc, d->sval[0], "string1"); + CuAssertStrEquals(tc, d->sval[1], "string3"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argstr_basic_008(CuTest* tc) { + struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none"); + struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none"); + struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C"); + struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "-cstring1", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 1); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertStrEquals(tc, c->sval[0], "string1"); + CuAssertTrue(tc, d->count == 0); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argstr_basic_009(CuTest* tc) { + struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none"); + struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none"); + struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C"); + struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "-Dstring1", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 2); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 0); + CuAssertTrue(tc, d->count == 1); + CuAssertStrEquals(tc, d->sval[0], "string1"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argstr_basic_010(CuTest* tc) { + struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none"); + struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none"); + struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C"); + struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "-Cstring1", "-Dstring2", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 1); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertStrEquals(tc, c->sval[0], "string1"); + CuAssertTrue(tc, d->count == 1); + CuAssertStrEquals(tc, d->sval[0], "string2"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argstr_basic_011(CuTest* tc) { + struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none"); + struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none"); + struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C"); + struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "-Dstring1", "-dstring2", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 1); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 0); + CuAssertTrue(tc, d->count == 2); + CuAssertStrEquals(tc, d->sval[0], "string1"); + CuAssertStrEquals(tc, d->sval[1], "string2"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argstr_basic_012(CuTest* tc) { + struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none"); + struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none"); + struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C"); + struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "-cs1", "-ds2", "-ds3", "-ds4", "-ds5", "-ds6", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 1); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertStrEquals(tc, c->sval[0], "s1"); + CuAssertTrue(tc, d->count == 4); + CuAssertStrEquals(tc, d->sval[0], "s2"); + CuAssertStrEquals(tc, d->sval[1], "s3"); + CuAssertStrEquals(tc, d->sval[2], "s4"); + CuAssertStrEquals(tc, d->sval[3], "s5"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argstr_basic_013(CuTest* tc) { + struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none"); + struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none"); + struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C"); + struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "-cs1", "-cs2", "-ds3", "-ds4", "-ds5", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 1); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertStrEquals(tc, c->sval[0], "s1"); + CuAssertTrue(tc, d->count == 3); + CuAssertStrEquals(tc, d->sval[0], "s3"); + CuAssertStrEquals(tc, d->sval[1], "s4"); + CuAssertStrEquals(tc, d->sval[2], "s5"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argstr_basic_014(CuTest* tc) { + struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none"); + struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none"); + struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C"); + struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "-Cs1", "-ds2", "-Ds3", "--delta=s4", "-bs5", "-Bs6", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 1); + CuAssertTrue(tc, a->count == 0); + CuAssertTrue(tc, b->count == 1); + CuAssertStrEquals(tc, b->sval[0], "s5"); + CuAssertTrue(tc, c->count == 1); + CuAssertStrEquals(tc, c->sval[0], "s1"); + CuAssertTrue(tc, d->count == 3); + CuAssertStrEquals(tc, d->sval[0], "s2"); + CuAssertStrEquals(tc, d->sval[1], "s3"); + CuAssertStrEquals(tc, d->sval[2], "s4"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argstr_basic_015(CuTest* tc) { + struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none"); + struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none"); + struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C"); + struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "-Cs1", "-ds2", "-Ds3", "--delta=s4", "--hello=s5", "--world=s6", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 1); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->sval[0], "s5"); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertStrEquals(tc, c->sval[0], "s1"); + CuAssertTrue(tc, d->count == 3); + CuAssertStrEquals(tc, d->sval[0], "s2"); + CuAssertStrEquals(tc, d->sval[1], "s3"); + CuAssertStrEquals(tc, d->sval[2], "s4"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +void test_argstr_basic_016(CuTest* tc) { + struct arg_str* a = arg_str0(NULL, "hello,world", "STRVAL", "either --hello or --world or none"); + struct arg_str* b = arg_str0("bB", NULL, "STRVAL", "either -b or -B or none"); + struct arg_str* c = arg_str1("cC", NULL, "STRVAL", "either -c or -C"); + struct arg_str* d = arg_strn("dD", "delta", "STRVAL", 2, 4, "-d|-D|--delta 2..4 occurences"); + struct arg_end* end = arg_end(20); + void* argtable[] = {a, b, c, d, end}; + int nerrors; + + char* argv[] = {"program", "-Cs1", "-ds2", "-Ds3", "--delta=s4", "--hello=s5", "X", NULL}; + int argc = sizeof(argv) / sizeof(char*) - 1; + + CuAssertTrue(tc, arg_nullcheck(argtable) == 0); + + nerrors = arg_parse(argc, argv, argtable); + + CuAssertTrue(tc, nerrors == 1); + CuAssertTrue(tc, a->count == 1); + CuAssertStrEquals(tc, a->sval[0], "s5"); + CuAssertTrue(tc, b->count == 0); + CuAssertTrue(tc, c->count == 1); + CuAssertStrEquals(tc, c->sval[0], "s1"); + CuAssertTrue(tc, d->count == 3); + CuAssertStrEquals(tc, d->sval[0], "s2"); + CuAssertStrEquals(tc, d->sval[1], "s3"); + CuAssertStrEquals(tc, d->sval[2], "s4"); + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); +} + +CuSuite* get_argstr_testsuite() { + CuSuite* suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_argstr_basic_001); + SUITE_ADD_TEST(suite, test_argstr_basic_002); + SUITE_ADD_TEST(suite, test_argstr_basic_003); + SUITE_ADD_TEST(suite, test_argstr_basic_004); + SUITE_ADD_TEST(suite, test_argstr_basic_005); + SUITE_ADD_TEST(suite, test_argstr_basic_006); + SUITE_ADD_TEST(suite, test_argstr_basic_007); + SUITE_ADD_TEST(suite, test_argstr_basic_008); + SUITE_ADD_TEST(suite, test_argstr_basic_009); + SUITE_ADD_TEST(suite, test_argstr_basic_010); + SUITE_ADD_TEST(suite, test_argstr_basic_011); + SUITE_ADD_TEST(suite, test_argstr_basic_012); + SUITE_ADD_TEST(suite, test_argstr_basic_013); + SUITE_ADD_TEST(suite, test_argstr_basic_014); + SUITE_ADD_TEST(suite, test_argstr_basic_015); + SUITE_ADD_TEST(suite, test_argstr_basic_016); + return suite; +} + +#if defined(_MSC_VER) +#pragma warning(pop) +#endif diff --git a/c/utf8filenamecheck/windows/bin/utf8filenamecheck.exe b/c/utf8filenamecheck/windows/bin/utf8filenamecheck.exe new file mode 100644 index 0000000..fa2ab2e Binary files /dev/null and b/c/utf8filenamecheck/windows/bin/utf8filenamecheck.exe differ diff --git a/c/utf8filenamecheck/windows/makefile.bat b/c/utf8filenamecheck/windows/makefile.bat new file mode 100644 index 0000000..a67a3c8 --- /dev/null +++ b/c/utf8filenamecheck/windows/makefile.bat @@ -0,0 +1,18 @@ +:: utf8filenamecheck Check if all the filenames in a fiven folder are UTF-8 +:: Copyright (C) 2023 Johannes 'Banana' Keßler +:: +:: This program is free software: you can redistribute it and/or modify +:: it under the terms of the GNU General Public License as published by +:: the Free Software Foundation, either version 3 of the License, or +:: (at your option) any later version. +:: +:: This program is distributed in the hope that it will be useful, +:: but WITHOUT ANY WARRANTY; without even the implied warranty of +:: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +:: GNU General Public License for more details. +:: +:: You should have received a copy of the GNU General Public License +:: along with this program. If not, see . +@echo off +cls +gcc -std=c99 -march=native -O3 -Wall -Iargtable/ -Isimdutf8check/ utf8filenamecheck.c argtable/argtable3.c -o bin/utf8filenamecheck.exe \ No newline at end of file diff --git a/c/utf8filenamecheck/windows/notes.txt b/c/utf8filenamecheck/windows/notes.txt new file mode 100644 index 0000000..6304e04 --- /dev/null +++ b/c/utf8filenamecheck/windows/notes.txt @@ -0,0 +1,5 @@ +https://github.com/adamretter/utf8-validator-c +https://stackoverflow.com/questions/1301402/example-invalid-utf8-string + +https://codeforwin.org/c-programming/c-program-to-list-all-files-in-a-directory-recursively +https://man7.org/linux/man-pages/man3/nftw.3.html \ No newline at end of file diff --git a/c/utf8filenamecheck/windows/simdutf8check/simdutf8check.h b/c/utf8filenamecheck/windows/simdutf8check/simdutf8check.h new file mode 100644 index 0000000..7644bfa --- /dev/null +++ b/c/utf8filenamecheck/windows/simdutf8check/simdutf8check.h @@ -0,0 +1,457 @@ +#ifndef SIMDUTF8CHECK_H +#define SIMDUTF8CHECK_H +#include +#include +#include +#include +#include +/* + * legal utf-8 byte sequence + * http://www.unicode.org/versions/Unicode6.0.0/ch03.pdf - page 94 + * + * Code Points 1st 2s 3s 4s + * U+0000..U+007F 00..7F + * U+0080..U+07FF C2..DF 80..BF + * U+0800..U+0FFF E0 A0..BF 80..BF + * U+1000..U+CFFF E1..EC 80..BF 80..BF + * U+D000..U+D7FF ED 80..9F 80..BF + * U+E000..U+FFFF EE..EF 80..BF 80..BF + * U+10000..U+3FFFF F0 90..BF 80..BF 80..BF + * U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF + * U+100000..U+10FFFF F4 80..8F 80..BF 80..BF + * + */ + +// all byte values must be no larger than 0xF4 +static inline void checkSmallerThan0xF4(__m128i current_bytes, + __m128i *has_error) { + // unsigned, saturates to 0 below max + *has_error = _mm_or_si128(*has_error, + _mm_subs_epu8(current_bytes, _mm_set1_epi8(0xF4))); +} + +static inline __m128i continuationLengths(__m128i high_nibbles) { + return _mm_shuffle_epi8( + _mm_setr_epi8(1, 1, 1, 1, 1, 1, 1, 1, // 0xxx (ASCII) + 0, 0, 0, 0, // 10xx (continuation) + 2, 2, // 110x + 3, // 1110 + 4), // 1111, next should be 0 (not checked here) + high_nibbles); +} + +static inline __m128i carryContinuations(__m128i initial_lengths, + __m128i previous_carries) { + + __m128i right1 = + _mm_subs_epu8(_mm_alignr_epi8(initial_lengths, previous_carries, 16 - 1), + _mm_set1_epi8(1)); + __m128i sum = _mm_add_epi8(initial_lengths, right1); + + __m128i right2 = _mm_subs_epu8(_mm_alignr_epi8(sum, previous_carries, 16 - 2), + _mm_set1_epi8(2)); + return _mm_add_epi8(sum, right2); +} + +static inline void checkContinuations(__m128i initial_lengths, __m128i carries, + __m128i *has_error) { + + // overlap || underlap + // carry > length && length > 0 || !(carry > length) && !(length > 0) + // (carries > length) == (lengths > 0) + __m128i overunder = + _mm_cmpeq_epi8(_mm_cmpgt_epi8(carries, initial_lengths), + _mm_cmpgt_epi8(initial_lengths, _mm_setzero_si128())); + + *has_error = _mm_or_si128(*has_error, overunder); +} + +// when 0xED is found, next byte must be no larger than 0x9F +// when 0xF4 is found, next byte must be no larger than 0x8F +// next byte must be continuation, ie sign bit is set, so signed < is ok +static inline void checkFirstContinuationMax(__m128i current_bytes, + __m128i off1_current_bytes, + __m128i *has_error) { + __m128i maskED = _mm_cmpeq_epi8(off1_current_bytes, _mm_set1_epi8(0xED)); + __m128i maskF4 = _mm_cmpeq_epi8(off1_current_bytes, _mm_set1_epi8(0xF4)); + + __m128i badfollowED = + _mm_and_si128(_mm_cmpgt_epi8(current_bytes, _mm_set1_epi8(0x9F)), maskED); + __m128i badfollowF4 = + _mm_and_si128(_mm_cmpgt_epi8(current_bytes, _mm_set1_epi8(0x8F)), maskF4); + + *has_error = _mm_or_si128(*has_error, _mm_or_si128(badfollowED, badfollowF4)); +} + +// map off1_hibits => error condition +// hibits off1 cur +// C => < C2 && true +// E => < E1 && < A0 +// F => < F1 && < 90 +// else false && false +static inline void checkOverlong(__m128i current_bytes, + __m128i off1_current_bytes, __m128i hibits, + __m128i previous_hibits, __m128i *has_error) { + __m128i off1_hibits = _mm_alignr_epi8(hibits, previous_hibits, 16 - 1); + __m128i initial_mins = _mm_shuffle_epi8( + _mm_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128, -128, + -128, -128, // 10xx => false + 0xC2, -128, // 110x + 0xE1, // 1110 + 0xF1), + off1_hibits); + + __m128i initial_under = _mm_cmpgt_epi8(initial_mins, off1_current_bytes); + + __m128i second_mins = _mm_shuffle_epi8( + _mm_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128, -128, + -128, -128, // 10xx => false + 127, 127, // 110x => true + 0xA0, // 1110 + 0x90), + off1_hibits); + __m128i second_under = _mm_cmpgt_epi8(second_mins, current_bytes); + *has_error = + _mm_or_si128(*has_error, _mm_and_si128(initial_under, second_under)); +} + +struct processed_utf_bytes { + __m128i rawbytes; + __m128i high_nibbles; + __m128i carried_continuations; +}; + +static inline void count_nibbles(__m128i bytes, + struct processed_utf_bytes *answer) { + answer->rawbytes = bytes; + answer->high_nibbles = + _mm_and_si128(_mm_srli_epi16(bytes, 4), _mm_set1_epi8(0x0F)); +} + +// check whether the current bytes are valid UTF-8 +// at the end of the function, previous gets updated +static struct processed_utf_bytes +checkUTF8Bytes(__m128i current_bytes, struct processed_utf_bytes *previous, + __m128i *has_error) { + struct processed_utf_bytes pb; + count_nibbles(current_bytes, &pb); + + checkSmallerThan0xF4(current_bytes, has_error); + + __m128i initial_lengths = continuationLengths(pb.high_nibbles); + + pb.carried_continuations = + carryContinuations(initial_lengths, previous->carried_continuations); + + checkContinuations(initial_lengths, pb.carried_continuations, has_error); + + __m128i off1_current_bytes = + _mm_alignr_epi8(pb.rawbytes, previous->rawbytes, 16 - 1); + checkFirstContinuationMax(current_bytes, off1_current_bytes, has_error); + + checkOverlong(current_bytes, off1_current_bytes, pb.high_nibbles, + previous->high_nibbles, has_error); + return pb; +} + +static bool validate_utf8_fast(const char *src, size_t len) { + size_t i = 0; + __m128i has_error = _mm_setzero_si128(); + struct processed_utf_bytes previous = {.rawbytes = _mm_setzero_si128(), + .high_nibbles = _mm_setzero_si128(), + .carried_continuations = + _mm_setzero_si128()}; + if (len >= 16) { + for (; i <= len - 16; i += 16) { + __m128i current_bytes = _mm_loadu_si128((const __m128i *)(src + i)); + previous = checkUTF8Bytes(current_bytes, &previous, &has_error); + } + } + + // last part + if (i < len) { + char buffer[16]; + memset(buffer, 0, 16); + memcpy(buffer, src + i, len - i); + __m128i current_bytes = _mm_loadu_si128((const __m128i *)(buffer)); + previous = checkUTF8Bytes(current_bytes, &previous, &has_error); + } else { + has_error = + _mm_or_si128(_mm_cmpgt_epi8(previous.carried_continuations, + _mm_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 1)), + has_error); + } + + return _mm_testz_si128(has_error, has_error); +} + +#ifdef __AVX2__ + +/*****************************/ +static inline __m256i push_last_byte_of_a_to_b(__m256i a, __m256i b) { + return _mm256_alignr_epi8(b, _mm256_permute2x128_si256(a, b, 0x21), 15); +} + +static inline __m256i push_last_2bytes_of_a_to_b(__m256i a, __m256i b) { + return _mm256_alignr_epi8(b, _mm256_permute2x128_si256(a, b, 0x21), 14); +} + +// all byte values must be no larger than 0xF4 +static inline void avxcheckSmallerThan0xF4(__m256i current_bytes, + __m256i *has_error) { + // unsigned, saturates to 0 below max + *has_error = _mm256_or_si256( + *has_error, _mm256_subs_epu8(current_bytes, _mm256_set1_epi8(0xF4))); +} + +static inline __m256i avxcontinuationLengths(__m256i high_nibbles) { + return _mm256_shuffle_epi8( + _mm256_setr_epi8(1, 1, 1, 1, 1, 1, 1, 1, // 0xxx (ASCII) + 0, 0, 0, 0, // 10xx (continuation) + 2, 2, // 110x + 3, // 1110 + 4, // 1111, next should be 0 (not checked here) + 1, 1, 1, 1, 1, 1, 1, 1, // 0xxx (ASCII) + 0, 0, 0, 0, // 10xx (continuation) + 2, 2, // 110x + 3, // 1110 + 4 // 1111, next should be 0 (not checked here) + ), + high_nibbles); +} + +static inline __m256i avxcarryContinuations(__m256i initial_lengths, + __m256i previous_carries) { + + __m256i right1 = _mm256_subs_epu8( + push_last_byte_of_a_to_b(previous_carries, initial_lengths), + _mm256_set1_epi8(1)); + __m256i sum = _mm256_add_epi8(initial_lengths, right1); + + __m256i right2 = _mm256_subs_epu8( + push_last_2bytes_of_a_to_b(previous_carries, sum), _mm256_set1_epi8(2)); + return _mm256_add_epi8(sum, right2); +} + +static inline void avxcheckContinuations(__m256i initial_lengths, + __m256i carries, __m256i *has_error) { + + // overlap || underlap + // carry > length && length > 0 || !(carry > length) && !(length > 0) + // (carries > length) == (lengths > 0) + __m256i overunder = _mm256_cmpeq_epi8( + _mm256_cmpgt_epi8(carries, initial_lengths), + _mm256_cmpgt_epi8(initial_lengths, _mm256_setzero_si256())); + + *has_error = _mm256_or_si256(*has_error, overunder); +} + +// when 0xED is found, next byte must be no larger than 0x9F +// when 0xF4 is found, next byte must be no larger than 0x8F +// next byte must be continuation, ie sign bit is set, so signed < is ok +static inline void avxcheckFirstContinuationMax(__m256i current_bytes, + __m256i off1_current_bytes, + __m256i *has_error) { + __m256i maskED = + _mm256_cmpeq_epi8(off1_current_bytes, _mm256_set1_epi8(0xED)); + __m256i maskF4 = + _mm256_cmpeq_epi8(off1_current_bytes, _mm256_set1_epi8(0xF4)); + + __m256i badfollowED = _mm256_and_si256( + _mm256_cmpgt_epi8(current_bytes, _mm256_set1_epi8(0x9F)), maskED); + __m256i badfollowF4 = _mm256_and_si256( + _mm256_cmpgt_epi8(current_bytes, _mm256_set1_epi8(0x8F)), maskF4); + + *has_error = + _mm256_or_si256(*has_error, _mm256_or_si256(badfollowED, badfollowF4)); +} + +// map off1_hibits => error condition +// hibits off1 cur +// C => < C2 && true +// E => < E1 && < A0 +// F => < F1 && < 90 +// else false && false +static inline void avxcheckOverlong(__m256i current_bytes, + __m256i off1_current_bytes, __m256i hibits, + __m256i previous_hibits, + __m256i *has_error) { + __m256i off1_hibits = push_last_byte_of_a_to_b(previous_hibits, hibits); + __m256i initial_mins = _mm256_shuffle_epi8( + _mm256_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128, + -128, -128, -128, // 10xx => false + 0xC2, -128, // 110x + 0xE1, // 1110 + 0xF1, -128, -128, -128, -128, -128, -128, -128, -128, + -128, -128, -128, -128, // 10xx => false + 0xC2, -128, // 110x + 0xE1, // 1110 + 0xF1), + off1_hibits); + + __m256i initial_under = _mm256_cmpgt_epi8(initial_mins, off1_current_bytes); + + __m256i second_mins = _mm256_shuffle_epi8( + _mm256_setr_epi8(-128, -128, -128, -128, -128, -128, -128, -128, -128, + -128, -128, -128, // 10xx => false + 127, 127, // 110x => true + 0xA0, // 1110 + 0x90, -128, -128, -128, -128, -128, -128, -128, -128, + -128, -128, -128, -128, // 10xx => false + 127, 127, // 110x => true + 0xA0, // 1110 + 0x90), + off1_hibits); + __m256i second_under = _mm256_cmpgt_epi8(second_mins, current_bytes); + *has_error = _mm256_or_si256(*has_error, + _mm256_and_si256(initial_under, second_under)); +} + +struct avx_processed_utf_bytes { + __m256i rawbytes; + __m256i high_nibbles; + __m256i carried_continuations; +}; + +static inline void avx_count_nibbles(__m256i bytes, + struct avx_processed_utf_bytes *answer) { + answer->rawbytes = bytes; + answer->high_nibbles = + _mm256_and_si256(_mm256_srli_epi16(bytes, 4), _mm256_set1_epi8(0x0F)); +} + +// check whether the current bytes are valid UTF-8 +// at the end of the function, previous gets updated +static struct avx_processed_utf_bytes +avxcheckUTF8Bytes(__m256i current_bytes, + struct avx_processed_utf_bytes *previous, + __m256i *has_error) { + struct avx_processed_utf_bytes pb; + avx_count_nibbles(current_bytes, &pb); + + avxcheckSmallerThan0xF4(current_bytes, has_error); + + __m256i initial_lengths = avxcontinuationLengths(pb.high_nibbles); + + pb.carried_continuations = + avxcarryContinuations(initial_lengths, previous->carried_continuations); + + avxcheckContinuations(initial_lengths, pb.carried_continuations, has_error); + + __m256i off1_current_bytes = + push_last_byte_of_a_to_b(previous->rawbytes, pb.rawbytes); + avxcheckFirstContinuationMax(current_bytes, off1_current_bytes, has_error); + + avxcheckOverlong(current_bytes, off1_current_bytes, pb.high_nibbles, + previous->high_nibbles, has_error); + return pb; +} + +// check whether the current bytes are valid UTF-8 +// at the end of the function, previous gets updated +static struct avx_processed_utf_bytes +avxcheckUTF8Bytes_asciipath(__m256i current_bytes, + struct avx_processed_utf_bytes *previous, + __m256i *has_error) { + if (_mm256_testz_si256(current_bytes, + _mm256_set1_epi8(0x80))) { // fast ascii path + *has_error = _mm256_or_si256( + _mm256_cmpgt_epi8(previous->carried_continuations, + _mm256_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 1)), + *has_error); + return *previous; + } + + struct avx_processed_utf_bytes pb; + avx_count_nibbles(current_bytes, &pb); + + avxcheckSmallerThan0xF4(current_bytes, has_error); + + __m256i initial_lengths = avxcontinuationLengths(pb.high_nibbles); + + pb.carried_continuations = + avxcarryContinuations(initial_lengths, previous->carried_continuations); + + avxcheckContinuations(initial_lengths, pb.carried_continuations, has_error); + + __m256i off1_current_bytes = + push_last_byte_of_a_to_b(previous->rawbytes, pb.rawbytes); + avxcheckFirstContinuationMax(current_bytes, off1_current_bytes, has_error); + + avxcheckOverlong(current_bytes, off1_current_bytes, pb.high_nibbles, + previous->high_nibbles, has_error); + return pb; +} + +static bool validate_utf8_fast_avx_asciipath(const char *src, size_t len) { + size_t i = 0; + __m256i has_error = _mm256_setzero_si256(); + struct avx_processed_utf_bytes previous = { + .rawbytes = _mm256_setzero_si256(), + .high_nibbles = _mm256_setzero_si256(), + .carried_continuations = _mm256_setzero_si256()}; + if (len >= 32) { + for (; i <= len - 32; i += 32) { + __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(src + i)); + previous = + avxcheckUTF8Bytes_asciipath(current_bytes, &previous, &has_error); + } + } + + // last part + if (i < len) { + char buffer[32]; + memset(buffer, 0, 32); + memcpy(buffer, src + i, len - i); + __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(buffer)); + previous = avxcheckUTF8Bytes(current_bytes, &previous, &has_error); + } else { + has_error = _mm256_or_si256( + _mm256_cmpgt_epi8(previous.carried_continuations, + _mm256_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 1)), + has_error); + } + + return _mm256_testz_si256(has_error, has_error); +} + +static bool validate_utf8_fast_avx(const char *src, size_t len) { + size_t i = 0; + __m256i has_error = _mm256_setzero_si256(); + struct avx_processed_utf_bytes previous = { + .rawbytes = _mm256_setzero_si256(), + .high_nibbles = _mm256_setzero_si256(), + .carried_continuations = _mm256_setzero_si256()}; + if (len >= 32) { + for (; i <= len - 32; i += 32) { + __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(src + i)); + previous = avxcheckUTF8Bytes(current_bytes, &previous, &has_error); + } + } + + // last part + if (i < len) { + char buffer[32]; + memset(buffer, 0, 32); + memcpy(buffer, src + i, len - i); + __m256i current_bytes = _mm256_loadu_si256((const __m256i *)(buffer)); + previous = avxcheckUTF8Bytes(current_bytes, &previous, &has_error); + } else { + has_error = _mm256_or_si256( + _mm256_cmpgt_epi8(previous.carried_continuations, + _mm256_setr_epi8(9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, + 9, 9, 9, 9, 9, 9, 9, 1)), + has_error); + } + + return _mm256_testz_si256(has_error, has_error); +} + +#endif // __AVX2__ +#endif diff --git a/c/utf8filenamecheck/windows/utf8filenamecheck.c b/c/utf8filenamecheck/windows/utf8filenamecheck.c new file mode 100644 index 0000000..764cc09 --- /dev/null +++ b/c/utf8filenamecheck/windows/utf8filenamecheck.c @@ -0,0 +1,147 @@ +/** + * utf8filenamecheck Check if all the filenames in a fiven folder are UTF-8 + * Copyright (C) 2023 Johannes 'Banana' Keßler + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * 2023 Small windows C tool to check if paths in a folder are utf-8 formatted + * This is the windows version + */ + +#include +#include +#include +#include + +// https://linux.die.net/man/3/nftw +#define _XOPEN_SOURCE 500 +#include + +// https://www.argtable.org +#include + +// https://github.com/simdutf/simdutf +#include + +/** + * global arg_xxx structs + * https://www.argtable.org/ + */ +struct arg_lit *verbose, *quiet, *help; +struct arg_file *folderToRead; +struct arg_end *end; +const char *program_version = "1.0"; +const char *program_bug_address = "https://://www.bananas-playground.net"; + +struct cmdArguments { + int quiet, verbose; + char *folder_to_read; +}; + +struct cmdArguments arguments; + +/** + * the callback function for nftw + * https://linux.die.net/man/3/nftw + */ +static int nftw_callback(const char *fpath, + const struct stat *sb, + int tflag, + struct FTW *ftwbuf) { + if (strcmp(fpath, ".") == 0 || strcmp(fpath, "..") == 0) { + return 0; + } + + if(tflag == FTW_DNR) { + if(!arguments.quiet) printf("Can not read %s", fpath); + } + + bool result = validate_utf8_fast(fpath, strlen(fpath)); + if(result) { + if(!arguments.quiet) printf("%s Valid OK \n", fpath); + } else { + printf("%s Valid FAILED \n", fpath); + } + + // continue + return 0; +} + +/** + * the main stuff + */ +int main(int argc, char *argv[]) { + + /** + * command line argument default values + */ + arguments.quiet = 0; + arguments.verbose = 0; + arguments.folder_to_read = "."; + + /** + * https://www.argtable.org/ + */ + void *argtable[] = { + help = arg_litn(NULL, "help", 0, 1, "Display this help and exit"), + quiet = arg_litn("q", "quiet", 0, 1, "Display only false ones"), + verbose = arg_litn("v", "verbose", 0, 1, "Verbose additional output"), + folderToRead = arg_filen(NULL, NULL, "", 1, 1, "Folder to read"), + end = arg_end(20), + }; + /* argtable parsing */ + int nerrors; + nerrors = arg_parse(argc,argv,argtable); + /* special case: '--help' takes precedence over error reporting */ + if (help->count > 0) { + printf("Usage: utf8check.exe"); + arg_print_syntax(stdout, argtable, "\n"); + arg_print_glossary(stdout, argtable, " %-25s %s\n"); + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); + return(1); + } + /* If the parser returned any errors then display them and exit */ + if (nerrors > 0) { + /* Display the error details contained in the arg_end struct.*/ + arg_print_errors(stdout, end, "utf8check.exe"); + printf("Try '%s --help' for more information.\n", "utf8check.exe"); + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); + return(1); + } + else { + arguments.quiet = quiet->count; + arguments.verbose = verbose->count; + arguments.folder_to_read = folderToRead->filename[0]; + } + + if(arguments.verbose) { + printf ("Folder = %s\n" + "Verbose = %s\n" + "Quiet = %s\n\n", + arguments.folder_to_read, + arguments.verbose ? "yes" : "no", + arguments.quiet ? "yes" : "no" + ); + } + + if (nftw(arguments.folder_to_read, nftw_callback, 15, FTW_PHYS)== -1) { + perror("Reading dir failed"); + exit(EXIT_FAILURE); + } + + arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); + exit(EXIT_SUCCESS); +} \ No newline at end of file