# strlen splitflie and strcat



## max21 (Jun 1, 2018)

How can I split this file into three parts and save them to disk with new names?  From that point, I should be able to insert any addition html code in another file in-between to create a new updated single file with all parts intact.  So the second part to this question is how would I bring all these parts back together again all at once?  Could someone provide a working script to get me started?  Even bits and pieces can help for now.

I also need to know what programming language to use.  I google ASM, Perl and PHP and none of them speak of doing such a thing for any UNIX other then splitting big files into smaller ones of the same size and such.  I would like to dedicate to PHP or C++ for web programming provided it can do deep text manipulation as described here.  Years ago I done this in all kinds of ways using MASM32 on Windows, but now it’s all about doing things automatically in the background on my FreeBSD vps.

BTW: if anyone find anything near this for FreeBSD on the internet please clue me up.  I been searching for two straight days; once again!

I got stop at No prefix under the Programming Forum.  I don't know what to use so I guess this is a general question.

*Example File*

```
html, body {
    height: 100%;
}
#container {
    position: relative;
    height: auto;
    max-height: 100%;
}
#content {
    position: absolute;
    height: auto;
    max-height: 100%;
}
#header {
    position: absolute;
    width: 100%;
    height: 150px;
    top: 0px;
    left: 0px;
}
#footer {
    position: absolute;
    width: 100%;
    height: 150px;
    left: 0px;
    bottom: 0px;
}

#content .inner {
    margin-top: 150px;
    margin-bottom: 150px;
}
```


*Split-1:*

```
[code]html, body {
    height: 100%;
}
#container {
    position: relative;
    height: auto;
    max-height: 100%;
}
#content {
    position: absolute;
    height: auto;
    max-height: 100%;
}
```


*Split-2:*

```
#header {
    position: absolute;
    width: 100%;
    height: 150px;
    top: 0px;
    left: 0px;
}
```


*Split-3:*

```
#footer {
    position: absolute;
    width: 100%;
    height: 150px;
    left: 0px;
    bottom: 0px;
}
#content .inner {
    margin-top: 150px;
    margin-bottom: 150px;
}
```


----------



## obsigna (Jun 1, 2018)

Currently, I am working on a project of mine named ContentCGI, which is intended to be a backend to Anthony Blackshaw's ContentTools frontend. Both projects are on GitHub, and while ContentCGI is not ready for prime time yet, the ContentTools are well established already.

What you are looking for, is basically done by the content-delegate plugin of the ContentCGI daemon, which on the fly injects the ContenTools widget CSS and JS tags into per GET request outgoing HTML files, and updates that files with any changes sent by the user agent via incoming POST requests.

The injection, spliting and joining is done in hard-core C, even if the source file appears to be an Objective-C one, Obejctive-C is used for different purposes.

If you are comfortable with C, then look at the following code for how splitting, injection and joining could be done effectively in C:

The GET responder section:
https://github.com/cyclaero/ContentCGI/blob/master/plugins/content-delegate/content-delegate.m#L211


The POST responder section:
https://github.com/cyclaero/ContentCGI/blob/master/plugins/content-delegate/content-delegate.m#L321
If this seems too much byte banging to you, then you probably would want to look out for a higher level language.


----------



## ralphbsz (Jun 1, 2018)

Do you have to do this once, or do you have to do this many times?

Once: Use an editor of your choice.  I personally prefer emacs, but I hear other people like vi(m).  

Many times?  There are many tools available, but ASM (I presume you mean assembly), perl and PHP are all way off base.  Those are programming languages.  What you need is a simple tool for file manipulation of text files.  I define "text files" as those they consist of lines of human-readable characters, with sensible line length.

I think the best (simplest) tool would for splitting would be awk.  It is a very old and well-known and documented tool, available on FreeBSD (and all other Unixes, including Linux).  For how to implement it in awk, the answer depends crucially on how exactly you define the split operation.  For example, you might tell us that the first part is always 13 lines long.  Then a good awk program might be something like "{if (NR<=13) print}, and you're done getting the first part out . Your prescription might also be "go to the content section, and then wait for the closing bracket".  That awk program would be several lines long (3 or 4 lines).  Since I'm too lazy to write the awk program for you, I would suggest that you read some web pages with awk examples and tutorials, and it will become clear to you.

For putting things back together, there is a very simple solution: put the parts into individual files, and then use "cat".


----------



## max21 (Jun 1, 2018)

obsigna, I blew my chance to learn c.  Back in the Roaring 90’s, hutch the creator of masm32 persuaded new comers to be anti-c, Delphi and against any form of scripting.  I had just learned to use Delphi well after a few month before jumping-ship.  Just like all things was and still is; Linux vs BSD, PHP vs Python, etc.

Your project is way over my head for _now_!  It’s my fault! I presented the wrong type as an example file and generalized things with my comment about web programming, which I should have not done. .. but still, it brought out the big-boys and look what I will be getting into in the near future    I might go c or C++  if I can’t get it work in ASM.  I got to dedicate to something other for the last time for FreeBSD… and now I hear about AWK.  Nonetheless, for now I take the easy road as long as it’s fail-proof.

More about type files:  it got to work for any type of configuration file that has known tag in it which make it easy to find.  It’s all about updating your own file your own way.  No database or editor needed for something this simple.  Speed is not important.  It will be a one-time daily cron-job completed in ms.

By doing it yourself you can set it up for any kind of file that is not yet ready to be placed for the internet or to change things on the desktop.  My new .css file gave me the idea.  It need to be daily interactive.  For example, any changed made such as the weather temperature, pricing or the Dow Jones Industrial Average.  For me I prefer a standalone program or script to certified thing off-line before sending the .css file to the web site visitors.  Automation is the goal, then comes ContentCGI* and such.  I hope to paint the complete picture of what I am trying to do and why.  I seek no other solution made by ready-made programs outside of this and as far as an editor it will be an editor that runs by itself.

I use the html file as the example because it has tag that can be found by any find-function.

Basically this is what it should be capable of and nothing more.   It been many years since I use to do something like this using the masm32 assembler, so below is only to provide a complete concept.

*Take it from the Bottom to the Top:*

0) Use readfile and read the file into buffer-0
………….…………………………………………………….

1) In buffer-0 use find to find this tag ->#content .inner
2) strlen all characters below it.
3) save #content .inner and anything below it as part-1.txt
4) find #content .inner again
5) zero-out all characters in #content .inner and below it
………………………………………………………………….

1) find #footer
2) strlen all characters below it.
3) save #content .inner and anything below it as part-2.txt
4) find #footer again
5) zero-out all characters in #footer
………………………………………………………………….

1) find #header
2) strlen all characters below it.
3) save #header with nothing below it as part-3.txt
4) find #header again
5) zero-out all characters in #header
………….…………………………………………………….

1) html, body is all that’s left

2) save #header as part-4.txt
………….…………………………………………………….

Another way would be to use readfile and read the same file into buffer0 - buffer1 - buffer2 - buffer3 - buffer-4 and play with it from there.  If I try to explain it better I will surely loss focus, expecially when I don't know what to use for this OS, forever.

ralphbsz, I’m reading about AWK right now.  My only fear is the syntax can be just as difficult as c and c is pure lighting below assembler but it has the potential to live forever.  But still AWK got to be the way to go.  Btw: hutch the (self named) Ol' Dirty Bastard would curse us if we call it assembly.


----------



## Deleted member 54719 (Jun 1, 2018)

I use perl for this kind of stuff.  As long as the search patterns are newline terminated it's pretty easy to split files (either into memory buffers, or as temporary files, depending upon expected size).

There doesn't seem to be much perl love these days.


----------



## Deleted member 54719 (Jun 1, 2018)

This should get your started...


```
$top = "html, body \\{";
$replace = "#header \\{";
$bottom = "#footer \\{";

@topLines = ();                        # holds top section lines
@bottomLines = ();                    # holds bottom section lines

# --- read input file
open(INPUT, "<input.txt") || die "Cannot open input.txt?\n";

while (<INPUT>) {

    if (/^\s*$top\s*$/) {
        $where = 1;
    } elsif (/^\s*$replace\s*$/) {
        $where  = 2;
    } elsif (/^\s*$bottom\s$/) {
        $where = 3;
    }
   
    if ($where == 1) {
        push @topLines, $_;
    } elsif ($where == 2) {

    } elsif ($where == 3) {
        push @bottomLines, $_;
    }

}
close INPUT;


# --- show what was saved in the arrays
print "----- top ---------\n";
foreach $i (@topLines) {
    print $i;
}

print "\n\n--------- bottom ----------\n";
foreach $i (@bottomLines) {
    print $i;
}
```


----------

