commit 16952a12a592b32567a2f0eb46890edac74d2f5a
parent c7f4f5e1cbe021b3f04e1ed0aadb66b94da3b4d1
Author: Martin Klöckner <mjkloeckner@gmail.com>
Date: Tue, 30 Apr 2024 20:55:27 -0300
merge branch 'tp2'
Diffstat:
18 files changed, 1875 insertions(+), 0 deletions(-)
diff --git a/tps/2/.gitignore b/tps/2/.gitignore
@@ -0,0 +1,8 @@
+*.docx
+*.docx#
+*.o
+*.csv
+*.xml
+*.bin
+main
+*.sh
diff --git a/tps/2/LICENSE b/tps/2/LICENSE
@@ -0,0 +1,661 @@
+ GNU AFFERO GENERAL PUBLIC LICENSE
+ Version 3, 19 November 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The GNU Affero General Public License is a free, copyleft license for
+software and other kinds of works, specifically designed to ensure
+cooperation with the community in the case of network server software.
+
+ The licenses for most software and other practical works are designed
+to take away your freedom to share and change the works. By contrast,
+our General Public Licenses are 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.
+
+ 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.
+
+ Developers that use our General Public Licenses protect your rights
+with two steps: (1) assert copyright on the software, and (2) offer
+you this License which gives you legal permission to copy, distribute
+and/or modify the software.
+
+ A secondary benefit of defending all users' freedom is that
+improvements made in alternate versions of the program, if they
+receive widespread use, become available for other developers to
+incorporate. Many developers of free software are heartened and
+encouraged by the resulting cooperation. However, in the case of
+software used on network servers, this result may fail to come about.
+The GNU General Public License permits making a modified version and
+letting the public access it on a server without ever releasing its
+source code to the public.
+
+ The GNU Affero General Public License is designed specifically to
+ensure that, in such cases, the modified source code becomes available
+to the community. It requires the operator of a network server to
+provide the source code of the modified version running there to the
+users of that server. Therefore, public use of a modified version, on
+a publicly accessible server, gives the public access to the source
+code of the modified version.
+
+ An older license, called the Affero General Public License and
+published by Affero, was designed to accomplish similar goals. This is
+a different license, not a version of the Affero GPL, but Affero has
+released a new version of the Affero GPL which permits relicensing under
+this license.
+
+ 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 Affero 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. Remote Network Interaction; Use with the GNU General Public License.
+
+ Notwithstanding any other provision of this License, if you modify the
+Program, your modified version must prominently offer all users
+interacting with it remotely through a computer network (if your version
+supports such interaction) an opportunity to receive the Corresponding
+Source of your version by providing access to the Corresponding Source
+from a network server at no charge, through some standard or customary
+means of facilitating copying of software. This Corresponding Source
+shall include the Corresponding Source for any work covered by version 3
+of the GNU General Public License that is incorporated pursuant to the
+following paragraph.
+
+ 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 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 work with which it is combined will remain governed by version
+3 of the GNU General Public License.
+
+ 14. Revised Versions of this License.
+
+ The Free Software Foundation may publish revised and/or new versions of
+the GNU Affero 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 Affero 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 Affero 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 Affero 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.
+
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero 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 Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+Also add information on how to contact you by electronic and paper mail.
+
+ If your software can interact with users remotely through a computer
+network, you should also make sure that it provides a way for users to
+get its source. For example, if your program is a web application, its
+interface could display a "Source" link that leads users to an archive
+of the code. There are many ways you could offer source, and different
+solutions will be better for different programs; see section 13 for the
+specific requirements.
+
+ 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 AGPL, see
+<https://www.gnu.org/licenses/>.
diff --git a/tps/2/Makefile b/tps/2/Makefile
@@ -0,0 +1,70 @@
+CC=gcc
+CFLAGS=-pedantic -Wall -g
+SRCFOLDER=source
+HFOLDER=include
+PROGNAME=main
+
+all: main clean
+
+main: cla.o status.o utils.o main.o user.o vector.o load.o
+ $(CC) $(CFLAGS) main.o cla.o status.o utils.o user.o load.o vector.o -o $(PROGNAME)
+
+main.o: $(HFOLDER)/cla.h $(HFOLDER)/status.h $(HFOLDER)/user.h $(HFOLDER)/load.h
+ $(CC) $(CFLAGS) -c $(SRCFOLDER)/main.c
+
+cla.o: $(HFOLDER)/cla.h $(HFOLDER)/status.h
+ $(CC) $(CFLAGS) -c $(SRCFOLDER)/cla.c
+
+vector.o: $(HFOLDER)/vector.h $(HFOLDER)/status.h
+ $(CC) $(CFLAGS) -c $(SRCFOLDER)/vector.c
+
+status.o: $(HFOLDER)/status.h
+ $(CC) $(CFLAGS) -c $(SRCFOLDER)/status.c
+
+utils.o: $(HFOLDER)/status.h $(HFOLDER)/user.h $(HFOLDER)/vector.h
+ $(CC) $(CFLAGS) -c $(SRCFOLDER)/utils.c
+
+user.o: $(HFOLDER)/status.h $(HFOLDER)/user.h
+ $(CC) $(CFLAGS) -c $(SRCFOLDER)/user.c
+
+load.o: $(HFOLDER)/status.h $(HFOLDER)/user.h $(HFOLDER)/vector.h $(HFOLDER)/utils.h $(HFOLDER)/load.h $(HFOLDER)/cla.h
+ $(CC) $(CFLAGS) -c $(SRCFOLDER)/load.c
+
+clean:
+ rm *.o
+
+run5:
+ ./main -fmt csv -out output.csv -in examples/test_file_5.csv -ti 1320498000 -tf 1320498046
+
+run20:
+ ./main -fmt csv -out output.csv -in examples/test_file_20.csv -ti 1320498000 -tf 1320498046
+
+run2k:
+ ./main -fmt csv -out output.csv -in examples/test_file_2k.csv -ti 1320498000 -tf 1325499000
+
+run50:
+ ./main -fmt csv -out output.csv -in examples/test_file_50.csv -ti 1320498000 -tf 1320498049
+
+run500:
+ ./main -fmt csv -out output.csv -in examples/test_file_500.csv -ti 1320498000 -tf 1320529000
+
+run10k:
+ ./main -fmt csv -out output.csv -in examples/test_file_10k.csv -ti 1320498000 -tf 1320529000
+
+run50k:
+ ./main -fmt csv -out output.csv -in examples/test_file_50k.csv -ti 1320498000 -tf 1420529000
+
+run100k:
+ ./main -fmt csv -out output.csv -in examples/test_file_100k.csv -ti 1320498000 -tf 1420529000
+
+run500k:
+ ./main -fmt csv -out output.csv -in examples/test_file_500k.csv -ti 1320498000 -tf 1420529000
+
+run50m:
+ ./main -fmt csv -out output.csv -in examples/test_file_50m.csv -ti 1320498000 -tf 1420529000
+
+run50xml:
+ ./main -fmt xml -out output.xml -in examples/test_file_50.csv -ti 1320498000 -tf 1320498049
+
+run5kxml:
+ ./main -fmt xml -out output.xml -in examples/test_file_5k.csv -ti 1320498000 -tf 1360498049
diff --git a/tps/2/README.md b/tps/2/README.md
@@ -0,0 +1,2 @@
+# 9511_project03
+Command line program that parses a bank .csv file and generates and output file in format .csv or .xml containing the debits and credits of every user.
diff --git a/tps/2/examples/input_gen.py b/tps/2/examples/input_gen.py
@@ -0,0 +1,116 @@
+from string import digits
+from time import strftime, gmtime
+from random import randint, choice
+
+LINES = 100000
+
+# OUTPUT:
+# ID_TRANSACCION, ID_USUARIO, FECHA, MONTO, NUMERO DE TRAJETA, DESCRIPCION
+# 123412,1,05/11/2011 10:00:00,-10,4916288217067475, Compra supermercado
+
+descs = [ "Compras supermercado", "Pago tarjeta", "Compras libreria", "Pago Mecanico", "Pago Dentista", "Pago Servicios online", "Compras Ferreteria", "Compras Accesorios Informatica", "Compras farmacia", "Ventas online", "Extraccion cajero" ]
+
+def calculate_luhn(cc):
+ num = list(map(int, str(cc)))
+ check_digit = 10 - sum(num[-2::-2] + [sum(divmod(d * 2, 10)) for d in num[::-2]]) % 10
+ return 0 if check_digit == 10 else check_digit
+
+def generate_card(type):
+ """
+ Prefill some values based on the card type
+ """
+ card_types = ["americanexpress","visa13", "visa16","mastercard","discover"]
+
+ def prefill(t):
+ # typical number of digits in credit card
+ def_length = 16
+
+ """
+ Prefill with initial numbers and return it including the total number of digits
+ remaining to fill
+ """
+ if t == card_types[0]:
+ # american express starts with 3 and is 15 digits long
+ # override the def lengths
+ return [3, randint(4,7)], 13
+
+ elif t == card_types[1] or t == card_types[2]:
+ # visa starts with 4
+ if t.endswith("16"):
+ return [4], def_length - 1
+ else:
+ return [4], 12
+
+ elif t == card_types[3]:
+ # master card start with 5 and is 16 digits long
+ return [5, randint(1,5)], def_length - 2
+
+ elif t == card_types[4]:
+ # discover card starts with 6011 and is 16 digits long
+ return [6, 0, 1, 1], def_length - 4
+
+ else:
+ # this section probably not even needed here
+ return [], def_length
+
+ def finalize(nums):
+ """
+ Make the current generated list pass the Luhn check by checking and adding
+ the last digit appropriately bia calculating the check sum
+ """
+ check_sum = 0
+
+ #is_even = True if (len(nums) + 1 % 2) == 0 else False
+
+ """
+ Reason for this check offset is to figure out whether the final list is going
+ to be even or odd which will affect calculating the check_sum.
+ This is mainly also to avoid reversing the list back and forth which is specified
+ on the Luhn algorithm.
+ """
+ check_offset = (len(nums) + 1) % 2
+
+ for i, n in enumerate(nums):
+ if (i + check_offset) % 2 == 0:
+ n_ = n*2
+ check_sum += n_ -9 if n_ > 9 else n_
+ else:
+ check_sum += n
+ return nums + [10 - (check_sum % 10) ]
+
+ # main body
+ t = type.lower()
+ if t not in card_types:
+ print("Unknown type: '%s'" % type)
+ print("Please pick one of these supported types: %s" % card_types)
+ return
+
+ initial, rem = prefill(t)
+ so_far = initial + [randint(1,9) for x in range(rem - 1)]
+ return ("".join(map(str,finalize(so_far))))
+
+def generate_file(max_lines):
+ id_transaction_base = 123400
+ id_user_base = 1
+ id_user_max = 10000
+ amount_base = 1
+ amount_max = 100
+ for i in range(max_lines):
+ id_transaction = (i + id_transaction_base)
+ id_user = randint(id_user_base, id_user_max)
+
+ card_nr = generate_card("visa16")
+ while calculate_luhn(card_nr) != 0:
+ card_nr = generate_card("visa16")
+
+ date = strftime("%d.%m.%Y %H:%M:%S", gmtime(1320487200 + i))
+
+ j = randint(1, len(descs) - 1)
+ desc = descs[j]
+
+ if j > 8: amount = randint(amount_base, amount_max)
+ else: amount = randint(-amount_max, -amount_base)
+
+ print(id_transaction,id_user,date,amount,card_nr,desc, sep=',')
+
+generate_file(LINES)
diff --git a/tps/2/include/cla.h b/tps/2/include/cla.h
@@ -0,0 +1,40 @@
+#ifndef CLA__H
+#define CLA__H
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+
+#include "status.h"
+
+#define NO_ARGS_ENTERED 1
+#define NORMAL_AMOUNT_ARGS 11
+#define FLAGS_MAX 5
+#define FORMATS_MAX 2
+
+typedef enum {
+ FLAG_FMT,
+ FLAG_OUT,
+ FLAG_IN,
+ FLAG_TI,
+ FLAG_TF
+} flags_t;
+
+typedef struct {
+ char *fmt;
+ FILE *fi, *fo;
+ unsigned long ti, tf;
+} cla_T, *cla_t;
+
+status_t validate_arguments(int, char **, cla_t);
+status_t check_flags_position(int, char **);
+status_t check_flags_repeated(int, char **);
+
+status_t cla_create(cla_t *);
+status_t cla_setup(int, char **, cla_t);
+status_t cla_destroy(cla_t);
+
+extern const char *available_flags[FLAGS_MAX];
+extern const char *available_formats[FORMATS_MAX];
+
+#endif
diff --git a/tps/2/include/load.h b/tps/2/include/load.h
@@ -0,0 +1,19 @@
+#ifndef LOAD__H
+#define LOAD__H
+
+#include "../include/cla.h"
+#include "../include/user.h"
+#include "../include/utils.h"
+#include "../include/vector.h"
+#include "../include/status.h"
+
+#define STR_MSG_END_PROCSS_EN "End processing input file"
+
+#define STR_MSG_END_PROCSS_ES "Se termino el procesamiento del archivo"
+#define STR_MSG_LINES_PROCSSD_ES "Lineas procesadas:"
+#define STR_MSG_USRS_REGISTERD_ES "Usuarios registrados:"
+
+
+status_t load_users_to_vector(ADT_Vector_t **, cla_t);
+
+#endif
diff --git a/tps/2/include/status.h b/tps/2/include/status.h
@@ -0,0 +1,45 @@
+#ifndef STATUS__H
+#define STATUS__H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+
+#define MSG_ERROR_RETRY "Verifique y vuelva a intentar"
+#define MSG_EXIT_SUCCESS "Ejecución terminada exitosamente"
+#define MSG_USERS_REGISTERED "Usuarios registrados"
+#define MSG_PROCESED_LINES "Lineas procesadas"
+#define STR_INVALID_CARD_NUMBER "Número no válido"
+
+#define STATUS_T_MAX 13
+
+typedef enum {
+ OK,
+ ELEM_NOT_FOUND,
+ ERROR_MEMORY,
+ ERROR_WRONG_FLAGS,
+ ERROR_WRONG_TIME,
+ ERROR_INVALID_POS,
+ ERROR_MISSING_ARGS,
+ ERROR_OPENING_FILE,
+ ERROR_CORRUPT_DATA,
+ ERROR_FLAG_REPEATED,
+ ERROR_FLAG_NOT_FOUND,
+ ERROR_FORMAT_NOT_FOUND,
+ ERROR_NULL_POINTER
+} status_t;
+
+typedef enum {
+ POS_ID_TXN,
+ POS_USER_ID,
+ POS_TXN_DATE,
+ POS_AMOUNT,
+ POS_CARD_NUMBER,
+ POS_DESC
+} csv_pos_t;
+
+void show_status(status_t st);
+
+extern const char *status_strings[STATUS_T_MAX];
+
+#endif
diff --git a/tps/2/include/user.h b/tps/2/include/user.h
@@ -0,0 +1,29 @@
+#ifndef USER__H
+#define USER__H
+
+#include "status.h"
+#include "utils.h"
+
+#define CSV_OUT_FILE_DELIM ","
+
+typedef unsigned long ulong;
+
+typedef struct {
+ /* id: user id / c: user credits / d: user debits */
+ ulong id, c, d;
+} user_t;
+
+status_t user_create(user_t **);
+status_t user_set_data(user_t *, ulong, ulong, ulong);
+status_t user_destroy(user_t **);
+
+int user_equals(const void *, const void *);
+
+status_t user_print_as_csv(const void *, FILE *);
+status_t user_print_as_xml(const void *, FILE *);
+
+int user_comparator_credits_minmax(const void *, const void *);
+int user_comparator_credits_maxmin(const void *, const void *);
+
+
+#endif
diff --git a/tps/2/include/utils.h b/tps/2/include/utils.h
@@ -0,0 +1,46 @@
+#ifndef UTILS__H
+#define UTILS__H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdbool.h>
+
+#include "cla.h"
+#include "user.h"
+#include "status.h"
+#include "vector.h"
+
+#define _XOPEN_SOURCE
+#define __USE_XOPEN
+#include <time.h>
+
+#define IN_FILE_MAX_LEN 100
+#define IN_FILE_DELIM ","
+#define IN_FILE_FIELDS 6
+#define IN_FILE_FIELDS_MAX_LEN 50
+
+#define STR_FMT_CSV "csv"
+#define STR_FMT_XML "xml"
+
+#define CARD_NO_VALID_LEN 16
+
+#define XML_STR_HEADER "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+#define XML_STR_ROOT "root"
+#define XML_STR_ROW "row"
+#define XML_STR_ID "user"
+#define XML_STR_CREDIT "credits"
+#define XML_STR_DEBIT "debits"
+
+status_t string_split(char *, char **, char *);
+void clean_array(char **);
+void clean_buffer(char *);
+
+status_t create_2darray(char ***, size_t, size_t);
+status_t destroy_2darray(char **, size_t);
+
+status_t get_date(time_t *, char *);
+
+bool is_valid_card(char *);
+
+#endif
diff --git a/tps/2/include/vector.h b/tps/2/include/vector.h
@@ -0,0 +1,44 @@
+#ifndef VECTOR__H
+#define VECTOR__H
+
+#include <stdlib.h>
+
+#include "status.h"
+
+#define VECTOR_INIT_SIZE 10
+#define VECTOR_GROWTH_FACTOR 2
+
+#define XML_STR_HEADER "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+#define XML_STR_ROOT "root"
+#define XML_STR_ROW "row"
+
+typedef status_t (*printer_t)(const void *, FILE *);
+typedef int (*comparator_t)(const void *, const void *);
+
+typedef struct {
+ void **a;
+ size_t size, alloc;
+
+ printer_t printer;
+ comparator_t comparator;
+
+} ADT_Vector_t;
+
+status_t ADT_Vector_create(ADT_Vector_t **);
+status_t ADT_Vector_add(ADT_Vector_t **, void *);
+status_t ADT_Vector_destroy(ADT_Vector_t **);
+
+status_t ADT_Vector_print(const ADT_Vector_t *, FILE *);
+status_t ADT_Vector_sort(ADT_Vector_t *, comparator_t);
+
+status_t ADT_Vector_set_elem(ADT_Vector_t **, void *, size_t);
+status_t ADT_Vector_get_elem(const ADT_Vector_t *, void **, size_t);
+status_t ADT_Vector_get_elem_pos(const ADT_Vector_t *, void *, size_t *);
+
+status_t ADT_Vector_set_printer(ADT_Vector_t *, printer_t);
+status_t ADT_Vector_set_comparator(ADT_Vector_t *, comparator_t);
+
+status_t ADT_Vector_export_as_xml(const ADT_Vector_t *, FILE *);
+status_t ADT_Vector_export_as_csv(const ADT_Vector_t *, FILE *);
+
+#endif
diff --git a/tps/2/source/cla.c b/tps/2/source/cla.c
@@ -0,0 +1,146 @@
+#include "../include/cla.h"
+
+const char *available_flags[] = { "-fmt", "-out", "-in", "-ti", "-tf" };
+const char *available_formats[] = { "csv", "xml" };
+
+status_t validate_arguments(int argc, char **argv, cla_t cla)
+{
+ status_t st;
+
+ if(argv == NULL || cla == NULL) return ERROR_NULL_POINTER;
+ if((argc == NO_ARGS_ENTERED) || (argc != NORMAL_AMOUNT_ARGS))
+ return ERROR_MISSING_ARGS;
+
+ if((st = check_flags_position(argc, argv))) return st;
+ if((st = check_flags_repeated(argc, argv))) return st;
+
+ /* Asigna a la estructura 'cla' los argumentos ingresados */
+ if((st = cla_setup(argc, argv, cla))) return st;
+
+ return OK;
+}
+
+/* No chequea argumentos ya que se considera subfuncion de validate_arguments */
+status_t check_flags_position(int argc, char **argv)
+{
+ size_t i;
+ bool prev_was_flag = 0;
+ bool current_arg_is_flag = 0;
+
+ for(i = 1; i < argc; i++, prev_was_flag = current_arg_is_flag) {
+ if(argv[i][0] == '-') current_arg_is_flag = 1;
+ else current_arg_is_flag = 0;
+
+ if(current_arg_is_flag && prev_was_flag) return ERROR_WRONG_FLAGS;
+ }
+ return OK;
+}
+
+status_t check_flags_repeated(int argc, char **argv)
+{
+ size_t i, j, k, fflags_index;
+ int founded_flags[FLAGS_MAX];
+
+ if(argv == NULL) return ERROR_NULL_POINTER;
+
+ /* Inicializa a -1 para evitar confusiones con 0 */
+ for(i = 0; i < FLAGS_MAX; i++) founded_flags[i] = -1;
+
+ /* Itera solo sobre las flags, guarda la posicion de la flag con respecto
+ * a available_flags a forma de tener una manera de checkear por repeticiones,
+ * si la posicion aparece multiples veces es un error */
+ for(i = 1, fflags_index = 0; i <= (argc - 2); i += 2) {
+ for(j = 0; j < FLAGS_MAX; j++) {
+ if(!strcmp(argv[i], available_flags[j])) {
+ for(k = 0; k < fflags_index; k++)
+ if(founded_flags[k] == j)
+ return ERROR_FLAG_REPEATED;
+
+ founded_flags[fflags_index++] = j;
+ break;
+ }
+ if((j + 1) == FLAGS_MAX) return ERROR_FLAG_NOT_FOUND;
+ }
+ }
+ return OK;
+}
+
+status_t cla_setup(int argc, char **argv, cla_t cla)
+{
+ char *endptr;
+ size_t i;
+ FILE *fp;
+ flags_t f;
+
+ for(i = 1; i < argc; i += 2) {
+ for(f = FLAG_FMT; f < FLAGS_MAX; f++) {
+ if(!strcmp(available_flags[f], argv[i])) {
+ switch (f) {
+ case FLAG_FMT:
+ strcpy(cla->fmt, argv[i + 1]);
+ break;
+
+ case FLAG_OUT:
+ if((fp = fopen(argv[i + 1], "wt")) == NULL)
+ return ERROR_OPENING_FILE;
+
+ cla->fo = fp;
+ break;
+
+ case FLAG_IN:
+ if((fp = fopen(argv[i + 1], "rt")) == NULL)
+ return ERROR_OPENING_FILE;
+
+ cla->fi = fp;
+ break;
+
+ case FLAG_TI:
+ cla->ti = strtoul(argv[i + 1], &endptr, 10);
+ if(*endptr != '\0') return ERROR_WRONG_TIME;
+ break;
+
+ case FLAG_TF:
+ cla->tf = strtoul(argv[i + 1], &endptr, 10);
+ if(*endptr != '\0') return ERROR_WRONG_TIME;
+ break;
+
+ default: return ERROR_FLAG_NOT_FOUND;
+ }
+ }
+ }
+ }
+ return OK;
+}
+
+
+status_t cla_create(cla_t *cla)
+{
+ if(cla == NULL) return ERROR_NULL_POINTER;
+
+ if((*cla = (cla_t)malloc(sizeof(cla_T))) == NULL)
+ return ERROR_MEMORY;
+
+ if(((*cla)->fmt = calloc(sizeof(char), 100)) == NULL) {
+ free(cla);
+ cla = NULL;
+ return ERROR_MEMORY;
+ }
+
+ (*cla)->fi = NULL;
+ (*cla)->fo = NULL;
+
+ return OK;
+}
+
+status_t cla_destroy(cla_t cla)
+{
+ if(cla == NULL) return ERROR_NULL_POINTER;
+
+ if(cla->fi != NULL) fclose(cla->fi);
+ if(cla->fo != NULL) fclose(cla->fo);
+
+ free(cla->fmt);
+ free(cla);
+
+ return OK;
+}
diff --git a/tps/2/source/load.c b/tps/2/source/load.c
@@ -0,0 +1,122 @@
+#include "../include/load.h"
+
+status_t load_users_to_vector(ADT_Vector_t **v, cla_t cla)
+{
+ status_t st;
+ char buffer[IN_FILE_MAX_LEN];
+ char *endptr1, *endptr2, **data;
+ user_t *u1, *u2, *u3;
+ time_t epoch;
+ long long amount;
+ size_t user_pos, parsed_lines, users_created;
+ ulong id, c, d;
+
+ if(v == NULL || cla == NULL) return ERROR_NULL_POINTER;
+
+ if((st = create_2darray(&data, IN_FILE_FIELDS, IN_FILE_FIELDS_MAX_LEN)) != OK)
+ return st;
+
+ parsed_lines = 0;
+ users_created = 0;
+ while(fgets(buffer, IN_FILE_MAX_LEN, cla->fi)) {
+ if((st = user_create(&u1)) != OK) {
+ destroy_2darray(data, IN_FILE_FIELDS);
+ return st;
+ }
+
+ if((st = string_split(buffer, data, IN_FILE_DELIM)) != OK) {
+ free(u1);
+ destroy_2darray(data, IN_FILE_FIELDS);
+ return st;
+ }
+
+ id = strtol(data[POS_USER_ID], &endptr1, 10);
+ amount = strtol(data[POS_AMOUNT], &endptr2, 10);
+
+ if(*endptr1 || *endptr2) {
+ free(u1);
+ destroy_2darray(data, IN_FILE_FIELDS);
+ return ERROR_CORRUPT_DATA;
+ }
+
+ if(amount > 0) { c = amount; d = 0; }
+ else if(amount < 0) { c = 0; d = -amount; }
+
+ if((st = user_set_data(u1, id, c, d)) != OK) {
+ free(u1);
+ destroy_2darray(data, IN_FILE_FIELDS);
+ return ERROR_CORRUPT_DATA;
+ }
+
+ if((st = get_date(&epoch, data[POS_TXN_DATE])) != OK){
+ free(u1);
+ destroy_2darray(data, IN_FILE_FIELDS);
+ return ERROR_CORRUPT_DATA;
+ }
+
+ /* Comprueba el que el tiempo este dentro de los valores pasados como argumentos */
+ if(epoch < cla->ti) continue;
+ else if(epoch > cla->tf) break;
+
+ /* Comprueba que la tarjeta usada en la transaccion sea la correcta */
+ if(!is_valid_card(data[POS_CARD_NUMBER])) {
+ fprintf(stderr, "%s: %s\n", STR_INVALID_CARD_NUMBER, data[POS_CARD_NUMBER]);
+ continue;
+ }
+
+ /* Busca el id del usuario en el vector */
+ if((st = ADT_Vector_get_elem_pos(*v, u1, &user_pos)) == OK) {
+ /* Si llega aca lo encontró, entonces se lo asigna a un usuario temporal */
+ if((st = ADT_Vector_get_elem(*v, (void **)&u2, user_pos) != OK)) {
+ destroy_2darray(data, IN_FILE_FIELDS);
+ free(u1);
+ return st;
+ }
+
+ /* Crea un usuario nuevo para almacenar la copia */
+ if((st = user_create(&u3)) != OK) {
+ destroy_2darray(data, IN_FILE_FIELDS);
+ free(u1);
+ return st;
+ }
+
+ /* Copia el contenido del usuario */
+ *u3 = *u2;
+
+ /* Asigna a la copia el nuevo monto */
+ if(amount > 0) u3->c += amount;
+ else if(amount < 0) u2->d -= amount;
+
+ /* Asigna la copia al vector en el lugar que estaba el usuario original */
+ if((st = ADT_Vector_set_elem(v, u3, user_pos)) != OK) {
+ destroy_2darray(data, IN_FILE_FIELDS);
+ free(u1);
+ }
+
+ free(u1);
+ free(u2);
+ }
+
+ /* Hubo un error distinto a no encontrar el usuario */
+ else if(st != ELEM_NOT_FOUND) {
+ destroy_2darray(data, IN_FILE_FIELDS);
+ return st;
+ }
+
+ else {
+ /* Si no lo encuentra entonces lo agrega al vector */
+ if((st = ADT_Vector_add(v, u1)) != OK){
+ destroy_2darray(data, IN_FILE_FIELDS);
+ return st;
+ }
+ users_created++;
+ }
+ clean_buffer(buffer); clean_array(data);
+ parsed_lines++;
+ } /* End while */
+
+ destroy_2darray(data, IN_FILE_FIELDS);
+ printf("%s\n%s%11ld\n%s%8ld\n", STR_MSG_END_PROCSS_ES, STR_MSG_LINES_PROCSSD_ES, parsed_lines, STR_MSG_USRS_REGISTERD_ES, users_created);
+
+ return OK;
+}
diff --git a/tps/2/source/main.c b/tps/2/source/main.c
@@ -0,0 +1,92 @@
+#include "../include/cla.h"
+#include "../include/user.h"
+#include "../include/load.h"
+#include "../include/utils.h"
+#include "../include/status.h"
+#include "../include/vector.h"
+
+int main (int argc, char *argv[])
+{
+ status_t st;
+ cla_t cla;
+ ADT_Vector_t *v;
+
+ /* Asigna memoria a cla (c.l.a.: "command line arguments") */
+ if((st = cla_create(&cla)) != OK) {
+ show_status(st);
+ return st;
+ }
+
+ /* Valida que los argumentos sean correctos y asigna valores a cla */
+ if((st = validate_arguments(argc, argv, cla)) != OK) {
+ show_status(st);
+ cla_destroy(cla);
+ return st;
+ }
+
+ /* Crea un vector */
+ if((st = ADT_Vector_create(&v)) != OK) {
+ show_status(st);
+ cla_destroy(cla);
+ return st;
+ }
+
+ /* Setea el comparador a ADT_Vector */
+ if((st = ADT_Vector_set_comparator(v, user_equals)) != OK) {
+ show_status(st);
+ cla_destroy(cla);
+ ADT_Vector_destroy(&v);
+ return st;
+ }
+
+ /* Carga el vector con los usuarios del archivo de entrada */
+ if((st = load_users_to_vector(&v, cla)) != OK) {
+ show_status(st);
+ cla_destroy(cla);
+ ADT_Vector_destroy(&v);
+ return st;
+ }
+
+ /* Ordena el vector con los usuarios */
+ if((st = ADT_Vector_sort(v, user_comparator_credits_maxmin)) != OK) {
+ show_status(st);
+ cla_destroy(cla);
+ ADT_Vector_destroy(&v);
+ return st;
+ }
+
+ /* Setea el impresor e imprime ADT_Vector según el argumento ingresado */
+ if(!strcmp(cla->fmt, STR_FMT_CSV)) {
+ if((st = ADT_Vector_set_printer(v, user_print_as_csv)) != OK) {
+ show_status(st);
+ cla_destroy(cla);
+ ADT_Vector_destroy(&v);
+ return st;
+ }
+ /* E imprime el vector con los usuarios */
+ if((st = ADT_Vector_export_as_csv(v, cla->fo)) != OK) {
+ show_status(st);
+ cla_destroy(cla);
+ ADT_Vector_destroy(&v);
+ return st;
+ }
+ }
+ else if(!strcmp(cla->fmt, STR_FMT_XML)) {
+ if((st = ADT_Vector_set_printer(v, user_print_as_xml)) != OK) {
+ show_status(st);
+ cla_destroy(cla);
+ ADT_Vector_destroy(&v);
+ return st;
+ }
+ if((st = ADT_Vector_export_as_xml(v, cla->fo)) != OK) {
+ show_status(st);
+ cla_destroy(cla);
+ ADT_Vector_destroy(&v);
+ return st;
+ }
+ }
+ show_status(st);
+ cla_destroy(cla);
+ ADT_Vector_destroy(&v);
+ return OK;
+}
diff --git a/tps/2/source/status.c b/tps/2/source/status.c
@@ -0,0 +1,44 @@
+#include "../include/status.h"
+
+#ifdef __DEBUG_MODE__
+
+const char *status_strings[] = {
+ "(OK)",
+ "(ELEM_NOT_FOUND)",
+ "(ERROR_MEMORY)",
+ "(ERROR_WRONG_FLAGS)",
+ "(ERROR_WRONG_TIME)",
+ "(ERROR_INVALID_POS)"
+ "(ERROR_MISSING_ARGS)",
+ "(ERROR_OPENING_FILE)",
+ "(ERROR_CORRUPT_DATA)",
+ "(ERROR_FLAG_REPEATED)",
+ "(ERROR_FLAG_NOT_FOUND)",
+ "(ERROR_FORMAT_NOT_FOUND)",
+ "(ERROR_NULL_POINTER)"
+};
+
+#else
+
+const char *status_strings[] = {
+ "Todo se ejecuto correctamente.\nTenga un buen dia.",
+ "El elemento no pertenece al vector.",
+ "ERROR: Hubo un problema con la memoria, puede que sea escasa.",
+ "ERROR: No se reconoce un argumento.",
+ "ERROR: El tiempo ingresado no es correcto.",
+ "ERROR: En los argumentos ingresados.",
+ "ERROR: Faltan argumentos.",
+ "ERROR: El archivo de se pudo abrir, puede que no exista o el nombre sea incorrecto.",
+ "ERROR: Puede que el archivo de entrada este vacio, corrupto o no se encuentre.",
+ "ERROR: Hay una flag repetida.",
+ "ERROR: Flag no encontrada.",
+ "ERROR: El formato de salida no se reconoce.",
+ "ERROR: Hubo un problema durante la ejecucion."
+};
+
+#endif
+
+void show_status(status_t st)
+{
+ fprintf(stderr, "\n%s\n", status_strings[st]);
+}
diff --git a/tps/2/source/user.c b/tps/2/source/user.c
@@ -0,0 +1,100 @@
+#include "../include/user.h"
+
+status_t user_create(user_t **user)
+{
+ if((*user = (user_t *)malloc(sizeof(user_t))) == NULL)
+ return ERROR_MEMORY;
+
+ (*user)->id = 0;
+ (*user)->c = 0;
+ (*user)->d = 0;
+
+ return OK;
+}
+
+status_t user_destroy(user_t **user)
+{
+ if(user == NULL) return ERROR_NULL_POINTER;
+
+ free(*user);
+ *user = NULL;
+
+ return OK;
+}
+
+status_t user_set_data(user_t *user, ulong id, ulong c, ulong d)
+{
+ if(user == NULL) return ERROR_NULL_POINTER;
+
+ user->id = id;
+ user->c = c;
+ user->d = d;
+
+ return OK;
+}
+
+int user_equals(const void *a, const void *b)
+{
+ user_t *A, *B;
+
+ if(a == NULL || b == NULL) return ERROR_NULL_POINTER;
+
+ A = (user_t *)a;
+ B = (user_t *)b;
+
+ if(A->id == B->id) return 1;
+
+ return 0;
+}
+
+status_t user_print_as_csv(const void *u, FILE *fp)
+{
+ if(u == NULL || fp == NULL) return ERROR_NULL_POINTER;
+
+ user_t *user = (user_t *)u;
+
+ fprintf(fp, "%ld%s%ld%s%ld\n", user->id, CSV_OUT_FILE_DELIM, user->c, CSV_OUT_FILE_DELIM, user->d);
+
+ return OK;
+}
+
+status_t user_print_as_xml(const void *u, FILE *fp)
+{
+ if(u == NULL || fp == NULL) return ERROR_NULL_POINTER;
+
+ user_t *user = (user_t *)u;
+
+ fprintf(fp, "\t\t<%s>%ld</%s>\n", XML_STR_ID, user->id, XML_STR_ID);
+ fprintf(fp, "\t\t<%s>%ld</%s>\n", XML_STR_CREDIT, user->c, XML_STR_CREDIT);
+ fprintf(fp, "\t\t<%s>%ld</%s>\n", XML_STR_DEBIT, user->d, XML_STR_DEBIT);
+
+ return OK;
+}
+
+int user_comparator_credits_minmax(const void *a, const void *b)
+{
+ user_t *A, *B;
+
+ if(a == NULL || b == NULL) return ERROR_NULL_POINTER;
+
+ A = *(user_t **)a;
+ B = *(user_t **)b;
+
+ if(A->c > B->c) return 1;
+ else if(A->c == B->c) return 0;
+ return -1;
+}
+
+int user_comparator_credits_maxmin(const void *a, const void *b)
+{
+ user_t *A, *B;
+
+ if(a == NULL || b == NULL) return ERROR_NULL_POINTER;
+
+ A = *(user_t **)a;
+ B = *(user_t **)b;
+
+ if(A->c < B->c) return 1;
+ else if(A->c == B->c) return 0;
+ return -1;
+}
diff --git a/tps/2/source/utils.c b/tps/2/source/utils.c
@@ -0,0 +1,121 @@
+#include "../include/utils.h"
+
+status_t string_split(char *s, char **data, char *delim)
+{
+ char *p, *tmp;
+ size_t fields = 0;
+
+ if(s == NULL || data == NULL || delim == NULL)
+ return ERROR_NULL_POINTER;
+
+ for(p = s; (tmp = strtok(p, delim)); p = NULL)
+ strcpy(data[fields++], tmp);
+
+ if(fields != IN_FILE_FIELDS)
+ return ERROR_CORRUPT_DATA;
+
+ return OK;
+}
+
+void clean_buffer(char *buf)
+{
+ size_t i;
+
+ for(i = 0; i < IN_FILE_MAX_LEN; i++)
+ buf[i] = '\0';
+}
+
+void clean_array(char **data)
+{
+ size_t i, j;
+
+ for(i = 0; i < IN_FILE_FIELDS; i++) {
+ for(j = 0; j < IN_FILE_FIELDS_MAX_LEN; j++)
+ data[i][j] = '\0';
+ }
+}
+
+status_t create_2darray(char ***arr, size_t r, size_t c) {
+ size_t i;
+
+ if(((*arr) = malloc(sizeof(char *) * r)) == NULL)
+ return ERROR_MEMORY;
+
+ for(i = 0; i < r; i++) {
+ if(((*arr)[i] = calloc(sizeof(char), c)) == NULL) {
+ destroy_2darray((*arr), c);
+ return ERROR_MEMORY;
+ }
+ }
+
+ return OK;
+}
+
+status_t destroy_2darray(char **arr, size_t r)
+{
+ size_t i;
+
+ if(arr == NULL) return ERROR_NULL_POINTER;
+
+ for(i = 0; i < r; i++) {
+ free(arr[i]);
+ arr[i] = NULL;
+ }
+
+ free(arr);
+ return OK;
+}
+
+status_t get_date(time_t *e, char *str)
+{
+ struct tm tm;
+
+ if(e == NULL || str == NULL) return ERROR_NULL_POINTER;
+
+ /* Desactiva el horario de verano */
+ tm.tm_isdst = 0;
+
+ switch(str[2]) {
+ case '/':
+ strptime(str, "%d/%m/%Y %H:%M:%S", &tm);
+ *e = mktime(&tm);
+ break;
+ case '-':
+ strptime(str, "%d-%m-%Y %H:%M:%S", &tm);
+ *e = mktime(&tm);
+ break;
+ case '.':
+ strptime(str, "%d.%m.%Y %H:%M:%S", &tm);
+ *e = mktime(&tm);
+ break;
+
+ default: return ERROR_CORRUPT_DATA; break;
+ }
+ return OK;
+}
+
+bool is_valid_card(char *card_no)
+{
+ size_t i, j, k;
+ int arr[4][4], sum;
+ char tmp[2];
+
+ if(card_no == NULL) return false;
+ if(strlen(card_no) != CARD_NO_VALID_LEN) return false;
+
+ for(i = 0, k = 0, sum = 0; i < 4; i++) {
+ for(j = 0; j < 4; j++) {
+ arr[i][j] = (card_no[k++] - '0');
+ if(j % 2) {
+ arr[i][j] *= 2;
+ sprintf(tmp, "%d", arr[i][j]);
+ if(!(strlen(tmp) - 1)) arr[i][j] = (tmp[0] - '0');
+ else arr[i][j] = ((tmp[0] - '0') + (tmp[1] - '0'));
+
+ }
+ sum += arr[i][j];
+ }
+ }
+
+ return (sum % 10) ? false : true;
+}
diff --git a/tps/2/source/vector.c b/tps/2/source/vector.c
@@ -0,0 +1,170 @@
+#include "../include/vector.h"
+
+status_t ADT_Vector_create(ADT_Vector_t **v)
+{
+ if(v == NULL) return ERROR_NULL_POINTER;
+
+ if((*v = (ADT_Vector_t *)malloc(sizeof(ADT_Vector_t))) == NULL)
+ return ERROR_MEMORY;
+
+ if(((*v)->a = (void **)malloc(sizeof(void *) * VECTOR_INIT_SIZE)) == NULL)
+ return ERROR_MEMORY;
+
+ (*v)->size = 0;
+ (*v)->alloc = VECTOR_INIT_SIZE;
+ (*v)->printer = NULL;
+ (*v)->comparator = NULL;
+
+ return OK;
+}
+
+status_t ADT_Vector_add(ADT_Vector_t **v, void *e)
+{
+ void *tmp;
+
+ if(v == NULL || e == NULL) return ERROR_NULL_POINTER;
+
+ /* Extends the array in case low memory left */
+ if(((*v)->size + 1) == (*v)->alloc) {
+ if((tmp = (void **)realloc((*v)->a, sizeof(void *) * (*v)->alloc * VECTOR_GROWTH_FACTOR)) == NULL)
+ return ERROR_MEMORY;
+ else {
+ (*v)->a = tmp;
+ (*v)->alloc *= VECTOR_GROWTH_FACTOR;
+ }
+ }
+
+ (*v)->a[(*v)->size++] = e;
+
+ return OK;
+}
+
+status_t ADT_Vector_destroy(ADT_Vector_t **v)
+{
+ if(v == NULL) return ERROR_NULL_POINTER;
+
+ for(size_t i = 0; i < (*v)->size; i++) {
+ free((*v)->a[i]);
+ (*v)->a[i] = NULL;
+ }
+
+ free((*v)->a);
+ free(*v);
+ *v = NULL;
+
+ return OK;
+}
+
+/* Esta funcion no puede crear elementos nuevos, solo setear los que ya estan */
+status_t ADT_Vector_set_elem(ADT_Vector_t **v, void *e, size_t pos)
+{
+ if(v == NULL || e == NULL) return ERROR_NULL_POINTER;
+ else if(pos > (*v)->size) return ERROR_INVALID_POS;
+
+ (*v)->a[pos] = e;
+
+ return OK;
+}
+
+status_t ADT_Vector_get_elem(const ADT_Vector_t *v, void **e, size_t pos)
+{
+ if(v == NULL || e == NULL) return ERROR_NULL_POINTER;
+
+ *e = v->a[pos];
+
+ return OK;
+}
+
+status_t ADT_Vector_get_elem_pos(const ADT_Vector_t *v, void *e, size_t *pos)
+{
+ size_t i;
+
+ if(v == NULL || e == NULL) return ERROR_NULL_POINTER;
+
+ for(i = 0; i < v->size; i++) {
+ if(v->comparator(e, v->a[i])) {
+ *pos = i;
+ return OK;
+ }
+ }
+
+ return ELEM_NOT_FOUND;
+}
+
+status_t ADT_Vector_print(const ADT_Vector_t *v, FILE *fp)
+{
+ status_t st;
+ size_t i;
+
+ if(v == NULL) return ERROR_NULL_POINTER;
+
+ for(i = 0; i < v->size; i++)
+ if((st = v->printer(v->a[i], fp)) != OK)
+ return st;
+
+ return OK;
+}
+
+status_t ADT_Vector_sort(ADT_Vector_t *v, comparator_t pf)
+{
+ if(v == NULL) return ERROR_NULL_POINTER;
+
+ /* Quick sort */
+ qsort(v->a, v->size, sizeof(void *), pf);
+
+ return OK;
+}
+
+status_t ADT_Vector_set_printer(ADT_Vector_t *v, printer_t pf)
+{
+ if(v == NULL) return ERROR_NULL_POINTER;
+
+ v->printer = pf;
+
+ return OK;
+}
+
+status_t ADT_Vector_set_comparator(ADT_Vector_t *v, comparator_t pf)
+{
+ if(v == NULL) return ERROR_NULL_POINTER;
+
+ v->comparator = pf;
+
+ return OK;
+}
+
+status_t ADT_Vector_export_as_csv(const ADT_Vector_t *v, FILE *fp)
+{
+ status_t st;
+ size_t i;
+
+ if(v == NULL) return ERROR_NULL_POINTER;
+
+ for(i = 0; i < v->size; i++)
+ if((st = v->printer(v->a[i], fp)) != OK)
+ return st;
+
+ return OK;
+}
+
+status_t ADT_Vector_export_as_xml(const ADT_Vector_t *v, FILE *fp)
+{
+ status_t st;
+ size_t i;
+
+ if(v == NULL) return ERROR_NULL_POINTER;
+
+ fprintf(fp, "%s\n", XML_STR_HEADER);
+ fprintf(fp, "<%s>\n", XML_STR_ROOT);
+
+ for(i = 0; i < v->size; i++){
+ fprintf(fp, "\t<%s>\n", XML_STR_ROW);
+ if((st = v->printer(v->a[i], fp)) != OK)
+ return st;
+
+ fprintf(fp, "\t</%s>\n", XML_STR_ROW);
+ }
+
+ fprintf(fp, "</%s>\n", XML_STR_ROOT);
+ return OK;
+}