I have a file called Strings.h, that I use to localize an app I have. I want to search through all of my class files, and find out if and where I am using each string, and output the classes and line numbers for each string.
My thought is to use Python, but maybe that’s the wrong tool for the job. Also, I have a basic algorithm, but I worry it will take too long to run. Can you write this script to do what I want, or even just suggest a better algorithm?
Strings.h looks like this:
#import "NonLocalizedStrings.h"
#pragma mark Coordinate Behavior Strings
#define LATITUDE_WORD NSLocalizedString(@"Latitude", @"used in coordinate behaviors")
#define LONGITUDE_WORD NSLocalizedString(@"Longitude", @"used in coordinate behaviors")
#define DEGREES_WORD NSLocalizedString(@"Degrees", @"used in coordinate behaviors")
#define MINUTES_WORD NSLocalizedString(@"Minutes", @"Used in coordiante behaviors")
#define SECONDS_WORD NSLocalizedString(@"Seconds", @"Used in DMSBehavior.m")
...
The script should take each line that starts with #define, and then make a list of the word that appears after #define (e.g.) LATITUDE_WORD
The pseudocode might be:
file = strings.h
for line in file:
extract word after #define
search_words.push(word)
print search_words
[LATITUDE_WORD, LONGITUDE_WORD, DEGREES_WORD, MINUTES_WORD, SECONDS WORD]
After I have the list of words, my pseudocode is something like:
found_words = {}
for word in words:
found_words[word] = []
for file in files:
for line in file:
for word in search_words:
if line contains word:
found_words[word].push((filename, linenumber))
print found_words
So, found words would look something like:
{
LATITUDE_WORD: [
(foo.m, 42),
(bar.m, 132)
],
LONGITUDE_WORD: [
(baz.m, 22),
(bim.m, 112)
],
}
How about this [in bash] ?
Output:
EDIT: I’ve altered the code above to redirect it’s output to a file
matches, so we can use that to show words that are never found: