Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
6
66
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Eric Vidal
66
Commits
0a785ccd
Commit
0a785ccd
authored
5 years ago
by
Eric Vidal
Browse files
Options
Downloads
Patches
Plain Diff
split instance function
parent
e1b410b9
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/lib66/instance.c
+145
-0
145 additions, 0 deletions
src/lib66/instance.c
with
145 additions
and
0 deletions
src/lib66/instance.c
0 → 100644
+
145
−
0
View file @
0a785ccd
/*
* instance.c
*
* Copyright (c) 2018-2019 Eric Vidal <eric@obarun.org>
*
* All rights reserved.
*
* This file is part of Obarun. It is subject to the license terms in
* the LICENSE file found in the top-level directory of this
* distribution.
* This file may not be copied, modified, propagated, or distributed
* except according to the terms contained in the LICENSE file./
*/
#include
<66/utils.h>
#include
<string.h>
#include
<oblibs/error2.h>
#include
<oblibs/files.h>
#include
<oblibs/string.h>
#include
<oblibs/stralist.h>
#include
<oblibs/directory.h>
#include
<skalibs/stralloc.h>
#include
<skalibs/djbunix.h>
int
insta_replace
(
stralloc
*
sa
,
char
const
*
src
,
char
const
*
cpy
)
{
int
curr
,
count
;
if
(
!
src
||
!*
src
)
return
0
;
size_t
len
=
strlen
(
src
)
;
size_t
clen
=
strlen
(
cpy
)
;
curr
=
count
=
0
;
for
(
int
i
=
0
;
(
size_t
)
i
<
len
;
i
++
)
if
(
src
[
i
]
==
'@'
)
count
++
;
size_t
resultlen
=
len
+
(
clen
*
count
)
;
char
result
[
resultlen
+
1
]
;
for
(
int
i
=
0
;
(
size_t
)
i
<
len
;
i
++
)
{
if
(
src
[
i
]
==
'@'
)
{
if
(((
size_t
)
i
+
1
)
==
len
)
break
;
if
(
src
[
i
+
1
]
==
'I'
)
{
memcpy
(
result
+
curr
,
cpy
,
clen
)
;
curr
=
curr
+
clen
;
i
=
i
+
2
;
}
}
result
[
curr
++
]
=
src
[
i
]
;
}
result
[
curr
]
=
0
;
return
stralloc_obreplace
(
sa
,
result
)
;
}
/** instance -> 0, copy -> 1 */
int
insta_splitname
(
stralloc
*
sa
,
char
const
*
name
,
int
len
,
int
what
)
{
char
const
*
copy
;
size_t
tlen
=
len
+
1
;
char
template
[
tlen
+
1
]
;
memcpy
(
template
,
name
,
tlen
)
;
template
[
tlen
]
=
0
;
copy
=
name
+
tlen
;
if
(
!
what
)
return
stralloc_obreplace
(
sa
,
template
)
;
else
return
stralloc_obreplace
(
sa
,
copy
)
;
}
int
insta_create
(
stralloc
*
sasv
,
stralloc
*
sv
,
char
const
*
src
,
int
len
)
{
char
*
fdtmp
;
char
const
*
copy
;
size_t
tlen
=
len
+
1
;
stralloc
sa
=
STRALLOC_ZERO
;
stralloc
tmp
=
STRALLOC_ZERO
;
char
template
[
tlen
+
1
]
;
memcpy
(
template
,
sv
->
s
,
tlen
)
;
template
[
tlen
]
=
0
;
copy
=
sv
->
s
+
tlen
;
fdtmp
=
dir_create_tmp
(
&
tmp
,
"/tmp"
,
copy
)
;
if
(
!
fdtmp
)
{
VERBO3
strerr_warnwu1x
(
"create instance tmp dir"
)
;
return
0
;
}
if
(
!
file_readputsa
(
&
sa
,
src
,
template
)){
VERBO3
strerr_warnwu4sys
(
"open: "
,
src
,
"/"
,
template
)
;
return
0
;
}
if
(
!
insta_replace
(
&
sa
,
sa
.
s
,
copy
)){
VERBO3
strerr_warnwu2x
(
"replace instance character at: "
,
sa
.
s
)
;
return
0
;
}
/** remove the last \0 */
sa
.
len
--
;
if
(
!
file_write_unsafe
(
tmp
.
s
,
copy
,
sa
.
s
,
sa
.
len
)){
VERBO3
strerr_warnwu4sys
(
"create instance service file: "
,
tmp
.
s
,
"/"
,
copy
)
;
return
0
;
}
if
(
!
read_svfile
(
sasv
,
copy
,
tmp
.
s
))
return
0
;
if
(
rm_rf
(
tmp
.
s
)
<
0
){
VERBO3
strerr_warnwu2x
(
"remove tmp directory: "
,
tmp
.
s
)
;
return
0
;
}
stralloc_free
(
&
sa
)
;
stralloc_free
(
&
tmp
)
;
return
stralloc_obreplace
(
sv
,
copy
)
;
}
int
insta_check
(
char
const
*
svname
)
{
int
r
;
r
=
get_len_until
(
svname
,
'@'
)
;
if
(
r
<
0
)
return
-
1
;
return
r
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment