summaryrefslogtreecommitdiff
path: root/src/base/my_path.c
blob: 2cf25f8f07e595838a7254dce6f8ef7d736a655d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "main.h"
#include "homeDirector.h"
#include "textFile.h"
#include "path.h"
#include "my_path.h"

static char *path_prefix;

int path_init()
{
#if 0
	char str[STR_PATH_SIZE];
	textFile_t *text_file;

	sprintf(str, "%s/path", home_director_get());

	text_file = text_file_load(str);

	if( text_file == NULL )
	{
		fprintf(stderr, "I dont load path\n");
		exit(-1);
	}

	if( text_file->text->count == 0 )
	{
		fprintf(stderr, "%s is empty\n", str);
		exit(-1);
	}

	path_prefix = strdup(text_file->text->list[0]);

	text_file_destroy(text_file);

#endif

	path_prefix = malloc((strlen(PREFIX)+1)* sizeof(char));
	strcpy(path_prefix, PREFIX);
	strcat(path_prefix, "/");

	printf("use prefix:%s\n", path_prefix);

	return 0;
}

char* path_get_prefix()
{
	return path_prefix;
}

int path_set_prefix(char *path, int size, const char *subpath, const char *name)
{
	char *prefix;
	char *slash;
	int len;

	prefix = path_prefix;

/*
	printf("subpath = %s\n", subpath);
	printf("name = %s\n", name);
*/
	if( isFillPath(name) )
	{
		snprintf(path, size, "%s", name);
		//printf("%s\n", path);
		return 1;
	}

	if( isFillPath(subpath) )
	{
		snprintf(path, size, "%s", subpath);
		//printf("%s\n", path);
		return 1;
	}

	len = strlen(subpath);

	slash = "/";

	if( subpath[len-1] == '/' )
	{
		slash = "";
	}

/*
	printf("slash = %s\n", slash);
	printf("prefix = %s\n", prefix);
*/
	snprintf(path, size, "%s%s%s%s", prefix, subpath, slash, name);
	//printf("file %s\n", path);

	return 1;
}

int path_quit()
{
	if( path_prefix != NULL )
	{
		free(path_prefix);
	}

	return 0;
}