Page 1 of 1
Alphabetize C functions in .c file?
Posted: Thu Feb 23, 2012 1:57 am
by Baker
Anything out there that can do this that is known to anyone?
I have a zillion functions and I'd like to get them in alphabetical order.
Header = easy. C source file ... Google is failing me.
Re: Alphabetize C functions in .c file?
Posted: Thu Feb 23, 2012 7:43 am
by andrewj
In VIM (a text editor) I would make all the functions sit on a single line, pass it to :!sort, and re-run its automatic code formatter to put it all back to normal.
That probably doesn't help you much
P.S. the vim command to put a function on a single line is just :.,/^}/join
(run from the top of the function and assuming the last } appears at the start of the line)
P.P.S. the vim built-in code formatter didn't work, would need to pass each function to :!indent
Re: Alphabetize C functions in .c file?
Posted: Thu Feb 23, 2012 3:00 pm
by revelator
hmm im not sure maybe astyle can do it or maybe ctags... going to try since i have both so hang on.
Re: Alphabetize C functions in .c file?
Posted: Thu Feb 23, 2012 3:04 pm
by revelator
Code: Select all
#include<iostream>
#include<cstring>
#include<fstream>
using namespace std;
void sortString(char array[], int num);
void outputString(char array[], int num);
int main() {
int columns=30, rows=0;
char filename[30], letter;
ifstream fin;
cout << "Enter the file name: ";
cin >> filename;
fin.open(filename);
if (!fin) {
cout << "Failure to open the file.";
} else {
while (fin >> letter) {
if (letter == '\n')
rows++;
}
fin.clear();
fin.seekg(0,ios::beg);
char name[rows][columns];
rows = 0;
while (fin.get(name[rows][columns])) {
rows++;
}
sortString(name, rows+1);
outputString(name, rows+1);
}
return 0;
}
void sortString(char array[], int num) {
int i, j;
char check[num][30];
for (i=1; i<num; i++) {
strcpy(check, array[i][30]);
for (j=1; j>=1 && (strcmp(check, array[30][j-1])==1); j--) {
array[j][30] = array[j-1][30];
array[j-1][30] = check;
}
}
}
void outputString(char array[], int num) {
for (int i=0; i<num; i++) {
cout << array[i][30] << endl;
}
}
use in a console project and its c++

Re: Alphabetize C functions in .c file?
Posted: Thu Feb 23, 2012 5:42 pm
by Baker
I would have thought that something like this would have long existed, but I guess not so I am writing my own and am rather close to done. Does involve some work and I having to write some block detection based on brace indentation count, but I should learn how to handle these things so ...
@ Reckless, that just sorts lines from the looks of the source. I want it to sort C functions in a c file.
Re: Alphabetize C functions in .c file?
Posted: Thu Feb 23, 2012 7:35 pm
by revelator
code not by me was posted on some forum and works for sorting c function names alphabetically but it does have a few quirks he noted (crashes on empty functions).
besides that yeah probably better to write your own

Re: Alphabetize C functions in .c file?
Posted: Thu Feb 23, 2012 7:39 pm
by Baker
reckless wrote:
besides that yeah probably better to write your own

Was a productive experience ...
Code: Select all
// Do not put zero length strings through to this function
char* SortedText_FromText_Alloc (const char* originalText, const char delimChar1, const char delimChar2)
{
const size_t buffersize = strSizeToDuplicate (originalText);
char* newText = Memory_calloc (1, buffersize, "Sorted text out");
int numBlocks = 0;
textSort_t* FirstBlock = TextSort_calloc_On_Text_As_Zero_Length (originalText, 0, &numBlocks);
textSort_t* alphaBest = NULL;
do
{
// Phase 1. Locate breaking points. After every "}"
const fbool asBlocks = !delimChar2;
int curlyBracketDepth = 0; // For AsBlocks mode
textSort_t* previousBlock = FirstBlock;
textSort_t* currentBlock;
for (int i = 0; originalText[i]; i ++)
{
const char myChar = originalText[i];
// To do ... honor "//" comments through end of a line Maybe fbool isInNewLineComments and only check for end of sequence
// To do ... honor "/*" comments through "*/" Maybe fbool isInAsteriskComments and only check for end of sequence
do
{
if (asBlocks == True && myChar != delimChar1) break; // No delim found
if (asBlocks && myChar != delimChar1 && myChar != delimChar2 ) break; // No delim found
if (asBlocks == True)
{
curlyBracketDepth += myChar == delimChar1 ? 1 : -1; // If delim1 + 1, if delim2 -1 to level
if (curlyBracketDepth > 0) break; // No split
}
// Split!
currentBlock = TextSort_New_Block_Text_At_X_With_Close_Old (originalText, i, previousBlock, &numBlocks);
} while (0);
}
// Finalize current block
TextSort_Finalize_With_Stop_And_Next ( currentBlock, strlen(originalText), NULL); // currentBlock.stop = i - 1; // End of string before null terminator
} while (0);
do
{
// Stage 2: Sort those functions ... move through each cursor and run down the alphaSorted link list
// One cursor at a time slides down the alpha sort
for (textSort_t* newEntry = FirstBlock; newEntry; newEntry = newEntry->allocNext)
{
textSort_t* previousAlphaCursor;
// Head of list scenarios. List is empty or higher alphabetically than head of the list
if (alphaBest == NULL || TextSortAutoNCompare (originalText, newEntry, alphaBest) < 0 )
{
newEntry->alphaNext = alphaBest; // Tell item it is after me alphabetically
alphaBest = newEntry; // Assume head of list position
continue; // Get out. Do next item.
}
do
{ // Find position in list
textSort_t* previousAlphaCursor = alphaBest;
textSort_t* alphaCursor = alphaBest->alphaNext;
// Stop when we hit first item that we are alphabetically higher than
for (; alphaCursor && TextSortAutoNCompare (originalText, newEntry, alphaCursor) > 0; previousAlphaCursor = alphaCursor, alphaCursor = alphaCursor->alphaNext);
// When we come out of the for loop, we have the right insertion position or NULL
newEntry->alphaNext = alphaCursor;
previousAlphaCursor->alphaNext = newEntry;
} while (0);
}
// We should have a nice linked list at this point
} while (0);
do
{
// Reconstruct string by running through linked list. Advance cursor
size_t textCursor = 0;
for (textSort_t* linkCursor = alphaBest; linkCursor; linkCursor = linkCursor->alphaNext);
StringRangeCopyIntoAtMoveCursor (originalText, linkCursor->startOffset, linkCursor->length, newText, &textCursor);
// String should be reconstructed now
} while (0);
do
{
textSort_t* linkCursor = FirstBlock;
for (textSort_t* nextLink = linkCursor->nextAlloc; linkCursor; linkCursor = nextLink, nextlink = linkCursor->next);
Memory_free(linkCursor);
} while (0);
return newText; // Calling function has responsibility free it
}
Base navigation
-------------------
Generic keypress. Enter keypress.
Left Arrow, Right Arrow
Up Arrow = Same offset within one row higher, downarrow same
home. Beginning of line. End. End of line.
ctrl-home. Beginning of file. Ctrl-end. End of file.
alt-arrow. Next word.
Shift select.
paste. insert
display special characters
windowing
-------------------
Cursor pos.
Cursor down, up, left, right
Page up, page down
Insert mode, delete
Beginning of file, end of file
Sort lines
Selection
Behavior of tab?
Search
Replace
Window (Top, bottom, left, right)
Search. Replace.
Find in files (what where). (path, pattern,
Untested thus far.
Re: Alphabetize C functions in .c file?
Posted: Thu Feb 23, 2012 9:09 pm
by revelator
looks like a good start

and it will probably be welcome by others i noticed a ton of threads from others searching for the same when i tried to lookup something that could do this.