• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Script to add newline to end of all source files, for GCC_WARN_ABOUT_MISSING_NEWLINE
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Script to add newline to end of all source files, for GCC_WARN_ABOUT_MISSING_NEWLINE


  • Subject: Re: Script to add newline to end of all source files, for GCC_WARN_ABOUT_MISSING_NEWLINE
  • From: Cem Karan <email@hidden>
  • Date: Fri, 7 Apr 2006 07:09:39 -0400

Hi all,

Xcode/gcc has a warning for files that do not end in a newline. Many of
my files no not. I figure someone out there has already written a
script that will fix all files... anyone? :) My scripting abilities are poor.


Thanks,

Sean, here is the script that I normally use to clean up files. It does a little more than just add a newline to the end though:


--------------- Start Script ----------------------------
#!/usr/bin/python

import sys

def processArg(arg):
"""
This function accepts what should be the path to a file, attempts to
open the file for reading, read it, strip the whitespace off of the
end of every line, join all of the lines together again with newlines
between, and then write the file back to where it came from. It
does NOT try to figure out if a file is binary or not; it just assumes
that it is getting text that python understands and can decode
correctly.
"""
inputfile = open(arg, 'r')
text = inputfile.read()
inputfile.close()
lines = text.splitlines()
outputLines = []
for line in lines:
outputLines.append(line.rstrip())


    outputText = '\n'.join(outputLines) + '\n'

    if text != outputText:
        print "Overwriting " + str(arg)
        outputfile = open(arg, 'w')
        outputfile.write(outputText)
        outputfile.flush() # Overly cautious
        outputfile.close()
# End of processArg

if (len(sys.argv) > 1):
    for arg in sys.argv[1:]:
        processArg(arg)

--------------- End Script ------------------------------

--------------- Batch processing script -----------------
#!/bin/bash

# Searches thru all the files in a directory that match the given names
# and runs them all thru fileCleaner.py
find . \! -path '*.svn*' -type f \( -name '*.s' -or -name '*.cpp' -or -name '*.h' -or -name '*.c' \) -exec echo \"{}\" \; | xargs fileCleaner.py
--------------- End script ------------------------------


The first script assumes that it is getting a list of file paths as its arguments, and attempts to read each one. The second script assumes that you've placed the first in a file named "fileCleaner.py", and made the file executable, and placed it in your path. Assuming all that, it will search from the current working directory and clean all files that it finds. Add/change/etc. the search terms to get what you need.

Alternatively, make a menu script item with the following as your script. See "file:///Developer/ADC Reference Library/ documentation/DeveloperTools/Conceptual/XcodeUserGuide/Contents/ Resources/en.lproj/index.html" for more info on how to install this.

--------------- Start Script ----------------------------
#! /usr/bin/python
# -*- coding: utf-8 -*-

# -- PB User Script Info --
# %%%{PBXName=Clean file}%%%
# %%%{PBXInput=AllText}%%%
# %%%{PBXOutput=ReplaceAllText}%%%

import sys

text = sys.stdin.read()
lines = text.splitlines()
outputLines = []
for line in lines:
    outputLines.append(line.rstrip())

outputText = '\n'.join(outputLines)

print outputText


--------------- End Script ------------------------------

Good luck,
Cem Karan
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: Building for PowerPC *only* from an Intel-based Mac
  • Next by Date: How to change debug executable to a new path
  • Previous by thread: Why PDE can not work after add new class into project.
  • Next by thread: How to change debug executable to a new path
  • Index(es):
    • Date
    • Thread