Giter VIP home page Giter VIP logo

un-obsolize's Introduction

un-obsolize

A Python script to assist in converting obsolete (K&R) forward declarations and function definitions in C code. The K&R syntax is replaced with ANSI syntax.

Forward declarations and function defininitions can be located in separate files and conversion will still work, provided that the script is invoked on all relavant files.

Files are backed up before any changes are made. Backed up files are named {filename}.bak.

A Note on Functionality

I wrote this script to help automate some mindless tasks for work. It is not intended to be a full-blown solution that handles every single odd conversion case. This script served its purpose for the specific code base that I was converting, but your milelage may vary.

This script is extremely limited. It is not a C parser. Function declarations must have the exact same format as shown in the conversion sample below in order for them to be detected. (Yes, the return value must be on a different line than the function name.) Even then, this script may have false positives and false negatives. For this reason, the script will, by default, prompt for confirmation before making any changes. Files are always backed up before any changes are made.

Conversion Sample

This script will convert in the following way:

Obsolete K&R syntax:

// Obsolete forward declaration
int foo();

// Obsolete function implementation
int            // This return value must be on a separate line in order for it to be detected
foo(a, b, c)
  int a;
  void *b;
  char c;
{
  // Stuff
}

Converted ANSI syntax:

// ANSI forward declaration
int foo(int a, void *b, char c)

// ANSI function implementation
int foo(int a, void *b, char c)
{
  // Stuff
}

Requirements

Python >= 3.2

Usage

This script is intended to be run from the shell:

python3 convert.py

Arguments

usage: convert.py [-h] [-r] [-d] [-xc] [--ext NEW_EXT] [--re INPUT_RE] [-gm]
                  [file [file ...]]

Convert some C code.

positional arguments:
  file           Files to operate on. Optional if [-r/-d] is applied

optional arguments:
  -h, --help     show this help message and exit
  -r             Recurse and operate on all files in the current and sub-
                 directories (implies -d)
  -d             Operate on all files in the current directory
  -xc            Do not prompt for confirmation before making a change.
  --ext NEW_EXT  Specify a new file extension to change the converted files
                 to. Useful when converting from *.c to *.cpp. Example: [--ext
                 cpp]
  --re INPUT_RE  Specify a Python regex to filter the files that are found
                 with [-d/-r]. Default is `\S+\.c$`, which will usually match
                 *.c files.
  -gm            Only valid with [--ext]. When changing file extensions,
                 perform a Git move. Useful when operating in a Git
                 repository.

un-obsolize's People

Contributors

license-fn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

xtheone

un-obsolize's Issues

var declarations won't work if on one line

Functions like this are also not detected.

int
func(a, b)
int a, b
{
}

This one is a false positive.

if (formula_counter)
formula_counter=0;

if(formula_counter =0)

But this script saved me a lot of work, Tnx!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.