diff --git a/__pycache__/app.cpython-311.pyc b/__pycache__/app.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..7ba17f9b6f7afe6d13d7f289b128d85c48edc22a Binary files /dev/null and b/__pycache__/app.cpython-311.pyc differ diff --git a/__pycache__/extract_function.cpython-311.pyc b/__pycache__/extract_function.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..cae1dca4ac30eb9a90e00087c08c97de1d792aa1 Binary files /dev/null and b/__pycache__/extract_function.cpython-311.pyc differ diff --git a/app.py b/app.py index b3c48715d8ee4cedf6a342606090ff778e8f0f21..1fd09b7dcb08bd14d6f82d657d8c1dfd815efb07 100644 --- a/app.py +++ b/app.py @@ -1,3 +1,45 @@ """ all flask apis will go in here -""" \ No newline at end of file +""" + +from flask import Flask, request, jsonify +from extract_function import extract_functions_with_body + +app = Flask(__name__) + + +@app.route('/') +def home(): + return "hello world" + +@app.route('/extract-functions', methods=['POST']) +def extract_functions(): + try: + c_code = request.json.get('c_code', '') + if not c_code: + return jsonify({"error": "No C code provided"}), 400 + + functions = extract_functions_with_body(c_code) + + return jsonify({"Functions": functions}), 200 + except Exception as e: + return jsonify({"error": str(e)}), 500 + + +@app.route('/upload-c-file', methods=['POST']) +def upload_c_file(): + try: + uploaded_file = request.files.get('file') + if not uploaded_file: + return jsonify({"error": "No file uploaded"}), 400 + + c_code = uploaded_file.read().decode('utf-8') + + functions = extract_functions_with_body(c_code) + + return jsonify({"Functions": functions}), 200 + except Exception as e: + return jsonify({"error": str(e)}), 500 + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file diff --git a/extract_function.py b/extract_function.py new file mode 100644 index 0000000000000000000000000000000000000000..7d285462514446f69bb96b285c328808fb33f3a5 --- /dev/null +++ b/extract_function.py @@ -0,0 +1,19 @@ +import re +import json + +def extract_functions_with_body(c_code): + function_pattern = re.compile( + r'([a-zA-Z_][a-zA-Z0-9_]*\s+\**\s*[a-zA-Z_][a-zA-Z0-9_]*\s*\([^)]*\))\s*{([^{}]*({[^{}]*})*[^{}]*)}', + re.DOTALL + ) + + functions = [] + for match in function_pattern.findall(c_code): + header = match[0].strip() + body = match[1].strip() + + functions.append({ + "Header": header, + "Body": body + }) + return functions diff --git a/sample.c b/sample.c new file mode 100644 index 0000000000000000000000000000000000000000..710c634aa9268b7d9b7fc8593b4c60dd9eca3fa3 --- /dev/null +++ b/sample.c @@ -0,0 +1,32 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef struct Node { + int data; + struct Node* next; +} Node; + +Node* createNode(int data) { + Node* newNode = (Node*)malloc(sizeof(Node)); + newNode->data = data; + newNode->next = NULL; + return newNode; +} + +void printList(Node* head) { + Node* temp = head; + while (temp != NULL) { + printf("%d -> ", temp->data); + temp = temp->next; + } + printf("NULL\n"); +} + +void freeList(Node* head) { + Node* temp; + while (head != NULL) { + temp = head; + head = head->next; + free(temp); + } +}