You might want to use the IDAPython wrapper functions. As you mentioned, the IDA API is quite poorly documented. One of the best, but not easiest way to actually understand it is to check the IDAPython wrapper library in your IDA/Python/idc.py folder.
Based on what I've seen in the idc.py file, there's some functions you might want to check out to help you out.
Creates a comment to the current line:
MakeComm(ScreenEA(), "Comment Test")
Creates a repetable comment to the current line:
MakeRptCmt(ScreenEA(), "Repeatable Comment")
Retrieves the comment from the current line:
Specify 1 to get the repeatable comment or 0 to get the normal comment as the second argument. Note that the function
CommentEx is a wrapper around
c = GetCommentEx(ScreenEA(), 1)
Or even easier is to use
RptCmt(ea), which are wrappers on
def Comment(ea): return GetCommentEx(ea, 0)
"""Returns the regular comment or None"""
def RptCmt(ea): return GetCommentEx(ea, 1)
"""Returns the repeatable comment or None"""
And then you have all the special functions to retrieve comments from specific structures such as Enums, Functions, Constants...I don't believe I have seen a specific function to retrieve all comments, but it's fairly easy to build, as you did:
I've made this code that looks for lines containing a 'jump: 0x???' comment/repeatable comments. Not the best code (slow), but it illustrates the use of the IDA Python comments functions.
def get_comments(_startea, _endea, _filter):
matches = 
ea = _startea
for ea in range(_startea, _endea):
cmt1 = Comment(ea)
cmt2 = RptCmt(ea)
cmt = ""
cmt += cmt1
cmt += cmt2
re_match = re.match(_filter, cmt, re.I)
if (re_match and len(re_match.groups()) > 0):
MakeComm(ScreenEA(), 'jump: 0xBADC0DE')
filter = r'(.*)(jump: 0x)([0-9a-fA-F]+)(.*)'
addrs = get_comments(MinEA(), MaxEA(), filter)