1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Merge common and util

This commit is contained in:
James Booth 2012-08-23 01:08:06 +01:00
parent 8b7975bdf3
commit 0fe70ce7d3
15 changed files with 91 additions and 142 deletions

View File

@ -1,18 +1,18 @@
bin_PROGRAMS = profanity
profanity_SOURCES = src/command.c src/contact.c src/history.c src/jabber.h \
src/preferences.c src/prof_autocomplete.c src/status_bar.c src/util.h \
src/preferences.c src/prof_autocomplete.c src/status_bar.c \
src/command.h src/contact.h src/history.h src/log.c src/preferences.h \
src/prof_autocomplete.h src/title_bar.c src/windows.c src/common.c \
src/contact_list.c src/input_win.c src/log.h src/profanity.c \
src/prof_history.c src/ui.h src/common.h src/ contact_list.h src/jabber.c \
src/main.c src/profanity.h src/prof_history.h src/util.c src/chat_log.c \
src/main.c src/profanity.h src/prof_history.h src/chat_log.c \
src/chat_log.h src/tinyurl.c src/tinyurl.h
TESTS = tests/testsuite
check_PROGRAMS = tests/testsuite
tests_testsuite_SOURCES = tests/test_contact_list.c src/contact_list.c src/contact.c \
tests/test_util.c tests/test_prof_history.c src/prof_history.c src/util.c \
tests/test_prof_autocomplete.c src/prof_autocomplete.c src/common.c tests/testsuite.c
tests/test_common.c tests/test_prof_history.c src/prof_history.c src/common.c \
tests/test_prof_autocomplete.c src/prof_autocomplete.c tests/testsuite.c
tests_testsuite_LDADD = -lheadunit -lstdc++
man_MANS = docs/profanity.1

View File

@ -27,7 +27,7 @@
#include "chat_log.h"
#include "common.h"
#include "util.h"
#include "common.h"
#include "log.h"
static GHashTable *logs;

View File

@ -25,6 +25,9 @@
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <glib.h>
@ -57,3 +60,72 @@ create_dir(char *name)
if (errno == ENOENT)
e = mkdir(name, S_IRWXU);
}
void
get_time(char *thetime)
{
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(thetime, 80, "%H:%M", timeinfo);
}
char *
str_replace(const char *string, const char *substr,
const char *replacement)
{
char *tok = NULL;
char *newstr = NULL;
char *oldstr = NULL;
char *head = NULL;
if (string == NULL)
return NULL;
if ( substr == NULL ||
replacement == NULL ||
(strcmp(substr, "") == 0))
return strdup (string);
newstr = strdup (string);
head = newstr;
while ( (tok = strstr ( head, substr ))) {
oldstr = newstr;
newstr = malloc ( strlen ( oldstr ) - strlen ( substr ) +
strlen ( replacement ) + 1 );
if ( newstr == NULL ) {
free (oldstr);
return NULL;
}
memcpy ( newstr, oldstr, tok - oldstr );
memcpy ( newstr + (tok - oldstr), replacement, strlen ( replacement ) );
memcpy ( newstr + (tok - oldstr) + strlen( replacement ),
tok + strlen ( substr ),
strlen ( oldstr ) - strlen ( substr ) - ( tok - oldstr ) );
memset ( newstr + strlen ( oldstr ) - strlen ( substr ) +
strlen ( replacement ) , 0, 1 );
head = newstr + (tok - oldstr) + strlen( replacement );
free (oldstr);
}
return newstr;
}
int
str_contains(char str[], int size, char ch)
{
int i;
for (i = 0; i < size; i++) {
if (str[i] == ch)
return 1;
}
return 0;
}

View File

@ -54,5 +54,9 @@ typedef enum {
void p_slist_free_full(GSList *items, GDestroyNotify free_func);
void create_config_directory(void);
void create_dir(char *name);
void get_time(char *thetime);
char * str_replace(const char *string, const char *substr,
const char *replacement);
int str_contains(char str[], int size, char ch);
#endif

View File

@ -29,14 +29,12 @@
#include "contact.h"
#include "contact_list.h"
#include "prof_autocomplete.h"
#include "log.h"
static PAutocomplete ac;
void
contact_list_init(void)
{
log_msg(PROF_LEVEL_INFO, "prof", "Initialising contact list");
ac = p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(PEqualDeepFunc)p_contacts_equal_deep,

View File

@ -47,7 +47,7 @@
#include "ui.h"
#include "history.h"
#include "preferences.h"
#include "util.h"
#include "common.h"
#include "command.h"
static WINDOW *inp_win;

View File

@ -30,7 +30,7 @@
#include "chat_log.h"
#include "contact_list.h"
#include "ui.h"
#include "util.h"
#include "common.h"
#include "preferences.h"
#define PING_INTERVAL 120000 // 2 minutes

View File

@ -86,6 +86,7 @@ profanity_init(const int disable_tls, char *log_level)
gui_init();
jabber_init(disable_tls);
cmd_init();
log_msg(PROF_LEVEL_INFO, "prof", "Initialising contact list");
contact_list_init();
atexit(_profanity_shutdown);
}

View File

@ -26,7 +26,7 @@
#include <ncurses.h>
#include "ui.h"
#include "util.h"
#include "common.h"
static WINDOW *status_bar;
static char *message = NULL;

View File

@ -1,95 +0,0 @@
/*
* util.c
*
* Copyright (C) 2012 James Booth <boothj5@gmail.com>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
void
get_time(char *thetime)
{
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(thetime, 80, "%H:%M", timeinfo);
}
char *
str_replace (const char *string, const char *substr,
const char *replacement)
{
char *tok = NULL;
char *newstr = NULL;
char *oldstr = NULL;
char *head = NULL;
if (string == NULL)
return NULL;
if ( substr == NULL ||
replacement == NULL ||
(strcmp(substr, "") == 0))
return strdup (string);
newstr = strdup (string);
head = newstr;
while ( (tok = strstr ( head, substr ))) {
oldstr = newstr;
newstr = malloc ( strlen ( oldstr ) - strlen ( substr ) +
strlen ( replacement ) + 1 );
if ( newstr == NULL ) {
free (oldstr);
return NULL;
}
memcpy ( newstr, oldstr, tok - oldstr );
memcpy ( newstr + (tok - oldstr), replacement, strlen ( replacement ) );
memcpy ( newstr + (tok - oldstr) + strlen( replacement ),
tok + strlen ( substr ),
strlen ( oldstr ) - strlen ( substr ) - ( tok - oldstr ) );
memset ( newstr + strlen ( oldstr ) - strlen ( substr ) +
strlen ( replacement ) , 0, 1 );
head = newstr + (tok - oldstr) + strlen( replacement );
free (oldstr);
}
return newstr;
}
int
str_contains(char str[], int size, char ch)
{
int i;
for (i = 0; i < size; i++) {
if (str[i] == ch)
return 1;
}
return 0;
}

View File

@ -1,31 +0,0 @@
/*
* util.h
*
* Copyright (C) 2012 James Booth <boothj5@gmail.com>
*
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef UTIL_H
#define UTIL_H
void get_time(char *thetime);
char * str_replace(const char *string, const char *substr,
const char *replacement);
int str_contains(char str[], int size, char ch);
#endif

View File

@ -31,7 +31,7 @@
#endif
#include "ui.h"
#include "util.h"
#include "common.h"
#include "contact.h"
#include "command.h"
#include "preferences.h"

View File

@ -1,7 +1,7 @@
#include <stdlib.h>
#include <string.h>
#include <head-unit.h>
#include "util.h"
#include "common.h"
void replace_one_substr(void)
{
@ -146,9 +146,9 @@ void replace_when_new_null(void)
assert_string_equals("hello", result);
}
void register_util_tests(void)
void register_common_tests(void)
{
TEST_MODULE("util tests");
TEST_MODULE("common tests");
TEST(replace_one_substr);
TEST(replace_one_substr_beginning);
TEST(replace_one_substr_end);

View File

@ -5,7 +5,7 @@ int main(void)
{
register_prof_history_tests();
register_contact_list_tests();
register_util_tests();
register_common_tests();
register_prof_autocomplete_tests();
run_suite();
return 0;

View File

@ -3,7 +3,7 @@
void register_prof_history_tests(void);
void register_contact_list_tests(void);
void register_util_tests(void);
void register_common_tests(void);
void register_prof_autocomplete_tests(void);
#endif