Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

raison

macrumors member
Original poster
Nov 13, 2009
83
4
I want to parse /etc/hosts and manipulate it by adding/removing hosts. I'm using Swift to build an OSX application, but before I build a parser for /etc/hosts, I want to make sure there's nothing out there already available so I don't have to reinvent the wheels. From my researches, I found:

1. mDNSResponder seems to parse /etc/hosts according to the hosts manpage, but I couldn't find an exposed API to manipulate the hosts file;

2. gethostent seems to be able to do that, but not sure how to use standard C code on Swift and link libc to the project.

Any help is appreciated, thanks! :)
 
Use the best tool for the job. If other languages can do this out of the box, what makes you think Swift is the best?

Use something that can talk with C more effortlessly. Like Obj-C.
 
Thanks for all the replies, I ended up building my own parser using enumerateSubstringsInRange, it worked like a charm according to my unit tests. Fingers crossed :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.