Task 117: .CSS File Format
Task 117: .CSS File Format
The .CSS file format refers to Cascading Style Sheets, a text-based stylesheet language used to describe the presentation and styling of web documents written in HTML or XML. It is not a binary format and does not have structured headers or encoded fields like many proprietary file formats; instead, it follows a syntax for defining rulesets consisting of selectors, properties, and values. The official specifications are maintained by the World Wide Web Consortium (W3C). Additional details on the format, including its typical UTF-8 encoding and lack of a magic number signature, are documented by the Library of Congress.
The .CSS file format is text-based and does not have intrinsic binary properties or structures (e.g., no fixed headers, offsets, or encoded fields). Therefore, the "properties intrinsic to its file system" refer to standard file system metadata attributes associated with any file, including .CSS files. These are accessed via operating system APIs and include:
- Filename
- Size (in bytes)
- Creation time (birth time, where available)
- Last modification time
- Last access time
- Mode (permissions, typically in octal representation)
- Owner user ID (UID)
- Group ID (GID)
Note: Some properties (e.g., UID, GID, creation time) may not be available or consistent across all operating systems or environments (e.g., Windows vs. Unix-like systems, or browser contexts).
Two direct download links for example .CSS files:
Below is a self-contained HTML page with embedded JavaScript that can be embedded in a Ghost blog (or any HTML-based blog platform). It creates a drag-and-drop area where the user can drop a .CSS file. Upon drop, it checks the file extension and MIME type, then dumps the available file system properties to the screen (using the browser's File API, which has limited access to metadata compared to server-side languages). No server-side processing is required.
- Below is a Python class. It can open a .CSS file (assuming UTF-8 encoding for read/decode), read its content, write new content to a file, and print the file system properties from the list above (using
os.stat
). Run it in a Python environment with file system access.
import os
import time
class CSSFileHandler:
def __init__(self, filepath):
self.filepath = filepath
if not self.filepath.endswith('.css'):
raise ValueError("File must have .css extension")
def read(self):
"""Read and decode the file content as UTF-8 text."""
with open(self.filepath, 'r', encoding='utf-8') as f:
return f.read()
def write(self, content):
"""Write text content to the file."""
with open(self.filepath, 'w', encoding='utf-8') as f:
f.write(content)
def print_properties(self):
"""Print file system properties."""
stat = os.stat(self.filepath)
print(f"Filename: {os.path.basename(self.filepath)}")
print(f"Size (bytes): {stat.st_size}")
print(f"Creation time: {time.ctime(stat.st_birthtime) if hasattr(stat, 'st_birthtime') else 'N/A'}")
print(f"Last modification time: {time.ctime(stat.st_mtime)}")
print(f"Last access time: {time.ctime(stat.st_atime)}")
print(f"Mode (permissions): {oct(stat.st_mode)}")
print(f"Owner user ID (UID): {stat.st_uid}")
print(f"Group ID (GID): {stat.st_gid}")
# Example usage:
# handler = CSSFileHandler('example.css')
# content = handler.read()
# handler.write('body { color: red; }')
# handler.print_properties()
- Below is a Java class. It can open a .CSS file, read its content (decoded as UTF-8), write new content, and print the file system properties (using
java.nio.file
for cross-platform access; note that UID/GID require POSIX support, which may not be available on all systems).
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.time.Instant;
public class CSSFileHandler {
private final Path filepath;
public CSSFileHandler(String filepath) {
this.filepath = Paths.get(filepath);
if (!filepath.endsWith(".css")) {
throw new IllegalArgumentException("File must have .css extension");
}
}
public String read() throws IOException {
// Read and decode as UTF-8
return new String(Files.readAllBytes(filepath), StandardCharsets.UTF_8);
}
public void write(String content) throws IOException {
Files.write(filepath, content.getBytes(StandardCharsets.UTF_8));
}
public void printProperties() throws IOException {
BasicFileAttributes attrs = Files.readAttributes(filepath, BasicFileAttributes.class);
System.out.println("Filename: " + filepath.getFileName());
System.out.println("Size (bytes): " + attrs.size());
System.out.println("Creation time: " + attrs.creationTime());
System.out.println("Last modification time: " + attrs.lastModifiedTime());
System.out.println("Last access time: " + attrs.lastAccessTime());
// For mode, UID, GID: Requires PosixFileAttributes if on Unix-like system
if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
PosixFileAttributes posixAttrs = Files.readAttributes(filepath, PosixFileAttributes.class);
System.out.println("Mode (permissions): " + posixAttrs.permissions());
System.out.println("Owner user ID (UID): " + posixAttrs.owner().getName()); // Name instead of ID
System.out.println("Group ID (GID): " + posixAttrs.group().getName()); // Name instead of ID
} else {
System.out.println("Mode (permissions): N/A (POSIX not supported)");
System.out.println("Owner user ID (UID): N/A (POSIX not supported)");
System.out.println("Group ID (GID): N/A (POSIX not supported)");
}
}
// Example usage:
// public static void main(String[] args) throws IOException {
// CSSFileHandler handler = new CSSFileHandler("example.css");
// String content = handler.read();
// handler.write("body { color: red; }");
// handler.printProperties();
// }
}
- Below is a JavaScript class (assuming a Node.js environment for file system access via
fs
). It can open a .CSS file, read its content (decoded as UTF-8), write new content, and print the file system properties.
const fs = require('fs');
const path = require('path');
class CSSFileHandler {
constructor(filepath) {
this.filepath = filepath;
if (!filepath.endsWith('.css')) {
throw new Error('File must have .css extension');
}
}
read() {
// Read and decode as UTF-8
return fs.readFileSync(this.filepath, 'utf-8');
}
write(content) {
fs.writeFileSync(this.filepath, content, 'utf-8');
}
printProperties() {
const stats = fs.statSync(this.filepath);
console.log(`Filename: ${path.basename(this.filepath)}`);
console.log(`Size (bytes): ${stats.size}`);
console.log(`Creation time: ${stats.birthtime}`);
console.log(`Last modification time: ${stats.mtime}`);
console.log(`Last access time: ${stats.atime}`);
console.log(`Mode (permissions): ${stats.mode.toString(8)}`);
console.log(`Owner user ID (UID): ${stats.uid}`);
console.log(`Group ID (GID): ${stats.gid}`);
}
}
// Example usage:
// const handler = new CSSFileHandler('example.css');
// const content = handler.read();
// handler.write('body { color: red; }');
// handler.printProperties();
- Below is a C++ class (since standard C does not have classes; using C++ for object-oriented structure). It can open a .CSS file, read its content (assuming UTF-8, read as string), write new content, and print the file system properties (using
<sys/stat.h>
for Unix-like systems; note that this may require adjustments for Windows).
#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h>
#include <ctime>
#include <stdexcept>
class CSSFileHandler {
private:
std::string filepath;
public:
CSSFileHandler(const std::string& fp) : filepath(fp) {
if (filepath.substr(filepath.find_last_of(".") + 1) != "css") {
throw std::invalid_argument("File must have .css extension");
}
}
std::string read() {
// Read as text (UTF-8 assumed)
std::ifstream file(filepath);
if (!file) {
throw std::runtime_error("Failed to open file for reading");
}
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return content;
}
void write(const std::string& content) {
std::ofstream file(filepath);
if (!file) {
throw std::runtime_error("Failed to open file for writing");
}
file << content;
}
void printProperties() {
struct stat stats;
if (stat(filepath.c_str(), &stats) != 0) {
throw std::runtime_error("Failed to get file stats");
}
std::cout << "Filename: " << filepath.substr(filepath.find_last_of("/\\") + 1) << std::endl;
std::cout << "Size (bytes): " << stats.st_size << std::endl;
std::cout << "Creation time: N/A (use st_ctime on some systems: " << std::ctime(&stats.st_ctime) << ")" << std::endl;
std::cout << "Last modification time: " << std::ctime(&stats.st_mtime) << std::endl;
std::cout << "Last access time: " << std::ctime(&stats.st_atime) << std::endl;
std::cout << "Mode (permissions): " << std::oct << stats.st_mode << std::endl;
std::cout << "Owner user ID (UID): " << stats.st_uid << std::endl;
std::cout << "Group ID (GID): " << stats.st_gid << std::endl;
}
};
// Example usage:
// int main() {
// try {
// CSSFileHandler handler("example.css");
// std::string content = handler.read();
// handler.write("body { color: red; }");
// handler.printProperties();
// } catch (const std::exception& e) {
// std::cerr << e.what() << std::endl;
// }
// return 0;
// }