Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / hwpfilter / source / hstyle.cxx
blobe0573edf9cad06dc1996269565874484801a63fa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "precompile.h"
22 #include <comphelper/newarray.hxx>
24 #include "hwplib.h"
25 #include "hwpfile.h"
26 #include "hstyle.h"
28 enum
29 { MAXSTYLENAME = 20 };
31 #define DATA static_cast<StyleData *>(style)
33 struct StyleData
35 char name[MAXSTYLENAME + 1];
36 CharShape cshape;
37 ParaShape pshape;
40 static char buffer[MAXSTYLENAME + 1];
42 HWPStyle::HWPStyle()
44 nstyles = 0;
45 style = nullptr;
49 HWPStyle::~HWPStyle()
51 delete[]DATA;
52 nstyles = 0;
56 char *HWPStyle::GetName(int n) const
58 if (!(n >= 0 && n < nstyles))
59 return nullptr;
60 return DATA[n].name;
64 void HWPStyle::SetName(int n, char const *name)
66 if (n >= 0 && n < nstyles)
68 if (name)
70 #if defined __GNUC__ && (__GNUC__ == 8 || __GNUC__ == 9) && !defined __clang__
71 #pragma GCC diagnostic push
72 #pragma GCC diagnostic ignored "-Wstringop-truncation"
73 #endif
74 auto const p = DATA[n].name;
75 strncpy(p, name, MAXSTYLENAME);
76 p[MAXSTYLENAME] = '\0'; // just in case, even though the array is zero-initialized
77 #if defined __GNUC__ && (__GNUC__ == 8 || __GNUC__ == 9) && !defined __clang__
78 #pragma GCC diagnostic pop
79 #endif
81 else
82 DATA[n].name[0] = 0;
87 CharShape *HWPStyle::GetCharShape(int n) const
89 if (!(n >= 0 && n < nstyles))
90 return nullptr;
91 return &DATA[n].cshape;
95 void HWPStyle::SetCharShape(int n, CharShape const * cshapep)
97 if (n >= 0 && n < nstyles)
99 if (cshapep)
100 DATA[n].cshape = *cshapep;
101 else
102 memset(&DATA[n].cshape, 0, sizeof(CharShape));
107 ParaShape *HWPStyle::GetParaShape(int n) const
109 if (!(n >= 0 && n < nstyles))
110 return nullptr;
111 return &DATA[n].pshape;
115 void HWPStyle::SetParaShape(int n, ParaShape const * pshapep)
117 if (n >= 0 && n < nstyles)
119 if (pshapep)
120 DATA[n].pshape = *pshapep;
121 else
122 DATA[n].pshape = ParaShape();
127 void HWPStyle::Read(HWPFile & hwpf)
129 CharShape cshape;
130 ParaShape pshape;
132 hwpf.Read2b(&nstyles, 1);
133 style = ::comphelper::newArray_null<StyleData>(nstyles);
134 if (!style)
135 return;
137 for (int ii = 0; ii < nstyles; ii++)
139 hwpf.ReadBlock(buffer, MAXSTYLENAME);
140 cshape.Read(hwpf);
141 pshape.Read(hwpf);
143 SetName(ii, buffer);
144 SetCharShape(ii, &cshape);
145 SetParaShape(ii, &pshape);
146 if (hwpf.State())
147 return;
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */