Utility functions

Utility functions

Synopsis

                    NZBCreatorGroup;
enum                NZBCreatorSearchOnlyIn;
                    NZBCreatorSearchOpts;
                    NZBCreatorFile;
                    NZBCreatorCollection;
                    NZBCreatorSearchResult;
#define             NG_CALLBACK                         (f)
void                (*NGCallback)                       (void);
#define             NGFileMonitor
struct              NGList;
typedef             NGRegex;
typedef             NGType;
void                nntpgrab_utils_perform_base_initialization
                                                        (void);
NGRegex *           nntpgrab_utils_regex_compile        (const char *regex);
void                nntpgrab_utils_regex_free           (NGRegex *re);
const char **       nntpgrab_utils_regex_match          (NGRegex *re,
                                                         const char *line);
void                nntpgrab_utils_regex_matches_free   (const char **matches);
ngboolean           nntpgrab_utils_strip_subject        (const char *subject,
                                                         char **subject_without_partnum,
                                                         int *file_num,
                                                         int *total_files,
                                                         char **filename,
                                                         char **extension,
                                                         NNTPFileType *file_type,
                                                         int *par2_startnum,
                                                         int *num_par2_blocks,
                                                         int *part_num,
                                                         int *total_parts);
NNTPFileType        nntpgrab_utils_get_file_type_of_filename
                                                        (const char *filename);
void                nntpgrab_utils_calculate_file_size  (nguint64 file_size,
                                                         char *file_size_str,
                                                         int file_size_str_len);
int                 nntpgrab_utils_calculate_estimated_time_remaining
                                                        (int bytes_received1,
                                                         int bytes_received2,
                                                         int bytes_received3,
                                                         int bytes_received4,
                                                         int bytes_received5,
                                                         int bytes_received6,
                                                         int bytes_received7,
                                                         int bytes_received8,
                                                         int bytes_received9,
                                                         int bytes_received10,
                                                         nguint64 file_size);
void                nntpgrab_utils_get_readable_time_remaining
                                                        (int estimated_time_remaining,
                                                         char *time_remaining_str,
                                                         int time_remaining_str_len);
void                nntpgrab_utils_get_readable_finished_time
                                                        (int estimated_time_remaining,
                                                         char *time_remaining_str,
                                                         int time_remaining_str_len);
void                nntpgrab_utils_sanitize_text        (char *text,
                                                         int length);
void                nntpgrab_utils_strip_nzb_extension  (char *filename);
void                nntpgrab_utils_sanitize_collection_name
                                                        (char *collection_name);
ngboolean           nntpgrab_utils_extract_par2set_name_from_par2_filename
                                                        (const char *par2filename,
                                                         char *par2set,
                                                         int par2set_length);
ngboolean           nntpgrab_utils_get_folder_listing   (const char *parent,
                                                         NGList **folders);
void                nntpgrab_utils_free_folder_listing  (NGList *folders);
NNTPGrabNZB *       nntpgrab_utils_parse_nzb_file       (const char *contents,
                                                         char **errmsg);
void                nntpgrab_utils_nzb_file_free        (NNTPGrabNZB *nzbfile);
NGList *            nntpgrab_utils_nzbcreator_get_all_groups
                                                        (char **errmsg);
void                nntpgrab_utils_nzbcreator_free_groups
                                                        (NGList *groups);
NZBCreatorSearchResult * nntpgrab_utils_nzbcreator_perform_search
                                                        (NZBCreatorSearchOpts opts,
                                                         char **errmsg);
void                nntpgrab_utils_nzbcreator_free_result
                                                        (NZBCreatorSearchResult *result);
char *              nntpgrab_utils_nzbcreator_generate_NZB
                                                        (NGList *file_ids,
                                                         time_t oldest_stamp,
                                                         time_t newest_stamp,
                                                         char **errmsg);
NGFileMonitor *     nntpgrab_utils_monitor_directory    (const char *path,
                                                         char **errmsg);
void                nntpgrab_utils_cancel_directory_monitor
                                                        (NGFileMonitor *monitor);
void                ng_free                             (void *data);
NGList *            ng_list_append                      (NGList *list,
                                                         void *data);
NGList *            ng_list_prepend                     (NGList *list,
                                                         void *data);
NGList *            ng_list_remove                      (NGList *list,
                                                         void *data);
unsigned int        ng_list_length                      (NGList *list);
void                ng_list_free                        (NGList *list);
#define             ng_list_previous                    (list)
#define             ng_list_next                        (list)
void                ng_signal_connect                   (void *instance,
                                                         const char *signal_name,
                                                         NGCallback cb_handler,
                                                         void *data);
void                ng_signal_handlers_disconnect_by_func
                                                        (void *instance,
                                                         NGCallback cb_handler,
                                                         void *data);
void                ng_signal_handlers_block_by_func    (void *instance,
                                                         NGCallback cb_handler,
                                                         void *data);
void                ng_signal_handlers_unblock_by_func  (void *instance,
                                                         NGCallback cb_handler,
                                                         void *data);
void                ng_event_handler_loop_run           (void);
void                ng_event_handler_loop_quit          (void);
int                 ng_event_handler_loop_depth         (void);

Description

Details

NZBCreatorGroup

typedef struct {
    char newsgroup[512];
    int group_id;
} NZBCreatorGroup;

char newsgroup[512];

The name of the newsgroup

int group_id;

An identifier for this group

enum NZBCreatorSearchOnlyIn

typedef enum {
    NZBCREATOR_SEARCH_ONLY_IN_SUBJECTS,
    NZBCREATOR_SEARCH_ONLY_IN_FILENAMES,
    NZBCREATOR_SEARCH_ONLY_IN_POSTERS
} NZBCreatorSearchOnlyIn;

NZBCreatorSearchOpts

typedef struct {
    char query[64];
    char username[64];
    char password[64];
    NZBCreatorSearchOnlyIn search_only_in;
    int max_age;
    ngint64 minimal_file_size;
    ngint64 maximal_file_size;
    int percentage_complete;
    int group_to_search;
    NNTPFileType file_type;
    int offset;
} NZBCreatorSearchOpts;

char query[64];

The search query

char username[64];

The username which will be used to perform the search query (unused in NNTPGrab 0.7)

char password[64];

The password which will be used to perform the search query (unused in NNTPGrab 0.7)

NZBCreatorSearchOnlyIn search_only_in;

Indicate what type of data needs to be searched

int max_age;

The maximum age of search results (in days)

ngint64 minimal_file_size;

The minimum size of files in bytes

ngint64 maximal_file_size;

The maximum size of files in bytes

int percentage_complete;

Only return results which are this percentage complete (0 to 100)

int group_to_search;

Limit searching to one specific groups (see the group_id field from NZBCreatorGroup)

NNTPFileType file_type;

Only search files of this type

int offset;

The search result offset

NZBCreatorFile

typedef struct {
    ngint64 file_id;
    char subject[256];
    char poster[256];
    int num_parts_found;
    int num_parts_expected;
    NNTPFileType file_type;
    ngint64 file_size;
    time_t stamp;
    int complete_percentage;
} NZBCreatorFile;

ngint64 file_id;

An identifier for this file

char subject[256];

The subject of this file

char poster[256];

The poster of this file

int num_parts_found;

The number of parts which are found which belong to this file

int num_parts_expected;

The number of parts which are expected which belong to this file

NNTPFileType file_type;

The type of this file

ngint64 file_size;

The size of this file in bytes

time_t stamp;

The stamp of the first part of this file

int complete_percentage;

The percentage which indicates how complete this file is

NZBCreatorCollection

typedef struct {
    ngint64 collection_id;
    char collection_name[256];
    char short_collection_name[256];
    char poster[256];
    char newsgroup[256];
    ngint64 total_size;
    time_t stamp;
    int num_parts_found;
    int num_parts_expected;
    int complete_percentage;
    int num_regular_files;
    int num_par2_files;
    ngint64 first_file_id;
} NZBCreatorCollection;

ngint64 collection_id;

char collection_name[256];

The name of the collection (most frequently this is the subject of the first file in the collection)

char short_collection_name[256];

An abbreviated version of collection_name

char poster[256];

char newsgroup[256];

The newsgroup in which all the files from this collection are poster

ngint64 total_size;

The total size in bytes of this collection

time_t stamp;

The stamp of the first part of the first file in this collection

int num_parts_found;

int num_parts_expected;

int complete_percentage;

int num_regular_files;

int num_par2_files;

ngint64 first_file_id;


NZBCreatorSearchResult

typedef struct {
    int number_of_hits;
    int number_of_results;
    int results_per_page;
    int offset;
    NGList *collections;             // List of NZBCreatorCollection* instances
} NZBCreatorSearchResult;

int number_of_hits;

The number of hits which were found for the given query

int number_of_results;

The number of files which were found and are part of the collections

int results_per_page;

The number of search results per page

int offset;

The search result offset

NGList *collections;

A list containing all returned collections. [element-type NZBCreatorCollection]

NG_CALLBACK()

#define NG_CALLBACK(f)                   ((NGCallback) (f))

NGCallback ()

void                (*NGCallback)                       (void);

NGFileMonitor

#define NGFileMonitor GFileMonitor

struct NGList

struct NGList {
  void *data;
  NGList *next;
  NGList *prev;
};

NGRegex

typedef void NGRegex;

NGType

typedef void*                            NGType;

nntpgrab_utils_perform_base_initialization ()

void                nntpgrab_utils_perform_base_initialization
                                                        (void);

Perform the initialization of the GLib system. Needs to be called before any other NNTPGrab function in non-GLib-based frontends


nntpgrab_utils_regex_compile ()

NGRegex *           nntpgrab_utils_regex_compile        (const char *regex);

Compile a regular expression

regex :

The (perl-compatible) regular expression which needs to be compiled

Returns :

A compiled regular expression

nntpgrab_utils_regex_free ()

void                nntpgrab_utils_regex_free           (NGRegex *re);

Free a compiled regular expression

re :

The compiled regular expression

nntpgrab_utils_regex_match ()

const char **       nntpgrab_utils_regex_match          (NGRegex *re,
                                                         const char *line);

Perform a match against a compiled regular expression

re :

The compiled regular expression

line :

The data which need to be matched against the compiled regular expression

Returns :

A NULL-terminated array of matches items (needs to be free'd using nntpgrab_utils_regex_matches_free())

nntpgrab_utils_regex_matches_free ()

void                nntpgrab_utils_regex_matches_free   (const char **matches);

Free an array of matched items


nntpgrab_utils_strip_subject ()

ngboolean           nntpgrab_utils_strip_subject        (const char *subject,
                                                         char **subject_without_partnum,
                                                         int *file_num,
                                                         int *total_files,
                                                         char **filename,
                                                         char **extension,
                                                         NNTPFileType *file_type,
                                                         int *par2_startnum,
                                                         int *num_par2_blocks,
                                                         int *part_num,
                                                         int *total_parts);

Strip a subject into useable pieces of data All the parameters (except subject) can be NULL to ignore them All the char* parameters need to be free'd using ng_free()

Returns :

TRUE is the strip was successfull

nntpgrab_utils_get_file_type_of_filename ()

NNTPFileType        nntpgrab_utils_get_file_type_of_filename
                                                        (const char *filename);

Find out the file type of a given filename If the file type couldn't be found out, the value NNTP_FILE_TYPE_UNKNOWN will be returned

filename :

The filename whose file type needs to be found out

Returns :

The file type (as an enumeration)

nntpgrab_utils_calculate_file_size ()

void                nntpgrab_utils_calculate_file_size  (nguint64 file_size,
                                                         char *file_size_str,
                                                         int file_size_str_len);

Transform a file size into a human readable notation

file_size :

The file size which needs to be transformed

file_size_str :

Pointer to the location where the result needs to be saved. [out]

file_size_str_len :

The maximum length of the file_size_str buffer

nntpgrab_utils_calculate_estimated_time_remaining ()

int                 nntpgrab_utils_calculate_estimated_time_remaining
                                                        (int bytes_received1,
                                                         int bytes_received2,
                                                         int bytes_received3,
                                                         int bytes_received4,
                                                         int bytes_received5,
                                                         int bytes_received6,
                                                         int bytes_received7,
                                                         int bytes_received8,
                                                         int bytes_received9,
                                                         int bytes_received10,
                                                         nguint64 file_size);

Calculate the estimated time remaining to complete the given file size The bytes_received parameters can be retrieved from the signalhandler for the signal "traffic-monitor-update"

bytes_received1 :

The number of bytes received in now() - 10

bytes_received2 :

The number of bytes received in now() - 9

bytes_received3 :

The number of bytes received in now() - 8

bytes_received4 :

The number of bytes received in now() - 7

bytes_received5 :

The number of bytes received in now() - 6

bytes_received6 :

The number of bytes received in now() - 5

bytes_received7 :

The number of bytes received in now() - 4

bytes_received8 :

The number of bytes received in now() - 3

bytes_received9 :

The number of bytes received in now() - 2

bytes_received10 :

The number of bytes received in now() - 1

file_size :

The file size in bytes

Returns :

The number of seconds in which the given file_size can be downloaded

nntpgrab_utils_get_readable_time_remaining ()

void                nntpgrab_utils_get_readable_time_remaining
                                                        (int estimated_time_remaining,
                                                         char *time_remaining_str,
                                                         int time_remaining_str_len);

Transform a estimated time remaining into a human readable value for the time remaining

estimated_time_remaining :

The estimated time remaining (in seconds)

time_remaining_str :

Pointer to the location where the human readable notation (like '3 minutes and 10 seconds') can be saved. [out]

time_remaining_str_len :

The maximum length of the time_remaining_str buffer

nntpgrab_utils_get_readable_finished_time ()

void                nntpgrab_utils_get_readable_finished_time
                                                        (int estimated_time_remaining,
                                                         char *time_remaining_str,
                                                         int time_remaining_str_len);

Transform a estimated time remaining into a human readable value for the expected time to finish

estimated_time_remaining :

The estimated time remaining (in seconds)

time_remaining_str :

Pointer to the location where the human readable notation (like 'Friday February 6 2009 - 22:15') can be saved. [out]

time_remaining_str_len :

The maximum length of the time_remaining_str buffer

nntpgrab_utils_sanitize_text ()

void                nntpgrab_utils_sanitize_text        (char *text,
                                                         int length);

Check whether the given text contains invalid UTF-8 characters and if they're found, replace them with something more sane

text :

The text which need to be sanitized. [inout]

length :

The length of the text

nntpgrab_utils_strip_nzb_extension ()

void                nntpgrab_utils_strip_nzb_extension  (char *filename);

Remove the extension from a filename

filename :

The filename whose extension need to be stripped. [inout]

nntpgrab_utils_sanitize_collection_name ()

void                nntpgrab_utils_sanitize_collection_name
                                                        (char *collection_name);

Remove forbidden characters from the collection name

collection_name :

The collection name which need to be sanitized. [inout]

nntpgrab_utils_extract_par2set_name_from_par2_filename ()

ngboolean           nntpgrab_utils_extract_par2set_name_from_par2_filename
                                                        (const char *par2filename,
                                                         char *par2set,
                                                         int par2set_length);

Extract the name of a PAR2 set out of a PAR2 filename

par2filename :

The filename of the PAR2 file which needs to be extracted

par2set :

Pointer to a location where the result can be saved. [out]

par2set_length :

Size of the par2set length

Returns :

TRUE of success (par2set will be set), FALSE on failure

nntpgrab_utils_get_folder_listing ()

ngboolean           nntpgrab_utils_get_folder_listing   (const char *parent,
                                                         NGList **folders);

Retrieves a list containing all the sub-folders which are in the given folder

parent :

The name of the directory whose sub-folders should be retrieved. If NULL, all available drives will be returned (when the host is running Windows)

folders :

Pointer to a NGList*. The list of sub-folders will be placed in this field. This list needs to be free'd using the function nntpgrab_config_free_folder_listing(). [out][element-type NNTPGrabFolder]

Returns :

TRUE on sucess, FALSE if the given directory isn't found or readable

nntpgrab_utils_free_folder_listing ()

void                nntpgrab_utils_free_folder_listing  (NGList *folders);

Free a list of sub-folders

folders :

A list containing subfolders as returned by the function nntpgrab_config_get_folder_listing()

nntpgrab_utils_parse_nzb_file ()

NNTPGrabNZB *       nntpgrab_utils_parse_nzb_file       (const char *contents,
                                                         char **errmsg);

Parse the contents of a NZB file and put the information in a structure

contents :

The contents of the NZB file

errmsg :

Pointer to a char*. If an errors occurs, the reason will be placed in this field. Needs to be freed using ngfree(). Can be NULL to ignore errors. [out][allow-none]

Returns :

A structure containing information about the NZB file. Needs to be free'd using the function nntpgrab_utils_nzb_file_free(). If the value NULL is returned, an error occured and errmsg will be set

nntpgrab_utils_nzb_file_free ()

void                nntpgrab_utils_nzb_file_free        (NNTPGrabNZB *nzbfile);

Free a structure containing details about a NZB file

nzbfile :

The structure containing information about the NZB file

nntpgrab_utils_nzbcreator_get_all_groups ()

NGList *            nntpgrab_utils_nzbcreator_get_all_groups
                                                        (char **errmsg);

Retrieve a list of all the usenet groups which are indexed by the NZBCreator service

Returns :

A list of all the indexed usenet groups. Needs to be free'd using nntpgrab_utils_nzbcreator_free_groups(). If an error occures, the value NULL will be returned and errmsg will be set. [element-type NZBCreatorGroup]

nntpgrab_utils_nzbcreator_free_groups ()

void                nntpgrab_utils_nzbcreator_free_groups
                                                        (NGList *groups);

Free the list of usenet groups which are indexed by the NZBCreator service

groups :

A list of all the indexed usenet groups

nntpgrab_utils_nzbcreator_perform_search ()

NZBCreatorSearchResult * nntpgrab_utils_nzbcreator_perform_search
                                                        (NZBCreatorSearchOpts opts,
                                                         char **errmsg);

Perform a search using the NZBCreator service

opts :

A structure containing information about the requested search

errmsg :

Pointer to a char*. If an errors occurs, the reason will be placed in this field. Needs to be freed using ngfree(). Can be NULL to ignore errors. [allow-none][out]

Returns :

A structure containing the search results. Needs to be free'd using nntpgrab_utils_nzbcreator_free_result(). If an error occurs, the value NULL will be returned and errmsg will be set. [element-type NZBCreatorSearchResults]

nntpgrab_utils_nzbcreator_free_result ()

void                nntpgrab_utils_nzbcreator_free_result
                                                        (NZBCreatorSearchResult *result);

Free the NZBCreator search results

result :

The NZBCreator search results

nntpgrab_utils_nzbcreator_generate_NZB ()

char *              nntpgrab_utils_nzbcreator_generate_NZB
                                                        (NGList *file_ids,
                                                         time_t oldest_stamp,
                                                         time_t newest_stamp,
                                                         char **errmsg);

Generate a NZB file based on a list of file ID's

file_ids :

A list containing file ID's of all the files which needs to be retrieved. [element-type int]

oldest_stamp :

The timestamp of the oldest file in the collection

newest_stamp :

The timestamp of the newest file in the collection

errmsg :

Pointer to a char*. If an errors occurs, the reason will be placed in this field. Needs to be freed using ngfree(). Can be NULL to ignore errors. [allow-none][out]

Returns :

The contents of the NZB file containing all the details of the given file_ids. Needs to be free'd using ng_free(). If an error occurs, the value NULL will be returned and errmsg will be set

nntpgrab_utils_monitor_directory ()

NGFileMonitor *     nntpgrab_utils_monitor_directory    (const char *path,
                                                         char **errmsg);

Start the monitoring of a directory The NGFileMonitor is an GObject where the event 'changed' can be caught using ng_signal_connect()

path :

The directory which needs to be monitored for changes

errmsg :

Pointer to a char*. If an errors occurs, the reason will be placed in this field. Needs to be freed using ngfree(). Can be NULL to ignore errors. [allow-none][out]

Returns :

An instance of a NGFileMonitor or NULL in case an error occured (errmsg will be set)

nntpgrab_utils_cancel_directory_monitor ()

void                nntpgrab_utils_cancel_directory_monitor
                                                        (NGFileMonitor *monitor);

Cancel the monitoring of a directory

monitor :

The instance of the NGFileMonitor which needs to be stopped

ng_free ()

void                ng_free                             (void *data);

Free data allocated by NNTPGrab functions

data :

The data which needs to be free'd

ng_list_append ()

NGList *            ng_list_append                      (NGList *list,
                                                         void *data);

Append an item to a list

Returns :

The (possibly) new start of the list

ng_list_prepend ()

NGList *            ng_list_prepend                     (NGList *list,
                                                         void *data);

Insert an item to the beginning of a list

list :

The list where the item needs to be added to (can be NULL to create a new list)

data :

The item which needs to be added

Returns :

The (possibly) new start of the list

ng_list_remove ()

NGList *            ng_list_remove                      (NGList *list,
                                                         void *data);

Remove an item from a list Note that if data isn't part of the list, this function will do nothing

Returns :

The (possibly) new start of the list

ng_list_length ()

unsigned int        ng_list_length                      (NGList *list);

Retrieve the length of a list

list :

The list of which the length must be returned

Returns :

The length of the list

ng_list_free ()

void                ng_list_free                        (NGList *list);

Free a list Note that this function only free's the list itself, NOT the items which were in it!

list :

The list which needs to be free'd

ng_list_previous()

#define ng_list_previous(list)           ((list) ? (((NGList *)(list))->prev) : NULL)

list :

The list of which the previous item needs to be retrieved Returns: The previous item in list or NULL when there's no previous item

ng_list_next()

#define ng_list_next(list)               ((list) ? (((NGList *)(list))->next) : NULL)

list :

The list of which the next item needs to be retrieved Returns: The next item in list or NULL when there's no next item

ng_signal_connect ()

void                ng_signal_connect                   (void *instance,
                                                         const char *signal_name,
                                                         NGCallback cb_handler,
                                                         void *data);

Register a signal handler The file nntpgrab_core/marshall.list contains a list of known signals and callback function prototypes for the NNTPGrabCore

instance :

The instance of an GObject, for example NNTPGrabCore or NNTPGrabAutoImport

signal_name :

The name of the signal for which a signaler handler needs to be registered

cb_handler :

The function which need to be called every time the given signal is emit

data :

Data used as extra argument in the callback functions

ng_signal_handlers_disconnect_by_func ()

void                ng_signal_handlers_disconnect_by_func
                                                        (void *instance,
                                                         NGCallback cb_handler,
                                                         void *data);

ng_signal_handlers_block_by_func ()

void                ng_signal_handlers_block_by_func    (void *instance,
                                                         NGCallback cb_handler,
                                                         void *data);

Temporary disable a signal handler for a given callback function

instance :

The instance of an GObject, for example NNTPGrabCore or NNTPGrabAutoImport

cb_handler :

The callback function which need to be temporary blocked

data :

The callback data used for the given cb_handler

ng_signal_handlers_unblock_by_func ()

void                ng_signal_handlers_unblock_by_func  (void *instance,
                                                         NGCallback cb_handler,
                                                         void *data);

ng_event_handler_loop_run ()

void                ng_event_handler_loop_run           (void);

Start the event handler loop. This function waits for incoming events and passes these on to all the registered event handlers This function only returns as soon as the function ng_event_handler_loop_quit() is called from an event handler or another thread This function doesn't need to be used for the GTK+ frontend as it is equivalent to gtk_main()


ng_event_handler_loop_quit ()

void                ng_event_handler_loop_quit          (void);

Stop the event handler loop This function doesn't need to be used for the GTK+ frontend as it is equivalent to gtk_main_quit()


ng_event_handler_loop_depth ()

int                 ng_event_handler_loop_depth         (void);

Returns :

the depth of stack calls in the event handler loop This function returns 0 when no event handler loop is running yet