can anyone help me? here is my problem.
everytime i try to print the head node data and current node data, only first 25 characters from the string are printed.
here is the source code..
Code:
// main.c
#include "libdirectory/libdirectory.h"
#include <stdio.h>
int main( int argc, char *argv[])
{
stringlist sl = directory_read_contents("/home/dipesh/Pictures");
stringlist s = string_list_init();
string_list_append(&s, "asdfghklqwertyyui");
string_list_append(&s, "asdfghklqwertyyuifsddffop");
//printf("%s\n", s.head->data);
//printf("%d\n", string_length(s.head->data));
//printf("%d\n", string_length(sl.head->data));
/*while ( sl.current != sl.tail ) {
printf("%s\n", sl.current->data);
string_list_next(&sl);
}*/
string_list_free(&sl);
string_list_free(&s);
return 0;
}
Code:
// libstringman.h
#include <stdlib.h>
#define FALSE 0
#define TRUE !FALSE
typedef int boolean;
typedef enum {
STRING_LIST_SORT_NONE,
STRING_LIST_SORT_ASCENDING,
STRING_LIST_SORT_DESCENDING
}stringlistsorttype;
struct _stringnode {
int index;
char *data;
struct _stringnode *next;
struct _stringnode *prev;
};
typedef struct _stringnode stringnode;
struct _stringlist {
stringnode *head;
stringnode *tail;
stringnode *current;
int len;
};
typedef struct _stringlist stringlist;
// String Operations
char *string_copy(char *src); // copies the string from src and returns the destination string
int string_length(char *str); // returns the length of the string
char *string_join(char *str1, char *str2); // joins the str1 and str2, returns a new string
boolean string_is_equal(char *str, char *str2); // checks whether two strings are equal. not similar to strcmp.
char *string_split(char *str, char delimiter); // splits a string using a delimiter
// String List
stringlist string_list_init(void);
void string_list_append(stringlist *sl, char *str);
void string_list_next(stringlist *sl);
void string_list_prev(stringlist *sl);
void string_list_free(stringlist *sl);
Code:
// libstringman.c
#include "libstringman.h"
#include <stdio.h>
char *string_copy(char *src)
{
char *dest = NULL;
int i = 0;
dest = (char *)malloc(sizeof(string_length(src)));
while ( src[i] != '\0' ) {
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return dest;
}
int string_length(char *str)
{
int i = 0;
char temp = str[0];
while ( temp != '\0' ) {
temp = str[++i];
}
return i;
}
char *string_join(char *str1, char *str2)
{
char *temp = NULL;
int i = 0, j = 0;
temp = malloc(sizeof(string_length(str1) + string_length(str2)));
while ( str1[i] != '\0' ) {
temp[i] = str1[i];
i++;
}
while ( str2[j] != '\0' ) {
temp[i++] = str2[j++];
}
return temp;
}
boolean string_is_equal(char *str1, char *str2)
{
if ( string_length(str1) != string_length(str2) ) {
return FALSE;
} else {
int i = 0;
while ( str1[i] != '\0' || str2[i] != '\0' ) {
if ( str1[i] != str2[i] )
return FALSE;
i++;
}
return TRUE;
}
}
char *string_split(char *str, char delimeter)
{
char *temp = NULL;
int i = 0, j = 0, flag = 0;
while ( str[i] != '\0' ) {
if ( str[i] == delimeter )
flag = 1;
if ( flag == 0 )
i++;
else {
i++;
j++;
}
}
temp = malloc(j);
i = i - j;
j = 0;
while ( str[i] != '\0' ) {
temp[j] = str[i];
i++;
j++;
}
temp[j] = '\0';
return temp;
}
stringlist string_list_init(void)
{
stringlist sl;
sl.len = 0;
sl.current = NULL;
sl.head = NULL;
sl.tail = NULL;
return sl;
}
void string_list_append(stringlist *sl, char *str)
{
stringnode *temp = NULL;
temp = (stringnode *)malloc(sizeof(stringnode));
temp->data = string_copy(str);
//printf("%s\n", temp->data);
if ( sl->len == 0 ) {
temp->index = 0;
temp->next = NULL;
temp->prev = NULL;
sl->head = temp;
sl->tail = temp;
sl->len = 1;
sl->current = sl->tail;
printf("head : %s\n", sl->head->data);
printf("tail : %s\n", sl->tail->data);
printf("current : %s\n", sl->current->data);
} else {
temp->index = sl->len++;
temp->next = NULL;
temp->prev = sl->tail;
sl->tail->next = temp;
sl->tail = temp;
printf("head : %s\n", sl->head->data);
printf("tail : %s\n", sl->tail->data);
printf("current : %s\n", sl->current->data);
}
//printf("%s\n", sl->current->data);
temp = NULL;
}
void string_list_next(stringlist *sl)
{
sl->current = sl->current->next;
if ( sl->current == NULL )
sl->current = sl->head;
}
void string_list_prev(stringlist *sl)
{
sl->current = sl->current->prev;
if ( sl->current == NULL )
sl->current = sl->tail;
}
void string_list_free(stringlist *sl)
{
stringnode *temp;
sl->current = sl->head;
while ( sl->current != NULL ) {
temp = sl->current->next;
free(sl->current);
sl->current = temp;
}
free(sl->head);
free(sl->tail);
sl->head = NULL;
sl->tail = NULL;
sl->len = 0;
}
Code:
// libdirectory.h
#include <dirent.h>
#include <sys/stat.h>
#include "../libstringman/libstringman.h"
stringlist directory_read_contents(char *path);
Code:
// libdirectory.c
#include "libdirectory.h"
#include <stdio.h>
stringlist directory_read_contents(char *path)
{
struct stat st;
DIR *dir;
struct dirent *drent;
stringlist sl = string_list_init();
stat(path, &st);
if ( S_ISDIR(st.st_mode) ) {
dir = opendir(path);
while ( drent = readdir(dir) ) {
string_list_append(&sl, drent->d_name);
}
}
return sl;
}
Code:
head : widescreen-landscape-99-1920x1200.jpg <-- first time, its printing fine
tail : widescreen-landscape-99-1920x1200.jpg
current : widescreen-landscape-99-1920x1200.jpg
head : widescreen-landscape-99-1 <-- from the second time, only 25 characters
tail : BloodDragon_wallpaper_wide_1680x1050.jpg
current : widescreen-landscape-99-1
head : widescreen-landscape-99-1
tail : original1.jpeg
current : widescreen-landscape-99-1
head : widescreen-landscape-99-1
tail : original.jpeg
current : widescreen-landscape-99-1
head : widescreen-landscape-99-1
tail : 128.jpeg
current : widescreen-landscape-99-1
head : widescreen-landscape-99-1
tail : 2011-05-18-132559.jpg
current : widescreen-landscape-99-1
head : widescreen-landscape-99-1
tail : 87.jpeg
current : widescreen-landscape-99-1
head : widescreen-landscape-99-1
tail : 52.jpeg
i've also attached the original source file..