lu yuan
lu yuan

Reputation: 7227

Post a notification in .c file

A progress bar is used to show us the percentage of email that has been delivered. Im using libetpan.xcodeproj to send email and the percentage can be calculated in the following code. It is written in c. Problem is how could I post a notification in c.file to tell the observer (it is an objective-c class) that the percentage has been increased. Any help is appreciated:)

mailstream_helper.c

static inline int send_data_crlf_progress(mailstream * s,
                      const char *message, size_t size,
                      int quoted, size_t progr_rate,
                      progress_function * progr_fun,
                      mailprogress_function *
                      progr_context_fun, void *context)
{
    const char *current;
    size_t count;
    size_t last;
    size_t remaining;

    count = 0;
    last = 0;

    current = message;
    remaining = size;

    int i = 0;
    while (remaining > 0) {
        ssize_t length;

        if (quoted) {
            if (current[0] == '.')
                if (mailstream_write(s, ".", 1) == -1)
                    goto err;
        }

        length = send_data_line(s, current, remaining);
        if (length < 0)
            goto err;

        current += length;

        count += length;
        if (progr_rate != 0) {
            if (count - last >= progr_rate) {
                if (progr_fun != NULL) {
                    (*progr_fun) (count, size);
                }
                if (progr_context_fun != NULL) {
                    (*progr_context_fun) (count, size, context);
                }
                last = count;
            }
        }

        remaining -= length;
        //I need to post a notification here to let my
            //progress bar know how much data has been sent.        
        printf("No.%d rateOfSentData:%d end\r\n", ++i,
               1.0 - (float) remaining / (float) size);

    }

    return 0;

err:
    return -1;
}

Upvotes: 0

Views: 142

Answers (1)

David Hoerl
David Hoerl

Reputation: 41642

Rename the file to have a .m suffix. It's now a ObjC file with pure C code in it (now). Then add a message at end to send a notification as you would elsewhere using NSNotificationCenter.

If for some reason this rename cannot be done have it call a c function that you then add to an existing .m file to do notification.

Upvotes: 2

Related Questions