Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 563525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:40:11+00:00 2026-05-13T12:40:11+00:00

I need to search all the *.c source files in the path to find

  • 0

I need to search all the *.c source files in the path to find a reference to a *.h header to find unused C headers. I wrote a ruby script but it feel very clumsy.

I create an array with all C files and an array with all the H files.
I iterate over the header file array. For each header I open each C file and look for a reference to the header.

Is there a easier or better way?

require 'ftools'
require 'find'

# add a file search
class File
  def self.find(dir, filename="*.*", subdirs=true)
    Dir[ subdirs ? File.join(dir.split(/\\/), "**", filename) : File.join(dir.split(/\\/), filename) ]
  end
end

files = File.find(".", "*.c", true)
headers = File.find(".", "*.h", true)

headers.each do |file|

  #puts "Searching for #{file}(#{File.basename(file)})"
  found = 0

  files.each do |cfile|
    #puts "searching in #{cfile}"
    if File.read(cfile).downcase.include?(File.basename(file).downcase)
        found += 1
    end
  end

  puts "#{file} used #{found} times"

end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:40:11+00:00Added an answer on May 13, 2026 at 12:40 pm

    As already pointed out, you can use Dir#glob to simplify your file-finding. You could also consider switching your loops, which would mean opening each C file once, instead of once per H file.

    I’d consider going with something like the following, which ran on the Ruby source in 3 seconds:

    # collect the File.basename for all h files in tree
    hfile_names = Dir.glob("**/*.h").collect{|hfile| File.basename(hfile) }
    
    h_counts = Hash.new(0) # somewhere to store the counts
    
    Dir.glob("**/*.c").each do |cfile| # enumerate the C files
      file_text = File.read(cfile) # downcase here if necessary
      hfile_names.each do |hfile|
        h_counts[hfile] += 1 if file_text.include?(hfile)
      end
    end
    
    h_counts.each { |file, found| puts "#{file} used #{found} times" }
    

    EDIT: That won’t list H files not referenced in any C files. To be certain to catch those, the hash would have to be explicitly initialised:

    h_counts = {}
    hfile_names.each { |hfile| h_counts[hfile] = 0 }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to search for all users containing a certain text string in their
I need to search a string and replace all occurrences of %FirstName% and %PolicyAmount%
I need to search for a pattern in files. For example the content of
How do I tell GDB in *nix to search for source files inside a
I have edited several files ISO-8859-15 encoded php source files with netbeans 6.7.1, but
The problem is to extend the binary search algorithm to find all occurrences of
I need to find content of forms from HTML source file, I did some
I have a need to use the DOS command FINDSTR to search in all
I need to search a string in the string array. I dont want to
I need to search across multiple columns from two tables in my database using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.