You are not logged in.
Pages: 1
Topic closed
Hi
Is it possible to write a CMS (content management system) in pure C? How would the best way to approach it?
I'm a C beginner and thought this would be a nice project to enhance my C skills.
The CMS should be as simple as possible. My webserver is lighttpd.
Any advice is welcome.
Offline
I'm not a dev, but maybe you should start with simpler tasks: a clock, a calculator, calendar - you can combine them into your CMS one day.
Offline
I think you may end up with a realization of Greenspun's Tenth Rule. In this particular case you can replace Common Lisp with PHP
But you asked for advice. I would try to implement some kind of static HTML page generator + curses or gui based progam to fill database records with data. If you need anything to be displayed dynamically (e.g. visitor counter) you can generate PHP code and run it every time through the interpreter with lighthttpd. Later on you can replace it with custom CGI script and generate everything on the fly.
If I were you I would use a different tool for the job.
Last edited by j.roszk (2010-07-30 20:36:59)
Offline
I would not try that with C. C is ~portable compiler things like webservers are written with it but you would have to implement many old wheels that alredy exists for other languages.
{ Github } {Blog and other stuff }
Offline
you're probably right. I just found this: http://sourceforge.net/projects/damn-small-cms/
but it seems to be abandonded for years with the code no longer available.
I also found that this guy obviously has his own C CMS, but it's not open source: http://blog.fefe.de/faq.html
but he also wrote his own webserver and some custom libs for it, which is way beyond my understandings yet.
Offline
you're probably right. I just found this: http://sourceforge.net/projects/damn-small-cms/
but it seems to be abandonded for years with the code no longer available.I also found that this guy obviously has his own C CMS, but it's not open source: http://blog.fefe.de/faq.html
but he also wrote his own webserver and some custom libs for it, which is way beyond my understandings yet.
This guy attempted to write a replacement for glibc IIRC so he's not quite a newbie. It should tell you loud and clear that writing a cms in C in not a sport for the meek.
Offline
Interesting topic. I've also been experimenting with web applications in C and it's not as difficult as it might sound. However, all you need is a library which does a great deal of effort to prevent common errors problems like buffer overflows. As soon as more your design gets more and more complex, a template engine becomes indispensable.
But most importantly, you have to ensure that your code is well-structured. For my projects, I am using radical concepts to enforce an object-oriented approach:
1) A .c file always contains a class.
2) Methods must be implemented like this: Class_Method(Class *this, String var1, String var2)
3) Each class must have a constructor, Class_Init(...), and may have a destructor, Class_Destroy().
4) Namespaces must be used for all libraries and may be used for the application itself.
I always separate the web code from the library code. The web interface is where you interact with the user, whereas in the library, you're processing the data. It's important to make this distinction because it makes your code much cleaner and more debuggable (Writing test cases for web interfaces is far more difficult than just calling a function for doing the same.)
Function names can get very long. That's why I don't use namespaces in the applications. But the libraries should have a namespace because most functions in the web interface are actually called after a library function which could lead to conflicts otherwise.
Ok, this sounds very theoretical, so here's a real-life example: I'm currently working on a news crawler completely written in C.
It has a console interface which checks some websites and downloads news to disk which doesn't appear in the index yet.
There's also a web interface which uses the same library as the console interface. Hence, no code duplication. It can query the local archive or just query the websites in real time.
The web interface consists of resources. A resource is defined at the end of a file, e.g.:
ResourceInterface Listing_Methods = {
.isRequested = (void *) Listing_IsRequested,
.new = (void *) Listing_New,
.init = (void *) Listing_Init,
.destroy = (void *) Listing_Destroy,
.onHeader = NULL,
.onQueryParameter = (void *) Listing_OnQueryParameter,
.onBodyParameter = (void *) Listing_OnBodyParameter,
.setPath = (void *) Listing_SetPath,
.onRequest = (void *) Listing_OnRequest
};
The isRequested method compares the path and returns true when the resource matches. Each resource can even accept multiple paths, e.g.:
bool Listing_IsRequested(String path) {
return String_BeginsWith(path, $("/category"))
|| String_BeginsWith(path, $("/archive"))
|| String_BeginsWith(path, $("/list"));
}
A resource may also have a function for extracting parts from the URL. Here's one I'm using:
void Listing_SetPath(Listing *this, String path) {
StringArray *parts = String_Split(path, '/');
if (String_Equals(parts->buf[1], $("list"))) {
this->path.type = Spider_ArticleType_List;
this->path.u.list = HeapString(0);
if (parts->len > 2) {
this->path.u.list = String_Clone(parts->buf[2]);
}
} else if (String_Equals(parts->buf[1], $("archive"))) {
this->path.type = Spider_ArticleType_Archive;
this->path.u.archive.provider = HeapString(0);
this->path.u.archive.month = HeapString(0);
if (parts->len > 2) {
this->path.u.archive.provider = String_Clone(parts->buf[2]);
if (parts->len > 3) {
this->path.u.archive.month = String_Clone(parts->buf[3]);
}
}
} else if (String_Equals(parts->buf[1], $("category"))) {
this->path.type = Spider_ArticleType_Category;
this->path.u.category = Spider_Category_Unset;
if (parts->len > 2) {
this->path.u.category = Spider_Category_Resolve(parts->buf[2]);
}
}
Array_Destroy(parts);
}
It might look very lengthy but it's actually covering a lot of cases. I wouldn't even know how to do the same with regular expressions.
This will work for:
/list
/list/To Read
/archive
/archive/Guardian
/archive/Guardian/2010-08
/category
/category/Health
As you can see, it's all actually very readable. Of course, you could also use a router and regular expressions for all these things. It's a matter of taste but I like it better to have everything together. I don't want these things scattered over the whole project while they can be encapsulated.
Screenshot:
tim@tim-laptop /Projects/Spider/Web $ du -h httpd.bin
1,2M httpd.bin
The whole application, with HTTP server and all necessary libraries built in weighs only 1.2 MiB! No need for any third-party libraries or runtimes.
I can highly recommend it to anyone. Writing websites in C will be a bit different from what we're used to but it also offers a great flexibility. With the right compiler and some tricks, you can make things nearly as comfortable as in Python or Ruby. What's even more worthwhile is the learning effect: You'll start thinking completely differently about solving problems helping you to write faster and more secure applications.
Last edited by tindzk (2010-08-01 13:50:23)
Offline
Wt is a very C++-ey web framework, so it's not quite what you're looking for.
It sure looks interesting though.
I'm currently looking for a framework to use for a project I'll be starting soon and this looks mighty tempting as by far most comfortable with C++/Qt than any other language or framework.
Offline
Interesting topic. I've also been experimenting with web applications in C and it's not as difficult as it might sound. However, all you need is a library which does a great deal of effort to prevent common errors problems like buffer overflows. As soon as more your design gets more and more complex, a template engine becomes indispensable.
But most importantly, you have to ensure that your code is well-structured. For my projects, I am using radical concepts to enforce an object-oriented approach:
1) A .c file always contains a class.
2) Methods must be implemented like this: Class_Method(Class *this, String var1, String var2)
3) Each class must have a constructor, Class_Init(...), and may have a destructor, Class_Destroy().
4) Namespaces must be used for all libraries and may be used for the application itself.
Um..., any reason not to use C++ if you're this into a strictly object-oriented approach?
Offline
Um..., any reason not to use C++ if you're this into a strictly object-oriented approach?
I've actually tried to learn C++ but it turned out that this language is far too complicated. A glimpse at Boost shows its utter complexity.
Interestingly, I make a lot more mistakes in C++ than in C. Hence, for me it's less productive.
C is WYSIWYG; I have the complete control over what the program does. There are no silly type-dependent ambiguities like the 'equal' operator.
C++ is incompatible to C; I'd have to insert lots of #ifdefs to make my headers C++-compatible. Why? Shouldn't it be the other way round?
Compiling C++ takes much longer than C. I don't know why but it's annoying.
Even people like Torvalds to whom I'd attribute a certain expertise share my opinion about C++. Are they all wrong?
In a Lisp discussion, I can remember someone calling C a 'lingua franca'. I think that's an excellent comparison. Sure, even C++ will be still written in a few years but C is the basis for all major operating systems. It's used almost exclusively in embedded systems.
Why should I be using C++ apart from OO? I want a transparent, fast and simple language. C++, I'm afraid, doesn't comply with my requirements.
Offline
I would have a look at this:
http://www.boutell.com/cgic/
Offline
tavianator wrote:Um..., any reason not to use C++ if you're this into a strictly object-oriented approach?
...snip...
Any reason not to use C while you're using C? If you're going to use OOP, why not just use that little part of C++? The #ifdefs are almost nothing.
Offline
I can really recommend tntnet (http://tntnet.org) it's a full webserver written in C++ with a very smart usage (php-like) http://tntnet.org/exampleCalc.html
Offline
Any reason not to use C while you're using C? If you're going to use OOP, why not just use that little part of C++? The #ifdefs are almost nothing.
Sure, writing a few #ifdefs is not much effort but why do I have to "fix" my C headers just to make them C++ compatible? It's a tedious task and I wouldn't ever want to do it for a larger code base.
Even though using the OO subset of C++ sounds seductive, there are yet too many differences (cf. http://nothings.org/computer/cpp.html) and incompatibilities between C++ and C to make programming in it a pleasant experience.
Last edited by tindzk (2010-08-02 12:37:39)
Offline
saline wrote:Any reason not to use C while you're using C? If you're going to use OOP, why not just use that little part of C++? The #ifdefs are almost nothing.
Sure, writing a few #ifdefs is not much effort but why do I have to "fix" my C headers just to make them C++ compatible? It's a tedious task and I wouldn't ever want to do it for a larger code base.
Even though using the OO subset of C++ sounds seductive, there are yet too many differences (cf. http://nothings.org/computer/cpp.html) and incompatibilities between C++ and C to make programming in it a pleasant experience.
Touche.
Offline
saline wrote:Any reason not to use C while you're using C? If you're going to use OOP, why not just use that little part of C++? The #ifdefs are almost nothing.
Sure, writing a few #ifdefs is not much effort but why do I have to "fix" my C headers just to make them C++ compatible? It's a tedious task and I wouldn't ever want to do it for a larger code base.
Even though using the OO subset of C++ sounds seductive, there are yet too many differences (cf. http://nothings.org/computer/cpp.html) and incompatibilities between C++ and C to make programming in it a pleasant experience.
Because C++ isn't strictly backward-compatible with C? For the record, here's all I had to do for my ~10,000 line raytracer's headers: http://gitorious.org/dimension/dimensio … n.h#line54
Anyway, I was in no way suggesting that C++ is somehow better than C. All I meant was that forcing the OO paradigm that strictly on your C code makes no sense, as C isn't an OO-paradigm language.
Sorry for getting so off topic, I'll go now
Offline
sorry, for bringing this old thread up again, but I now found a project others may also be interested in:
http://cppcms.sourceforge.net/wikipp/en/page/main
a web framework written in c++
they also published a benchmark to compare it to perl/python/PHP CMSs, which is pretty impressive:
http://art-blog.no-ip.info/cppcms/blog/post/67
Offline
you could also create a basic webpage in php, compile it with hiphop to C++ and go further from that
< Daenyth> and he works prolifically
4 8 15 16 23 42
Offline
you could also create a basic webpage in php, compile it with hiphop to C++ and go further from that
thanks I never heard of this before. I didn't know facebook contributed something for the open source community.
Offline
Dieter@be wrote:you could also create a basic webpage in php, compile it with hiphop to C++ and go further from that
thanks I never heard of this before. I didn't know facebook contributed something for the open source community.
seriously? they contribute a *lot*
< Daenyth> and he works prolifically
4 8 15 16 23 42
Offline
C is really not particulalry well matched for a CMS, particularly a web-based CMS. If you are looking to tone your C skills then I suggest you just download the kernel source code and peruse it for a while looking up certain things that confuse you (there will be many) and then possibly hack on it. For web stuff you have a plethora of languages at your disposal: PHP, Ruby (on Rails), Java, etc... However, there is nothing stopping you from writing the high performance bits in C and then interoperating with them via one of the aforementioned languages.
Offline
You might want to look at fcgi which is about 5 times faster than cgi, you can use existing webserver such as nginx or cherokee.
Offline
BinaryTiers uses a very simple C syntax. It might be used in any scenario where you need Content Management, from basic web applications to complex social networking websites :
Offline
This thread is over a year old. I am going to close this thread in accordance with our policy
If anyone is experiencing an issue, please feel free to start a new thread and reference this thread. That way, we can keep old information from muddying the waters of contemporary issues.
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
Pages: 1
Topic closed