C代写:CIS2750 SVG Parser

使用libxml2对SVG文件进行解析,提取文件中关于Group, Rectangle, Circle, Path以及Attribute的信息。



This is Module 1 of Assignment 1. It will focus on creating a set of structs representing SVG files using libxml2. Module 2 will describe a set of accessor and search functions that will accompany the parser and the structs. Module 2 may also contain additional submission details.


In this assignment, you need to implement a library to parse the SVG files. SVG is one of the most common vector graphics formats, and is supported by every Web browser.

The link to the format description is posted in the Assignment 1 description. format before doing the assignment. Make sure you understand the

According to the specification, an SVG object contains:

  • one or more svg elements
  • 0 or more groups, which can contain other groups
  • a large number of primitives, along with text, colour attributes, fill attributes, etc..

Our Assignment 1 parser will assume a somewhat simpler SVG file:

  • one svg element
  • 0 or more groups, which can contain other groups
  • rectangles, circles, and paths - no other geometric primitives

Read the specification and pay attention to the specification details, e.g. what to do if the attributes x or y are not specified in a circle or rectangle.

This structure is represented by the various types in SVGParser.h.

The SVG standard uses the common XML language. To help you build the parser, you will use the libxml2 library, once of the most common C XML parsers. It will build an XML tree for you from an SVG file. You will then use it to create an SVGimage struct with all of its components. The XML parser will do most of the heavy lifting for you.

The documentation on libxml2 is available here: http://www.xmlsoft.org/html/index.html. You can use the sample code to get started, but you must site it in your submission. You cannot use any other sample code.

Your assignment will be graded using an automated test suite, so you must follow all requirements exactly, or you will lose marks.

Required Functions

Read the comments in SVGParser.h carefully. They provide additional implementation details. You must implement every function in SVGParser.h; they are also listed below. If you do not complete any of the functions, you must provide a stub for every one them.

Applications using the parser library will include SVGParser.h in their main program. The SVGParser.h header has been provided for you. Do not change it in any way. SVGParser.h is the public “face” our our vector image API. All the helper functions are internal implementation details, and should not be publicly/ globally visible. When we grade your code, we will use the standard SVGParser.h to compile and run it.

If you create additional header files, include them in the c. files that use them.
SVG parser functions

SVGimage* createSVGimage(char* fileName)

This function does the parsing and allocated a SVG image object. It accepts a filename If the file has been parsed successfully, a pointer to the newly created SVGimage object is returned. If the parsing fails for any reason, the function must return NULL.
Parsing can fail for a number of reasons, but libxml hides them from us. The xmlReadFile function will simply return NULL instead of an XML doc if the file is invalid for any reason.

char* SVGimageToString(SVGimage* img)

This function returns a humanly readable string representation of the entire vector image object. It will be used mostly by you, for debugging your parser. It must not modify the vector image object in any way. The function must allocate the string dynamically.

Void deleteSVGimage(SVGimage* img);

This function deallocates the object, including all of its subcomponents.

Helper functions

In addition the above functions, you must also write a number of helper functions. We will need to store the types Group, Rectangle, Circle, Path, and Attribute in lists.

void deleteAttribute(void* data);
char* attributeToString(void* data);
int compareAttributes(const void *first, const void *second);

void deleteGroup(void* data);
char* groupToString(void* data);
int compareGroups(const void *first, const void *second);

void deleteRectangle(void* data);
char* rectangleToString(void* data);
int compareRectangles(const void *first, const void *second);

void deleteCircle(void* data);
char* circleToString(void* data);
int compareCircles(const void *first, const void *second);

void deletePath(void* data);
char* pathToString(void* data);
int comparePaths(const void *first, const void *second);

Additional guidelines and requirements

In addition, it is strongly recommended that you write additional helpers functions for parsing the file - e.g. creating a Circle, Group, etc. from an xmlNode. You are free to create your additional “helper functions” in a separate .c file, if you find some recurring processing steps that you would like to factor out into a single place.

Do not place headers for these additional helper function in SVGParser.h. They must be in a separate header file, since they are internal to your implementation and not for public users of the parser package.

For your own test purposes, you will also want to code a main program in another .c file that calls your functions with a variety of test cases, However, you must not submit that file with your assignment. Also, do not put your main() function in SVGParser.h. Failure to do this may cause the test program will fail if you incorrectly include main() in our shared library file; you will lose marks for that, and may get a 0 for the assignment.

Your functions are supposed to be robust. They will be tested with various kinds of invalid data and must detect problems without crashing. If your functions encounter a problem, they must free all memory and return.

Function naming

You are welcome to name your helper functions as you see fit. However, do not put the underscore (_) character at the start of your function names. That is reserved solely for the test harness functions. Failure to do so may result in run-time errors due to name collisions - and a grade of zero (0) as a result.

Linked list

You are expected to use a linked list for storing various vector image components. You can use the list implementation that I use in the class examples (and that you use for A0). You can also use your own. However, your implementation must be compliant with the List API defined in LinkedListAPI.h. Failure to do so may result in a grade deductions, up to and including a grade of zero.

  1. Implement a simple XML parser
  2. Implement a simple SVGimage parser that that extracts the required property of the SVG image component and a namespace). (an svg
  3. Add a basic deleteSVGimage functionality and test for memory leaks
  4. Add a basic SVGimageToString functionality and test for memory leaks
  5. Add handling of multiple attributes for an svg component
    • Update deleteSVGimage and SVGimageToString functionality.
    • Test for memory leaks
  6. Add handling of one of the geometric primitive types, e.g. Rectangles.
    • Update deleteSVGimage and SVGimageToString functionality.
    • Test for memory leaks
  7. Add handling of Rectangles with attributes
  8. Add handling of Circles and Paths (one at a time)
  9. Add handling of Circles and Paths with attributes
  10. Add handling of Groups
    • you guessed it
    • you guessed it
  11. Add handling of Groups with attributes
    • Have a beer, and ignore the rest of the assignment.
    • Just kidding. Yes, keep updating delete/toString functions, and testing for leaks
  12. Implement proper the Module2 functions
  13. Test for memory leaks

Important points


  • Do be careful about upper/lower case.
  • Do include comments with your name and student ID at the top of every file you submit

Do not:

  • Do not submit SVGParser.h or LinkedListAPI.h. If they are submitted, they will be overwritten.
  • Do not change the given typedefs or function prototypes in SVGParser.h
  • Do not hardcode any path or directory information into #include statements, e.g. #include "../include/SomeHeader.h
  • Do not submit any main() functions
  • Do not use exit() function calls anywhere in your code. Since you’re writing a library, it must always return the control to the caller using the return statement
  • Do not exit the program from one of the parser functions if a problem is encountered, return an error value instead.
  • Do not print anything to the command line.
  • Do not assume that your pointers are valid. Always check for NULL pointers in function arguments.

Failure to follow any of the above points may result in loss of marks, or even a zero for the assignment if they cause compiler errors with the test harness.

Submission structure

The submission must have the following directory structure:

assign1/        - contains the Makefile file.
assign1/bin     - should be empty, but this is where the Makefile will place the shared lib files. Note: be careful if you use Git, since it can ignore empty directories, and mess up your submission.
assign1/src     - contains SVGParser.c , LinkedListAPI.c, and your additional source files.
assign1/include - contains your additional headers. Do not submit SVGParser.h and LinkedListAPI.h.


You will need to provide a Makefile with the following functionality:

  • make parser creates a shared library libsvgparse.so in assign1/bin
  • make clean removes all .o and .so files


Your code will be tested by an automated harness. You will submit a Makefile, which will be used to produce a shared library. This library will then be tested on the SoCS Linux servers by a precompiled executable file containing the test harness, as well as another executable file with simple memory leak tests. Your library must implement the assignment API exactly as specified, or you will get run-time errors because the executable files will not find functions in the library that they expect.

Your code must compile, run, and have all of the specified functionality implemented. Any compiler errors will result in the automatic grade of zero for the assignment.

Marks will be deducted for:

  • Incorrect and missing functionality
  • Deviations from the assignment requirements
  • Run-time errors, including infinite loops
  • Compiler warnings
  • Memory leaks reported by valgrind
  • Memory errors reported by valgrind
  • Failure to follow submission instructions


Infinite loops will also result in a grade of zero.

Submit your files as a Zip archive using Moodle. File name must be A1FirstnameLastname.zip.

Late submissions: see course outline for late submission policies.